Revision 30619 branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/persistence/impl/AbstractPersistenceManager.java
AbstractPersistenceManager.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.persistence.impl; |
2 | 2 |
|
3 |
import java.util.Collections; |
|
3 | 4 |
import java.util.HashMap; |
4 | 5 |
import java.util.Iterator; |
5 | 6 |
import java.util.List; |
6 | 7 |
import java.util.Map; |
7 | 8 |
import java.util.Set; |
9 |
import java.util.TreeSet; |
|
10 |
import java.util.Map.Entry; |
|
11 |
import java.util.regex.Pattern; |
|
8 | 12 |
|
9 | 13 |
import org.gvsig.tools.dynobject.DynClass; |
10 | 14 |
import org.gvsig.tools.dynobject.DynObject; |
... | ... | |
12 | 16 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
13 | 17 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
14 | 18 |
import org.gvsig.tools.dynobject.exception.DynObjectValidateException; |
19 |
import org.gvsig.tools.persistence.PersistenceClassNotRegistered; |
|
15 | 20 |
import org.gvsig.tools.persistence.PersistenceCreateException; |
16 | 21 |
import org.gvsig.tools.persistence.PersistenceException; |
17 | 22 |
import org.gvsig.tools.persistence.PersistenceFactory; |
18 | 23 |
import org.gvsig.tools.persistence.PersistenceManager; |
24 |
import org.gvsig.tools.persistence.PersistenceRuntimeException; |
|
19 | 25 |
import org.gvsig.tools.persistence.PersistenceTypeNotSupportedException; |
20 | 26 |
import org.gvsig.tools.persistence.PersistenceValidateExceptions; |
21 | 27 |
import org.gvsig.tools.persistence.Persistent; |
... | ... | |
25 | 31 |
|
26 | 32 |
public abstract class AbstractPersistenceManager implements |
27 | 33 |
ImplementationPersistenceManager { |
28 |
protected HashMap definitions = new HashMap(); |
|
34 |
public static final String DEFAULT_DOMAIN_URL = "http://www.gvsig.org"; |
|
35 |
public static final String DEFAULT_DOMAIN_NAME = "gvSIG"; |
|
36 |
|
|
37 |
public static final Pattern VALID_DOMAIN_NAME_PATTERN = Pattern |
|
38 |
.compile("[\\w][\\d\\w_]*"); |
|
29 | 39 |
protected Map classes; |
40 |
protected Map domainsURL; |
|
30 | 41 |
protected int autoValidationMode = PersistenceManager.DISABLED; |
31 | 42 |
|
32 | 43 |
|
44 |
private class ClassInfo { |
|
45 |
public DynStruct definition = null; |
|
46 |
public String domainName = null; |
|
47 |
public Object theClass = null; |
|
48 |
} |
|
49 |
|
|
50 |
|
|
33 | 51 |
protected AbstractPersistenceManager() { |
34 | 52 |
classes = new HashMap(); |
53 |
domainsURL = new HashMap(); |
|
35 | 54 |
} |
36 | 55 |
|
37 |
public void addAlias(String name, Class aClass) { |
|
38 |
classes.put(name, aClass); |
|
56 |
public void addAlias(String name, Class aClass) |
|
57 |
throws PersistenceClassNotRegistered { |
|
58 |
ClassInfo info = (ClassInfo) classes.get(aClass.getName()); |
|
59 |
if (info == null) { |
|
60 |
throw new PersistenceClassNotRegistered(aClass.getName()); |
|
61 |
} |
|
62 |
classes.put(name, info); |
|
39 | 63 |
} |
40 | 64 |
|
41 | 65 |
/* (non-Javadoc) |
42 | 66 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#createInstaceOfObject(org.gvsig.tools.persistence.PersistentState) |
43 | 67 |
*/ |
44 | 68 |
public Object createInstaceOfObject(PersistentState state, Class classOfState, PersistenceFactory factory) |
45 |
throws PersistenceException, PersistenceCreateException { |
|
69 |
throws PersistenceException, PersistenceCreateException, |
|
70 |
PersistenceClassNotRegistered { |
|
71 |
// TODO IMPLEMENTAR FACTORY |
|
46 | 72 |
String className = state.getTheClassName(); |
47 | 73 |
if (className == null) { |
48 | 74 |
throw new PersistenceException("The class name is not stored in the state."); |
... | ... | |
50 | 76 |
try { |
51 | 77 |
Class theClass; |
52 | 78 |
|
53 |
Object x = classes.get(className); |
|
79 |
ClassInfo info = (ClassInfo) classes.get(className); |
|
80 |
|
|
81 |
if (info == null) { |
|
82 |
throw new PersistenceClassNotRegistered(className); |
|
83 |
} |
|
84 |
|
|
85 |
Object x = info.theClass; |
|
86 |
|
|
87 |
|
|
54 | 88 |
if (x instanceof Class) { |
55 | 89 |
theClass = (Class) x; |
56 | 90 |
} else if (x instanceof String) { |
... | ... | |
66 | 100 |
} catch (IllegalAccessException e) { |
67 | 101 |
throw new PersistenceCreateException(state, e); |
68 | 102 |
} |
69 |
|
|
70 | 103 |
} |
71 | 104 |
|
72 | 105 |
|
... | ... | |
92 | 125 |
} |
93 | 126 |
|
94 | 127 |
public void registerClass(Class persistentClass, DynStruct definition) { |
95 |
definitions.put(persistentClass.getName(), definition); |
|
96 |
classes.put(persistentClass.getName(), persistentClass); |
|
128 |
registerClass(persistentClass, definition, null); |
|
97 | 129 |
} |
98 | 130 |
|
99 | 131 |
public DynStruct getDefinition(Class persistentClass) { |
100 |
return (DynStruct) definitions.get(persistentClass.getName());
|
|
132 |
return getDefinition(persistentClass.getName());
|
|
101 | 133 |
} |
102 | 134 |
|
103 | 135 |
/* (non-Javadoc) |
104 | 136 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#getDefinition(java.lang.String) |
105 | 137 |
*/ |
106 | 138 |
public DynStruct getDefinition(String persistentClassName) { |
107 |
return (DynStruct) definitions.get(persistentClassName); |
|
139 |
ClassInfo info = (ClassInfo) classes.get(persistentClassName); |
|
140 |
if (info == null) { |
|
141 |
return null; |
|
142 |
} |
|
143 |
return info.definition; |
|
108 | 144 |
} |
109 | 145 |
|
110 | 146 |
|
111 | 147 |
public Iterator getPersistentClasses() { |
112 |
return definitions.keySet().iterator(); |
|
148 |
Iterator iter = classes.values().iterator(); |
|
149 |
Set classesSet = new TreeSet(); |
|
150 |
ClassInfo info; |
|
151 |
while (iter.hasNext()) { |
|
152 |
info = (ClassInfo) iter.next(); |
|
153 |
classesSet.add(info.theClass); |
|
154 |
} |
|
155 |
return classesSet.iterator(); |
|
113 | 156 |
} |
114 | 157 |
|
115 |
public void removeDefinition(Class persistentClass) { |
|
116 |
definitions.remove(persistentClass.getName()); |
|
117 |
} |
|
118 | 158 |
|
119 | 159 |
/* |
120 | 160 |
* (non-Javadoc) |
... | ... | |
279 | 319 |
*/ |
280 | 320 |
public ImplementationPersistentState createState(Object theOriginal, |
281 | 321 |
PersistentContext context) |
282 |
throws PersistenceException { |
|
322 |
throws PersistenceException, |
|
323 |
PersistenceClassNotRegistered, PersistenceTypeNotSupportedException { |
|
283 | 324 |
|
284 | 325 |
ImplementationPersistentState state = createPersistentStateInstance(context); |
285 |
state.setTheClassName(theOriginal.getClass().getName()); |
|
326 |
String theClass = theOriginal.getClass().getName(); |
|
327 |
state.setTheClassName(theClass); |
|
328 |
if (!classes.containsKey(theClass)) { |
|
329 |
throw new PersistenceClassNotRegistered(theClass); |
|
330 |
} |
|
286 | 331 |
context.addReference(theOriginal, state); |
287 | 332 |
PersistenceFactory factory = getFactoryFor(theOriginal); |
288 | 333 |
if (factory != null) { |
... | ... | |
317 | 362 |
|
318 | 363 |
/* |
319 | 364 |
* (non-Javadoc) |
320 |
*
|
|
365 |
* |
|
321 | 366 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager# |
322 | 367 |
* createrObjectReference(java.lang.Integer) |
323 | 368 |
*/ |
... | ... | |
333 | 378 |
* .Class) |
334 | 379 |
*/ |
335 | 380 |
public void registerClass(Class persistentClass) { |
336 |
classes.put(persistentClass.getName(), persistentClass); |
|
337 |
|
|
381 |
registerClass(persistentClass, null, null, null); |
|
338 | 382 |
} |
339 | 383 |
|
340 | 384 |
/* |
... | ... | |
400 | 444 |
* .Class, org.gvsig.tools.dynobject.DynStruct, java.lang.String) |
401 | 445 |
*/ |
402 | 446 |
public void registerClass(Class persistentClass, DynStruct definition, |
403 |
String namespace) { |
|
404 |
// TODO process namespace |
|
405 |
registerClass(persistentClass, definition); |
|
447 |
String domainName) { |
|
406 | 448 |
|
449 |
registerClass(persistentClass, definition, domainName, null); |
|
450 |
|
|
407 | 451 |
} |
408 | 452 |
|
409 | 453 |
/* |
410 | 454 |
* (non-Javadoc) |
411 | 455 |
* |
412 | 456 |
* @see |
457 |
* org.gvsig.tools.persistence.PersistenceManager#registerClass(java.lang |
|
458 |
* .Class, org.gvsig.tools.dynobject.DynStruct, java.lang.String) |
|
459 |
*/ |
|
460 |
public void registerClass(Class persistentClass, DynStruct definition, |
|
461 |
String domainName, String domainURL) { |
|
462 |
|
|
463 |
String domainURLStored = (String) domainsURL.get(domainName); |
|
464 |
String domainURLToUse; |
|
465 |
ClassInfo info = new ClassInfo(); |
|
466 |
info.definition = definition; |
|
467 |
info.theClass = persistentClass; |
|
468 |
if (domainName == null) { |
|
469 |
info.domainName = DEFAULT_DOMAIN_NAME; |
|
470 |
domainURLToUse = DEFAULT_DOMAIN_URL; |
|
471 |
} else { |
|
472 |
if (!VALID_DOMAIN_NAME_PATTERN.matcher(domainName).matches()) { |
|
473 |
throw new PersistenceRuntimeException( |
|
474 |
"invalid domain name identifier: '" + domainName + "'"); |
|
475 |
} |
|
476 |
info.domainName = domainName; |
|
477 |
if (domainURL == null) { |
|
478 |
if (domainURLStored == null) { |
|
479 |
domainURLToUse = DEFAULT_DOMAIN_URL; |
|
480 |
} else { |
|
481 |
domainURLToUse = domainURLStored; |
|
482 |
} |
|
483 |
} else { |
|
484 |
domainURLToUse = domainURL; |
|
485 |
} |
|
486 |
} |
|
487 |
|
|
488 |
if (domainURLStored == null) { |
|
489 |
domainsURL.put(info.domainName, domainURLToUse); |
|
490 |
} else if (!domainURLStored.equals(domainURLToUse)){ |
|
491 |
throw new PersistenceRuntimeException("Domain URL conflict in " |
|
492 |
+ info.domainName + ": " + domainURLStored + " " |
|
493 |
+ domainURLToUse); |
|
494 |
} |
|
495 |
|
|
496 |
classes.put(persistentClass.getName(), info); |
|
497 |
|
|
498 |
|
|
499 |
} |
|
500 |
|
|
501 |
|
|
502 |
/* |
|
503 |
* (non-Javadoc) |
|
504 |
* |
|
505 |
* @see |
|
413 | 506 |
* org.gvsig.tools.persistence.PersistenceManager#registerFactory(org.gvsig |
414 | 507 |
* .tools.persistence.PersistenceFactory) |
415 | 508 |
*/ |
... | ... | |
421 | 514 |
/* |
422 | 515 |
* (non-Javadoc) |
423 | 516 |
* |
424 |
* @seeorg.gvsig.tools.persistence.impl.ImplementationPersistenceManager# |
|
517 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#
|
|
425 | 518 |
* getFactoryFor(java.lang.Object) |
426 | 519 |
*/ |
427 | 520 |
public PersistenceFactory getFactoryFor(Object object) { |
... | ... | |
432 | 525 |
/* |
433 | 526 |
* (non-Javadoc) |
434 | 527 |
* |
435 |
* @seeorg.gvsig.tools.persistence.impl.ImplementationPersistenceManager# |
|
528 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#
|
|
436 | 529 |
* getFactoryFor(org.gvsig.tools.persistence.PersistentState) |
437 | 530 |
*/ |
438 | 531 |
public PersistenceFactory getFactoryFor(PersistentState state) { |
... | ... | |
455 | 548 |
/* |
456 | 549 |
* (non-Javadoc) |
457 | 550 |
* |
458 |
* @seeorg.gvsig.tools.persistence.impl.ImplementationPersistenceManager# |
|
551 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#
|
|
459 | 552 |
* getFactoryFor(java.lang.Class) |
460 | 553 |
*/ |
461 | 554 |
public PersistenceFactory getFactoryFor(Class klass) { |
... | ... | |
484 | 577 |
} |
485 | 578 |
|
486 | 579 |
} |
580 |
|
|
581 |
/* |
|
582 |
* (non-Javadoc) |
|
583 |
* |
|
584 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager# |
|
585 |
* getDomainName(java.lang.String) |
|
586 |
*/ |
|
587 |
public String getDomainName(String theClassName) |
|
588 |
throws PersistenceClassNotRegistered { |
|
589 |
ClassInfo info = (ClassInfo) classes.get(theClassName); |
|
590 |
if (info == null) { |
|
591 |
throw new PersistenceClassNotRegistered(theClassName); |
|
592 |
} |
|
593 |
return info.domainName; |
|
594 |
} |
|
595 |
|
|
596 |
/* |
|
597 |
* (non-Javadoc) |
|
598 |
* |
|
599 |
* @see |
|
600 |
* org.gvsig.tools.persistence.impl.ImplementationPersistenceManager#getDomains |
|
601 |
* () |
|
602 |
*/ |
|
603 |
public Map getDomains() { |
|
604 |
return Collections.unmodifiableMap(domainsURL); |
|
605 |
} |
|
606 |
|
|
607 |
/* |
|
608 |
* (non-Javadoc) |
|
609 |
* |
|
610 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager# |
|
611 |
* getDomainURL(java.lang.String) |
|
612 |
*/ |
|
613 |
public String getDomainURL(String domainName) { |
|
614 |
return (String) domainsURL.get(domainName); |
|
615 |
} |
|
616 |
|
|
617 |
/* |
|
618 |
* (non-Javadoc) |
|
619 |
* |
|
620 |
* @see org.gvsig.tools.persistence.impl.ImplementationPersistenceManager# |
|
621 |
* getDomainDefinitions(java.lang.String) |
|
622 |
*/ |
|
623 |
public Map getDomainDefinitions(String domainName) { |
|
624 |
if (!domainsURL.containsKey(domainName)) { |
|
625 |
throw new PersistenceRuntimeException("Domain not registered"); |
|
626 |
} |
|
627 |
Map definitions = new HashMap(); |
|
628 |
Entry entry; |
|
629 |
ClassInfo info; |
|
630 |
Iterator iter = classes.entrySet().iterator(); |
|
631 |
while (iter.hasNext()) { |
|
632 |
entry = (Entry) iter.next(); |
|
633 |
info = (ClassInfo) entry.getValue(); |
|
634 |
if (domainName.equals(info.domainName)) { |
|
635 |
definitions.put(entry.getKey(), info.definition); |
|
636 |
} |
|
637 |
} |
|
638 |
return definitions; |
|
639 |
} |
|
487 | 640 |
} |
Also available in: Unified diff