gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynObjectManager.java @ 146
History | View | Annotate | Download (15.6 KB)
1 |
package org.gvsig.tools.dynobject.impl; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.InputStream; |
5 |
import java.util.Arrays; |
6 |
import java.util.Collections; |
7 |
import java.util.HashMap; |
8 |
import java.util.HashSet; |
9 |
import java.util.Iterator; |
10 |
import java.util.List; |
11 |
import java.util.Map; |
12 |
import java.util.Set; |
13 |
|
14 |
import org.gvsig.tools.ToolsLocator; |
15 |
import org.gvsig.tools.dynobject.DynClass; |
16 |
import org.gvsig.tools.dynobject.DynMethod; |
17 |
import org.gvsig.tools.dynobject.DynObject; |
18 |
import org.gvsig.tools.dynobject.DynObjectManager; |
19 |
import org.gvsig.tools.dynobject.DynObjectRuntimeException; |
20 |
import org.gvsig.tools.dynobject.DynStruct; |
21 |
import org.gvsig.tools.dynobject.exception.DuplicateDynClassException; |
22 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
23 |
import org.gvsig.tools.dynobject.exception.DynMethodIllegalCodeException; |
24 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
25 |
import org.gvsig.tools.dynobject.exception.IllegalDynMethodException; |
26 |
import org.gvsig.tools.dynobject.exception.IllegalDynMethodInvocationException; |
27 |
import org.slf4j.Logger; |
28 |
import org.slf4j.LoggerFactory; |
29 |
import org.xmlpull.v1.XmlPullParser; |
30 |
import org.xmlpull.v1.XmlPullParserException; |
31 |
|
32 |
public class DefaultDynObjectManager implements DynObjectManager { |
33 |
|
34 |
private final static Logger LOG = LoggerFactory.getLogger(DefaultDynObjectManager.class); |
35 |
|
36 |
private static DefaultDynObjectManager manager = null; |
37 |
|
38 |
private class MethodInfo { |
39 |
int code;
|
40 |
DynClass dynClass; |
41 |
DynMethod dynMethod; |
42 |
Class theClass;
|
43 |
|
44 |
MethodInfo(Class theClass, DynClass dynClass, DynMethod dynMethod, int code) { |
45 |
this.code = code;
|
46 |
this.dynClass = dynClass;
|
47 |
this.dynMethod = dynMethod;
|
48 |
this.theClass = theClass;
|
49 |
} |
50 |
|
51 |
String getKey() {
|
52 |
return DefaultDynObjectManager.getKey(theClass, dynClass, dynMethod);
|
53 |
} |
54 |
|
55 |
void check(Class theClass, int code) throws DynMethodException { |
56 |
if( code != this.code) { |
57 |
throw new DynMethodIllegalCodeException(dynMethod.getName(),this.code, code); |
58 |
} |
59 |
if( theClass != null ) { |
60 |
if( this.theClass == null ) { |
61 |
throw new IllegalDynMethodInvocationException(dynMethod.getName(), theClass); |
62 |
} |
63 |
if (!this.theClass.isAssignableFrom(theClass)) { |
64 |
throw new IllegalDynMethodInvocationException(dynMethod.getName(),theClass); |
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
void check(DynClass dynClass, int code) throws DynMethodException { |
70 |
if( code != this.code) { |
71 |
throw new DynMethodIllegalCodeException(dynMethod.getName(),this.code, code); |
72 |
} |
73 |
if( dynClass != null ) { |
74 |
if( this.dynClass == null ) { |
75 |
throw new IllegalDynMethodInvocationException(dynMethod.getName(), dynClass); |
76 |
} |
77 |
if( dynClass != this.dynClass || !dynClass.getName().equalsIgnoreCase(this.dynClass.getName()) ) { |
78 |
throw new IllegalDynMethodInvocationException(dynMethod.getName(), dynClass); |
79 |
} |
80 |
} |
81 |
} |
82 |
} |
83 |
|
84 |
private class ClassesNamespaces { |
85 |
private Map defaultNamespace; |
86 |
private Map namespaces; |
87 |
|
88 |
ClassesNamespaces() { |
89 |
this.namespaces = new HashMap(); |
90 |
this.defaultNamespace = new HashMap(); |
91 |
} |
92 |
|
93 |
public Map addNamespace(String name) { |
94 |
Map namespace = new HashMap(); |
95 |
this.namespaces.put(name.toLowerCase(), namespace);
|
96 |
return namespace;
|
97 |
} |
98 |
|
99 |
public Map getNamespace(String name) { |
100 |
return (Map) this.namespaces.get(name.toLowerCase()); |
101 |
} |
102 |
|
103 |
public void clear() { |
104 |
this.defaultNamespace.clear();
|
105 |
this.namespaces.clear();
|
106 |
} |
107 |
|
108 |
public boolean containsClass(String name) { |
109 |
name = name.toLowerCase(); |
110 |
if( this.defaultNamespace.containsKey(name)) { |
111 |
return true; |
112 |
} |
113 |
|
114 |
Iterator it = this.namespaces.values().iterator(); |
115 |
while( it.hasNext() ) {
|
116 |
Map names = (Map)it.next(); |
117 |
if( names.containsKey(name)) {
|
118 |
return true; |
119 |
} |
120 |
} |
121 |
return false; |
122 |
} |
123 |
|
124 |
public boolean containsClass(String namespace, String name) { |
125 |
name = name.toLowerCase(); |
126 |
if( namespace == null ) { |
127 |
return this.defaultNamespace.containsKey(name); |
128 |
} |
129 |
Map space = this.getNamespace(namespace); |
130 |
if( space == null ) { |
131 |
return false; |
132 |
} |
133 |
return space.containsKey(name);
|
134 |
} |
135 |
|
136 |
public boolean containsClass(DynClass dynClass) { |
137 |
if( this.defaultNamespace.containsValue(dynClass)) { |
138 |
return true; |
139 |
} |
140 |
|
141 |
Iterator it = this.namespaces.values().iterator(); |
142 |
while( it.hasNext() ) {
|
143 |
Map names = (Map)it.next(); |
144 |
if( names.containsValue(dynClass)) {
|
145 |
return true; |
146 |
} |
147 |
} |
148 |
return false; |
149 |
} |
150 |
|
151 |
public DynClass get(String name, String namespace) { |
152 |
if( namespace == null ) { |
153 |
return (DynClass) this.defaultNamespace.get(name.toLowerCase()); |
154 |
} |
155 |
Map space = this.getNamespace(namespace); |
156 |
if( space == null ) { |
157 |
return null; |
158 |
} |
159 |
return (DynClass) space.get(name.toLowerCase());
|
160 |
} |
161 |
|
162 |
public Set keySet() { |
163 |
Set keyset = new HashSet(); |
164 |
Iterator it = this.iterator(); |
165 |
while( it.hasNext() ) {
|
166 |
DynClass dynClass = (DynClass) it.next(); |
167 |
keyset.add( dynClass.getFullName()) ; |
168 |
} |
169 |
return keyset;
|
170 |
} |
171 |
|
172 |
public Iterator iterator() { |
173 |
final class MyIterator implements Iterator { |
174 |
Iterator current;
|
175 |
Iterator others;
|
176 |
MyIterator(Iterator main, Iterator others) { |
177 |
this.current = main;
|
178 |
this.others = others;
|
179 |
} |
180 |
public boolean hasNext() { |
181 |
if( this.current.hasNext() ) { |
182 |
return true; |
183 |
} |
184 |
while( this.others.hasNext() ) { |
185 |
this.current = (Iterator) others.next(); |
186 |
if( this.current.hasNext() ) { |
187 |
return true; |
188 |
} |
189 |
} |
190 |
return false; |
191 |
} |
192 |
|
193 |
public Object next() { |
194 |
if( this.current.hasNext() ) { |
195 |
return this.current.next(); |
196 |
} |
197 |
while( this.others.hasNext() ) { |
198 |
this.current = (Iterator) others.next(); |
199 |
if( this.current.hasNext() ) { |
200 |
return this.current.next(); |
201 |
} |
202 |
} |
203 |
return null; |
204 |
} |
205 |
|
206 |
public void remove() { |
207 |
throw new UnsupportedOperationException(); |
208 |
} |
209 |
} |
210 |
|
211 |
return new MyIterator(this.defaultNamespace.values().iterator(), this.namespaces.values().iterator()); |
212 |
} |
213 |
|
214 |
public Object add(DynStruct dynClass) { |
215 |
String name = dynClass.getName().toLowerCase();
|
216 |
Map namespace;
|
217 |
if( dynClass.getNamespace() != null ) { |
218 |
namespace = (Map) this.getNamespace(dynClass.getNamespace()); |
219 |
if( namespace == null ) { |
220 |
namespace = this.addNamespace(dynClass.getNamespace());
|
221 |
} |
222 |
} else {
|
223 |
namespace = this.defaultNamespace;
|
224 |
} |
225 |
if( namespace.containsKey(name) ) {
|
226 |
throw new DuplicateDynClassException(dynClass); |
227 |
} |
228 |
return namespace.put(name, dynClass);
|
229 |
} |
230 |
|
231 |
public void remove(DynStruct dynClass) { |
232 |
String name = dynClass.getName().toLowerCase();
|
233 |
Map namespace;
|
234 |
if( dynClass.getNamespace() != null ) { |
235 |
namespace = (Map) this.getNamespace(dynClass.getNamespace()); |
236 |
if( namespace == null ) { |
237 |
namespace = this.addNamespace(dynClass.getNamespace());
|
238 |
} |
239 |
} else {
|
240 |
namespace = this.defaultNamespace;
|
241 |
} |
242 |
if( namespace.containsKey(name) ) {
|
243 |
namespace.remove(name); |
244 |
} |
245 |
} |
246 |
|
247 |
public int size() { |
248 |
int count = this.defaultNamespace.size(); |
249 |
|
250 |
Iterator it = this.namespaces.values().iterator(); |
251 |
while( it.hasNext() ) {
|
252 |
Map names = (Map)it.next(); |
253 |
count += names.size(); |
254 |
} |
255 |
return count;
|
256 |
} |
257 |
|
258 |
|
259 |
} |
260 |
|
261 |
private Map anonymousClasses; |
262 |
private ClassesNamespaces classes;
|
263 |
private Map methodsMap; |
264 |
private MethodInfo[] methods; |
265 |
|
266 |
public static DefaultDynObjectManager getManager() { |
267 |
if (manager == null) { |
268 |
manager = new DefaultDynObjectManager();
|
269 |
} |
270 |
return manager;
|
271 |
} |
272 |
|
273 |
static String getKey(Class theClass, DynClass dynClass, DynMethod dynMethod) { |
274 |
return DefaultDynObjectManager.getKey(theClass, dynClass, dynMethod.getName());
|
275 |
} |
276 |
|
277 |
static String getKey(Class theClass, DynClass dynClass, String methodName) { |
278 |
if( dynClass == null ) { |
279 |
return theClass.getName() + ":" + methodName; |
280 |
} else {
|
281 |
return dynClass.getName() + ":" + methodName; |
282 |
} |
283 |
} |
284 |
|
285 |
|
286 |
public DefaultDynObjectManager() {
|
287 |
this.classes = new ClassesNamespaces(); |
288 |
this.anonymousClasses = new HashMap(); |
289 |
this.methodsMap = new HashMap(); |
290 |
this.methods = null; |
291 |
} |
292 |
|
293 |
public DynClass createDynClass(String name, String description) { |
294 |
return new DefaultDynClass(this, name, description); |
295 |
} |
296 |
|
297 |
public DynClass createDynClass(String namespace, String name, String description) { |
298 |
return new DefaultDynClass(this, namespace, name, description); |
299 |
} |
300 |
|
301 |
public void add(DynClass dynClass) { |
302 |
try {
|
303 |
((DefaultDynClass)dynClass).check(); |
304 |
} catch(Exception ex) { |
305 |
throw new DynObjectRuntimeException(ex); |
306 |
} |
307 |
this.classes.add( dynClass);
|
308 |
LOG.trace("Add DynClass definition {}.", new Object[] { dynClass.getFullName() }); |
309 |
|
310 |
} |
311 |
|
312 |
public DynClass add(String name, String description) { |
313 |
DynClass dynClass = (DynClass) this.classes.get(name.toLowerCase(), null); |
314 |
if (dynClass == null) { |
315 |
dynClass = this.createDynClass(name, description);
|
316 |
this.add(dynClass);
|
317 |
} |
318 |
return dynClass;
|
319 |
} |
320 |
|
321 |
public DynClass add(String name) { |
322 |
return this.add(name, null); |
323 |
} |
324 |
|
325 |
public void remove(DynStruct dynClass) { |
326 |
this.classes.remove(dynClass);
|
327 |
} |
328 |
|
329 |
// public static String getFullName(String namespace, String name) {
|
330 |
// if( namespace == null ) {
|
331 |
// return name;
|
332 |
// }
|
333 |
// return namespace + ":" + name;
|
334 |
// }
|
335 |
// public static String[] splitFullName(String fullname) {
|
336 |
// String[] name = new String[] { null, fullname };
|
337 |
// int x=fullname.indexOf(':');
|
338 |
// if( x>-1 ) {
|
339 |
// name[0] = fullname.substring(0, x);
|
340 |
// name[1] = fullname.substring(x+1);
|
341 |
// }
|
342 |
// return name;
|
343 |
//
|
344 |
// }
|
345 |
|
346 |
public DynClass get(String theName) { |
347 |
DynClassName name = new DynClassName(theName);
|
348 |
return this.get(name.getNamespace(),name.getName()); |
349 |
} |
350 |
|
351 |
public DynClass get(String namespace, String name ) { |
352 |
return (DynClass) this.classes.get(name,namespace); |
353 |
} |
354 |
|
355 |
public DynClass get(DynClass[] superClasses) { |
356 |
StringBuffer name = new StringBuffer(); |
357 |
for( int i=0; i<superClasses.length; i++) { |
358 |
name.append(superClasses[i].getName()).append("+");
|
359 |
} |
360 |
DefaultDynClass dynClass = (DefaultDynClass) this.anonymousClasses.get(name.toString());
|
361 |
if( dynClass == null ) { |
362 |
dynClass = new DefaultDynClass(this, name.toString(), null, superClasses); |
363 |
dynClass.setAnonymous(true);
|
364 |
} |
365 |
return dynClass;
|
366 |
} |
367 |
|
368 |
public int getCount() { |
369 |
return this.classes.size(); |
370 |
} |
371 |
|
372 |
public List getNames() { |
373 |
String[] names = (String[]) this.classes.keySet().toArray(); |
374 |
Arrays.sort(names);
|
375 |
return Collections.unmodifiableList(Arrays.asList(names)); |
376 |
} |
377 |
|
378 |
public boolean has(String name) { |
379 |
return this.classes.containsClass(name); |
380 |
} |
381 |
|
382 |
public boolean has(String namespace ,String name) { |
383 |
return this.classes.containsClass(namespace, name); |
384 |
} |
385 |
|
386 |
public Iterator interator() { |
387 |
return this.classes.iterator(); |
388 |
} |
389 |
public DynObject createDynObject(String dynClassName) { |
390 |
DynClassName name = new DynClassName(dynClassName);
|
391 |
return this.createDynObject(name.getName(), name.getNamespace()); |
392 |
} |
393 |
|
394 |
public DynObject createDynObject(String dynClassName, String namespace) { |
395 |
|
396 |
DynClass dynClass = (DynClass) this.classes.get(dynClassName, namespace);
|
397 |
if (dynClass == null) { |
398 |
throw new IllegalArgumentException("Can't locate class '"+ new DynClassName(namespace,dynClassName).getFullName()+"'."); |
399 |
} |
400 |
return this.createDynObject(dynClass); |
401 |
} |
402 |
|
403 |
public DynObject createDynObject(DynStruct dynClass) {
|
404 |
return new DefaultDynObject(dynClass); |
405 |
} |
406 |
|
407 |
public void consolide() { |
408 |
Iterator it = this.classes.iterator(); |
409 |
while( it.hasNext() ) {
|
410 |
DefaultDynClass dc = (DefaultDynClass) it.next(); |
411 |
dc.consolide(); |
412 |
} |
413 |
it = this.anonymousClasses.values().iterator();
|
414 |
while( it.hasNext() ) {
|
415 |
DefaultDynClass dc = (DefaultDynClass) it.next(); |
416 |
dc.consolide(); |
417 |
} |
418 |
} |
419 |
|
420 |
|
421 |
public int registerDynMethod(DynClass dynClass, DynMethod dynMethod) { |
422 |
((DefaultDynClass)dynClass).addMethod(dynMethod); |
423 |
return registerDynMethod(null, dynClass, dynMethod); |
424 |
} |
425 |
|
426 |
public int registerDynMethod(Class theClass, DynMethod dynMethod) { |
427 |
return registerDynMethod(theClass, null, dynMethod); |
428 |
} |
429 |
|
430 |
int registerDynMethod(Class theClass, DynClass dynClass, DynMethod dynMethod) { |
431 |
MethodInfo info = new MethodInfo(theClass, dynClass, dynMethod, 0); |
432 |
MethodInfo oldInfo = (MethodInfo) methodsMap.get(info.getKey()); |
433 |
if (oldInfo != null) { |
434 |
// Update the method info
|
435 |
oldInfo.dynClass = dynClass; |
436 |
oldInfo.dynMethod = dynMethod; |
437 |
return oldInfo.code;
|
438 |
} |
439 |
if (methods == null) { |
440 |
methods = new MethodInfo[1]; |
441 |
info.code = 0;
|
442 |
} else {
|
443 |
MethodInfo[] temp1 = new MethodInfo[methods.length + 1]; |
444 |
System.arraycopy(methods, 0, temp1, 0, methods.length); |
445 |
info.code = temp1.length - 1;
|
446 |
methods = temp1; |
447 |
} |
448 |
methods[info.code] = info; |
449 |
methodsMap.put(info.getKey(), info); |
450 |
|
451 |
return info.code;
|
452 |
} |
453 |
|
454 |
public Object invokeDynMethod(Object self, int code, DynObject context) throws DynMethodException{ |
455 |
|
456 |
try {
|
457 |
/*
|
458 |
* Intentamos ejecutar la operacion, y si peta ya haremos las
|
459 |
* comprobaciones oportunas para lanzar la excepcion que toque.
|
460 |
*
|
461 |
* Asi evitamos codigo de comprobacion para los casos que valla bien
|
462 |
* que deberian ser la mayoria.
|
463 |
*/
|
464 |
return methods[code].dynMethod.invoke(self, context);
|
465 |
} catch (RuntimeException e) { |
466 |
getDynMethod(self, code); |
467 |
throw e;
|
468 |
} catch (DynMethodException e) {
|
469 |
getDynMethod(self, code); |
470 |
throw e;
|
471 |
} |
472 |
|
473 |
} |
474 |
|
475 |
public int getDynMethodCode(DynClass dynClass, String methodName) throws DynMethodException { |
476 |
String key = DefaultDynObjectManager.getKey(null, dynClass, methodName); |
477 |
MethodInfo info = (MethodInfo) methodsMap.get(key); |
478 |
if( info == null ) { |
479 |
throw new IllegalDynMethodException(methodName, dynClass); |
480 |
} |
481 |
info.check(dynClass, info.code); |
482 |
return info.code;
|
483 |
} |
484 |
|
485 |
public int getDynMethodCode(Class theClass, String methodName) throws DynMethodException { |
486 |
String key = DefaultDynObjectManager.getKey(theClass, null, methodName); |
487 |
MethodInfo info = (MethodInfo) methodsMap.get(key); |
488 |
if( info == null ) { |
489 |
throw new IllegalDynMethodException(methodName, theClass); |
490 |
} |
491 |
info.check(theClass, info.code); |
492 |
return info.code;
|
493 |
} |
494 |
|
495 |
public DynMethod getDynMethod(int code) throws DynMethodException { |
496 |
if (code >= methods.length) {
|
497 |
throw new DynMethodNotSupportedException(code, "{null}"); |
498 |
} |
499 |
MethodInfo info = methods[code]; |
500 |
info.check((Class)null, code); |
501 |
return info.dynMethod;
|
502 |
} |
503 |
|
504 |
public DynMethod getDynMethod(Object obj, int code) |
505 |
throws DynMethodException {
|
506 |
return getDynMethod(obj.getClass(), code);
|
507 |
} |
508 |
|
509 |
public DynMethod getDynMethod(Class theClass, int code) |
510 |
throws DynMethodException {
|
511 |
if (code >= methods.length) {
|
512 |
throw new DynMethodNotSupportedException(code, theClass.getName()); |
513 |
} |
514 |
MethodInfo info = methods[code]; |
515 |
info.check(theClass, code); |
516 |
return info.dynMethod;
|
517 |
} |
518 |
|
519 |
public DynMethod getDynMethod(DynClass dynClass, int code) |
520 |
throws DynMethodException {
|
521 |
if (code >= methods.length) {
|
522 |
throw new DynMethodNotSupportedException(code, dynClass.getName()); |
523 |
} |
524 |
MethodInfo info = methods[code]; |
525 |
info.check(dynClass, code); |
526 |
return info.dynMethod;
|
527 |
} |
528 |
|
529 |
public DynMethod getDynMethod(DynObject dynObject, int code) |
530 |
throws DynMethodException {
|
531 |
return getDynMethod(dynObject.getDynClass(), code);
|
532 |
} |
533 |
|
534 |
public void validate(DynObject object) { |
535 |
// TODO
|
536 |
return;
|
537 |
} |
538 |
|
539 |
public Class getDefaultClassOfType(int type) { |
540 |
return ToolsLocator.getDataTypesManager().getDefaultClass(type);
|
541 |
} |
542 |
|
543 |
public Map importDynClassDefinitions(InputStream resource, |
544 |
ClassLoader loader) throws XmlPullParserException, IOException { |
545 |
return new DynClassImportHelper().importDefinitions(resource, loader, null); |
546 |
} |
547 |
|
548 |
public Map importDynClassDefinitions(XmlPullParser parser, |
549 |
ClassLoader loader, String defaultNamespace) throws XmlPullParserException, IOException { |
550 |
return new DynClassImportHelper().importDefinitions(parser, loader, defaultNamespace); |
551 |
} |
552 |
|
553 |
public Map importDynClassDefinitions(InputStream resource, |
554 |
ClassLoader loader, String defaultNamespace) |
555 |
throws XmlPullParserException, IOException { |
556 |
return new DynClassImportHelper().importDefinitions(resource, loader, defaultNamespace); |
557 |
} |
558 |
|
559 |
} |