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