Revision 1447
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/PersistenceManager.java | ||
---|---|---|
449 | 449 |
* data which has been serialized by the same implementation. |
450 | 450 |
* </p> |
451 | 451 |
* |
452 |
* @param reader
|
|
452 |
* @param in
|
|
453 | 453 |
* @return |
454 |
* @throws org.gvsig.tools.persistence.exception.PersistenceException |
|
454 | 455 |
*/ |
455 | 456 |
public PersistentState loadState(InputStream in) |
456 | 457 |
throws PersistenceException; |
... | ... | |
467 | 468 |
* data which has been serialized by the same implementation. |
468 | 469 |
* </p> |
469 | 470 |
* |
471 |
* @param state |
|
470 | 472 |
* @param out |
471 |
* @return |
|
472 | 473 |
* @throws PersistenceValidateExceptions |
473 | 474 |
*/ |
474 | 475 |
public void saveState(PersistentState state, OutputStream out) |
475 | 476 |
throws PersistenceException, PersistenceValidateExceptions; |
476 | 477 |
|
478 |
public void saveState(PersistentState state, OutputStream out, boolean collectErrors) |
|
479 |
throws PersistenceException, PersistenceValidateExceptions; |
|
480 |
|
|
477 | 481 |
/** |
478 | 482 |
* <p> |
479 | 483 |
* De-serializes an state from the data read from the provided |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/xml/XMLPersistentStateWriter.java | ||
---|---|---|
52 | 52 |
import org.gvsig.tools.persistence.PersistentContext; |
53 | 53 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
54 | 54 |
import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException; |
55 |
import org.gvsig.tools.persistence.spi.PersistentContextServices; |
|
55 | 56 |
import org.gvsig.tools.persistence.spi.PersistentContextServices.ObjectReference; |
56 | 57 |
import org.gvsig.tools.persistence.spi.PersistentStateServices; |
57 | 58 |
|
... | ... | |
172 | 173 |
// iter all states |
173 | 174 |
while (itStates.hasNext()) { |
174 | 175 |
curState = (PersistentStateServices) itStates.next(); |
175 |
writeState(curState); |
|
176 |
try { |
|
177 |
writeState(curState); |
|
178 |
} catch(Exception ex) { |
|
179 |
if( context.getCollectErrors() ) { |
|
180 |
context.addError(ex); |
|
181 |
} else { |
|
182 |
throw ex; |
|
183 |
} |
|
184 |
} |
|
176 | 185 |
|
177 | 186 |
} |
178 | 187 |
// States end: |
... | ... | |
198 | 207 |
Object value; |
199 | 208 |
|
200 | 209 |
itNames = curState.getNames(); |
210 |
if( itNames==null || ! itNames.hasNext() ) { |
|
211 |
// The state is empty. Don't output any thing. |
|
212 |
|
|
213 |
} |
|
214 |
|
|
201 | 215 |
String theClassName = curState.getTheClassName(); |
202 | 216 |
String tagName = getXMLValidName( theClassName); |
203 | 217 |
DynStruct definition = curState.getDefinition(); |
204 |
if( definition != null ) { |
|
218 |
|
|
219 |
if( definition != null ) { |
|
205 | 220 |
tagName = getXMLValidName( curState.getDefinition().getFullName() ); |
206 | 221 |
} |
207 | 222 |
|
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/xml/XMLPersistenceManager.java | ||
---|---|---|
111 | 111 |
return context.getRoot().getState(); |
112 | 112 |
} |
113 | 113 |
|
114 |
@Override |
|
114 | 115 |
public void saveState(PersistentState state, OutputStream out) |
115 | 116 |
throws PersistenceException, PersistenceValidateExceptions { |
116 |
XMLPersistentStateWriter xmlWriter = getWriterInstance(); |
|
117 |
this.saveState(state, out, false); |
|
118 |
} |
|
119 |
|
|
120 |
@Override |
|
121 |
public void saveState(PersistentState state, OutputStream out, boolean collectErrors) |
|
122 |
throws PersistenceException, PersistenceValidateExceptions { |
|
123 |
((PersistentContextServices)state.getContext()).setCollectErrors(collectErrors); |
|
124 |
XMLPersistentStateWriter xmlWriter = getWriterInstance(); |
|
125 |
xmlWriter.write((PersistentStateServices) state, out); |
|
117 | 126 |
|
118 |
xmlWriter.write((PersistentStateServices) state, out); |
|
119 |
|
|
120 | 127 |
} |
128 |
|
|
121 | 129 |
|
122 | 130 |
protected XMLPersistentStateReader getReaderInstance(){ |
123 | 131 |
return new XMLPersistentStateReader(this); |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/impl/DelegatedContainer.java | ||
---|---|---|
50 | 50 |
|
51 | 51 |
package org.gvsig.tools.persistence.impl; |
52 | 52 |
|
53 |
import java.io.ByteArrayOutputStream; |
|
54 |
import java.text.MessageFormat; |
|
53 | 55 |
import java.util.Map.Entry; |
54 | 56 |
|
55 | 57 |
import org.gvsig.tools.persistence.spi.PersistenceManagerServices; |
56 | 58 |
import org.gvsig.tools.persistence.spi.PersistentContextServices; |
57 | 59 |
import org.gvsig.tools.persistence.spi.PersistentContextServices.ObjectReference; |
60 |
import org.slf4j.Logger; |
|
61 |
import org.slf4j.LoggerFactory; |
|
58 | 62 |
|
59 | 63 |
|
60 | 64 |
public class DelegatedContainer { |
61 | 65 |
|
62 |
private PersistentContextServices context; |
|
63 |
private PersistenceManagerServices manager; |
|
66 |
private static final Logger logger = LoggerFactory.getLogger(DelegatedContainer.class); |
|
67 |
|
|
68 |
private final PersistentContextServices context; |
|
69 |
private final PersistenceManagerServices manager; |
|
64 | 70 |
|
65 | 71 |
public DelegatedContainer(PersistenceManagerServices manager, |
66 | 72 |
PersistentContextServices context) { |
... | ... | |
78 | 84 |
|
79 | 85 |
private class WrappedEntry implements Entry { |
80 | 86 |
|
81 |
private Entry entry; |
|
87 |
private final Entry entry;
|
|
82 | 88 |
|
83 | 89 |
public WrappedEntry(Entry entry) { |
84 | 90 |
this.entry = entry; |
85 | 91 |
} |
86 | 92 |
|
93 |
@Override |
|
87 | 94 |
public Object getKey() { |
88 | 95 |
return getObject(entry.getKey()); |
89 | 96 |
} |
90 | 97 |
|
98 |
@Override |
|
91 | 99 |
public Object getValue() { |
92 | 100 |
return getObject(entry.getValue()); |
93 | 101 |
} |
94 | 102 |
|
103 |
@Override |
|
95 | 104 |
public Object setValue(Object arg0) { |
96 | 105 |
throw new UnsupportedOperationException(); |
97 | 106 |
} |
... | ... | |
104 | 113 |
* If the object passed as parameter is not a object reference, |
105 | 114 |
* the it is returned as is. |
106 | 115 |
* |
107 |
* @param object
|
|
116 |
* @param wrapper
|
|
108 | 117 |
* @return object references by the parameter or it self. |
109 | 118 |
*/ |
110 | 119 |
protected Object getObject(Object wrapper) { |
111 | 120 |
if (wrapper instanceof ObjectReference) { |
112 | 121 |
ObjectReference ref = (ObjectReference) wrapper; |
113 |
return ref.getObject(); |
|
122 |
try { |
|
123 |
return ref.getObject(); |
|
124 |
} catch(Exception ex) { |
|
125 |
String state = "(unnown)"; |
|
126 |
try { |
|
127 |
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
|
128 |
this.manager.saveState(ref.getState(), buffer); |
|
129 |
state = buffer.toString(); |
|
130 |
} catch(Exception exx) { |
|
131 |
|
|
132 |
} |
|
133 |
String msg = MessageFormat.format( |
|
134 |
"Can't retrieve object from persistent state of id {0}. state: {1}", |
|
135 |
ref.getId().toString(), |
|
136 |
state |
|
137 |
); |
|
138 |
logger.warn(msg,ex); |
|
139 |
throw ex; |
|
140 |
} |
|
114 | 141 |
} else if (wrapper instanceof Entry) { |
115 | 142 |
return new WrappedEntry((Entry) wrapper); |
116 | 143 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/impl/AbstractPersistentState.java | ||
---|---|---|
936 | 936 |
|
937 | 937 |
} |
938 | 938 |
} catch (RuntimeException e) { |
939 |
logger.info("Can't fix relative paths in " + className, e); |
|
939 |
String msg = "Can't fix relative paths in '" + className + "'"; |
|
940 |
if( context.getCollectErrors() ) { |
|
941 |
context.addError(new RuntimeException(msg)); |
|
942 |
} |
|
943 |
logger.info(msg, e); |
|
940 | 944 |
} |
941 | 945 |
} |
942 | 946 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/impl/DelegatedCollection.java | ||
---|---|---|
63 | 63 |
public class DelegatedCollection extends DelegatedContainer implements |
64 | 64 |
Collection { |
65 | 65 |
|
66 |
private Collection delegated; |
|
66 |
private final Collection delegated;
|
|
67 | 67 |
|
68 | 68 |
public DelegatedCollection(Collection delegated, |
69 | 69 |
PersistenceManagerServices manager, PersistentContextServices context) { |
... | ... | |
71 | 71 |
this.delegated = delegated; |
72 | 72 |
} |
73 | 73 |
|
74 |
@Override |
|
74 | 75 |
public boolean add(Object arg0) { |
75 | 76 |
throw new UnsupportedOperationException(); |
76 | 77 |
} |
77 | 78 |
|
79 |
@Override |
|
78 | 80 |
public boolean addAll(Collection arg0) { |
79 | 81 |
throw new UnsupportedOperationException(); |
80 | 82 |
} |
81 | 83 |
|
84 |
@Override |
|
82 | 85 |
public void clear() { |
83 | 86 |
throw new UnsupportedOperationException(); |
84 | 87 |
} |
85 | 88 |
|
89 |
@Override |
|
86 | 90 |
public boolean contains(Object o) { |
87 | 91 |
return delegated.contains(getWrapper(o)); |
88 | 92 |
} |
89 | 93 |
|
94 |
@Override |
|
90 | 95 |
public boolean containsAll(Collection arg0) { |
91 | 96 |
Iterator iter = arg0.iterator(); |
92 | 97 |
while (iter.hasNext()) { |
... | ... | |
97 | 102 |
return true; |
98 | 103 |
} |
99 | 104 |
|
105 |
@Override |
|
100 | 106 |
public boolean isEmpty() { |
101 | 107 |
return delegated.isEmpty(); |
102 | 108 |
} |
103 | 109 |
|
110 |
@Override |
|
104 | 111 |
public Iterator iterator() { |
105 | 112 |
Iterator it = delegated.iterator(); |
106 | 113 |
return this.getWrappedIterator(it); |
107 | 114 |
} |
108 | 115 |
|
116 |
@Override |
|
109 | 117 |
public boolean remove(Object o) { |
110 | 118 |
throw new UnsupportedOperationException(); |
111 | 119 |
} |
112 | 120 |
|
121 |
@Override |
|
113 | 122 |
public boolean removeAll(Collection arg0) { |
114 | 123 |
throw new UnsupportedOperationException(); |
115 | 124 |
} |
116 | 125 |
|
126 |
@Override |
|
117 | 127 |
public boolean retainAll(Collection arg0) { |
118 | 128 |
throw new UnsupportedOperationException(); |
119 | 129 |
} |
120 | 130 |
|
121 | 131 |
|
132 |
@Override |
|
122 | 133 |
public Object[] toArray() { |
123 | 134 |
Object[] r = new Object[size()]; |
124 | 135 |
Iterator iter = iterator(); |
... | ... | |
130 | 141 |
return r; |
131 | 142 |
} |
132 | 143 |
|
144 |
@Override |
|
133 | 145 |
public Object[] toArray(Object[] a) { |
134 | 146 |
int size = size(); |
135 | 147 |
if (a.length < size) { |
... | ... | |
149 | 161 |
return a; |
150 | 162 |
} |
151 | 163 |
|
164 |
@Override |
|
152 | 165 |
public int size() { |
153 | 166 |
return delegated.size(); |
154 | 167 |
} |
... | ... | |
156 | 169 |
protected ListIterator getWrappedIterator(Iterator iterator) { |
157 | 170 |
class DelegatedIterator implements ListIterator { |
158 | 171 |
|
159 |
private Iterator delegated; |
|
172 |
private final Iterator delegated;
|
|
160 | 173 |
private ListIterator delegatedListIterator; |
161 | 174 |
|
162 | 175 |
public DelegatedIterator(Iterator deletaged) { |
... | ... | |
166 | 179 |
} |
167 | 180 |
} |
168 | 181 |
|
182 |
@Override |
|
169 | 183 |
public boolean hasNext() { |
170 | 184 |
return delegated.hasNext(); |
171 | 185 |
} |
172 | 186 |
|
187 |
@Override |
|
173 | 188 |
public Object next() { |
174 |
Object o = delegated.next(); |
|
175 |
return getObject(o); |
|
189 |
Object o = null; |
|
190 |
while( delegated.hasNext() ) { |
|
191 |
Object referece = delegated.next(); |
|
192 |
try { |
|
193 |
o = getObject(referece); |
|
194 |
} catch(Exception ex) { |
|
195 |
if( getContext().getCollectErrors() ) { |
|
196 |
getContext().addError(ex); |
|
197 |
} else { |
|
198 |
throw ex; |
|
199 |
} |
|
200 |
} |
|
201 |
if( o != null ) { |
|
202 |
break; |
|
203 |
} |
|
204 |
} |
|
205 |
return o; |
|
176 | 206 |
} |
177 | 207 |
|
208 |
@Override |
|
178 | 209 |
public void remove() { |
179 | 210 |
throw new UnsupportedOperationException(); |
180 | 211 |
} |
181 | 212 |
|
213 |
@Override |
|
182 | 214 |
public void add(Object arg0) { |
183 | 215 |
throw new UnsupportedOperationException(); |
184 | 216 |
|
185 | 217 |
} |
186 | 218 |
|
219 |
@Override |
|
187 | 220 |
public boolean hasPrevious() { |
188 | 221 |
if (delegatedListIterator == null) { |
189 | 222 |
throw new UnsupportedOperationException(); |
... | ... | |
191 | 224 |
return delegatedListIterator.hasPrevious(); |
192 | 225 |
} |
193 | 226 |
|
227 |
@Override |
|
194 | 228 |
public int nextIndex() { |
195 | 229 |
if (delegatedListIterator == null) { |
196 | 230 |
throw new UnsupportedOperationException(); |
... | ... | |
198 | 232 |
return delegatedListIterator.nextIndex(); |
199 | 233 |
} |
200 | 234 |
|
235 |
@Override |
|
201 | 236 |
public Object previous() { |
202 | 237 |
if (delegatedListIterator == null) { |
203 | 238 |
throw new UnsupportedOperationException(); |
... | ... | |
205 | 240 |
return getObject(delegatedListIterator.previous()); |
206 | 241 |
} |
207 | 242 |
|
243 |
@Override |
|
208 | 244 |
public int previousIndex() { |
209 | 245 |
if (delegatedListIterator == null) { |
210 | 246 |
throw new UnsupportedOperationException(); |
... | ... | |
212 | 248 |
return delegatedListIterator.previousIndex(); |
213 | 249 |
} |
214 | 250 |
|
251 |
@Override |
|
215 | 252 |
public void set(Object arg0) { |
216 | 253 |
throw new UnsupportedOperationException(); |
217 | 254 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/PersistentContext.java | ||
---|---|---|
52 | 52 |
*/ |
53 | 53 |
public PersistenceException getErrors(); |
54 | 54 |
|
55 |
/** |
|
56 |
* Set flag to collect errors. If this flags is true, |
|
57 |
* all errors in sets in state, or in saveToState are collected |
|
58 |
* for throw later. |
|
59 |
* |
|
60 |
* @param collectErrors |
|
61 |
*/ |
|
62 |
public void setCollectErrors(boolean collectErrors); |
|
55 | 63 |
|
64 |
/** |
|
65 |
* Get information about if collect errors or launch it |
|
66 |
* when produce. |
|
67 |
* |
|
68 |
* @return |
|
69 |
*/ |
|
70 |
public boolean getCollectErrors(); |
|
56 | 71 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/spi/PersistentContextServices.java | ||
---|---|---|
207 | 207 |
public PersistentIdentifier getIdentifier(String id) throws PersistenceException; |
208 | 208 |
|
209 | 209 |
/** |
210 |
* Set flag to collect errors. If this flags is true, |
|
211 |
* all errors in sets in state, or in saveToState are collected |
|
212 |
* for throw later. |
|
213 |
* |
|
214 |
* @param collectErrors |
|
215 |
*/ |
|
216 |
public void setCollectErrors(boolean collectErrors); |
|
217 |
|
|
218 |
/** |
|
219 |
* Get information about if collect errors or launch it |
|
220 |
* when produce. |
|
221 |
* |
|
222 |
* @return |
|
223 |
*/ |
|
224 |
public boolean getCollectErrors(); |
|
225 |
|
|
226 |
/** |
|
227 | 210 |
* Validate al statetes en the context. |
228 | 211 |
*/ |
229 | 212 |
public void validate(int mode) throws PersistenceValidateExceptions; |
Also available in: Unified diff