Revision 301 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynObjectManager.java

View differences:

DefaultDynObjectManager.java
63 63
 */
64 64
public class DefaultDynObjectManager implements DynObjectManager {
65 65

  
66
	private final static Logger LOG = LoggerFactory.getLogger(DefaultDynObjectManager.class);
67
	
68
	private static DefaultDynObjectManager manager = null;
66
    private final static Logger LOG = LoggerFactory
67
        .getLogger(DefaultDynObjectManager.class);
69 68

  
70
	private class MethodInfo {
71
		int code;
72
		DynClass dynClass;
73
		DynMethod dynMethod;
74
		Class theClass;
69
    private static DefaultDynObjectManager manager = null;
75 70

  
76
		MethodInfo(Class theClass, DynClass dynClass, DynMethod dynMethod, int code) {
77
			this.code = code;
78
			this.dynClass = dynClass;
79
			this.dynMethod = dynMethod;
80
			this.theClass = theClass;
81
		}
71
    private class MethodInfo {
82 72

  
83
		String getKey() {
84
			return DefaultDynObjectManager.getKey(theClass, dynClass, dynMethod);
85
		}
73
        int code;
74
        DynClass dynClass;
75
        DynMethod dynMethod;
76
        Class theClass;
86 77

  
87
		void check(Class theClass, int code) throws DynMethodException {
88
			if( code != this.code) {
89
				throw new DynMethodIllegalCodeException(dynMethod.getName(),this.code, code);
90
			}
91
			if( theClass != null ) {
92
				if( this.theClass == null ) {
93
					throw new IllegalDynMethodInvocationException(dynMethod.getName(), theClass);
94
				}
95
				if (!this.theClass.isAssignableFrom(theClass)) {
96
					throw new IllegalDynMethodInvocationException(dynMethod.getName(),theClass);
97
				}
98
			}
99
		}
78
        MethodInfo(Class theClass, DynClass dynClass, DynMethod dynMethod,
79
            int code) {
80
            this.code = code;
81
            this.dynClass = dynClass;
82
            this.dynMethod = dynMethod;
83
            this.theClass = theClass;
84
        }
100 85

  
101
		void check(DynClass dynClass, int code) throws DynMethodException  {
102
			if( code != this.code) {
103
				throw new DynMethodIllegalCodeException(dynMethod.getName(),this.code, code);
104
			}
105
			if( dynClass != null ) {
106
				if( this.dynClass == null ) {
107
					throw new IllegalDynMethodInvocationException(dynMethod.getName(), dynClass);
108
				}
109
				if( dynClass != this.dynClass || !dynClass.getName().equalsIgnoreCase(this.dynClass.getName()) ) {
110
					throw new IllegalDynMethodInvocationException(dynMethod.getName(), dynClass);
111
				}
112
			}
113
		}
114
	}
86
        String getKey() {
87
            return DefaultDynObjectManager
88
                .getKey(theClass, dynClass, dynMethod);
89
        }
115 90

  
116
	private class ClassesNamespaces  {
117
		private Map defaultNamespace;
118
		private Map namespaces;
119
		
120
		ClassesNamespaces() {
121
			this.namespaces = new HashMap();
122
			this.defaultNamespace = new HashMap();
123
		}
124
		
125
		public Map addNamespace(String name) {
126
			Map namespace = new HashMap();
127
			this.namespaces.put(name.toLowerCase(), namespace);
128
			return namespace;
129
		}
130
		
131
		public Map getNamespace(String name) {
132
			return (Map) this.namespaces.get(name.toLowerCase());
133
		}
91
        void check(Class theClass, int code) throws DynMethodException {
92
            if (code != this.code) {
93
                throw new DynMethodIllegalCodeException(dynMethod.getName(),
94
                    this.code, code);
95
            }
96
            if (theClass != null) {
97
                if (this.theClass == null) {
98
                    throw new IllegalDynMethodInvocationException(
99
                        dynMethod.getName(), theClass);
100
                }
101
                if (!this.theClass.isAssignableFrom(theClass)) {
102
                    throw new IllegalDynMethodInvocationException(
103
                        dynMethod.getName(), theClass);
104
                }
105
            }
106
        }
134 107

  
135
		public void clear() {
136
			this.defaultNamespace.clear();
137
			this.namespaces.clear();
138
		}
108
        void check(DynClass dynClass, int code) throws DynMethodException {
109
            if (code != this.code) {
110
                throw new DynMethodIllegalCodeException(dynMethod.getName(),
111
                    this.code, code);
112
            }
113
            if (dynClass != null) {
114
                if (this.dynClass == null) {
115
                    throw new IllegalDynMethodInvocationException(
116
                        dynMethod.getName(), dynClass);
117
                }
118
                if (dynClass != this.dynClass
119
                    || !dynClass.getName().equalsIgnoreCase(
120
                        this.dynClass.getName())) {
121
                    throw new IllegalDynMethodInvocationException(
122
                        dynMethod.getName(), dynClass);
123
                }
124
            }
125
        }
126
    }
139 127

  
140
		public boolean containsClass(String name) {
141
			name = name.toLowerCase();
142
			if( this.defaultNamespace.containsKey(name)) {
143
				return true;
144
			}
145
			
146
			Iterator it = this.namespaces.values().iterator();
147
			while( it.hasNext() ) {
148
				Map names = (Map)it.next();
149
				if( names.containsKey(name)) {
150
					return true;
151
				}
152
			}
153
			return false;
154
		}
128
    private class ClassesNamespaces {
155 129

  
156
		public boolean containsClass(String namespace, String name) {
157
			name = name.toLowerCase();
158
			if( namespace == null ) {
159
				return this.defaultNamespace.containsKey(name);
160
			}
161
			Map space = this.getNamespace(namespace);
162
			if( space == null ) {
163
				return false;
164
			}
165
			return space.containsKey(name);
166
		}
130
        private Map defaultNamespace;
131
        private Map namespaces;
167 132

  
168
		public boolean containsClass(DynClass dynClass) {
169
			if( this.defaultNamespace.containsValue(dynClass)) {
170
				return true;
171
			}
172
			
173
			Iterator it = this.namespaces.values().iterator();
174
			while( it.hasNext() ) {
175
				Map names = (Map)it.next();
176
				if( names.containsValue(dynClass)) {
177
					return true;
178
				}
179
			}
180
			return false;
181
		}
133
        ClassesNamespaces() {
134
            this.namespaces = new HashMap();
135
            this.defaultNamespace = new HashMap();
136
        }
182 137

  
183
		public DynClass get(String name, String namespace) {
184
			if( namespace == null ) {
185
				return (DynClass) this.defaultNamespace.get(name.toLowerCase());
186
			}
187
			Map space = this.getNamespace(namespace);
188
			if( space == null ) {
189
				return null;
190
			}
191
			return (DynClass) space.get(name.toLowerCase());
192
		}
193
		
194
		public Set keySet() {
195
			Set keyset = new HashSet();
196
			Iterator it = this.iterator();
197
			while( it.hasNext() ) {
198
				DynClass dynClass = (DynClass) it.next();
199
				keyset.add( dynClass.getFullName()) ;
200
			}
201
			return keyset;
202
		}
138
        public Map addNamespace(String name) {
139
            Map namespace = new HashMap();
140
            this.namespaces.put(name.toLowerCase(), namespace);
141
            return namespace;
142
        }
203 143

  
204
		public Iterator iterator() {
205
			final class MyIterator implements Iterator {
206
				Iterator current;
207
				Iterator others;
208
				MyIterator(Iterator main, Iterator others) {
209
					this.current = main;
210
					this.others = others;
211
				}
212
				public boolean hasNext() {
213
					if( this.current.hasNext() ) {
214
						return true;
215
					}
216
					while( this.others.hasNext() )  {
217
						this.current = (Iterator) others.next();
218
						if( this.current.hasNext() ) {
219
							return true;
220
						}
221
					}
222
					return false;
223
				}
224
				
225
				public Object next() {
226
					if( this.current.hasNext() ) {
227
						return this.current.next();
228
					}
229
					while( this.others.hasNext() )  {
230
						this.current = (Iterator) others.next();
231
						if( this.current.hasNext() ) {
232
							return this.current.next();
233
						}
234
					}
235
					return null;
236
				}
237
				
238
				public void remove() {
239
					throw new UnsupportedOperationException();
240
				}
241
			}
242
			
243
			return new MyIterator(this.defaultNamespace.values().iterator(), this.namespaces.values().iterator());
244
		}
245
		
246
		public Object add(DynStruct dynClass) {
247
			String name = dynClass.getName().toLowerCase();
248
			Map namespace;
249
			if( dynClass.getNamespace() != null ) {
250
				namespace = (Map) this.getNamespace(dynClass.getNamespace());
251
				if( namespace == null ) {
252
					namespace = this.addNamespace(dynClass.getNamespace());
253
				}
254
			} else {
255
				namespace = this.defaultNamespace;
256
			}
257
			if( namespace.containsKey(name) ) {
258
				throw new DuplicateDynClassException(dynClass);
259
			}
260
			return namespace.put(name, dynClass);
261
		}
262
		
263
		public void remove(DynStruct dynClass) {
264
			String name = dynClass.getName().toLowerCase();
265
			Map namespace;
266
			if( dynClass.getNamespace() != null ) {
267
				namespace = (Map) this.getNamespace(dynClass.getNamespace());
268
				if( namespace == null ) {
269
					namespace = this.addNamespace(dynClass.getNamespace());
270
				}
271
			} else {
272
				namespace = this.defaultNamespace;
273
			}
274
			if( namespace.containsKey(name) ) {
275
				namespace.remove(name);
276
			}
277
		}
278
		
279
		public int size() {
280
			int count = this.defaultNamespace.size();
281
			
282
			Iterator it = this.namespaces.values().iterator();
283
			while( it.hasNext() ) {
284
				Map names = (Map)it.next();
285
				count += names.size();
286
			}
287
			return count;
288
		}
144
        public Map getNamespace(String name) {
145
            return (Map) this.namespaces.get(name.toLowerCase());
146
        }
289 147

  
290
		
291
	}
292
	
293
	private Map anonymousClasses;
294
	private ClassesNamespaces classes;
295
	private Map methodsMap;
296
	private MethodInfo[] methods;
148
        public void clear() {
149
            this.defaultNamespace.clear();
150
            this.namespaces.clear();
151
        }
297 152

  
298
	public static DefaultDynObjectManager getManager() {
299
		if (manager == null) {
300
			manager = new DefaultDynObjectManager();
301
		}
302
		return manager;
303
	}
153
        public boolean containsClass(String name) {
154
            name = name.toLowerCase();
155
            if (this.defaultNamespace.containsKey(name)) {
156
                return true;
157
            }
304 158

  
159
            Iterator it = this.namespaces.values().iterator();
160
            while (it.hasNext()) {
161
                Map names = (Map) it.next();
162
                if (names.containsKey(name)) {
163
                    return true;
164
                }
165
            }
166
            return false;
167
        }
168

  
169
        public boolean containsClass(String namespace, String name) {
170
            name = name.toLowerCase();
171
            if (namespace == null) {
172
                return this.defaultNamespace.containsKey(name);
173
            }
174
            Map space = this.getNamespace(namespace);
175
            if (space == null) {
176
                return false;
177
            }
178
            return space.containsKey(name);
179
        }
180

  
181
        public boolean containsClass(DynClass dynClass) {
182
            if (this.defaultNamespace.containsValue(dynClass)) {
183
                return true;
184
            }
185

  
186
            Iterator it = this.namespaces.values().iterator();
187
            while (it.hasNext()) {
188
                Map names = (Map) it.next();
189
                if (names.containsValue(dynClass)) {
190
                    return true;
191
                }
192
            }
193
            return false;
194
        }
195

  
196
        public DynClass get(String name, String namespace) {
197
            if (namespace == null) {
198
                return (DynClass) this.defaultNamespace.get(name.toLowerCase());
199
            }
200
            Map space = this.getNamespace(namespace);
201
            if (space == null) {
202
                return null;
203
            }
204
            return (DynClass) space.get(name.toLowerCase());
205
        }
206

  
207
        public Set keySet() {
208
            Set keyset = new HashSet();
209
            Iterator it = this.iterator();
210
            while (it.hasNext()) {
211
                DynClass dynClass = (DynClass) it.next();
212
                keyset.add(dynClass.getFullName());
213
            }
214
            return keyset;
215
        }
216

  
217
        public Iterator iterator() {
218
            final class MyIterator implements Iterator {
219

  
220
                Iterator current;
221
                Iterator others;
222

  
223
                MyIterator(Iterator main, Iterator others) {
224
                    this.current = main;
225
                    this.others = others;
226
                }
227

  
228
                public boolean hasNext() {
229
                    if (this.current.hasNext()) {
230
                        return true;
231
                    }
232
                    while (this.others.hasNext()) {
233
                        this.current = (Iterator) others.next();
234
                        if (this.current.hasNext()) {
235
                            return true;
236
                        }
237
                    }
238
                    return false;
239
                }
240

  
241
                public Object next() {
242
                    if (this.current.hasNext()) {
243
                        return this.current.next();
244
                    }
245
                    while (this.others.hasNext()) {
246
                        this.current = (Iterator) others.next();
247
                        if (this.current.hasNext()) {
248
                            return this.current.next();
249
                        }
250
                    }
251
                    return null;
252
                }
253

  
254
                public void remove() {
255
                    throw new UnsupportedOperationException();
256
                }
257
            }
258

  
259
            return new MyIterator(this.defaultNamespace.values().iterator(),
260
                this.namespaces.values().iterator());
261
        }
262

  
263
        public Object add(DynStruct dynClass) {
264
            String name = dynClass.getName().toLowerCase();
265
            Map namespace;
266
            if (dynClass.getNamespace() != null) {
267
                namespace = (Map) this.getNamespace(dynClass.getNamespace());
268
                if (namespace == null) {
269
                    namespace = this.addNamespace(dynClass.getNamespace());
270
                }
271
            } else {
272
                namespace = this.defaultNamespace;
273
            }
274
            if (namespace.containsKey(name)) {
275
                throw new DuplicateDynClassException(dynClass);
276
            }
277
            return namespace.put(name, dynClass);
278
        }
279

  
280
        public void remove(DynStruct dynClass) {
281
            String name = dynClass.getName().toLowerCase();
282
            Map namespace;
283
            if (dynClass.getNamespace() != null) {
284
                namespace = (Map) this.getNamespace(dynClass.getNamespace());
285
                if (namespace == null) {
286
                    namespace = this.addNamespace(dynClass.getNamespace());
287
                }
288
            } else {
289
                namespace = this.defaultNamespace;
290
            }
291
            if (namespace.containsKey(name)) {
292
                namespace.remove(name);
293
            }
294
        }
295

  
296
        public int size() {
297
            int count = this.defaultNamespace.size();
298

  
299
            Iterator it = this.namespaces.values().iterator();
300
            while (it.hasNext()) {
301
                Map names = (Map) it.next();
302
                count += names.size();
303
            }
304
            return count;
305
        }
306

  
307
    }
308

  
309
    private Map anonymousClasses;
310
    private ClassesNamespaces classes;
311
    private Map methodsMap;
312
    private MethodInfo[] methods;
313

  
314
    public static DefaultDynObjectManager getManager() {
315
        if (manager == null) {
316
            manager = new DefaultDynObjectManager();
317
        }
318
        return manager;
319
    }
320

  
305 321
    static String getKey(Class theClass, DynClass dynClass, DynMethod dynMethod) {
306
    	return DefaultDynObjectManager.getKey(theClass, dynClass, dynMethod.getName());
322
        return DefaultDynObjectManager.getKey(theClass, dynClass,
323
            dynMethod.getName());
307 324
    }
308 325

  
309 326
    static String getKey(Class theClass, DynClass dynClass, String methodName) {
310
		if( dynClass == null ) {
311
			return theClass.getName() + ":" + methodName;
312
		} else {
313
			return dynClass.getName() + ":" + methodName;
314
		}
327
        if (dynClass == null) {
328
            return theClass.getName() + ":" + methodName;
329
        } else {
330
            return dynClass.getName() + ":" + methodName;
331
        }
315 332
    }
316 333

  
334
    public DefaultDynObjectManager() {
335
        this.classes = new ClassesNamespaces();
336
        this.anonymousClasses = new HashMap();
337
        this.methodsMap = new HashMap();
338
        this.methods = null;
339
    }
317 340

  
318
	public DefaultDynObjectManager() {
319
		this.classes = new ClassesNamespaces();
320
		this.anonymousClasses = new HashMap();
321
		this.methodsMap = new HashMap();
322
		this.methods = null;
323
	}
341
    public DynClass createDynClass(String name, String description) {
342
        return new DefaultDynClass(this, name, description);
343
    }
324 344

  
325
	public DynClass createDynClass(String name, String description) {
326
		return new DefaultDynClass(this, name, description);
327
	}
345
    public DynClass createDynClass(String namespace, String name,
346
        String description) {
347
        return new DefaultDynClass(this, namespace, name, description);
348
    }
328 349

  
329
	public DynClass createDynClass(String namespace, String name, String description) {
330
		return new DefaultDynClass(this, namespace, name, description);
331
	}
350
    public void add(DynClass dynClass) {
351
        try {
352
            ((DefaultDynClass) dynClass).check();
353
        } catch (Exception ex) {
354
            throw new DynObjectRuntimeException(ex);
355
        }
356
        this.classes.add(dynClass);
357
        LOG.trace("Add DynClass definition {}.",
358
            new Object[] { dynClass.getFullName() });
332 359

  
333
	public void add(DynClass dynClass) {
334
		try {
335
			((DefaultDynClass)dynClass).check();
336
		} catch(Exception ex) {
337
			throw new DynObjectRuntimeException(ex);
338
		}
339
		this.classes.add( dynClass);
340
		LOG.trace("Add DynClass definition {}.", new Object[] { dynClass.getFullName() });
360
    }
341 361

  
342
	}
362
    public DynClass add(String name, String description) {
363
        DynClass dynClass =
364
            (DynClass) this.classes.get(name.toLowerCase(), null);
365
        if (dynClass == null) {
366
            dynClass = this.createDynClass(name, description);
367
            this.add(dynClass);
368
        }
369
        return dynClass;
370
    }
343 371

  
344
	public DynClass add(String name, String description) {
345
		DynClass dynClass = (DynClass) this.classes.get(name.toLowerCase(), null);
346
		if (dynClass == null) {
347
			dynClass = this.createDynClass(name, description);
348
			this.add(dynClass);
349
		}
350
		return dynClass;
351
	}
372
    public DynClass add(String name) {
373
        return this.add(name, null);
374
    }
352 375

  
353
	public DynClass add(String name) {
354
		return this.add(name, null);
355
	}
376
    public void remove(DynStruct dynClass) {
377
        this.classes.remove(dynClass);
378
    }
356 379

  
357
	public void remove(DynStruct dynClass)  {
358
		this.classes.remove(dynClass);
359
	}
380
    // public static String getFullName(String namespace, String name) {
381
    // if( namespace == null ) {
382
    // return name;
383
    // }
384
    // return namespace + ":" + name;
385
    // }
386
    // public static String[] splitFullName(String fullname) {
387
    // String[] name = new String[] { null, fullname };
388
    // int x=fullname.indexOf(':');
389
    // if( x>-1 ) {
390
    // name[0] = fullname.substring(0, x);
391
    // name[1] = fullname.substring(x+1);
392
    // }
393
    // return name;
394
    //
395
    // }
360 396

  
361
//	public static String  getFullName(String namespace, String name) {
362
//    	if( namespace == null ) {
363
//    		return name;
364
//    	}
365
//    	return namespace + ":" + name;
366
//    }
367
//	public static String[] splitFullName(String fullname) {
368
//		String[] name = new String[] { null, fullname };
369
//		int x=fullname.indexOf(':');
370
//		if( x>-1 ) {
371
//			name[0] = fullname.substring(0, x);
372
//			name[1] = fullname.substring(x+1);
373
//		}
374
//		return name;
375
//		
376
//	}
377
	
378
	public DynClass get(String theName) {
379
		DynClassName name = createDynClassName(theName);
380
		return this.get(name.getNamespace(),name.getName());
381
	}
397
    public DynClass get(String theName) {
398
        DynClassName name = createDynClassName(theName);
399
        return this.get(name.getNamespace(), name.getName());
400
    }
382 401

  
383
	public DynClass get(String namespace, String name ) {
384
		return (DynClass) this.classes.get(name,namespace);
385
	}
402
    public DynClass get(String namespace, String name) {
403
        return (DynClass) this.classes.get(name, namespace);
404
    }
386 405

  
387
	public DynClass get(DynClass[] superClasses) {
388
		StringBuffer name = new StringBuffer();
389
		for( int i=0; i<superClasses.length; i++) {
390
			name.append(superClasses[i].getName()).append("+");
391
		}
392
		DefaultDynClass dynClass = (DefaultDynClass) this.anonymousClasses.get(name.toString());
393
		if( dynClass == null ) {
394
			dynClass = new DefaultDynClass(this, name.toString(), null, superClasses);
395
			dynClass.setAnonymous(true);
396
		}
397
		return dynClass;
398
	}
406
    public DynClass get(DynClass[] superClasses) {
407
        StringBuffer name = new StringBuffer();
408
        for (int i = 0; i < superClasses.length; i++) {
409
            name.append(superClasses[i].getName()).append("+");
410
        }
411
        DefaultDynClass dynClass =
412
            (DefaultDynClass) this.anonymousClasses.get(name.toString());
413
        if (dynClass == null) {
414
            dynClass =
415
                new DefaultDynClass(this, name.toString(), null, superClasses);
416
            dynClass.setAnonymous(true);
417
        }
418
        return dynClass;
419
    }
399 420

  
400
	public int getCount() {
401
		return this.classes.size();
402
	}
421
    public int getCount() {
422
        return this.classes.size();
423
    }
403 424

  
404
	public List getNames() {
405
		String[] names = (String[]) this.classes.keySet().toArray();
406
		Arrays.sort(names);
407
		return Collections.unmodifiableList(Arrays.asList(names));
408
	}
425
    public List getNames() {
426
        String[] names = (String[]) this.classes.keySet().toArray();
427
        Arrays.sort(names);
428
        return Collections.unmodifiableList(Arrays.asList(names));
429
    }
409 430

  
410
	public boolean has(String name) {
411
		return this.classes.containsClass(name);
412
	}
431
    public boolean has(String name) {
432
        return this.classes.containsClass(name);
433
    }
413 434

  
414
	public boolean has(String namespace ,String name) {
415
		return this.classes.containsClass(namespace, name);
416
	}
435
    public boolean has(String namespace, String name) {
436
        return this.classes.containsClass(namespace, name);
437
    }
417 438

  
418
	public Iterator interator() {
419
		return this.classes.iterator();
420
	}
421
	public DynObject createDynObject(String dynClassName) {
422
		DynClassName name = createDynClassName(dynClassName);
423
		return this.createDynObject(name.getName(), name.getNamespace());
424
	}
439
    public Iterator interator() {
440
        return this.classes.iterator();
441
    }
425 442

  
426
	public DynObject createDynObject(String dynClassName, String namespace) {
427
		
428
		DynClass dynClass = (DynClass) this.classes.get(dynClassName, namespace);
429
		if (dynClass == null) {
430
			throw new IllegalArgumentException("Can't locate class '"
431
					+ createDynClassName(namespace, dynClassName).getFullName()
432
					+ "'.");
433
		}
434
		return this.createDynObject(dynClass);
435
	}
443
    public DynObject createDynObject(String dynClassName) {
444
        DynClassName name = createDynClassName(dynClassName);
445
        return this.createDynObject(name.getName(), name.getNamespace());
446
    }
436 447

  
437
	public DynObject createDynObject(DynStruct dynClass) {
438
		return new DefaultDynObject(dynClass);
439
	}
448
    public DynObject createDynObject(String dynClassName, String namespace) {
440 449

  
441
	public void consolide() {
442
		Iterator it = this.classes.iterator();
443
		while( it.hasNext() ) {
444
			DefaultDynClass dc = (DefaultDynClass) it.next();
445
			dc.consolide();
446
		}
447
		it = this.anonymousClasses.values().iterator();
448
		while( it.hasNext() ) {
449
			DefaultDynClass dc = (DefaultDynClass) it.next();
450
			dc.consolide();
451
		}
452
	}
450
        DynClass dynClass =
451
            (DynClass) this.classes.get(dynClassName, namespace);
452
        if (dynClass == null) {
453
            throw new IllegalArgumentException("Can't locate class '"
454
                + createDynClassName(namespace, dynClassName).getFullName()
455
                + "'.");
456
        }
457
        return this.createDynObject(dynClass);
458
    }
453 459

  
460
    public DynObject createDynObject(DynStruct dynClass) {
461
        return new DefaultDynObject(dynClass);
462
    }
454 463

  
455
	public int registerDynMethod(DynClass dynClass, DynMethod dynMethod) {
456
		((DefaultDynClass)dynClass).addMethod(dynMethod);
457
		return registerDynMethod(null, dynClass, dynMethod);
458
	}
464
    public void consolide() {
465
        Iterator it = this.classes.iterator();
466
        while (it.hasNext()) {
467
            DefaultDynClass dc = (DefaultDynClass) it.next();
468
            dc.consolide();
469
        }
470
        it = this.anonymousClasses.values().iterator();
471
        while (it.hasNext()) {
472
            DefaultDynClass dc = (DefaultDynClass) it.next();
473
            dc.consolide();
474
        }
475
    }
459 476

  
460
	public int registerDynMethod(Class theClass, DynMethod dynMethod) {
461
		return registerDynMethod(theClass, null, dynMethod);
462
	}
477
    public int registerDynMethod(DynClass dynClass, DynMethod dynMethod) {
478
        ((DefaultDynClass) dynClass).addMethod(dynMethod);
479
        return registerDynMethod(null, dynClass, dynMethod);
480
    }
463 481

  
464
	int registerDynMethod(Class theClass, DynClass dynClass, DynMethod dynMethod) {
465
		MethodInfo info = new MethodInfo(theClass, dynClass, dynMethod, 0);
466
		MethodInfo oldInfo = (MethodInfo) methodsMap.get(info.getKey());
467
		if (oldInfo != null) {
468
			// Update the method info
469
			oldInfo.dynClass = dynClass;
470
			oldInfo.dynMethod = dynMethod;
471
			return oldInfo.code;
472
		}
473
		if (methods == null) {
474
			methods = new MethodInfo[1];
475
			info.code = 0;
476
		} else {
477
			MethodInfo[] temp1 = new MethodInfo[methods.length + 1];
478
			System.arraycopy(methods, 0, temp1, 0, methods.length);
479
			info.code = temp1.length - 1;
480
			methods = temp1;
481
		}
482
		methods[info.code] = info;
483
		methodsMap.put(info.getKey(), info);
482
    public int registerDynMethod(Class theClass, DynMethod dynMethod) {
483
        return registerDynMethod(theClass, null, dynMethod);
484
    }
484 485

  
485
		return info.code;
486
	}
486
    int registerDynMethod(Class theClass, DynClass dynClass, DynMethod dynMethod) {
487
        MethodInfo info = new MethodInfo(theClass, dynClass, dynMethod, 0);
488
        MethodInfo oldInfo = (MethodInfo) methodsMap.get(info.getKey());
489
        if (oldInfo != null) {
490
            // Update the method info
491
            oldInfo.dynClass = dynClass;
492
            oldInfo.dynMethod = dynMethod;
493
            return oldInfo.code;
494
        }
495
        if (methods == null) {
496
            methods = new MethodInfo[1];
497
            info.code = 0;
498
        } else {
499
            MethodInfo[] temp1 = new MethodInfo[methods.length + 1];
500
            System.arraycopy(methods, 0, temp1, 0, methods.length);
501
            info.code = temp1.length - 1;
502
            methods = temp1;
503
        }
504
        methods[info.code] = info;
505
        methodsMap.put(info.getKey(), info);
487 506

  
488
	public Object invokeDynMethod(Object self, int code, DynObject context) throws DynMethodException{
507
        return info.code;
508
    }
489 509

  
490
		try {
491
			/*
492
			 * Intentamos ejecutar la operacion, y si peta ya haremos las
493
			 * comprobaciones oportunas para lanzar la excepcion que toque.
494
			 *
495
			 * Asi evitamos codigo de comprobacion para los casos que valla bien
496
			 * que deberian ser la mayoria.
497
			 */
498
			return methods[code].dynMethod.invoke(self, context);
499
		} catch (RuntimeException e) {
500
			getDynMethod(self, code);
501
			throw e;
502
		} catch (DynMethodException e) {
503
			getDynMethod(self, code);
504
			throw e;
505
		}
510
    public Object invokeDynMethod(Object self, int code, DynObject context)
511
        throws DynMethodException {
506 512

  
507
	}
513
        try {
514
            /*
515
             * Intentamos ejecutar la operacion, y si peta ya haremos las
516
             * comprobaciones oportunas para lanzar la excepcion que toque.
517
             * 
518
             * Asi evitamos codigo de comprobacion para los casos que valla bien
519
             * que deberian ser la mayoria.
520
             */
521
            return methods[code].dynMethod.invoke(self, context);
522
        } catch (RuntimeException e) {
523
            getDynMethod(self, code);
524
            throw e;
525
        } catch (DynMethodException e) {
526
            getDynMethod(self, code);
527
            throw e;
528
        }
508 529

  
509
	public int getDynMethodCode(DynClass dynClass, String methodName) throws DynMethodException  {
510
		String key = DefaultDynObjectManager.getKey(null, dynClass, methodName);
511
		MethodInfo info = (MethodInfo) methodsMap.get(key);
512
		if( info == null ) {
513
			throw new IllegalDynMethodException(methodName, dynClass);
514
		}
515
		info.check(dynClass, info.code);
516
		return info.code;
517
	}
530
    }
518 531

  
519
	public int getDynMethodCode(Class theClass, String methodName) throws DynMethodException {
520
		String key = DefaultDynObjectManager.getKey(theClass, null, methodName);
521
		MethodInfo info = (MethodInfo) methodsMap.get(key);
522
		if( info == null ) {
523
			throw new IllegalDynMethodException(methodName, theClass);
524
		}
525
		info.check(theClass, info.code);
526
		return info.code;
527
	}
532
    public int getDynMethodCode(DynClass dynClass, String methodName)
533
        throws DynMethodException {
534
        String key = DefaultDynObjectManager.getKey(null, dynClass, methodName);
535
        MethodInfo info = (MethodInfo) methodsMap.get(key);
536
        if (info == null) {
537
            throw new IllegalDynMethodException(methodName, dynClass);
538
        }
539
        info.check(dynClass, info.code);
540
        return info.code;
541
    }
528 542

  
529
	public DynMethod getDynMethod(int code) throws DynMethodException {
530
		if (code >= methods.length) {
531
			throw new DynMethodNotSupportedException(code, "{null}");
532
		}
533
		MethodInfo info = methods[code];
534
		info.check((Class)null, code);
535
		return info.dynMethod;
536
	}
543
    public int getDynMethodCode(Class theClass, String methodName)
544
        throws DynMethodException {
545
        String key = DefaultDynObjectManager.getKey(theClass, null, methodName);
546
        MethodInfo info = (MethodInfo) methodsMap.get(key);
547
        if (info == null) {
548
            throw new IllegalDynMethodException(methodName, theClass);
549
        }
550
        info.check(theClass, info.code);
551
        return info.code;
552
    }
537 553

  
538
	public DynMethod getDynMethod(Object obj, int code)
539
			throws DynMethodException {
540
		return getDynMethod(obj.getClass(), code);
541
	}
554
    public DynMethod getDynMethod(int code) throws DynMethodException {
555
        if (code >= methods.length) {
556
            throw new DynMethodNotSupportedException(code, "{null}");
557
        }
558
        MethodInfo info = methods[code];
559
        info.check((Class) null, code);
560
        return info.dynMethod;
561
    }
542 562

  
543
	public DynMethod getDynMethod(Class theClass, int code)
544
			throws DynMethodException {
545
		if (code >= methods.length) {
546
			throw new DynMethodNotSupportedException(code, theClass.getName());
547
		}
548
		MethodInfo info = methods[code];
549
		info.check(theClass, code);
550
		return info.dynMethod;
551
	}
563
    public DynMethod getDynMethod(Object obj, int code)
564
        throws DynMethodException {
565
        return getDynMethod(obj.getClass(), code);
566
    }
552 567

  
553
	public DynMethod getDynMethod(DynClass dynClass, int code)
554
			throws DynMethodException {
555
		if (code >= methods.length) {
556
			throw new DynMethodNotSupportedException(code, dynClass.getName());
557
		}
558
		MethodInfo info = methods[code];
559
		info.check(dynClass, code);
560
		return info.dynMethod;
561
	}
568
    public DynMethod getDynMethod(Class theClass, int code)
569
        throws DynMethodException {
570
        if (code >= methods.length) {
571
            throw new DynMethodNotSupportedException(code, theClass.getName());
572
        }
573
        MethodInfo info = methods[code];
574
        info.check(theClass, code);
575
        return info.dynMethod;
576
    }
562 577

  
563
	public DynMethod getDynMethod(DynObject dynObject, int code)
564
			throws DynMethodException {
565
		return getDynMethod(dynObject.getDynClass(), code);
566
	}
578
    public DynMethod getDynMethod(DynClass dynClass, int code)
579
        throws DynMethodException {
580
        if (code >= methods.length) {
581
            throw new DynMethodNotSupportedException(code, dynClass.getName());
582
        }
583
        MethodInfo info = methods[code];
584
        info.check(dynClass, code);
585
        return info.dynMethod;
586
    }
567 587

  
568
	public void validate(DynObject object) {
569
		// TODO
570
		return;
571
	}
572
	
573
	public Class getDefaultClassOfType(int type) {
574
		return ToolsLocator.getDataTypesManager().getDefaultClass(type);
575
	}
588
    public DynMethod getDynMethod(DynObject dynObject, int code)
589
        throws DynMethodException {
590
        return getDynMethod(dynObject.getDynClass(), code);
591
    }
576 592

  
577
	public Map importDynClassDefinitions(InputStream resource,
578
			ClassLoader loader) throws XmlPullParserException, IOException {
579
		return new DynClassImportHelper().importDefinitions(resource, loader, null);
580
	}
593
    public void validate(DynObject object) {
594
        // TODO
595
        return;
596
    }
581 597

  
582
	public Map importDynClassDefinitions(XmlPullParser parser,
583
			ClassLoader loader, String defaultNamespace) throws XmlPullParserException, IOException {
584
		return new DynClassImportHelper().importDefinitions(parser, loader, defaultNamespace);
585
	}
598
    public Class getDefaultClassOfType(int type) {
599
        return ToolsLocator.getDataTypesManager().getDefaultClass(type);
600
    }
586 601

  
587
	public Map importDynClassDefinitions(InputStream resource,
588
			ClassLoader loader, String defaultNamespace)
589
			throws XmlPullParserException, IOException {
590
		return new DynClassImportHelper().importDefinitions(resource, loader, defaultNamespace);
591
	}
602
    public Map importDynClassDefinitions(InputStream resource,
603
        ClassLoader loader) throws XmlPullParserException, IOException {
604
        return new DynClassImportHelper().importDefinitions(resource, loader,
605
            null);
606
    }
592 607

  
608
    public Map importDynClassDefinitions(XmlPullParser parser,
609
        ClassLoader loader, String defaultNamespace)
610
        throws XmlPullParserException, IOException {
611
        return new DynClassImportHelper().importDefinitions(parser, loader,
612
            defaultNamespace);
613
    }
614

  
615
    public Map importDynClassDefinitions(InputStream resource,
616
        ClassLoader loader, String defaultNamespace)
617
        throws XmlPullParserException, IOException {
618
        return new DynClassImportHelper().importDefinitions(resource, loader,
619
            defaultNamespace);
620
    }
621

  
593 622
    public DynObjectPagingHelper createDynObjectPagingHelper(DynObjectSet set)
594 623
        throws BaseException {
595 624
        return new DefaultDynObjectPagingHelper(set);
596 625
    }
597 626

  
598
	public DynClassName createDynClassName(String namespace, String name) {
599
		return new DefaultDynClassName(namespace, name);
600
	}
627
    public DynObjectPagingHelper createDynObjectPagingHelper(DynObjectSet set,
628
        int pageSize) throws BaseException {
629
        return new DefaultDynObjectPagingHelper(set, pageSize);
630
    }
601 631

  
602
	public DynClassName createDynClassName(String name) {
603
		return new DefaultDynClassName(name);
604
	}
632
    public DynClassName createDynClassName(String namespace, String name) {
633
        return new DefaultDynClassName(namespace, name);
634
    }
605 635

  
636
    public DynClassName createDynClassName(String name) {
637
        return new DefaultDynClassName(name);
638
    }
639

  
606 640
}

Also available in: Unified diff