gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / persistence / impl / DefaultPersistentContext.java @ 2145
History | View | Annotate | Download (13.5 KB)
1 | 802 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 73 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
25 | *
|
||
26 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
27 | * of the Valencian Government (CIT)
|
||
28 | *
|
||
29 | * This program is free software; you can redistribute it and/or
|
||
30 | * modify it under the terms of the GNU General Public License
|
||
31 | * as published by the Free Software Foundation; either version 2
|
||
32 | * of the License, or (at your option) any later version.
|
||
33 | *
|
||
34 | * This program is distributed in the hope that it will be useful,
|
||
35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
37 | * GNU General Public License for more details.
|
||
38 | *
|
||
39 | * You should have received a copy of the GNU General Public License
|
||
40 | * along with this program; if not, write to the Free Software
|
||
41 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
42 | * MA 02110-1301, USA.
|
||
43 | *
|
||
44 | */
|
||
45 | |||
46 | /*
|
||
47 | * AUTHORS (In addition to CIT):
|
||
48 | * 2009 IVER T.I {{Task}}
|
||
49 | */
|
||
50 | |||
51 | package org.gvsig.tools.persistence.impl; |
||
52 | |||
53 | 277 | jjdelcerro | import java.util.ArrayList; |
54 | 73 | jjdelcerro | import java.util.Iterator; |
55 | 277 | jjdelcerro | import java.util.LinkedHashMap; |
56 | import java.util.List; |
||
57 | 73 | jjdelcerro | import java.util.Map; |
58 | import java.util.Map.Entry; |
||
59 | 2145 | jjdelcerro | import org.apache.commons.lang3.mutable.MutableInt; |
60 | 73 | jjdelcerro | |
61 | import org.gvsig.tools.persistence.PersistenceFactory; |
||
62 | import org.gvsig.tools.persistence.PersistenceManager; |
||
63 | 1606 | jjdelcerro | import org.gvsig.tools.persistence.PersistentContext; |
64 | 73 | jjdelcerro | import org.gvsig.tools.persistence.PersistentState; |
65 | 836 | jldominguez | import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered; |
66 | 73 | jjdelcerro | import org.gvsig.tools.persistence.exception.PersistenceException; |
67 | import org.gvsig.tools.persistence.exception.PersistenceRuntimeException; |
||
68 | 277 | jjdelcerro | import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions; |
69 | 73 | jjdelcerro | import org.gvsig.tools.persistence.impl.exception.ObjectNotFoundException; |
70 | import org.gvsig.tools.persistence.impl.exception.PersistenceIDNotLoadedException; |
||
71 | 220 | jjdelcerro | import org.gvsig.tools.persistence.spi.PersistentContextServices; |
72 | 73 | jjdelcerro | import org.gvsig.tools.persistence.spi.PersistentIdentifier; |
73 | import org.gvsig.tools.persistence.spi.PersistentStateServices; |
||
74 | 1606 | jjdelcerro | import org.slf4j.Logger; |
75 | import org.slf4j.LoggerFactory; |
||
76 | 73 | jjdelcerro | |
77 | 220 | jjdelcerro | public class DefaultPersistentContext implements PersistentContextServices { |
78 | 73 | jjdelcerro | |
79 | 1606 | jjdelcerro | private static Logger LOG = LoggerFactory.getLogger(DefaultPersistentContext.class); |
80 | |||
81 | 73 | jjdelcerro | private Map idToReference; |
82 | private Map objToReference; |
||
83 | private PersistentIdentifier rootId;
|
||
84 | private PersistenceManager manager;
|
||
85 | 277 | jjdelcerro | private boolean collectErrors; |
86 | private PersistenceException errors;
|
||
87 | private boolean validated; |
||
88 | 2145 | jjdelcerro | private MutableInt referenceCounter;
|
89 | 73 | jjdelcerro | |
90 | 2145 | jjdelcerro | public DefaultPersistentContext(PersistenceManager manager, MutableInt referenceCounter) {
|
91 | 73 | jjdelcerro | this.manager = manager;
|
92 | 277 | jjdelcerro | this.collectErrors = false; |
93 | this.errors = null; |
||
94 | this.validated = false; |
||
95 | 2145 | jjdelcerro | this.referenceCounter = referenceCounter;
|
96 | 277 | jjdelcerro | |
97 | // Use LinkedHashMap for predictable order when save to a file
|
||
98 | this.idToReference = new LinkedHashMap(); |
||
99 | this.objToReference = new LinkedHashMap(); |
||
100 | 73 | jjdelcerro | } |
101 | |||
102 | 277 | jjdelcerro | private class DefaultPersistentIdentifier implements PersistentIdentifier, Comparable { |
103 | 73 | jjdelcerro | private String value; |
104 | |||
105 | DefaultPersistentIdentifier(String value) {
|
||
106 | this.value = value;
|
||
107 | } |
||
108 | |||
109 | 220 | jjdelcerro | // String getValue() {
|
110 | // return value;
|
||
111 | // }
|
||
112 | 73 | jjdelcerro | |
113 | public boolean equals(Object obj) { |
||
114 | if (!(obj instanceof DefaultPersistentIdentifier)) { |
||
115 | return false; |
||
116 | } |
||
117 | return value == ((DefaultPersistentIdentifier) obj).value;
|
||
118 | } |
||
119 | |||
120 | public boolean hasValue(Object value) { |
||
121 | return this.value.equals(value); |
||
122 | } |
||
123 | |||
124 | public int hashCode() { |
||
125 | return value.hashCode();
|
||
126 | } |
||
127 | |||
128 | public String toString() { |
||
129 | return value;
|
||
130 | } |
||
131 | 277 | jjdelcerro | |
132 | public int compareTo(Object obj) { |
||
133 | if (!(obj instanceof DefaultPersistentIdentifier)) { |
||
134 | return -1; |
||
135 | } |
||
136 | return value.compareTo( ((DefaultPersistentIdentifier) obj).value) ;
|
||
137 | } |
||
138 | 73 | jjdelcerro | } |
139 | |||
140 | 277 | jjdelcerro | private class ContextValue implements ObjectReference, Comparable { |
141 | 73 | jjdelcerro | private Object obj; |
142 | private PersistentState state;
|
||
143 | private PersistentIdentifier id;
|
||
144 | 1606 | jjdelcerro | private boolean isNullStateReported; |
145 | 73 | jjdelcerro | |
146 | ContextValue(PersistentIdentifier id, PersistentState state) { |
||
147 | this.id = id;
|
||
148 | this.state = state;
|
||
149 | this.obj = null; |
||
150 | 1606 | jjdelcerro | this.isNullStateReported = false; |
151 | } |
||
152 | 73 | jjdelcerro | |
153 | 1606 | jjdelcerro | @Override
|
154 | 73 | jjdelcerro | public boolean hasObject() { |
155 | return this.obj != null; |
||
156 | } |
||
157 | 1606 | jjdelcerro | |
158 | @Override
|
||
159 | 73 | jjdelcerro | public Object getObject() { |
160 | 1606 | jjdelcerro | if( this.state == null ) { |
161 | if( !isNullStateReported ) {
|
||
162 | try {
|
||
163 | throw new IllegalArgumentException("The persistent storage may be corrupt. Can't have state for id '"+id+"'."); |
||
164 | } catch(Exception ex) { |
||
165 | this.getContext().addError(ex);
|
||
166 | LOG.warn(ex.getMessage(), ex); |
||
167 | } |
||
168 | isNullStateReported = true;
|
||
169 | } |
||
170 | return null; |
||
171 | } |
||
172 | 73 | jjdelcerro | if( this.obj == null ) { |
173 | try {
|
||
174 | updateObjectInReference(this);
|
||
175 | 1606 | jjdelcerro | } catch (Exception e) { |
176 | if( this.getContext().getCollectErrors() ) { |
||
177 | this.getContext().addError(e);
|
||
178 | LOG.warn(e.getMessage(), e); |
||
179 | } else {
|
||
180 | throw new PersistenceRuntimeException(e); |
||
181 | } |
||
182 | 73 | jjdelcerro | } |
183 | } |
||
184 | return this.obj; |
||
185 | } |
||
186 | |||
187 | 1606 | jjdelcerro | @Override
|
188 | 73 | jjdelcerro | public PersistentState getState() {
|
189 | return this.state; |
||
190 | } |
||
191 | |||
192 | 1606 | jjdelcerro | @Override
|
193 | 73 | jjdelcerro | public PersistentIdentifier getId() {
|
194 | return this.id; |
||
195 | } |
||
196 | |||
197 | protected void update(Object object) { |
||
198 | this.obj = object;
|
||
199 | } |
||
200 | |||
201 | 1606 | jjdelcerro | @Override
|
202 | 73 | jjdelcerro | public boolean equals(Object obj) { |
203 | if (!(obj instanceof ContextValue)) { |
||
204 | return false; |
||
205 | } |
||
206 | return id.equals(((ContextValue) obj).id);
|
||
207 | } |
||
208 | |||
209 | 1606 | jjdelcerro | @Override
|
210 | 73 | jjdelcerro | public int hashCode() { |
211 | return id.hashCode();
|
||
212 | } |
||
213 | |||
214 | 1606 | jjdelcerro | @Override
|
215 | 73 | jjdelcerro | public String toString() { |
216 | String idStr = id == null ? "null" : id.toString(); |
||
217 | return getClass().getName().concat(": id = ").concat(idStr); |
||
218 | } |
||
219 | |||
220 | 1606 | jjdelcerro | @Override
|
221 | 277 | jjdelcerro | public int compareTo(Object arg0) { |
222 | if (!(obj instanceof ContextValue)) { |
||
223 | return -1; |
||
224 | } |
||
225 | return ((DefaultPersistentIdentifier)id).compareTo(((ContextValue) obj).id);
|
||
226 | } |
||
227 | |||
228 | 1606 | jjdelcerro | @Override
|
229 | public PersistentContext getContext() {
|
||
230 | return DefaultPersistentContext.this;
|
||
231 | } |
||
232 | |||
233 | 73 | jjdelcerro | } |
234 | |||
235 | 1606 | jjdelcerro | @Override
|
236 | 73 | jjdelcerro | public PersistentIdentifier getNewIdentifier() {
|
237 | 2145 | jjdelcerro | return new DefaultPersistentIdentifier(String.valueOf(referenceCounter.getAndIncrement())); |
238 | 73 | jjdelcerro | } |
239 | |||
240 | 1606 | jjdelcerro | @Override
|
241 | 73 | jjdelcerro | public PersistentIdentifier getIdentifier(String id) throws PersistenceException { |
242 | Iterator it = this.idToReference.values().iterator(); |
||
243 | while( it.hasNext() ) {
|
||
244 | ContextValue value = (ContextValue) it.next(); |
||
245 | if( ((DefaultPersistentIdentifier)(value.getId())).hasValue(id) ) {
|
||
246 | return value.getId();
|
||
247 | } |
||
248 | } |
||
249 | return new DefaultPersistentIdentifier(id); |
||
250 | } |
||
251 | |||
252 | public ObjectReference add(PersistentIdentifier id) throws PersistenceException { |
||
253 | 277 | jjdelcerro | this.validated = false; |
254 | 73 | jjdelcerro | ContextValue value = (ContextValue) idToReference.get(id); |
255 | if (value == null ) { |
||
256 | value = new ContextValue(id, null); |
||
257 | idToReference.put(value.getId(), value); |
||
258 | } |
||
259 | return value;
|
||
260 | } |
||
261 | |||
262 | public ObjectReference add(PersistentState state, Object obj) throws PersistenceException { |
||
263 | 277 | jjdelcerro | this.validated = false; |
264 | 73 | jjdelcerro | PersistentIdentifier id = ((PersistentStateServices)state).getId(); |
265 | ContextValue value = (ContextValue) idToReference.get(id); |
||
266 | if (value == null ) { |
||
267 | value = new ContextValue( id, state);
|
||
268 | idToReference.put(value.getId(), value); |
||
269 | } |
||
270 | if( value.getState() == null ) { |
||
271 | value.state = state; |
||
272 | } |
||
273 | if( obj != null ) { |
||
274 | value.update(obj); |
||
275 | objToReference.put(obj,value); |
||
276 | } |
||
277 | return value;
|
||
278 | } |
||
279 | |||
280 | public ObjectReference get(PersistentState state) throws PersistenceException { |
||
281 | PersistentIdentifier id = ((PersistentStateServices)state).getId(); |
||
282 | ContextValue ref = (ContextValue) idToReference.get(id); |
||
283 | if (ref == null ) { |
||
284 | ref = new ContextValue(id, state);
|
||
285 | idToReference.put(id, ref); |
||
286 | } |
||
287 | return ref;
|
||
288 | } |
||
289 | |||
290 | public ObjectReference get(PersistentIdentifier id) {
|
||
291 | ContextValue value = (ContextValue) idToReference.get(id); |
||
292 | return value;
|
||
293 | } |
||
294 | |||
295 | public ObjectReference get(Object obj) { |
||
296 | return (ContextValue) objToReference.get(obj);
|
||
297 | } |
||
298 | |||
299 | private void updateObjectInReference(ContextValue ref) throws PersistenceException { |
||
300 | /*
|
||
301 | * Note: this method is used by the inner class Reference.
|
||
302 | */
|
||
303 | 330 | fdiaz | PersistentStateServices state = (PersistentStateServices) ref.getState(); |
304 | PersistenceFactory factory = manager.getFactories().get(state); |
||
305 | 836 | jldominguez | |
306 | if (factory == null) { |
||
307 | /*
|
||
308 | * This happens if persistence refers to objects
|
||
309 | * of a missing plugin (example: persistence
|
||
310 | * has data about oracle connections but we dont have
|
||
311 | * the oracle plugin in the current execution)
|
||
312 | */
|
||
313 | 984 | jldominguez | String classname = ""; |
314 | if (state == null) { |
||
315 | classname = "[State is null]";
|
||
316 | } else {
|
||
317 | classname = state.getTheClassName(); |
||
318 | if (classname == null) { |
||
319 | classname = "[Null]";
|
||
320 | } |
||
321 | } |
||
322 | throw new PersistenceClassNotRegistered(classname); |
||
323 | 836 | jldominguez | } |
324 | |||
325 | 73 | jjdelcerro | |
326 | // This can be done in two step to avoid cyclic references
|
||
327 | 330 | fdiaz | state.setFactory(factory); |
328 | ref.update( factory.createFromState(state) ); |
||
329 | factory.loadFromState(state, ref.getObject()); |
||
330 | 73 | jjdelcerro | |
331 | objToReference.put(ref.getObject(), ref); |
||
332 | } |
||
333 | |||
334 | public void update(ObjectReference ref) throws PersistenceException { |
||
335 | 277 | jjdelcerro | this.validated = false; |
336 | 73 | jjdelcerro | this.updateObjectInReference((ContextValue) ref);
|
337 | } |
||
338 | |||
339 | private void updateAll() throws PersistenceException { |
||
340 | Iterator it = idToReference.entrySet().iterator();
|
||
341 | while ( it.hasNext() ) {
|
||
342 | ContextValue ref = (ContextValue) ((Entry) it.next()).getValue(); |
||
343 | this.update(ref);
|
||
344 | } |
||
345 | } |
||
346 | |||
347 | public void clear() { |
||
348 | 277 | jjdelcerro | this.validated = false; |
349 | 73 | jjdelcerro | Iterator it = idToReference.entrySet().iterator();
|
350 | while ( it.hasNext() ) {
|
||
351 | ContextValue ref = (ContextValue) ((Entry) it.next()).getValue(); |
||
352 | ref.update(null);
|
||
353 | } |
||
354 | } |
||
355 | |||
356 | public Iterator iterator() { |
||
357 | final class StatesIterator implements Iterator { |
||
358 | private Iterator referenceIterator; |
||
359 | |||
360 | 277 | jjdelcerro | StatesIterator(Map idToReference) {
|
361 | this.referenceIterator = idToReference.entrySet().iterator();
|
||
362 | 73 | jjdelcerro | } |
363 | 277 | jjdelcerro | |
364 | // StatesIterator(Map idToReference) {
|
||
365 | // List values = new ArrayList();
|
||
366 | // values.addAll(idToReference.values());
|
||
367 | // Collections.sort(values);
|
||
368 | // this.referenceIterator = values.iterator();
|
||
369 | // }
|
||
370 | |||
371 | 73 | jjdelcerro | public boolean hasNext() { |
372 | return referenceIterator.hasNext();
|
||
373 | } |
||
374 | public Object next() { |
||
375 | ContextValue ref = (ContextValue) ((Entry) referenceIterator.next()).getValue(); |
||
376 | 277 | jjdelcerro | // ContextValue ref = (ContextValue) referenceIterator.next();
|
377 | 73 | jjdelcerro | return ref.getState();
|
378 | } |
||
379 | public void remove() { |
||
380 | throw new UnsupportedOperationException(); |
||
381 | } |
||
382 | } |
||
383 | 277 | jjdelcerro | |
384 | return new StatesIterator(idToReference); |
||
385 | 73 | jjdelcerro | } |
386 | |||
387 | public PersistentState getState(Integer id) |
||
388 | throws PersistenceException {
|
||
389 | ContextValue ref = (ContextValue) this.get(id);
|
||
390 | if (ref == null) { |
||
391 | throw new PersistenceIDNotLoadedException(id); |
||
392 | } |
||
393 | return ref.getState();
|
||
394 | } |
||
395 | |||
396 | public PersistentState getState(Object obj) |
||
397 | throws PersistenceException {
|
||
398 | ContextValue ref = (ContextValue) this.get(obj);
|
||
399 | if (ref == null) { |
||
400 | return null; |
||
401 | } |
||
402 | return ref.getState();
|
||
403 | } |
||
404 | |||
405 | public Object getObject(Integer id) throws PersistenceException { |
||
406 | ContextValue ref = (ContextValue) this.get(id);
|
||
407 | if (ref == null) { |
||
408 | throw new PersistenceIDNotLoadedException(id); |
||
409 | } |
||
410 | return ref.getObject();
|
||
411 | } |
||
412 | |||
413 | public PersistentIdentifier getId(Object obj) throws PersistenceException { |
||
414 | ContextValue ref = (ContextValue) this.get(obj);
|
||
415 | if( ref != null ) { |
||
416 | return ref.getId();
|
||
417 | } |
||
418 | this.updateAll();
|
||
419 | ref = (ContextValue) this.get(obj);
|
||
420 | if (ref == null) { |
||
421 | throw new ObjectNotFoundException(); |
||
422 | } |
||
423 | return ref.getId();
|
||
424 | } |
||
425 | |||
426 | |||
427 | public void setRootId(PersistentIdentifier id) { |
||
428 | this.rootId = id;
|
||
429 | } |
||
430 | |||
431 | public ObjectReference getRoot() throws PersistenceException { |
||
432 | ObjectReference root = this.get(this.rootId); |
||
433 | return root;
|
||
434 | } |
||
435 | |||
436 | 277 | jjdelcerro | public void setCollectErrors(boolean collectErrors) { |
437 | this.collectErrors = collectErrors;
|
||
438 | } |
||
439 | |||
440 | public boolean getCollectErrors() { |
||
441 | return this.collectErrors; |
||
442 | } |
||
443 | |||
444 | public void addError(Throwable cause) { |
||
445 | if( this.errors == null ) { |
||
446 | this.errors = new PersistenceException(); |
||
447 | } |
||
448 | this.errors.add(cause);
|
||
449 | } |
||
450 | |||
451 | public PersistenceException getErrors() {
|
||
452 | return this.errors; |
||
453 | } |
||
454 | |||
455 | public void validate(int mode) throws PersistenceValidateExceptions { |
||
456 | if( this.validated ) { |
||
457 | return;
|
||
458 | } |
||
459 | List exceptions = new ArrayList(); |
||
460 | |||
461 | Iterator it = this.iterator(); |
||
462 | while( it.hasNext() ) {
|
||
463 | PersistentState astate = (PersistentState) it.next(); |
||
464 | try {
|
||
465 | this.manager.validate(astate,mode);
|
||
466 | } catch(Exception e) { |
||
467 | exceptions.add(e); |
||
468 | } |
||
469 | } |
||
470 | |||
471 | this.validated=true; |
||
472 | if (exceptions.size() > 0) { |
||
473 | throw new PersistenceValidateExceptions(exceptions); |
||
474 | } |
||
475 | } |
||
476 | 73 | jjdelcerro | } |