1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package org.jomc.ri.model;
37
38 import java.lang.ref.Reference;
39 import java.lang.ref.WeakReference;
40 import java.lang.reflect.Constructor;
41 import java.lang.reflect.Method;
42 import java.util.Map;
43 import javax.xml.bind.annotation.XmlTransient;
44 import org.jomc.model.Instance;
45 import org.jomc.model.Specification;
46 import org.jomc.util.WeakIdentityHashMap;
47 import static org.jomc.ri.model.RuntimeModelObjects.BOOTSTRAP_CLASSLOADER_KEY;
48 import static org.jomc.ri.model.RuntimeModelObjects.classesByClassLoaderAndNameCache;
49 import static org.jomc.ri.model.RuntimeModelObjects.createMap;
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" )
74
75
76 public class RuntimeInstance extends Instance implements RuntimeModelObject
77 {
78
79
80
81 @XmlTransient
82 static final Map<ClassLoader, Map<String, Reference<Class<?>[]>>> classesByClassLoaderAndInstanceCache =
83 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Class<?>[]>>>();
84
85
86 @XmlTransient
87 static final Map<ClassLoader, Map<String, Reference<Constructor<?>>>> constructorsByClassLoaderAndInstanceCache =
88 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Constructor<?>>>>();
89
90
91 @XmlTransient
92 static final Map<ClassLoader, Map<String, Reference<Method>>> methodsByClassLoaderAndInstanceCache =
93 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Method>>>();
94
95
96 @XmlTransient
97 static final Map<ClassLoader, Map<String, Boolean>> assignableFlagsByClassLoaderAndInstanceCache =
98 new WeakIdentityHashMap<ClassLoader, Map<String, Boolean>>();
99
100
101 @XmlTransient
102 static final Map<ClassLoader, Map<String, Reference<Class<?>>>> proxyClassesByClassLoaderAndInstanceCache =
103 new WeakIdentityHashMap<ClassLoader, Map<String, Reference<Class<?>>>>();
104
105
106 @XmlTransient
107 private volatile String javaClassFactoryMethodName;
108
109
110
111
112
113
114
115
116 public RuntimeInstance( final Instance instance )
117 {
118 super( instance );
119
120 if ( this.getAuthors() != null )
121 {
122 this.setAuthors( RuntimeModelObjects.getInstance().copyOf( this.getAuthors() ) );
123 }
124 if ( this.getDependencies() != null )
125 {
126 this.setDependencies( RuntimeModelObjects.getInstance().copyOf( this.getDependencies() ) );
127 }
128 if ( this.getDocumentation() != null )
129 {
130 this.setDocumentation( RuntimeModelObjects.getInstance().copyOf( this.getDocumentation() ) );
131 }
132 if ( this.getMessages() != null )
133 {
134 this.setMessages( RuntimeModelObjects.getInstance().copyOf( this.getMessages() ) );
135 }
136 if ( this.getProperties() != null )
137 {
138 this.setProperties( RuntimeModelObjects.getInstance().copyOf( this.getProperties() ) );
139 }
140 if ( this.getSpecifications() != null )
141 {
142 this.setSpecifications( RuntimeModelObjects.getInstance().copyOf( this.getSpecifications() ) );
143 }
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 @Override
165 public Class<?> getJavaClass( final ClassLoader classLoader ) throws ClassNotFoundException
166 {
167 ClassLoader classLoaderKey = classLoader;
168 if ( classLoaderKey == null )
169 {
170 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
171 }
172
173 synchronized ( classesByClassLoaderAndNameCache )
174 {
175 Class<?> javaClass = null;
176 Map<String, Reference<Class<?>>> map = classesByClassLoaderAndNameCache.get( classLoaderKey );
177
178 if ( map == null )
179 {
180 map = createMap();
181 classesByClassLoaderAndNameCache.put( classLoaderKey, map );
182 }
183
184 final Reference<Class<?>> reference = map.get( this.getClazz() );
185
186 if ( reference != null )
187 {
188 javaClass = reference.get();
189 }
190
191 if ( javaClass == null )
192 {
193 javaClass = super.getJavaClass( classLoader );
194 map.put( this.getClazz(), new WeakReference<Class<?>>( javaClass ) );
195 }
196
197 return javaClass;
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 @Override
222 public Class<?>[] getJavaClasses( final ClassLoader classLoader ) throws ClassNotFoundException
223 {
224 ClassLoader classLoaderKey = classLoader;
225 if ( classLoaderKey == null )
226 {
227 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
228 }
229
230 synchronized ( classesByClassLoaderAndInstanceCache )
231 {
232 Class<?>[] javaClasses = null;
233 Map<String, Reference<Class<?>[]>> map = classesByClassLoaderAndInstanceCache.get( classLoaderKey );
234
235 if ( map == null )
236 {
237 map = createMap();
238 classesByClassLoaderAndInstanceCache.put( classLoaderKey, map );
239 }
240
241 final Reference<Class<?>[]> reference = map.get( this.getIdentifier() );
242
243 if ( reference != null )
244 {
245 javaClasses = reference.get();
246 }
247
248 if ( javaClasses == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
249 {
250 javaClasses = super.getJavaClasses( classLoader );
251 map.put( this.getIdentifier(), new WeakReference<Class<?>[]>( javaClasses ) );
252 }
253
254 return javaClasses;
255 }
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 @Override
279 public Constructor<?> getJavaConstructor( final ClassLoader classLoader ) throws ClassNotFoundException
280 {
281 ClassLoader classLoaderKey = classLoader;
282 if ( classLoaderKey == null )
283 {
284 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
285 }
286
287 synchronized ( constructorsByClassLoaderAndInstanceCache )
288 {
289 Constructor<?> javaClassConstructor = null;
290 Map<String, Reference<Constructor<?>>> map = constructorsByClassLoaderAndInstanceCache.get( classLoaderKey );
291
292 if ( map == null )
293 {
294 map = createMap();
295 constructorsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
296 }
297
298 final Reference<Constructor<?>> reference = map.get( this.getIdentifier() );
299
300 if ( reference != null )
301 {
302 javaClassConstructor = reference.get();
303 }
304
305 if ( javaClassConstructor == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
306 {
307 javaClassConstructor = super.getJavaConstructor( classLoader );
308 map.put( this.getIdentifier(), new WeakReference<Constructor<?>>( javaClassConstructor ) );
309 }
310
311 return javaClassConstructor;
312 }
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 @Override
330 public String getJavaFactoryMethodName()
331 {
332 if ( this.javaClassFactoryMethodName == null )
333 {
334 this.javaClassFactoryMethodName = super.getJavaFactoryMethodName();
335 }
336
337 return this.javaClassFactoryMethodName;
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 @Override
362 public Method getJavaFactoryMethod( final ClassLoader classLoader ) throws ClassNotFoundException
363 {
364 ClassLoader classLoaderKey = classLoader;
365 if ( classLoaderKey == null )
366 {
367 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
368 }
369
370 synchronized ( methodsByClassLoaderAndInstanceCache )
371 {
372 Method javaClassFactoryMethod = null;
373 Map<String, Reference<Method>> map = methodsByClassLoaderAndInstanceCache.get( classLoaderKey );
374
375 if ( map == null )
376 {
377 map = createMap();
378 methodsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
379 }
380
381 final Reference<Method> reference = map.get( this.getIdentifier() );
382
383 if ( reference != null )
384 {
385 javaClassFactoryMethod = reference.get();
386 }
387
388 if ( javaClassFactoryMethod == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
389 {
390 javaClassFactoryMethod = super.getJavaFactoryMethod( classLoader );
391 map.put( this.getIdentifier(), new WeakReference<Method>( javaClassFactoryMethod ) );
392 }
393
394 return javaClassFactoryMethod;
395 }
396 }
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421 @Override
422 public boolean isJavaClassAssignable( final ClassLoader classLoader ) throws ClassNotFoundException
423 {
424 ClassLoader classLoaderKey = classLoader;
425 if ( classLoaderKey == null )
426 {
427 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
428 }
429
430 synchronized ( assignableFlagsByClassLoaderAndInstanceCache )
431 {
432 Map<String, Boolean> map = assignableFlagsByClassLoaderAndInstanceCache.get( classLoaderKey );
433
434 if ( map == null )
435 {
436 map = createMap();
437 assignableFlagsByClassLoaderAndInstanceCache.put( classLoaderKey, map );
438 }
439
440 Boolean javaClassAssignable = map.get( this.getIdentifier() );
441
442 if ( javaClassAssignable == null && !map.containsKey( this.getIdentifier() ) )
443 {
444 javaClassAssignable = super.isJavaClassAssignable( classLoader );
445 map.put( this.getIdentifier(), javaClassAssignable );
446 }
447
448 return javaClassAssignable == null ? false : javaClassAssignable;
449 }
450 }
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471 @Override
472 public Class<?> getJavaProxyClass( final ClassLoader classLoader ) throws ClassNotFoundException
473 {
474 ClassLoader classLoaderKey = classLoader;
475 if ( classLoaderKey == null )
476 {
477 classLoaderKey = BOOTSTRAP_CLASSLOADER_KEY;
478 }
479
480 synchronized ( proxyClassesByClassLoaderAndInstanceCache )
481 {
482 Class<?> javaProxyClass = null;
483 Map<String, Reference<Class<?>>> map = proxyClassesByClassLoaderAndInstanceCache.get( classLoaderKey );
484
485 if ( map == null )
486 {
487 map = createMap();
488 proxyClassesByClassLoaderAndInstanceCache.put( classLoaderKey, map );
489 }
490
491 final Reference<Class<?>> reference = map.get( this.getIdentifier() );
492
493 if ( reference != null )
494 {
495 javaProxyClass = reference.get();
496 }
497
498 if ( javaProxyClass == null && ( reference != null || !map.containsKey( this.getIdentifier() ) ) )
499 {
500 javaProxyClass = super.getJavaProxyClass( classLoader );
501 map.put( this.getIdentifier(), new WeakReference<Class<?>>( javaProxyClass ) );
502 }
503
504 return javaProxyClass;
505 }
506 }
507
508
509
510 public void gc()
511 {
512 this.gcOrClear( true, false );
513 }
514
515 public void clear()
516 {
517 this.javaClassFactoryMethodName = null;
518 this.gcOrClear( false, true );
519 }
520
521 private void gcOrClear( final boolean gc, final boolean clear )
522 {
523 if ( this.getAuthors() instanceof RuntimeModelObject )
524 {
525 if ( gc )
526 {
527 ( (RuntimeModelObject) this.getAuthors() ).gc();
528 }
529 if ( clear )
530 {
531 ( (RuntimeModelObject) this.getAuthors() ).clear();
532 }
533 }
534 if ( this.getDependencies() instanceof RuntimeModelObject )
535 {
536 if ( gc )
537 {
538 ( (RuntimeModelObject) this.getDependencies() ).gc();
539 }
540 if ( clear )
541 {
542 ( (RuntimeModelObject) this.getDependencies() ).clear();
543 }
544 }
545 if ( this.getDocumentation() instanceof RuntimeModelObject )
546 {
547 if ( gc )
548 {
549 ( (RuntimeModelObject) this.getDocumentation() ).gc();
550 }
551 if ( clear )
552 {
553 ( (RuntimeModelObject) this.getDocumentation() ).clear();
554 }
555 }
556 if ( this.getMessages() instanceof RuntimeModelObject )
557 {
558 if ( gc )
559 {
560 ( (RuntimeModelObject) this.getMessages() ).gc();
561 }
562 if ( clear )
563 {
564 ( (RuntimeModelObject) this.getMessages() ).clear();
565 }
566 }
567 if ( this.getProperties() instanceof RuntimeModelObject )
568 {
569 if ( gc )
570 {
571 ( (RuntimeModelObject) this.getProperties() ).gc();
572 }
573 if ( clear )
574 {
575 ( (RuntimeModelObject) this.getProperties() ).clear();
576 }
577 }
578 if ( this.getSpecifications() instanceof RuntimeModelObject )
579 {
580 if ( gc )
581 {
582 ( (RuntimeModelObject) this.getSpecifications() ).gc();
583 }
584 if ( clear )
585 {
586 ( (RuntimeModelObject) this.getSpecifications() ).clear();
587 }
588 }
589 }
590
591
592
593
594
595 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.3", comments = "See http://jomc.sourceforge.net/jomc/1.3/jomc-tools-1.3" )
596 public RuntimeInstance()
597 {
598
599 super();
600
601 }
602
603
604
605
606
607
608
609
610 }