Revision 2021
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/impl/DefaultFactories.java | ||
---|---|---|
41 | 41 |
public class DefaultFactories extends ArrayList implements Factories { |
42 | 42 |
|
43 | 43 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultFactories.class); |
44 |
/**
|
|
45 |
*
|
|
46 |
*/
|
|
47 |
private static final long serialVersionUID = -3790704153805598236L;
|
|
44 |
/**
|
|
45 |
*
|
|
46 |
*/
|
|
47 |
private static final long serialVersionUID = -3790704153805598236L;
|
|
48 | 48 |
|
49 |
private static final Pattern VALID_DOMAIN_NAME_PATTERN = Pattern.compile("[\\w][\\d\\w_]*");
|
|
49 |
private static final Pattern VALID_DOMAIN_NAME_PATTERN = Pattern.compile("[\\w][\\d\\w_]*");
|
|
50 | 50 |
|
51 |
private Map classToFactoryCache;
|
|
51 |
private Map classToFactoryCache;
|
|
52 | 52 |
|
53 |
public DefaultFactories() {
|
|
54 |
this.classToFactoryCache = new HashMap();
|
|
55 |
this.add(new DynObjectPersistenceFactory());
|
|
56 |
}
|
|
53 |
public DefaultFactories() {
|
|
54 |
this.classToFactoryCache = new HashMap();
|
|
55 |
this.add(new DynObjectPersistenceFactory());
|
|
56 |
}
|
|
57 | 57 |
|
58 |
public boolean add(PersistenceFactory factory, String classNameAlias) { |
|
59 |
if( !super.contains(factory) ) { |
|
60 |
this.add(factory); |
|
61 |
} |
|
62 |
this.classToFactoryCache.put(classNameAlias, factory); |
|
63 |
return true; |
|
64 |
} |
|
65 |
|
|
66 |
public boolean add(PersistenceFactory factory) { |
|
67 |
if( factory.getDomainName() == null ) { |
|
68 |
throw new PersistenceInvalidDomainNameException(); |
|
69 |
} |
|
70 |
|
|
71 |
if (!VALID_DOMAIN_NAME_PATTERN.matcher(factory.getDomainName()).matches()) { |
|
72 |
throw new PersistenceInvalidDomainNameException(factory.getDomainName()); |
|
73 |
} |
|
58 |
public boolean add(PersistenceFactory factory, String classNameAlias) { |
|
59 |
if (!super.contains(factory)) { |
|
60 |
this.add(factory); |
|
61 |
} |
|
62 |
this.classToFactoryCache.put(classNameAlias, factory); |
|
63 |
return true; |
|
64 |
} |
|
74 | 65 |
|
75 |
List factoryManagedClasses = factory.getManagedClasses(); |
|
76 |
Iterator it = factoryManagedClasses.iterator(); |
|
77 |
while( it.hasNext() ) { |
|
78 |
Class theClass = (Class) it.next(); |
|
79 |
classToFactoryCache.put(theClass.getName(), factory); |
|
80 |
} |
|
81 |
if(this.size()<1) { |
|
82 |
super.add(factory); |
|
83 |
} else { |
|
84 |
super.add(this.size()-1, factory); |
|
85 |
} |
|
86 |
return true; |
|
87 |
} |
|
66 |
public boolean add(PersistenceFactory factory) { |
|
67 |
if (factory.getDomainName() == null) { |
|
68 |
throw new PersistenceInvalidDomainNameException(); |
|
69 |
} |
|
88 | 70 |
|
89 |
/** |
|
90 |
* Return the associated persistence factory to the java class name passed as parameter |
|
91 |
* @param className |
|
92 |
* @return |
|
93 |
*/ |
|
94 |
public PersistenceFactory get(String className) { |
|
95 |
PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className); |
|
96 |
if (factory != null) { |
|
97 |
return factory; |
|
98 |
} |
|
99 |
Iterator factoriesIterator = super.iterator(); |
|
100 |
while( factoriesIterator.hasNext() ) { |
|
101 |
factory = (PersistenceFactory) factoriesIterator.next(); |
|
102 |
if( factory.getDefinition(className)!=null ) { |
|
103 |
classToFactoryCache.put(className, factory); |
|
104 |
return factory; |
|
105 |
} |
|
106 |
} |
|
107 |
return null; |
|
108 |
} |
|
109 |
|
|
110 |
public PersistenceFactory get(Class theClass) { |
|
111 |
String className = theClass.getName(); |
|
112 |
PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className); |
|
113 |
if (factory != null) { |
|
114 |
LOG.debug("DefaultFactories.get2("+theClass.getName()+") --> "+factory.getClass().getName()+" (cached)."); |
|
115 |
return factory; |
|
116 |
} |
|
71 |
if (!VALID_DOMAIN_NAME_PATTERN.matcher(factory.getDomainName()).matches()) { |
|
72 |
throw new PersistenceInvalidDomainNameException(factory.getDomainName()); |
|
73 |
} |
|
74 |
|
|
75 |
List factoryManagedClasses = factory.getManagedClasses(); |
|
76 |
Iterator it = factoryManagedClasses.iterator(); |
|
77 |
while (it.hasNext()) { |
|
78 |
Class theClass = (Class) it.next(); |
|
79 |
classToFactoryCache.put(theClass.getName(), factory); |
|
80 |
} |
|
81 |
if (this.size() < 1) { |
|
82 |
super.add(factory); |
|
83 |
} else { |
|
84 |
super.add(this.size() - 1, factory); |
|
85 |
} |
|
86 |
return true; |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* Return the associated persistence factory to the java class name passed |
|
91 |
* as parameter |
|
92 |
* |
|
93 |
* @param className |
|
94 |
* @return |
|
95 |
*/ |
|
96 |
public PersistenceFactory get(String className) { |
|
97 |
PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className); |
|
98 |
if (factory != null) { |
|
99 |
return factory; |
|
100 |
} |
|
101 |
Iterator factoriesIterator = super.iterator(); |
|
102 |
while (factoriesIterator.hasNext()) { |
|
103 |
factory = (PersistenceFactory) factoriesIterator.next(); |
|
104 |
if (factory.getDefinition(className) != null) { |
|
105 |
classToFactoryCache.put(className, factory); |
|
106 |
return factory; |
|
107 |
} |
|
108 |
} |
|
109 |
return null; |
|
110 |
} |
|
111 |
|
|
112 |
public PersistenceFactory get(Class theClass) { |
|
113 |
String className = theClass.getName(); |
|
114 |
PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className); |
|
115 |
if (factory != null) { |
|
116 |
LOG.debug("DefaultFactories.get2(" + theClass.getName() + ") --> " + factory.getClass().getName() + " (cached)."); |
|
117 |
return factory; |
|
118 |
} |
|
117 | 119 |
List<PersistenceFactory> factories = new ArrayList<>(); |
118 |
Iterator factoriesIterator = super.iterator();
|
|
119 |
while( factoriesIterator.hasNext() ) {
|
|
120 |
factory = (PersistenceFactory) factoriesIterator.next();
|
|
121 |
if( factory.getDefinition(className)!=null || factory.manages(theClass)) {
|
|
120 |
Iterator factoriesIterator = super.iterator();
|
|
121 |
while (factoriesIterator.hasNext()) {
|
|
122 |
factory = (PersistenceFactory) factoriesIterator.next();
|
|
123 |
if (factory.getDefinition(className) != null || factory.manages(theClass)) {
|
|
122 | 124 |
factories.add(factory); |
123 |
}
|
|
124 |
}
|
|
125 |
if( factories.isEmpty() ) {
|
|
125 |
}
|
|
126 |
}
|
|
127 |
if (factories.isEmpty()) {
|
|
126 | 128 |
return null; |
127 | 129 |
} |
128 |
if( factories.size()==1 ) {
|
|
130 |
if (factories.size() == 1) {
|
|
129 | 131 |
factory = factories.get(0); |
130 | 132 |
classToFactoryCache.put(className, factory); |
131 |
LOG.debug("DefaultFactories.get2("+theClass.getName()+") --> "+factory.getClass().getName()+".");
|
|
133 |
LOG.debug("DefaultFactories.get2(" + theClass.getName() + ") --> " + factory.getClass().getName() + ".");
|
|
132 | 134 |
return factory; |
133 | 135 |
} |
134 |
for( PersistenceFactory factory1 : factories ) {
|
|
135 |
if( factory1.getManagedClasses().contains(theClass) ) {
|
|
136 |
for (PersistenceFactory factory1 : factories) {
|
|
137 |
if (factory1.getManagedClasses().contains(theClass)) {
|
|
136 | 138 |
classToFactoryCache.put(className, factory1); |
137 |
LOG.debug("DefaultFactories.get2("+theClass.getName()+") ==> "+factory1.getClass().getName()+".");
|
|
139 |
LOG.debug("DefaultFactories.get2(" + theClass.getName() + ") ==> " + factory1.getClass().getName() + ".");
|
|
138 | 140 |
return factory1; |
139 | 141 |
} |
140 | 142 |
} |
141 |
return null;
|
|
142 |
}
|
|
143 |
|
|
143 |
return null;
|
|
144 |
}
|
|
145 |
|
|
144 | 146 |
// public PersistenceFactory get(Class theClass) { |
145 | 147 |
// String className = theClass.getName(); |
146 | 148 |
// PersistenceFactory factory = (PersistenceFactory) classToFactoryCache.get(className); |
... | ... | |
159 | 161 |
// } |
160 | 162 |
// return null; |
161 | 163 |
// } |
162 |
|
|
163 |
public PersistenceFactory get(Object object) { |
|
164 |
if( object instanceof String) { |
|
165 |
return this.get((String)object); |
|
166 |
} |
|
167 |
return this.get(object.getClass()); |
|
168 |
} |
|
164 |
public PersistenceFactory get(Object object) { |
|
165 |
if (object instanceof String) { |
|
166 |
return this.get((String) object); |
|
167 |
} |
|
168 |
return this.get(object.getClass()); |
|
169 |
} |
|
169 | 170 |
|
170 |
public PersistenceFactory get(PersistentState state) {
|
|
171 |
return get(state.getTheClassName());
|
|
172 |
}
|
|
171 |
public PersistenceFactory get(PersistentState state) {
|
|
172 |
return get(state.getTheClassName());
|
|
173 |
}
|
|
173 | 174 |
|
174 | 175 |
} |
Also available in: Unified diff