1 | // |
2 | // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-833 |
3 | // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> |
4 | // Any modifications to this file will be lost upon recompilation of the source schema. |
5 | // Generated on: 2010.02.03 at 01:25:12 AM UTC |
6 | // |
7 | |
8 | |
9 | package org.jomc.model; |
10 | |
11 | import java.util.ArrayList; |
12 | import java.util.Iterator; |
13 | import java.util.List; |
14 | import javax.annotation.Generated; |
15 | import javax.xml.bind.annotation.XmlAccessType; |
16 | import javax.xml.bind.annotation.XmlAccessorType; |
17 | import javax.xml.bind.annotation.XmlType; |
18 | |
19 | |
20 | /** |
21 | * List of modules. |
22 | * |
23 | * <p>Java class for Modules complex type. |
24 | * |
25 | * <p>The following schema fragment specifies the expected content contained within this class. |
26 | * |
27 | * <pre> |
28 | * <complexType name="Modules"> |
29 | * <complexContent> |
30 | * <extension base="{http://jomc.org/model}ModelObject"> |
31 | * <sequence> |
32 | * <element ref="{http://jomc.org/model}module" maxOccurs="unbounded" minOccurs="0"/> |
33 | * </sequence> |
34 | * </extension> |
35 | * </complexContent> |
36 | * </complexType> |
37 | * </pre> |
38 | * |
39 | * |
40 | */ |
41 | @XmlAccessorType(XmlAccessType.FIELD) |
42 | @XmlType(name = "Modules", propOrder = { |
43 | "module" |
44 | }) |
45 | @Generated(value = "com.sun.tools.xjc.Driver", date = "2010-02-03T01:25:12+00:00", comments = "JAXB RI vhudson-jaxb-ri-2.1-833") |
46 | public class Modules |
47 | extends ModelObject |
48 | implements Cloneable |
49 | { |
50 | |
51 | @Generated(value = "com.sun.tools.xjc.Driver", date = "2010-02-03T01:25:12+00:00", comments = "JAXB RI vhudson-jaxb-ri-2.1-833") |
52 | protected List<Module> module; |
53 | |
54 | /** |
55 | * Creates a new {@code Modules} instance. |
56 | * |
57 | */ |
58 | public Modules() { |
59 | // CC-XJC Version 1.2 Build 2009-11-15T21:50:02+0000 |
60 | super(); |
61 | } |
62 | |
63 | /** |
64 | * Creates a new {@code Modules} instance by deeply copying a given {@code Modules} instance. |
65 | * |
66 | * |
67 | * @param o |
68 | * The instance to copy. |
69 | * @throws NullPointerException |
70 | * if {@code o} is {@code null}. |
71 | */ |
72 | public Modules(final Modules o) { |
73 | // CC-XJC Version 1.2 Build 2009-11-15T21:50:02+0000 |
74 | super(o); |
75 | if (o == null) { |
76 | throw new NullPointerException("Cannot create a copy of 'Modules' from 'null'."); |
77 | } |
78 | // 'Module' collection. |
79 | copyModule(o.getModule(), getModule()); |
80 | } |
81 | |
82 | /** |
83 | * Gets the value of the module property. |
84 | * |
85 | * <p> |
86 | * This accessor method returns a reference to the live list, |
87 | * not a snapshot. Therefore any modification you make to the |
88 | * returned list will be present inside the JAXB object. |
89 | * This is why there is not a <CODE>set</CODE> method for the module property. |
90 | * |
91 | * <p> |
92 | * For example, to add a new item, do as follows: |
93 | * <pre> |
94 | * getModule().add(newItem); |
95 | * </pre> |
96 | * |
97 | * |
98 | * <p> |
99 | * Objects of the following type(s) are allowed in the list |
100 | * {@link Module } |
101 | * |
102 | * |
103 | */ |
104 | @Generated(value = "com.sun.tools.xjc.Driver", date = "2010-02-03T01:25:12+00:00", comments = "JAXB RI vhudson-jaxb-ri-2.1-833") |
105 | public List<Module> getModule() { |
106 | if (module == null) { |
107 | module = new ArrayList<Module>(); |
108 | } |
109 | return this.module; |
110 | } |
111 | |
112 | /** |
113 | * Copies all values of property {@code Module} deeply. |
114 | * |
115 | * @param target |
116 | * The target to copy {@code source} to. |
117 | * @param source |
118 | * The source to copy from. |
119 | * @throws NullPointerException |
120 | * if {@code source} or {@code target} is {@code null}. |
121 | */ |
122 | @SuppressWarnings("unchecked") |
123 | @Generated(value = "com.sun.tools.xjc.Driver", date = "2010-02-03T01:25:12+00:00", comments = "JAXB RI vhudson-jaxb-ri-2.1-833") |
124 | private static void copyModule(final List<Module> source, final List<Module> target) { |
125 | // CC-XJC Version 1.2 Build 2009-11-15T21:50:02+0000 |
126 | if (!source.isEmpty()) { |
127 | for (Iterator it = source.iterator(); it.hasNext(); ) { |
128 | final Object next = it.next(); |
129 | if (next instanceof Module) { |
130 | // CClassInfo: org.jomc.model.Module |
131 | target.add(((Module) next).clone()); |
132 | continue; |
133 | } |
134 | // Please report this at https://apps.sourceforge.net/mantisbt/ccxjc/ |
135 | throw new AssertionError((("Unexpected instance '"+ next)+"' for property 'Module' of class 'org.jomc.model.Modules'.")); |
136 | } |
137 | } |
138 | } |
139 | |
140 | /** |
141 | * Creates and returns a deep copy of this object. |
142 | * |
143 | * |
144 | * @return |
145 | * A deep copy of this object. |
146 | */ |
147 | @Override |
148 | @Generated(value = "com.sun.tools.xjc.Driver", date = "2010-02-03T01:25:12+00:00", comments = "JAXB RI vhudson-jaxb-ri-2.1-833") |
149 | public Modules clone() { |
150 | // CC-XJC Version 1.2 Build 2009-11-15T21:50:02+0000 |
151 | return new Modules(this); |
152 | } |
153 | |
154 | |
155 | /** |
156 | * Constant for the default name of the classpath module. |
157 | * @see #getClasspathModuleName() |
158 | */ |
159 | private static final String DEFAULT_CLASSPATH_MODULE_NAME = "Java Classpath"; |
160 | |
161 | /** Default classpath module name. */ |
162 | private static volatile String defaultClasspathModuleName; |
163 | |
164 | /** Empty {@code Class} array. */ |
165 | private static final Class[] NO_CLASSES = |
166 | { |
167 | }; |
168 | |
169 | /** Maps objects to {@code Instance}s. */ |
170 | @javax.xml.bind.annotation.XmlTransient private java.util.Map<Object,Instance> objects = new org.jomc.util.WeakIdentityHashMap(); |
171 | |
172 | /** |
173 | * Creates a new {@code Modules} instance taking a map backing the instance. |
174 | * |
175 | * @param objects The map backing the instance. |
176 | */ |
177 | public Modules( final java.util.Map<Object, Instance> objects ) |
178 | { |
179 | if ( objects == null ) |
180 | { |
181 | throw new NullPointerException( "objects" ); |
182 | } |
183 | |
184 | this.objects = objects; |
185 | } |
186 | |
187 | /** |
188 | * Creates a new {@code Modules} instance by deeply copying a given {@code Modules} instance taking a map backing |
189 | * the instance. |
190 | * |
191 | * @param o The instance to copy. |
192 | * @param objects The map backing the instance. |
193 | * |
194 | * @throws NullPointerException if {@code o} or {@code objects} is {@code null}. |
195 | */ |
196 | public Modules( final Modules o, final java.util.Map<Object, Instance> objects ) |
197 | { |
198 | super( o ); |
199 | if ( o == null ) |
200 | { |
201 | throw new NullPointerException( "Cannot create a copy of 'Modules' from 'null'." ); |
202 | } |
203 | if ( objects == null ) |
204 | { |
205 | throw new NullPointerException( "objects" ); |
206 | } |
207 | |
208 | copyModule( o.getModule(), getModule() ); |
209 | this.objects = objects; |
210 | } |
211 | |
212 | /** |
213 | * Gets the default classpath module name. |
214 | * <p>The default classpath module name is controlled by system property |
215 | * {@code org.jomc.model.Modules.defaultClasspathModuleName} holding the default classpath module name. If that |
216 | * property is not set, the {@code Java Classpath} default is returned.</p> |
217 | * |
218 | * @return The default classpath module name. |
219 | * |
220 | * @see #getClasspathModule(java.lang.String, java.lang.ClassLoader) |
221 | */ |
222 | public static String getDefaultClasspathModuleName() |
223 | { |
224 | if ( defaultClasspathModuleName == null ) |
225 | { |
226 | defaultClasspathModuleName = System.getProperty( "org.jomc.model.Modules.defaultClasspathModuleName", |
227 | DEFAULT_CLASSPATH_MODULE_NAME ); |
228 | |
229 | } |
230 | |
231 | return defaultClasspathModuleName; |
232 | } |
233 | |
234 | /** |
235 | * Sets the default classpath module name. |
236 | * |
237 | * @param value The new default classpath module name or {@code null}, |
238 | */ |
239 | public static void setDefaultClasspathModuleName( final String value ) |
240 | { |
241 | defaultClasspathModuleName = value; |
242 | } |
243 | |
244 | /** |
245 | * Gets a module holding model objects resolved by inspecting a given class loader. |
246 | * <p>This method searches this list of modules for unresolved references and tries to resolve each unresolved |
247 | * reference by inspecting the given class loader.</p> |
248 | * |
249 | * @param moduleName The name of the module to return. |
250 | * @param classLoader The class loader to use for resolving entities or {@code null} to resolve entities using the |
251 | * bootstrap class loader. |
252 | * |
253 | * @return A module holding model objects resolved by inspecting the given class loader or {@code null} if nothing |
254 | * is resolved. |
255 | * |
256 | * @throws NullPointerException if {@code moduleName} is {@code null}. |
257 | * |
258 | * @see #getDefaultClasspathModuleName() |
259 | * @see #getModule() |
260 | */ |
261 | public Module getClasspathModule( final String moduleName, final ClassLoader classLoader ) |
262 | { |
263 | if ( moduleName == null ) |
264 | { |
265 | throw new NullPointerException( "moduleName," ); |
266 | } |
267 | |
268 | final Module classpathModule = new Module(); |
269 | classpathModule.setVersion( System.getProperty( "java.specification.version" ) ); |
270 | classpathModule.setName( moduleName ); |
271 | |
272 | this.resolveClasspath( classpathModule, classLoader ); |
273 | |
274 | final boolean resolved = ( classpathModule.getSpecifications() != null && |
275 | !classpathModule.getSpecifications().getSpecification().isEmpty() ) || |
276 | ( classpathModule.getImplementations() != null && |
277 | !classpathModule.getImplementations().getImplementation().isEmpty() ); |
278 | |
279 | return resolved ? classpathModule : null; |
280 | } |
281 | |
282 | /** |
283 | * Gets a module for a given name from this list of modules. |
284 | * |
285 | * @param name The name of the module to return. |
286 | * |
287 | * @return The module with name {@code name} from the list or {@code null}, |
288 | * if no module matching {@code name} is found. |
289 | * |
290 | * @throws NullPointerException if {@code name} is {@code null}. |
291 | * |
292 | * @see #getModule() |
293 | */ |
294 | public Module getModule( final String name ) |
295 | { |
296 | if ( name == null ) |
297 | { |
298 | throw new NullPointerException( "name" ); |
299 | } |
300 | |
301 | for ( Module m : this.getModule() ) |
302 | { |
303 | if ( m.getName().equals( name ) ) |
304 | { |
305 | return m; |
306 | } |
307 | } |
308 | |
309 | return null; |
310 | } |
311 | |
312 | /** |
313 | * Gets all specifications declared in this list of modules. |
314 | * |
315 | * @return All specifications declared in the list or {@code null}, if no |
316 | * specifications are declared. |
317 | * |
318 | * @see #getModule() |
319 | */ |
320 | public Specifications getSpecifications() |
321 | { |
322 | final Specifications specifications = new Specifications(); |
323 | for ( Module m : this.getModule() ) |
324 | { |
325 | if ( m.getSpecifications() != null ) |
326 | { |
327 | specifications.getSpecification().addAll( m.getSpecifications().getSpecification() ); |
328 | } |
329 | } |
330 | |
331 | return specifications.getSpecification().isEmpty() ? null : specifications; |
332 | } |
333 | |
334 | /** |
335 | * Gets all implementations declared in this list of modules. |
336 | * |
337 | * @return All implementations declared in the list or {@code null}, if no |
338 | * implementations are declared. |
339 | * |
340 | * @see #getModule() |
341 | */ |
342 | public Implementations getImplementations() |
343 | { |
344 | final Implementations implementations = new Implementations(); |
345 | for ( Module m : this.getModule() ) |
346 | { |
347 | if ( m.getImplementations() != null ) |
348 | { |
349 | implementations.getImplementation().addAll( m.getImplementations().getImplementation() ); |
350 | } |
351 | } |
352 | |
353 | return implementations.getImplementation().isEmpty() ? null : implementations; |
354 | } |
355 | |
356 | /** |
357 | * Gets the module declaring a given specification from this list of modules. |
358 | * |
359 | * @param specification The identifier of the specification whose declaring module to return. |
360 | * |
361 | * @return The module declaring {@code specification} from the list or {@code null}, if no module |
362 | * is found declaring {@code specification}. |
363 | * |
364 | * @throws NullPointerException if {@code specification} is {@code null}. |
365 | * |
366 | * @see #getModule() |
367 | */ |
368 | public Module getModuleOfSpecification( final String specification ) |
369 | { |
370 | if ( specification == null ) |
371 | { |
372 | throw new NullPointerException( "specification" ); |
373 | } |
374 | |
375 | for ( Module m : this.getModule() ) |
376 | { |
377 | if ( m.getSpecifications() != null && m.getSpecifications().getSpecification( specification ) != null ) |
378 | { |
379 | return m; |
380 | } |
381 | } |
382 | |
383 | return null; |
384 | } |
385 | |
386 | /** |
387 | * Gets the module declaring a given implementation from this list of modules. |
388 | * |
389 | * @param implementation The identifier of the implementation whose declaring module to return. |
390 | * |
391 | * @return The module declaring {@code implementation} from the list or {@code null}, if no module |
392 | * is found declaring {@code implementation}. |
393 | * |
394 | * @throws NullPointerException if {@code implementation} is {@code null}. |
395 | * |
396 | * @see #getModule() |
397 | */ |
398 | public Module getModuleOfImplementation( final String implementation ) |
399 | { |
400 | if ( implementation == null ) |
401 | { |
402 | throw new NullPointerException( "implementation" ); |
403 | } |
404 | |
405 | for ( Module m : this.getModule() ) |
406 | { |
407 | if ( m.getImplementations() != null && m.getImplementations().getImplementation( implementation ) != null ) |
408 | { |
409 | return m; |
410 | } |
411 | } |
412 | |
413 | return null; |
414 | } |
415 | |
416 | /** |
417 | * Gets a specification for a given identifier from this list of modules. |
418 | * |
419 | * @param specification The identifier of the specification to return. |
420 | * |
421 | * @return The specification identified by {@code specification} from the list or {@code null}, |
422 | * if no specification matching {@code specification} is found. |
423 | * |
424 | * @throws NullPointerException if {@code specification} is {@code null}. |
425 | * |
426 | * @see #getModule() |
427 | */ |
428 | public Specification getSpecification( final String specification ) |
429 | { |
430 | if ( specification == null ) |
431 | { |
432 | throw new NullPointerException( "specification" ); |
433 | } |
434 | |
435 | for ( Module m : this.getModule() ) |
436 | { |
437 | if ( m.getSpecifications() != null ) |
438 | { |
439 | final Specification s = m.getSpecifications().getSpecification( specification ); |
440 | if ( s != null ) |
441 | { |
442 | return s; |
443 | } |
444 | } |
445 | } |
446 | |
447 | return null; |
448 | } |
449 | |
450 | /** |
451 | * Gets a specification for a given class from this list of modules. |
452 | * |
453 | * @param specification The class of the specification to return. |
454 | * |
455 | * @return The specification identified by {@code specification} from the list or {@code null}, |
456 | * if no specification matching {@code specification} is found. |
457 | * |
458 | * @throws NullPointerException if {@code specification} is {@code null}. |
459 | * |
460 | * @see #getModule() |
461 | */ |
462 | public Specification getSpecification( final Class specification ) |
463 | { |
464 | if ( specification == null ) |
465 | { |
466 | throw new NullPointerException( "specification" ); |
467 | } |
468 | |
469 | for ( Module m : this.getModule() ) |
470 | { |
471 | if ( m.getSpecifications() != null ) |
472 | { |
473 | final Specification s = m.getSpecifications().getSpecification( specification ); |
474 | if ( s != null ) |
475 | { |
476 | return s; |
477 | } |
478 | } |
479 | } |
480 | |
481 | return null; |
482 | } |
483 | |
484 | /** |
485 | * Gets all specifications an implementation implements from this list of modules. |
486 | * |
487 | * @param implementation The identifier of the implementation to get all implemented specifications of. |
488 | * |
489 | * @return List of all specifications implemented by {@code implementation} from the list or {@code null}, if no |
490 | * implementation matching {@code implementation} is found or if that implementation does not implement any |
491 | * specification. |
492 | * |
493 | * @throws NullPointerException if {@code implementation} is {@code null}. |
494 | * |
495 | * @see #getModule() |
496 | */ |
497 | public Specifications getSpecifications( final String implementation ) |
498 | { |
499 | if ( implementation == null ) |
500 | { |
501 | throw new NullPointerException( "implementation" ); |
502 | } |
503 | |
504 | final Specifications specs = new Specifications(); |
505 | this.collectSpecifications( this.getImplementation( implementation ), specs, new Implementations(), true ); |
506 | |
507 | Implementation declaration = null; |
508 | final Implementation impl = this.getImplementation( implementation ); |
509 | |
510 | if ( impl != null && impl.getClazz() != null && !impl.getIdentifier().equals( impl.getClazz() ) ) |
511 | { |
512 | for ( Module m : this.getModule() ) |
513 | { |
514 | if ( m.getImplementations() != null ) |
515 | { |
516 | for ( Implementation i : m.getImplementations().getImplementation() ) |
517 | { |
518 | if ( i.getClazz() != null && i.getClazz().equals( i.getIdentifier() ) && |
519 | i.getClazz().equals( impl.getClazz() ) ) |
520 | { |
521 | declaration = i; |
522 | break; |
523 | } |
524 | } |
525 | } |
526 | } |
527 | } |
528 | |
529 | if ( declaration != null ) |
530 | { |
531 | final Specifications declaredSpecifications = this.getSpecifications( declaration.getIdentifier() ); |
532 | if ( declaredSpecifications != null ) |
533 | { |
534 | for ( SpecificationReference r : declaredSpecifications.getReference() ) |
535 | { |
536 | if ( specs.getReference( r.getIdentifier() ) == null ) |
537 | { |
538 | specs.getReference().add( r ); |
539 | final Specification s = declaredSpecifications.getSpecification( r.getIdentifier() ); |
540 | if ( s != null ) |
541 | { |
542 | specs.getSpecification().add( s ); |
543 | } |
544 | } |
545 | } |
546 | } |
547 | } |
548 | |
549 | java.util.Collections.sort( specs.getSpecification(), new java.util.Comparator<Specification>() |
550 | { |
551 | |
552 | public int compare( final Specification o1, final Specification o2 ) |
553 | { |
554 | return o1.getIdentifier().compareTo( o2.getIdentifier() ); |
555 | } |
556 | |
557 | } ); |
558 | |
559 | java.util.Collections.sort( specs.getReference(), new java.util.Comparator<SpecificationReference>() |
560 | { |
561 | |
562 | public int compare( final SpecificationReference o1, final SpecificationReference o2 ) |
563 | { |
564 | return o1.getIdentifier().compareTo( o2.getIdentifier() ); |
565 | } |
566 | |
567 | } ); |
568 | |
569 | return specs.getSpecification().isEmpty() && specs.getReference().isEmpty() ? null : specs; |
570 | } |
571 | |
572 | /** |
573 | * Gets an implementation for a given identifier from this list of modules. |
574 | * |
575 | * @param implementation The identifier of the implementation to return. |
576 | * |
577 | * @return The implementation identified by {@code implementation} from the list or {@code null}, |
578 | * if no implementation matching {@code implementation} is found. |
579 | * |
580 | * @throws NullPointerException if {@code implementation} is {@code null}. |
581 | * |
582 | * @see #getModule() |
583 | */ |
584 | public Implementation getImplementation( final String implementation ) |
585 | { |
586 | if ( implementation == null ) |
587 | { |
588 | throw new NullPointerException( "implementation" ); |
589 | } |
590 | |
591 | for ( Module m : this.getModule() ) |
592 | { |
593 | if ( m.getImplementations() != null ) |
594 | { |
595 | final Implementation i = m.getImplementations().getImplementation( implementation ); |
596 | if ( i != null ) |
597 | { |
598 | return i; |
599 | } |
600 | } |
601 | } |
602 | |
603 | return null; |
604 | } |
605 | |
606 | /** |
607 | * Gets an implementation for a given class from this list of modules. |
608 | * |
609 | * @param implementation The class of the implementation to return. |
610 | * |
611 | * @return The implementation identified by {@code implementation} from the list or {@code null}, |
612 | * if no implementation matching {@code implementation} is found. |
613 | * |
614 | * @throws NullPointerException if {@code implementation} is {@code null}. |
615 | * |
616 | * @see #getModule() |
617 | */ |
618 | public Implementation getImplementation( final Class implementation ) |
619 | { |
620 | if ( implementation == null ) |
621 | { |
622 | throw new NullPointerException( "implementation" ); |
623 | } |
624 | |
625 | for ( Module m : this.getModule() ) |
626 | { |
627 | if ( m.getImplementations() != null ) |
628 | { |
629 | final Implementation i = m.getImplementations().getImplementation( implementation ); |
630 | if ( i != null ) |
631 | { |
632 | return i; |
633 | } |
634 | } |
635 | } |
636 | |
637 | return null; |
638 | } |
639 | |
640 | /** |
641 | * Gets an implementation for a given object from this list of modules. |
642 | * |
643 | * @param object The object of the implementation to return. |
644 | * |
645 | * @return The implementation identified by {@code object} from the list or {@code null}, |
646 | * if no implementation matching {@code object} is found. |
647 | * |
648 | * @throws NullPointerException if {@code object} is {@code null}. |
649 | * |
650 | * @see #getModule() |
651 | */ |
652 | public Implementation getImplementation( final Object object ) |
653 | { |
654 | return this.collectImplementation( object.getClass() ); |
655 | } |
656 | |
657 | /** |
658 | * Gets an implementation for a given name implementing a given specification from this list of |
659 | * modules. |
660 | * |
661 | * @param specification The identifier of the specification to return an implementation of. |
662 | * @param name The name of the implementation to return. |
663 | * |
664 | * @return The implementation with name {@code name} implementing the specification identified by |
665 | * {@code specification} from the list or {@code null}, if no such implementation is found. |
666 | * |
667 | * @throws NullPointerException if {@code specification} or {@code name} is {@code null}. |
668 | * |
669 | * @see #getModule() |
670 | */ |
671 | public Implementation getImplementation( final String specification, final String name ) |
672 | { |
673 | if ( specification == null ) |
674 | { |
675 | throw new NullPointerException( "specification" ); |
676 | } |
677 | if ( name == null ) |
678 | { |
679 | throw new NullPointerException( "name" ); |
680 | } |
681 | |
682 | final Implementations implementations = this.getImplementations( specification ); |
683 | if ( implementations != null ) |
684 | { |
685 | return implementations.getImplementationByName( name ); |
686 | } |
687 | |
688 | return null; |
689 | } |
690 | |
691 | /** |
692 | * Gets all dependencies of an implementation from this list of modules. |
693 | * |
694 | * @param implementation The identifier of the implementation to get all dependencies of. |
695 | * |
696 | * @return List of all dependencies of {@code implementation} from the list or {@code null}, |
697 | * if no dependencies are found. |
698 | * |
699 | * @throws NullPointerException if {@code implementation} is {@code null}. |
700 | * |
701 | * @see #getModule() |
702 | */ |
703 | public Dependencies getDependencies( final String implementation ) |
704 | { |
705 | if ( implementation == null ) |
706 | { |
707 | throw new NullPointerException( "implementation" ); |
708 | } |
709 | |
710 | final Dependencies dependencies = new Dependencies(); |
711 | this.collectDependencies( this.getImplementation( implementation ), dependencies, new Implementations(), true ); |
712 | |
713 | Implementation declaration = null; |
714 | final Implementation impl = this.getImplementation( implementation ); |
715 | |
716 | if ( impl != null && impl.getClazz() != null && !impl.getIdentifier().equals( impl.getClazz() ) ) |
717 | { |
718 | for ( Module m : this.getModule() ) |
719 | { |
720 | if ( m.getImplementations() != null ) |
721 | { |
722 | for ( Implementation i : m.getImplementations().getImplementation() ) |
723 | { |
724 | if ( i.getClazz() != null && i.getClazz().equals( i.getIdentifier() ) && |
725 | i.getClazz().equals( impl.getClazz() ) ) |
726 | { |
727 | declaration = i; |
728 | break; |
729 | } |
730 | } |
731 | } |
732 | } |
733 | } |
734 | |
735 | if ( declaration != null ) |
736 | { |
737 | final Dependencies declaredDependencies = this.getDependencies( declaration.getIdentifier() ); |
738 | if ( declaredDependencies != null ) |
739 | { |
740 | for ( Dependency d : declaredDependencies.getDependency() ) |
741 | { |
742 | if ( dependencies.getDependency( d.getName() ) == null ) |
743 | { |
744 | dependencies.getDependency().add( d ); |
745 | } |
746 | } |
747 | } |
748 | } |
749 | |
750 | java.util.Collections.sort( dependencies.getDependency(), new java.util.Comparator<Dependency>() |
751 | { |
752 | |
753 | public int compare( final Dependency o1, final Dependency o2 ) |
754 | { |
755 | return o1.getName().compareTo( o2.getName() ); |
756 | } |
757 | |
758 | } ); |
759 | |
760 | return dependencies.getDependency().isEmpty() ? null : dependencies; |
761 | } |
762 | |
763 | /** |
764 | * Gets all properties of an implementation from this list of modules. |
765 | * |
766 | * @param implementation The identifier of the implementation to get all properties of. |
767 | * |
768 | * @return List of all properties of {@code implementation} from the list or {@code null}, |
769 | * if no properties are found. |
770 | * |
771 | * @throws NullPointerException if {@code implementation} is {@code null}. |
772 | * |
773 | * @see #getModule() |
774 | */ |
775 | public Properties getProperties( final String implementation ) |
776 | { |
777 | if ( implementation == null ) |
778 | { |
779 | throw new NullPointerException( "implementation" ); |
780 | } |
781 | |
782 | final Properties properties = new Properties(); |
783 | this.collectProperties( this.getImplementation( implementation ), properties, new Implementations(), true ); |
784 | |
785 | Implementation declaration = null; |
786 | final Implementation impl = this.getImplementation( implementation ); |
787 | |
788 | if ( impl != null && impl.getClazz() != null && !impl.getIdentifier().equals( impl.getClazz() ) ) |
789 | { |
790 | for ( Module m : this.getModule() ) |
791 | { |
792 | if ( m.getImplementations() != null ) |
793 | { |
794 | for ( Implementation i : m.getImplementations().getImplementation() ) |
795 | { |
796 | if ( i.getClazz() != null && i.getClazz().equals( i.getIdentifier() ) && |
797 | i.getClazz().equals( impl.getClazz() ) ) |
798 | { |
799 | declaration = i; |
800 | break; |
801 | } |
802 | } |
803 | } |
804 | } |
805 | } |
806 | |
807 | if ( declaration != null ) |
808 | { |
809 | final Properties declaredProperties = this.getProperties( declaration.getIdentifier() ); |
810 | if ( declaredProperties != null ) |
811 | { |
812 | for ( Property p : declaredProperties.getProperty() ) |
813 | { |
814 | if ( properties.getProperty( p.getName() ) == null ) |
815 | { |
816 | properties.getProperty().add( p ); |
817 | } |
818 | } |
819 | } |
820 | } |
821 | |
822 | final Properties specified = this.getSpecifiedProperties( implementation ); |
823 | |
824 | if ( specified != null ) |
825 | { |
826 | for ( Property p : specified.getProperty() ) |
827 | { |
828 | if ( properties.getProperty( p.getName() ) == null ) |
829 | { |
830 | properties.getProperty().add( p ); |
831 | } |
832 | } |
833 | } |
834 | |
835 | java.util.Collections.sort( properties.getProperty(), new java.util.Comparator<Property>() |
836 | { |
837 | |
838 | public int compare( final Property o1, final Property o2 ) |
839 | { |
840 | return o1.getName().compareTo( o2.getName() ); |
841 | } |
842 | |
843 | } ); |
844 | |
845 | return properties.getProperty().isEmpty() ? null : properties; |
846 | } |
847 | |
848 | /** |
849 | * Gets all properties specified for an implementation from this list of modules. |
850 | * |
851 | * @param implementation The identifier of the implementation to return specified properties of. |
852 | * |
853 | * @return List of all properties specified for {@code implementation} from the list or {@code null}, |
854 | * if no properties are found. |
855 | * |
856 | * @throws NullPointerException if {@code implementation} is {@code null}. |
857 | * |
858 | * @see #getModule() |
859 | */ |
860 | public Properties getSpecifiedProperties( final String implementation ) |
861 | { |
862 | if ( implementation == null ) |
863 | { |
864 | throw new NullPointerException( "implementation" ); |
865 | } |
866 | |
867 | final Properties properties = new Properties(); |
868 | final Specifications specs = this.getSpecifications( implementation ); |
869 | |
870 | if ( specs != null ) |
871 | { |
872 | for ( Specification s : specs.getSpecification() ) |
873 | { |
874 | if ( s.getProperties() != null ) |
875 | { |
876 | properties.getProperty().addAll( s.getProperties().getProperty() ); |
877 | } |
878 | } |
879 | } |
880 | |
881 | java.util.Collections.sort( properties.getProperty(), new java.util.Comparator<Property>() |
882 | { |
883 | |
884 | public int compare( final Property o1, final Property o2 ) |
885 | { |
886 | return o1.getName().compareTo( o2.getName() ); |
887 | } |
888 | |
889 | } ); |
890 | |
891 | return properties.getProperty().isEmpty() ? null : properties; |
892 | } |
893 | |
894 | /** |
895 | * Gets all messages of an implementation from this list of modules. |
896 | * |
897 | * @param implementation The identifier of the implementation to get all messages of. |
898 | * |
899 | * @return List of all messages of {@code implementation} from the list or {@code null}, |
900 | * if no messages are found. |
901 | * |
902 | * @throws NullPointerException if {@code implementation} is {@code null}. |
903 | * |
904 | * @see #getModule() |
905 | */ |
906 | public Messages getMessages( final String implementation ) |
907 | { |
908 | if ( implementation == null ) |
909 | { |
910 | throw new NullPointerException( "implementation" ); |
911 | } |
912 | |
913 | final Messages msgs = new Messages(); |
914 | this.collectMessages( this.getImplementation( implementation ), msgs, new Implementations(), true ); |
915 | |
916 | Implementation declaration = null; |
917 | final Implementation impl = this.getImplementation( implementation ); |
918 | |
919 | if ( impl != null && impl.getClazz() != null && !impl.getIdentifier().equals( impl.getClazz() ) ) |
920 | { |
921 | for ( Module m : this.getModule() ) |
922 | { |
923 | if ( m.getImplementations() != null ) |
924 | { |
925 | for ( Implementation i : m.getImplementations().getImplementation() ) |
926 | { |
927 | if ( i.getClazz() != null && i.getClazz().equals( i.getIdentifier() ) && |
928 | i.getClazz().equals( impl.getClazz() ) ) |
929 | { |
930 | declaration = i; |
931 | break; |
932 | } |
933 | } |
934 | } |
935 | } |
936 | } |
937 | |
938 | if ( declaration != null ) |
939 | { |
940 | final Messages declaredMessages = this.getMessages( declaration.getIdentifier() ); |
941 | if ( declaredMessages != null ) |
942 | { |
943 | for ( Message m : declaredMessages.getMessage() ) |
944 | { |
945 | if ( msgs.getMessage( m.getName() ) == null ) |
946 | { |
947 | msgs.getMessage().add( m ); |
948 | } |
949 | } |
950 | } |
951 | } |
952 | |
953 | java.util.Collections.sort( msgs.getMessage(), new java.util.Comparator<Message>() |
954 | { |
955 | |
956 | public int compare( final Message o1, final Message o2 ) |
957 | { |
958 | return o1.getName().compareTo( o2.getName() ); |
959 | } |
960 | |
961 | } ); |
962 | |
963 | return msgs.getMessage().isEmpty() ? null : msgs; |
964 | } |
965 | |
966 | /** |
967 | * Gets all implementations implementing a given specification from this list of modules. |
968 | * |
969 | * @param specification The identifier of the specification to return all implementations of. |
970 | * |
971 | * @return All implementations implementing the specification identified by {@code specification} |
972 | * from the list or {@code null}, if no implementation implementing {@code specification} is found. |
973 | * |
974 | * @throws NullPointerException if {@code specification} is {@code null}. |
975 | * |
976 | * @see #getModule() |
977 | */ |
978 | public Implementations getImplementations( final String specification ) |
979 | { |
980 | if ( specification == null ) |
981 | { |
982 | throw new NullPointerException( "specification" ); |
983 | } |
984 | |
985 | final Implementations implementations = new Implementations(); |
986 | for ( Module m : this.getModule() ) |
987 | { |
988 | if ( m.getImplementations() != null ) |
989 | { |
990 | for ( Implementation i : m.getImplementations().getImplementation() ) |
991 | { |
992 | final Specifications specs = this.getSpecifications( i.getIdentifier() ); |
993 | |
994 | if ( specs != null ) |
995 | { |
996 | for ( Specification s : specs.getSpecification() ) |
997 | { |
998 | if ( specification.equals( s.getIdentifier() ) ) |
999 | { |
1000 | implementations.getImplementation().add( i ); |
1001 | } |
1002 | } |
1003 | } |
1004 | } |
1005 | } |
1006 | } |
1007 | |
1008 | return implementations.getImplementation().size() > 0 ? implementations : null; |
1009 | } |
1010 | |
1011 | /** |
1012 | * Merges this list of modules to a single module. |
1013 | * |
1014 | * @return A module holding all model objects from the list. |
1015 | */ |
1016 | public Module getMergedModule() |
1017 | { |
1018 | final Modules copy = this.clone(); |
1019 | final Module mergedModule = new Module(); |
1020 | |
1021 | for ( Module m : copy.getModule() ) |
1022 | { |
1023 | final java.util.Set<String> referencedMessages = new java.util.HashSet<String>(); |
1024 | final java.util.Set<String> referencedProperties = new java.util.HashSet<String>(); |
1025 | |
1026 | if ( m.getImplementations() != null ) |
1027 | { |
1028 | for ( Implementation i : m.getImplementations().getImplementation() ) |
1029 | { |
1030 | if ( mergedModule.getImplementations() == null ) |
1031 | { |
1032 | mergedModule.setImplementations( new Implementations() ); |
1033 | } |
1034 | |
1035 | if ( i.getMessages() != null && !i.getMessages().getReference().isEmpty() ) |
1036 | { |
1037 | for ( java.util.Iterator<MessageReference> it = i.getMessages().getReference().iterator(); |
1038 | it.hasNext(); ) |
1039 | { |
1040 | final String messageName = it.next().getName(); |
1041 | i.getMessages().getMessage().add( m.getMessages().getMessage( messageName ) ); |
1042 | referencedMessages.add( messageName ); |
1043 | it.remove(); |
1044 | } |
1045 | } |
1046 | |
1047 | if ( i.getProperties() != null && !i.getProperties().getReference().isEmpty() ) |
1048 | { |
1049 | for ( java.util.Iterator<PropertyReference> it = i.getProperties().getReference().iterator(); |
1050 | it.hasNext(); ) |
1051 | { |
1052 | final String propertyName = it.next().getName(); |
1053 | i.getProperties().getProperty().add( m.getProperties().getProperty( propertyName ) ); |
1054 | referencedProperties.add( propertyName ); |
1055 | it.remove(); |
1056 | } |
1057 | } |
1058 | |
1059 | mergedModule.getImplementations().getImplementation().add( i ); |
1060 | } |
1061 | } |
1062 | |
1063 | if ( m.getSpecifications() != null ) |
1064 | { |
1065 | if ( mergedModule.getSpecifications() == null ) |
1066 | { |
1067 | mergedModule.setSpecifications( new Specifications() ); |
1068 | } |
1069 | |
1070 | for ( Specification s : m.getSpecifications().getSpecification() ) |
1071 | { |
1072 | if ( s.getProperties() != null && !s.getProperties().getReference().isEmpty() ) |
1073 | { |
1074 | for ( java.util.Iterator<PropertyReference> it = s.getProperties().getReference().iterator(); |
1075 | it.hasNext(); ) |
1076 | { |
1077 | final String propertyName = it.next().getName(); |
1078 | s.getProperties().getProperty().add( m.getProperties().getProperty( propertyName ) ); |
1079 | referencedProperties.add( propertyName ); |
1080 | it.remove(); |
1081 | } |
1082 | } |
1083 | |
1084 | mergedModule.getSpecifications().getSpecification().add( s ); |
1085 | } |
1086 | } |
1087 | |
1088 | for ( String messageName : referencedMessages ) |
1089 | { |
1090 | for ( java.util.Iterator<Message> it = m.getMessages().getMessage().iterator(); it.hasNext(); ) |
1091 | { |
1092 | if ( messageName.equals( it.next().getName() ) ) |
1093 | { |
1094 | it.remove(); |
1095 | break; |
1096 | } |
1097 | } |
1098 | } |
1099 | |
1100 | for ( String propertyName : referencedProperties ) |
1101 | { |
1102 | for ( java.util.Iterator<Property> it = m.getProperties().getProperty().iterator(); it.hasNext(); ) |
1103 | { |
1104 | if ( propertyName.equals( it.next().getName() ) ) |
1105 | { |
1106 | it.remove(); |
1107 | break; |
1108 | } |
1109 | } |
1110 | } |
1111 | |
1112 | if ( m.getProperties() != null && !m.getProperties().getProperty().isEmpty() ) |
1113 | { |
1114 | if ( mergedModule.getProperties() == null ) |
1115 | { |
1116 | mergedModule.setProperties( new Properties() ); |
1117 | } |
1118 | |
1119 | mergedModule.getProperties().getProperty().addAll( m.getProperties().getProperty() ); |
1120 | } |
1121 | |
1122 | if ( m.getMessages() != null && !m.getMessages().getMessage().isEmpty() ) |
1123 | { |
1124 | if ( mergedModule.getMessages() == null ) |
1125 | { |
1126 | mergedModule.setMessages( new Messages() ); |
1127 | } |
1128 | |
1129 | mergedModule.getMessages().getMessage().addAll( m.getMessages().getMessage() ); |
1130 | } |
1131 | } |
1132 | |
1133 | return mergedModule; |
1134 | } |
1135 | |
1136 | /** |
1137 | * Gets the instance of an object from this list of modules. |
1138 | * |
1139 | * @param object The object to get the instance of. |
1140 | * |
1141 | * @return The instance of {@code object} from the list or {@code null} if no such instance is found. |
1142 | * |
1143 | * @throws NullPointerException if {@code object} is {@code null}. |
1144 | * |
1145 | * @see #getModule() |
1146 | * @see #createObject(org.jomc.model.Instance instance, java.lang.ClassLoader classLoader) |
1147 | */ |
1148 | public Instance getInstance( final Object object ) |
1149 | { |
1150 | if ( object == null ) |
1151 | { |
1152 | throw new NullPointerException( "object" ); |
1153 | } |
1154 | |
1155 | synchronized ( this.objects ) |
1156 | { |
1157 | Instance instance = (Instance) this.objects.get( object ); |
1158 | |
1159 | if ( instance == null ) |
1160 | { |
1161 | final Implementation i = this.getImplementation( object ); |
1162 | |
1163 | if ( i != null ) |
1164 | { |
1165 | instance = this.getInstance( i.getIdentifier() ); |
1166 | if ( instance != null ) |
1167 | { |
1168 | this.objects.put( object, instance ); |
1169 | } |
1170 | } |
1171 | } |
1172 | |
1173 | return instance; |
1174 | } |
1175 | } |
1176 | |
1177 | /** |
1178 | * Gets an instance for an implementation from this list of modules. |
1179 | * |
1180 | * @param implementation The identifier of the implementation to get an instance for. |
1181 | * |
1182 | * @return An instance for the implementation identified by {@code implementation} from the list or {@code null}, if |
1183 | * no such instance is found. |
1184 | * |
1185 | * @throws NullPointerException if {@code implementation} is {@code null}. |
1186 | * |
1187 | * @see #getModule() |
1188 | */ |
1189 | public Instance getInstance( final String implementation ) |
1190 | { |
1191 | if ( implementation == null ) |
1192 | { |
1193 | throw new NullPointerException( "implementation" ); |
1194 | } |
1195 | |
1196 | final Implementation i = this.getImplementation( implementation ); |
1197 | |
1198 | if ( i != null && i.getClazz() != null ) |
1199 | { |
1200 | final Instance instance = new Instance(); |
1201 | instance.setIdentifier( i.getIdentifier() ); |
1202 | instance.setName( i.getName() ); |
1203 | instance.setClazz( i.getClazz() ); |
1204 | instance.setStateless( i.isStateless() ); |
1205 | instance.setDependencies( this.getDependencies( implementation ) ); |
1206 | instance.setProperties( this.getProperties( implementation ) ); |
1207 | instance.setMessages( this.getMessages( implementation ) ); |
1208 | instance.setSpecifications( this.getSpecifications( implementation ) ); |
1209 | this.collectAny( i, instance.getAny(), new Implementations(), true ); |
1210 | return instance; |
1211 | } |
1212 | |
1213 | return null; |
1214 | } |
1215 | |
1216 | /** |
1217 | * Gets an instance for an implementation from this list of modules overridden with a given dependency. |
1218 | * |
1219 | * @param implementation The identifier of the implementation to get an instance for. |
1220 | * @param dependency The dependency to use for overriding model objects of the instance. |
1221 | * |
1222 | * @return An instance for the implementation identified by {@code implementation} from the list with any model |
1223 | * objects overridden using {@code dependency} or {@code null}, if no such instance is found. |
1224 | * |
1225 | * @throws NullPointerException if {@code implementation} or {@code dependency} is {@code null}. |
1226 | * |
1227 | * @see #getModule() |
1228 | */ |
1229 | public Instance getInstance( final String implementation, final Dependency dependency ) |
1230 | { |
1231 | if ( implementation == null ) |
1232 | { |
1233 | throw new NullPointerException( "implementation" ); |
1234 | } |
1235 | if ( dependency == null ) |
1236 | { |
1237 | throw new NullPointerException( "dependency" ); |
1238 | } |
1239 | |
1240 | Instance instance = this.getInstance( implementation ); |
1241 | |
1242 | if ( instance != null ) |
1243 | { |
1244 | final Specification dependencySpecification = this.getSpecification( dependency.getIdentifier() ); |
1245 | if ( dependencySpecification != null && dependencySpecification.getScope() == null && |
1246 | dependency.getProperties() != null && !dependency.getProperties().getProperty().isEmpty() ) |
1247 | { |
1248 | instance = new Instance( instance ); |
1249 | final Properties properties = new Properties(); |
1250 | properties.getProperty().addAll( dependency.getProperties().getProperty() ); |
1251 | |
1252 | if ( instance.getProperties() != null ) |
1253 | { |
1254 | for ( Property p : instance.getProperties().getProperty() ) |
1255 | { |
1256 | if ( properties.getProperty( p.getName() ) == null ) |
1257 | { |
1258 | properties.getProperty().add( p ); |
1259 | } |
1260 | } |
1261 | } |
1262 | |
1263 | instance.setProperties( properties ); |
1264 | } |
1265 | } |
1266 | |
1267 | return instance; |
1268 | } |
1269 | |
1270 | /** |
1271 | * Creates an object for a given instance from this list of modules. |
1272 | * |
1273 | * @param instance The instance to create an object of. |
1274 | * @param classLoader The class loader to use for creating the object or {@code null} to use the bootstrap class |
1275 | * loader. |
1276 | * |
1277 | * @return A new object of {@code instance} from the list or {@code null} if no such object is found. |
1278 | * |
1279 | * @throws NullPointerException if {@code instance} is {@code null}, |
1280 | * @throws InstantiationException if creating an object fails. |
1281 | * |
1282 | * @see #getModule() |
1283 | */ |
1284 | public Object createObject( final Instance instance, final ClassLoader classLoader ) throws InstantiationException |
1285 | { |
1286 | if ( instance == null ) |
1287 | { |
1288 | throw new NullPointerException( "instance" ); |
1289 | } |
1290 | |
1291 | Object object = null; |
1292 | |
1293 | try |
1294 | { |
1295 | final Class clazz = Class.forName( instance.getClazz(), true, classLoader ); |
1296 | |
1297 | if ( java.lang.reflect.Modifier.isPublic( clazz.getModifiers() ) ) |
1298 | { |
1299 | java.lang.reflect.Constructor ctor = null; |
1300 | |
1301 | if ( !java.lang.reflect.Modifier.isAbstract( clazz.getModifiers() ) ) |
1302 | { |
1303 | try |
1304 | { |
1305 | ctor = clazz.getConstructor( NO_CLASSES ); |
1306 | } |
1307 | catch ( final NoSuchMethodException e ) |
1308 | { |
1309 | ctor = null; |
1310 | } |
1311 | } |
1312 | |
1313 | boolean assignable = true; |
1314 | if ( instance.getSpecifications() != null ) |
1315 | { |
1316 | for ( Specification s : instance.getSpecifications().getSpecification() ) |
1317 | { |
1318 | if ( s.getClazz() != null ) |
1319 | { |
1320 | final Class specClass = Class.forName( s.getClazz(), true, classLoader ); |
1321 | if ( !specClass.isAssignableFrom( clazz ) ) |
1322 | { |
1323 | assignable = false; |
1324 | break; |
1325 | } |
1326 | } |
1327 | } |
1328 | } |
1329 | |
1330 | if ( ctor != null && assignable ) |
1331 | { |
1332 | synchronized ( this.objects ) |
1333 | { |
1334 | object = clazz.newInstance(); |
1335 | this.objects.put( object, instance ); |
1336 | } |
1337 | } |
1338 | else |
1339 | { |
1340 | java.lang.reflect.Method factoryMethod = null; |
1341 | String methodName = null; |
1342 | |
1343 | char[] c = instance.getName().toCharArray(); |
1344 | c[0] = Character.toUpperCase( c[0] ); |
1345 | methodName = "get" + String.valueOf( c ); |
1346 | |
1347 | boolean javaIdentifier = Character.isJavaIdentifierStart( c[0] ); |
1348 | if ( javaIdentifier ) |
1349 | { |
1350 | for ( int idx = c.length - 1; idx > 0; idx-- ) |
1351 | { |
1352 | if ( !Character.isJavaIdentifierPart( c[idx] ) ) |
1353 | { |
1354 | javaIdentifier = false; |
1355 | break; |
1356 | } |
1357 | } |
1358 | } |
1359 | |
1360 | if ( javaIdentifier ) |
1361 | { |
1362 | try |
1363 | { |
1364 | factoryMethod = clazz.getMethod( methodName, (Class[]) null ); |
1365 | } |
1366 | catch ( NoSuchMethodException e ) |
1367 | { |
1368 | factoryMethod = null; |
1369 | } |
1370 | } |
1371 | |
1372 | if ( factoryMethod == null ) |
1373 | { |
1374 | try |
1375 | { |
1376 | factoryMethod = clazz.getMethod( "getObject", (Class[]) null ); |
1377 | } |
1378 | catch ( NoSuchMethodException e ) |
1379 | { |
1380 | factoryMethod = null; |
1381 | } |
1382 | } |
1383 | |
1384 | if ( factoryMethod == null ) |
1385 | { |
1386 | throw new InstantiationException( this.getMessage( "failedCreatingObject", new Object[] |
1387 | { |
1388 | instance.getIdentifier(), clazz.getName(), methodName |
1389 | } ) ); |
1390 | |
1391 | } |
1392 | |
1393 | if ( java.lang.reflect.Modifier.isStatic( factoryMethod.getModifiers() ) ) |
1394 | { |
1395 | synchronized ( this.objects ) |
1396 | { |
1397 | object = factoryMethod.invoke( null, (Object[]) null ); |
1398 | if ( object != null ) |
1399 | { |
1400 | this.objects.put( object, instance ); |
1401 | } |
1402 | } |
1403 | } |
1404 | else if ( ctor != null ) |
1405 | { |
1406 | synchronized ( this.objects ) |
1407 | { |
1408 | final Object o = ctor.newInstance(); |
1409 | this.objects.put( o, instance ); |
1410 | object = factoryMethod.invoke( o, (Object[]) null ); |
1411 | this.objects.remove( o ); |
1412 | if ( object != null ) |
1413 | { |
1414 | this.objects.put( object, instance ); |
1415 | } |
1416 | } |
1417 | } |
1418 | else |
1419 | { |
1420 | throw new InstantiationException( this.getMessage( "failedCreatingObject", new Object[] |
1421 | { |
1422 | instance.getIdentifier(), clazz.getName(), methodName |
1423 | } ) ); |
1424 | |
1425 | } |
1426 | } |
1427 | } |
1428 | |
1429 | return object; |
1430 | } |
1431 | catch ( final java.lang.reflect.InvocationTargetException e ) |
1432 | { |
1433 | throw (InstantiationException) new InstantiationException().initCause( |
1434 | e.getTargetException() != null ? e.getTargetException() : e ); |
1435 | |
1436 | } |
1437 | catch ( final IllegalAccessException e ) |
1438 | { |
1439 | throw (InstantiationException) new InstantiationException().initCause( e ); |
1440 | } |
1441 | catch ( final ClassNotFoundException e ) |
1442 | { |
1443 | throw (InstantiationException) new InstantiationException().initCause( e ); |
1444 | } |
1445 | } |
1446 | |
1447 | void collectSpecifications( final Implementation implementation, final Specifications specifications, |
1448 | final Implementations seen, final boolean includeDeclared ) |
1449 | { |
1450 | if ( implementation != null ) |
1451 | { |
1452 | if ( seen.getImplementation( implementation.getIdentifier() ) == null ) |
1453 | { |
1454 | seen.getImplementation().add( implementation ); |
1455 | |
1456 | if ( includeDeclared && implementation.getSpecifications() != null ) |
1457 | { |
1458 | for ( SpecificationReference r : implementation.getSpecifications().getReference() ) |
1459 | { |
1460 | if ( specifications.getReference( r.getIdentifier() ) == null ) |
1461 | { |
1462 | specifications.getReference().add( r ); |
1463 | |
1464 | final Specification s = this.getSpecification( r.getIdentifier() ); |
1465 | if ( s != null && specifications.getSpecification( s.getIdentifier() ) == null ) |
1466 | { |
1467 | specifications.getSpecification().add( s ); |
1468 | } |
1469 | } |
1470 | } |
1471 | } |
1472 | |
1473 | if ( implementation.getImplementations() != null ) |
1474 | { |
1475 | for ( ImplementationReference r : implementation.getImplementations().getReference() ) |
1476 | { |
1477 | this.collectSpecifications( |
1478 | this.getImplementation( r.getIdentifier() ), specifications, seen, true ); |
1479 | |
1480 | } |
1481 | } |
1482 | } |
1483 | } |
1484 | } |
1485 | |
1486 | void collectDependencies( final Implementation implementation, final Dependencies dependencies, |
1487 | final Implementations seen, final boolean includeDeclared ) |
1488 | { |
1489 | if ( implementation != null ) |
1490 | { |
1491 | if ( seen.getImplementation( implementation.getIdentifier() ) == null ) |
1492 | { |
1493 | seen.getImplementation().add( implementation ); |
1494 | |
1495 | if ( includeDeclared && implementation.getDependencies() != null ) |
1496 | { |
1497 | for ( Dependency d : implementation.getDependencies().getDependency() ) |
1498 | { |
1499 | final Dependency dependency = dependencies.getDependency( d.getName() ); |
1500 | |
1501 | if ( dependency != null ) |
1502 | { |
1503 | if ( d.getProperties() != null ) |
1504 | { |
1505 | if ( dependency.getProperties() == null ) |
1506 | { |
1507 | dependency.setProperties( new Properties() ); |
1508 | } |
1509 | |
1510 | for ( Property p : d.getProperties().getProperty() ) |
1511 | { |
1512 | if ( dependency.getProperties().getProperty( p.getName() ) == null ) |
1513 | { |
1514 | dependency.getProperties().getProperty().add( p ); |
1515 | } |
1516 | } |
1517 | } |
1518 | } |
1519 | else |
1520 | { |
1521 | dependencies.getDependency().add( d ); |
1522 | } |
1523 | } |
1524 | } |
1525 | |
1526 | if ( implementation.getImplementations() != null ) |
1527 | { |
1528 | for ( ImplementationReference r : implementation.getImplementations().getReference() ) |
1529 | { |
1530 | this.collectDependencies( |
1531 | this.getImplementation( r.getIdentifier() ), dependencies, seen, true ); |
1532 | |
1533 | } |
1534 | } |
1535 | } |
1536 | } |
1537 | } |
1538 | |
1539 | void collectProperties( final Implementation implementation, final Properties properties, |
1540 | final Implementations seen, final boolean includeDeclared ) |
1541 | { |
1542 | if ( implementation != null ) |
1543 | { |
1544 | if ( seen.getImplementation( implementation.getIdentifier() ) == null ) |
1545 | { |
1546 | seen.getImplementation().add( implementation ); |
1547 | |
1548 | if ( includeDeclared && implementation.getProperties() != null ) |
1549 | { |
1550 | for ( Property p : implementation.getProperties().getProperty() ) |
1551 | { |
1552 | if ( properties.getProperty( p.getName() ) == null ) |
1553 | { |
1554 | properties.getProperty().add( p ); |
1555 | } |
1556 | } |
1557 | if ( !implementation.getProperties().getReference().isEmpty() ) |
1558 | { |
1559 | final Module m = this.getModuleOfImplementation( implementation.getIdentifier() ); |
1560 | |
1561 | if ( m != null ) |
1562 | { |
1563 | for ( PropertyReference ref : implementation.getProperties().getReference() ) |
1564 | { |
1565 | if ( properties.getProperty( ref.getName() ) == null ) |
1566 | { |
1567 | Property referenced = m.getProperties().getProperty( ref.getName() ); |
1568 | if ( referenced != null ) |
1569 | { |
1570 | referenced = new Property( referenced ); |
1571 | referenced.setDeprecated( ref.isDeprecated() ); |
1572 | referenced.setFinal( ref.isFinal() ); |
1573 | referenced.setOverride( ref.isOverride() ); |
1574 | properties.getProperty().add( referenced ); |
1575 | } |
1576 | } |
1577 | } |
1578 | } |
1579 | } |
1580 | } |
1581 | |
1582 | if ( implementation.getImplementations() != null ) |
1583 | { |
1584 | for ( ImplementationReference r : implementation.getImplementations().getReference() ) |
1585 | { |
1586 | this.collectProperties( this.getImplementation( r.getIdentifier() ), properties, seen, true ); |
1587 | } |
1588 | } |
1589 | } |
1590 | } |
1591 | } |
1592 | |
1593 | void collectMessages( final Implementation implementation, final Messages messages, |
1594 | final Implementations seen, boolean includeDeclared ) |
1595 | { |
1596 | if ( implementation != null ) |
1597 | { |
1598 | if ( seen.getImplementation( implementation.getIdentifier() ) == null ) |
1599 | { |
1600 | seen.getImplementation().add( implementation ); |
1601 | |
1602 | if ( includeDeclared && implementation.getMessages() != null ) |
1603 | { |
1604 | for ( Message msg : implementation.getMessages().getMessage() ) |
1605 | { |
1606 | if ( messages.getMessage( msg.getName() ) == null ) |
1607 | { |
1608 | messages.getMessage().add( msg ); |
1609 | } |
1610 | } |
1611 | if ( !implementation.getMessages().getReference().isEmpty() ) |
1612 | { |
1613 | final Module m = this.getModuleOfImplementation( implementation.getIdentifier() ); |
1614 | |
1615 | if ( m != null ) |
1616 | { |
1617 | for ( MessageReference ref : implementation.getMessages().getReference() ) |
1618 | { |
1619 | if ( messages.getMessage( ref.getName() ) == null ) |
1620 | { |
1621 | Message referenced = m.getMessages().getMessage( ref.getName() ); |
1622 | if ( referenced != null ) |
1623 | { |
1624 | referenced = new Message( referenced ); |
1625 | referenced.setDeprecated( ref.isDeprecated() ); |
1626 | referenced.setFinal( ref.isFinal() ); |
1627 | referenced.setOverride( ref.isOverride() ); |
1628 | messages.getMessage().add( referenced ); |
1629 | } |
1630 | } |
1631 | } |
1632 | } |
1633 | } |
1634 | } |
1635 | |
1636 | if ( implementation.getImplementations() != null ) |
1637 | { |
1638 | for ( ImplementationReference r : implementation.getImplementations().getReference() ) |
1639 | { |
1640 | this.collectMessages( this.getImplementation( r.getIdentifier() ), messages, seen, true ); |
1641 | } |
1642 | } |
1643 | } |
1644 | } |
1645 | } |
1646 | |
1647 | void collectAny( final Implementation implementation, final List<Object> any, final Implementations seen, |
1648 | final boolean includeDeclared ) |
1649 | { |
1650 | if ( implementation != null ) |
1651 | { |
1652 | if ( seen.getImplementation( implementation.getIdentifier() ) == null ) |
1653 | { |
1654 | seen.getImplementation().add( implementation ); |
1655 | |
1656 | if ( includeDeclared && !implementation.getAny().isEmpty() ) |
1657 | { |
1658 | for ( Object o : implementation.getAny() ) |
1659 | { |
1660 | if ( o instanceof org.w3c.dom.Element ) |
1661 | { |
1662 | if ( this.getElement( any, (org.w3c.dom.Element) o ) == null ) |
1663 | { |
1664 | any.add( o ); |
1665 | } |
1666 | |
1667 | continue; |
1668 | } |
1669 | |
1670 | if ( o instanceof javax.xml.bind.JAXBElement ) |
1671 | { |
1672 | if ( this.getElement( any, (javax.xml.bind.JAXBElement) o ) == null ) |
1673 | { |
1674 | any.add( o ); |
1675 | } |
1676 | |
1677 | continue; |
1678 | } |
1679 | |
1680 | any.add( o ); |
1681 | } |
1682 | } |
1683 | |
1684 | if ( implementation.getImplementations() != null ) |
1685 | { |
1686 | for ( ImplementationReference r : implementation.getImplementations().getReference() ) |
1687 | { |
1688 | this.collectAny( this.getImplementation( r.getIdentifier() ), any, seen, true ); |
1689 | } |
1690 | } |
1691 | } |
1692 | } |
1693 | } |
1694 | |
1695 | private Implementation collectImplementation( final Class clazz ) |
1696 | { |
1697 | Implementation i = this.getImplementation( clazz ); |
1698 | if ( i == null && clazz.getSuperclass() != null ) |
1699 | { |
1700 | i = this.collectImplementation( clazz.getSuperclass() ); |
1701 | } |
1702 | |
1703 | return i; |
1704 | } |
1705 | |
1706 | private org.w3c.dom.Element getElement( final List<Object> list, final org.w3c.dom.Element e ) |
1707 | { |
1708 | for ( Object o : list ) |
1709 | { |
1710 | if ( o instanceof org.w3c.dom.Element ) |
1711 | { |
1712 | final org.w3c.dom.Element current = (org.w3c.dom.Element) o; |
1713 | if ( ( e.getNamespaceURI() == null |
1714 | ? current.getNamespaceURI() == null |
1715 | : e.getNamespaceURI().equals( current.getNamespaceURI() ) ) && |
1716 | ( e.getLocalName() == null |
1717 | ? current.getLocalName() == null |
1718 | : e.getLocalName().equals( current.getLocalName() ) ) ) |
1719 | { |
1720 | return current; |
1721 | } |
1722 | } |
1723 | } |
1724 | |
1725 | return null; |
1726 | } |
1727 | |
1728 | private javax.xml.bind.JAXBElement getElement( final List<Object> list, final javax.xml.bind.JAXBElement e ) |
1729 | { |
1730 | for ( Object o : list ) |
1731 | { |
1732 | if ( o instanceof javax.xml.bind.JAXBElement ) |
1733 | { |
1734 | final javax.xml.bind.JAXBElement current = (javax.xml.bind.JAXBElement) o; |
1735 | if ( e.getName().equals( current.getName() ) ) |
1736 | { |
1737 | return current; |
1738 | } |
1739 | } |
1740 | } |
1741 | |
1742 | return null; |
1743 | } |
1744 | |
1745 | private void resolveClasspath( final Module cpModule, final ClassLoader classLoader ) |
1746 | { |
1747 | for ( Module m : this.getModule() ) |
1748 | { |
1749 | if ( m.getSpecifications() != null ) |
1750 | { |
1751 | this.resolveClasspath( m.getSpecifications(), cpModule, classLoader ); |
1752 | } |
1753 | |
1754 | if ( m.getImplementations() != null ) |
1755 | { |
1756 | this.resolveClasspath( m.getImplementations(), cpModule, classLoader ); |
1757 | } |
1758 | } |
1759 | } |
1760 | |
1761 | private void resolveClasspath( final SpecificationReference ref, final Module cpModule, |
1762 | final ClassLoader classLoader ) |
1763 | { |
1764 | if ( this.getSpecification( ref.getIdentifier() ) == null ) |
1765 | { |
1766 | this.resolveClasspath( ref.getIdentifier(), cpModule, classLoader ); |
1767 | } |
1768 | } |
1769 | |
1770 | private void resolveClasspath( final Specifications references, final Module cpModule, |
1771 | final ClassLoader classLoader ) |
1772 | { |
1773 | for ( SpecificationReference ref : references.getReference() ) |
1774 | { |
1775 | this.resolveClasspath( ref, cpModule, classLoader ); |
1776 | } |
1777 | } |
1778 | |
1779 | private void resolveClasspath( final Implementations implementations, final Module cpModule, |
1780 | final ClassLoader classLoader ) |
1781 | { |
1782 | for ( Implementation implementation : implementations.getImplementation() ) |
1783 | { |
1784 | if ( implementation.getSpecifications() != null ) |
1785 | { |
1786 | this.resolveClasspath( implementation.getSpecifications(), cpModule, classLoader ); |
1787 | } |
1788 | |
1789 | if ( implementation.getDependencies() != null ) |
1790 | { |
1791 | this.resolveClasspath( implementation.getDependencies(), cpModule, classLoader ); |
1792 | } |
1793 | } |
1794 | } |
1795 | |
1796 | private void resolveClasspath( final Dependencies dependencies, final Module cpModule, |
1797 | final ClassLoader classLoader ) |
1798 | { |
1799 | for ( Dependency dependency : dependencies.getDependency() ) |
1800 | { |
1801 | this.resolveClasspath( dependency, cpModule, classLoader ); |
1802 | } |
1803 | } |
1804 | |
1805 | private boolean resolveClasspath( final String identifier, final Module cpModule, final ClassLoader classLoader ) |
1806 | { |
1807 | boolean classpathSpecification = false; |
1808 | Specification specification = cpModule.getSpecifications() == null |
1809 | ? null : cpModule.getSpecifications().getSpecification( identifier ); |
1810 | |
1811 | if ( specification == null ) |
1812 | { |
1813 | try |
1814 | { |
1815 | final Class classpathSpec = Class.forName( identifier, true, classLoader ); |
1816 | if ( java.lang.reflect.Modifier.isPublic( classpathSpec.getModifiers() ) ) |
1817 | { |
1818 | classpathSpecification = true; |
1819 | String vendor = null; |
1820 | String version = null; |
1821 | |
1822 | if ( classpathSpec.getPackage() != null ) |
1823 | { |
1824 | vendor = classpathSpec.getPackage().getSpecificationVendor(); |
1825 | version = classpathSpec.getPackage().getSpecificationVersion(); |
1826 | } |
1827 | |
1828 | specification = new Specification(); |
1829 | specification.setIdentifier( identifier ); |
1830 | specification.setClazz( classpathSpec.getName() ); |
1831 | specification.setMultiplicity( Multiplicity.MANY ); |
1832 | specification.setVendor( vendor ); |
1833 | specification.setVersion( version ); |
1834 | |
1835 | if ( cpModule.getSpecifications() == null ) |
1836 | { |
1837 | cpModule.setSpecifications( new Specifications() ); |
1838 | } |
1839 | |
1840 | cpModule.getSpecifications().getSpecification().add( specification ); |
1841 | |
1842 | this.resolveClasspath( specification, cpModule, classLoader ); |
1843 | } |
1844 | } |
1845 | catch ( final ClassNotFoundException e ) |
1846 | { |
1847 | classpathSpecification = false; |
1848 | } |
1849 | } |
1850 | |
1851 | return classpathSpecification; |
1852 | } |
1853 | |
1854 | private boolean resolveClasspath( final Specification specification, final Module cpModule, |
1855 | final ClassLoader classLoader ) |
1856 | { |
1857 | boolean classpathImplementation = false; |
1858 | Implementation implementation = |
1859 | cpModule.getImplementations() == null ? null |
1860 | : cpModule.getImplementations().getImplementation( specification.getIdentifier() ); |
1861 | |
1862 | if ( implementation == null ) |
1863 | { |
1864 | String name = null; |
1865 | |
1866 | try |
1867 | { |
1868 | final Class classpathImpl = Class.forName( specification.getClazz(), true, classLoader ); |
1869 | |
1870 | if ( java.lang.reflect.Modifier.isPublic( classpathImpl.getModifiers() ) ) |
1871 | { |
1872 | if ( !java.lang.reflect.Modifier.isAbstract( classpathImpl.getModifiers() ) ) |
1873 | { |
1874 | try |
1875 | { |
1876 | classpathImpl.getConstructor( NO_CLASSES ); |
1877 | name = "init"; |
1878 | classpathImplementation = true; |
1879 | } |
1880 | catch ( final NoSuchMethodException e ) |
1881 | { |
1882 | classpathImplementation = false; |
1883 | } |
1884 | } |
1885 | |
1886 | if ( !classpathImplementation ) |
1887 | { |
1888 | final char[] c = classpathImpl.getName().substring( |
1889 | classpathImpl.getPackage().getName().length() + 1 ).toCharArray(); |
1890 | |
1891 | name = String.valueOf( c ); |
1892 | c[0] = Character.toUpperCase( c[0] ); |
1893 | |
1894 | if ( this.checkFactoryMethod( classpathImpl, classpathImpl, "getDefault" ) ) |
1895 | { |
1896 | name = "default"; |
1897 | classpathImplementation = true; |
1898 | } |
1899 | else if ( this.checkFactoryMethod( classpathImpl, classpathImpl, "getInstance" ) ) |
1900 | { |
1901 | name = "instance"; |
1902 | classpathImplementation = true; |
1903 | } |
1904 | else if ( this.checkFactoryMethod( classpathImpl, classpathImpl, "get" + String.valueOf( c ) ) ) |
1905 | { |
1906 | classpathImplementation = true; |
1907 | } |
1908 | |
1909 | } |
1910 | |
1911 | if ( classpathImplementation ) |
1912 | { |
1913 | String vendor = null; |
1914 | String version = null; |
1915 | if ( classpathImpl.getPackage() != null ) |
1916 | { |
1917 | vendor = classpathImpl.getPackage().getImplementationVendor(); |
1918 | version = classpathImpl.getPackage().getImplementationVersion(); |
1919 | } |
1920 | |
1921 | implementation = new Implementation(); |
1922 | implementation.setVendor( vendor ); |
1923 | implementation.setFinal( true ); |
1924 | implementation.setName( name ); |
1925 | implementation.setIdentifier( specification.getIdentifier() ); |
1926 | implementation.setClazz( classpathImpl.getName() ); |
1927 | implementation.setVersion( version ); |
1928 | |
1929 | final Specifications implemented = new Specifications(); |
1930 | final SpecificationReference ref = new SpecificationReference(); |
1931 | ref.setIdentifier( specification.getIdentifier() ); |
1932 | ref.setVersion( specification.getVersion() ); |
1933 | implemented.getReference().add( ref ); |
1934 | implementation.setSpecifications( implemented ); |
1935 | |
1936 | if ( cpModule.getImplementations() == null ) |
1937 | { |
1938 | cpModule.setImplementations( new Implementations() ); |
1939 | } |
1940 | |
1941 | cpModule.getImplementations().getImplementation().add( implementation ); |
1942 | } |
1943 | } |
1944 | } |
1945 | catch ( final ClassNotFoundException e ) |
1946 | { |
1947 | classpathImplementation = false; |
1948 | } |
1949 | } |
1950 | |
1951 | return classpathImplementation; |
1952 | } |
1953 | |
1954 | private boolean checkFactoryMethod( final Class clazz, final Class type, final String methodName ) |
1955 | { |
1956 | boolean factoryMethod = false; |
1957 | |
1958 | try |
1959 | { |
1960 | final java.lang.reflect.Method m = clazz.getMethod( methodName, (Class[]) null ); |
1961 | factoryMethod = java.lang.reflect.Modifier.isStatic( m.getModifiers() ) && |
1962 | type.isAssignableFrom( m.getReturnType() ); |
1963 | |
1964 | } |
1965 | catch ( final NoSuchMethodException e ) |
1966 | { |
1967 | factoryMethod = false; |
1968 | } |
1969 | |
1970 | return factoryMethod; |
1971 | } |
1972 | |
1973 | private String getMessage( final String key, final Object args ) |
1974 | { |
1975 | return new java.text.MessageFormat( java.util.ResourceBundle.getBundle( |
1976 | Modules.class.getName().replace( '.', '/' ), java.util.Locale.getDefault() ). |
1977 | getString( key ) ).format( args ); |
1978 | |
1979 | } |
1980 | |
1981 | |
1982 | } |