View Javadoc
1   /*
2    * Copyright 2007 Joachim Grueneis
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.xml.util.resolvers;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.exolab.castor.xml.ResolverException;
22  import org.exolab.castor.xml.XMLConstants;
23  
24  /**
25   * Resolve a class by looking for a descriptor class 'nearby'. The descriptor class has to follow
26   * some specific naming conventions (and maybe be in a special package).
27   * 
28   * @author <a href="mailto:jgrueneis AT gmail DOT com">Joachim Grueneis</a>
29   * @author <a href="mailto:stevendolg AT gxm DOT at">Steven Dolg</a>
30   * @version $Revision$ $Date$
31   * @since 1.2
32   */
33  public class ByDescriptorClass extends AbstractResolverClassCommand {
34    private static final Log LOG = LogFactory.getLog(ByDescriptorClass.class);
35  
36    /**
37     * No specific stuff needed.
38     */
39    public ByDescriptorClass() {
40      super();
41    }
42  
43    /**
44     * Tries to load an XMLClassDescriptor directly from an existing .class file. <br>
45     * The file that is searched for must be located in the classpath, have the name
46     * <code>className</code> + "Descriptor", and contain a valid XMLClassDescriptor. <br>
47     * If a descriptor is found it is added to the internal descriptor cache. <br>
48     * {@inheritDoc}
49     */
50    protected Map internalResolve(final String className, final ClassLoader classLoader,
51        final Map properties) throws ResolverException {
52  
53      HashMap results = new HashMap();
54      if (classLoader == null) {
55        LOG.debug("No class loader available.");
56        return results;
57      }
58  
59      StringBuilder descriptorClassName = new StringBuilder(className);
60      descriptorClassName.append(XMLConstants.DESCRIPTOR_SUFFIX);
61      Class descriptorClass = ResolveHelpers.loadClass(classLoader, descriptorClassName.toString());
62  
63      // If we didn't find the descriptor, look in descriptor package
64      if (descriptorClass == null) {
65        int offset = descriptorClassName.lastIndexOf(".");
66        if (offset != -1) {
67          descriptorClassName.insert(offset, ".");
68          descriptorClassName.insert(offset + 1, XMLConstants.DESCRIPTOR_PACKAGE);
69          descriptorClass = ResolveHelpers.loadClass(classLoader, descriptorClassName.toString());
70        }
71      }
72  
73      if (descriptorClass != null) {
74        try {
75          LOG.debug("Found descriptor: " + descriptorClass);
76          results.put(className, descriptorClass.newInstance());
77        } catch (InstantiationException e) {
78          LOG.debug("Ignored exception: " + e + "at loading descriptor class for: " + className);
79        } catch (IllegalAccessException e) {
80          LOG.debug("Ignored exception: " + e + "at loading descriptor class for: " + className);
81        }
82      }
83      return results;
84    }
85  }