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.ResolverException;
24 import org.exolab.castor.xml.XMLConstants;
25
26 /**
27 * Resolve a class by looking for a descriptor class 'nearby'. The descriptor
28 * class has to follow some specific naming conventions (and maybe be in a
29 * special package).
30 *
31 * @author <a href="mailto:jgrueneis AT gmail DOT com">Joachim Grueneis</a>
32 * @author <a href="mailto:stevendolg AT gxm DOT at">Steven Dolg</a>
33 * @version $Revision$ $Date$
34 * @since 1.2
35 */
36 public class ByDescriptorClass extends AbstractResolverClassCommand {
37 private static final Log LOG = LogFactory.getLog(ByDescriptorClass.class);
38
39 /**
40 * No specific stuff needed.
41 */
42 public ByDescriptorClass() {
43 super();
44 }
45
46 /**
47 * Tries to load an XMLClassDescriptor directly from an existing .class file.
48 * <br>
49 * The file that is searched for must be located in the classpath, have the name
50 * <code>className</code> + "Descriptor", and contain a valid XMLClassDescriptor.
51 * <br>
52 * If a descriptor is found it is added to the internal descriptor cache.
53 * <br>
54 * {@inheritDoc}
55 */
56 protected Map internalResolve(final String className, final ClassLoader classLoader,
57 final Map properties) throws ResolverException {
58
59 HashMap results = new HashMap();
60 if (classLoader == null) {
61 LOG.debug("No class loader available.");
62 return results;
63 }
64
65 StringBuffer descriptorClassName = new StringBuffer(className);
66 descriptorClassName.append(XMLConstants.DESCRIPTOR_SUFFIX);
67 Class descriptorClass = ResolveHelpers.loadClass(
68 classLoader, descriptorClassName.toString());
69
70 // If we didn't find the descriptor, look in descriptor package
71 if (descriptorClass == null) {
72 int offset = descriptorClassName.lastIndexOf(".");
73 if (offset != -1) {
74 descriptorClassName.insert(offset , ".");
75 descriptorClassName.insert(offset + 1, XMLConstants.DESCRIPTOR_PACKAGE);
76 descriptorClass = ResolveHelpers.loadClass(
77 classLoader, descriptorClassName.toString());
78 }
79 }
80
81 if (descriptorClass != null) {
82 try {
83 LOG.debug("Found descriptor: " + descriptorClass);
84 results.put(className, descriptorClass.newInstance());
85 } catch (InstantiationException e) {
86 LOG.debug("Ignored exception: " + e + "at loading descriptor class for: "
87 + className);
88 } catch (IllegalAccessException e) {
89 LOG.debug("Ignored exception: " + e + "at loading descriptor class for: "
90 + className);
91 }
92 }
93 return results;
94 }
95 }