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.Map;
17  
18  import org.apache.commons.logging.Log;
19  import org.apache.commons.logging.LogFactory;
20  import org.exolab.castor.mapping.ClassDescriptor;
21  import org.exolab.castor.xml.ResolverException;
22  import org.exolab.castor.xml.util.ResolverPackageCommand;
23  import org.exolab.castor.xml.util.ResolverStrategy;
24  
25  /**
26   * The abstract resolver command provides the argument checking, writes a debug message and reads
27   * the class loader from the properties... All specific code is found in the extended classes.
28   * 
29   * @author Joachim Grueneis, jgrueneis AT gmail DOT com
30   * @version $Id$
31   * @since 1.2
32   */
33  public abstract class AbstractResolverPackageCommand implements ResolverPackageCommand {
34  
35    private static final Log LOG = LogFactory.getLog(AbstractResolverPackageCommand.class);
36  
37    /**
38     * {@inheritDoc}
39     */
40    public final Map<String, ClassDescriptor> resolve(final String packageName, final Map properties)
41        throws ResolverException {
42      String pName;
43      if ((packageName == null) || ("".equals(packageName))) {
44        LOG.debug("Package name is empty! Anyhow, giving it a try...");
45        pName = "";
46      } else {
47        pName = packageName;
48      }
49  
50      if (LOG.isDebugEnabled()) {
51        LOG.debug(
52            "Now in resolve method: " + this.getClass().getName() + " resolving: " + packageName);
53      }
54  
55      ClassLoader classLoader = (ClassLoader) properties.get(ResolverStrategy.PROPERTY_CLASS_LOADER);
56      if (classLoader == null) {
57        LOG.debug("No domain class loader set, taking it from class.getClassLoader().");
58        classLoader = this.getClass().getClassLoader();
59      }
60      return internalResolve(pName, classLoader, properties);
61    }
62  
63    /**
64     * Is the given package name empty?
65     * 
66     * @param packageName the package name to check
67     * @return true if the String is empty
68     */
69    protected final boolean isEmptyPackageName(final String packageName) {
70      return ((packageName == null) || (packageName.length() == 0) || ("".equals(packageName)));
71    }
72  
73  
74    /**
75     * The required parameter checks are in the public method and here we expect that the resolve
76     * logic itself is implemented.
77     * 
78     * @param className the name of the class to resolve
79     * @param classLoader the class loader to use
80     * @param props the resolve properties to use
81     * @return a Map of className and XMLClassDescriptor
82     * @throws ResolverException in case of unrecoverable problems at resolving
83     */
84    protected abstract Map<String, ClassDescriptor> internalResolve(String packageName,
85        ClassLoader classLoader, Map props) throws ResolverException;
86  }