1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.xml.util.resolvers;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.exolab.castor.xml.Introspector;
24 import org.exolab.castor.xml.MarshalException;
25 import org.exolab.castor.xml.ResolverException;
26 import org.exolab.castor.xml.XMLClassDescriptor;
27 import org.exolab.castor.xml.util.ResolverStrategy;
28
29
30
31
32
33
34
35
36
37
38 public class ByIntrospection extends AbstractResolverClassCommand {
39
40
41
42 private static final Log LOG = LogFactory.getLog(ByIntrospection.class);
43
44
45
46
47 public ByIntrospection() {
48 super();
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62 protected Map internalResolve(final String className, final ClassLoader classLoader,
63 final Map properties) throws ResolverException {
64
65 Boolean useIntrospector =
66 (Boolean) properties.get(ResolverStrategy.PROPERTY_USE_INTROSPECTION);
67 HashMap results = new HashMap();
68 if (classLoader == null) {
69 LOG.debug("No class loader available.");
70 return results;
71 }
72
73 if ((useIntrospector != null) && (!useIntrospector.booleanValue())) {
74
75
76 LOG.debug("Introspection is disabled!");
77 return results;
78 }
79
80 Introspector introspector =
81 (Introspector) properties.get(ResolverStrategy.PROPERTY_INTROSPECTOR);
82 if (introspector == null) {
83 String message = "No Introspector defined in properties!";
84 LOG.warn(message);
85 throw new IllegalStateException(message);
86 }
87 Class clazz = ResolveHelpers.loadClass(classLoader, className);
88 if (clazz != null) {
89 try {
90 XMLClassDescriptor descriptor = introspector.generateClassDescriptor(clazz);
91 if (descriptor != null) {
92 if (LOG.isDebugEnabled()) {
93 LOG.debug("Found descriptor: " + descriptor);
94 }
95 results.put(clazz.getName(), descriptor);
96 }
97 } catch (MarshalException e) {
98 String message = "Failed to generate class descriptor for: "
99 + clazz + " with exception: " + e;
100 LOG.warn(message);
101 throw new ResolverException(message);
102 }
103 }
104 return results;
105 }
106 }