1 /*
2 * Copyright 2007 Joachim Grueneis
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Resolve a class by creating a generic descriptor based on the informations
31 * read from the class with introspection.
32 *
33 * @author <a href="mailto:jgrueneis AT gmail DOT com">Joachim Grueneis</a>
34 * @author <a href="mailto:stevendolg AT gxm DOT at">Steven Dolg</a>
35 * @version $Revision$ $Date$
36 * @since 1.2
37 */
38 public class ByIntrospection extends AbstractResolverClassCommand {
39 /**
40 * Logger to be used.
41 */
42 private static final Log LOG = LogFactory.getLog(ByIntrospection.class);
43
44 /**
45 * No specific stuff needed.
46 */
47 public ByIntrospection() {
48 super();
49 }
50
51 /**
52 * Creates an XMLClassDescriptor for the given type by using introspection.
53 * This method will rely on the <code>Introspector</code> set with
54 * <code>setIntrospector</code>. If a descriptor is successfully created it
55 * will be added to the DescriptorCache.
56 * <br>
57 * <b>NOTE</b>: If this XMLClassDescriptorResolver is NOT configured to use
58 * introspection this method will NOT create an descriptor.<br>
59 * <br>
60 * {@inheritDoc}
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 // I know the logic is a bit weired... either introspection is explicitly
75 // disabled or it is ok!
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 }