View Javadoc
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.net.URL;
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.castor.mapping.BindingType;
27  import org.castor.mapping.MappingUnmarshaller;
28  import org.exolab.castor.mapping.ClassDescriptor;
29  import org.exolab.castor.mapping.Mapping;
30  import org.exolab.castor.mapping.MappingException;
31  import org.exolab.castor.mapping.MappingLoader;
32  import org.exolab.castor.xml.ResolverException;
33  import org.exolab.castor.xml.XMLConstants;
34  
35  /**
36   * Tries to load the package mapping file for the given package.
37   * <br/>
38   * If the mapping file is available and could be loaded properly the
39   * descriptors listed in it are added to the description class cache
40   * of the configuration.
41   * <br/>
42   * To disable loading of pacakge mapping files just don't use this
43   * command.
44   * <br/>
45   * If a mapping file is not available for the given package this method
46   * will not load any descriptors and not throw any exceptions.<br/>
47   * The class loader specified in the configuration is used.
48   * <br/>
49   * The mapping file - if available - is loaded using the
50   * <code>MappingLoader</code>.<br/>
51   * <br/>
52   * Further calls to this command with the same package name will not be
53   * processed.
54   * 
55   * @author <a href="mailto:jgrueneis AT gmail DOT com">Joachim Grueneis</a>
56   * @author <a href="mailto:stevendolg AT gxm DOT at">Steven Dolg</a>
57   * @version $Revision$ $Date$
58   * @since 1.2
59   */
60  public class ByPackageMapping extends AbstractResolverPackageCommand {
61  	private static final Log LOG = LogFactory.getLog(ByPackageMapping.class);
62      
63      private List<String> _loadedPackages = new ArrayList<String>();
64  
65      /**
66       * No specific stuff needed.
67       */
68  	public ByPackageMapping() {
69          super();
70  	}
71  
72      /**
73       * Loads a package mapping file for the given package name using the
74       * provided ClassLoader.
75       *
76       * @param packageName The name of the package to load the mapping file for.
77       * @return The loaded Mapping or <code>null</code> if no mapping file
78       *         is available for the given package.
79       * @throws MappingException
80       */
81      private Mapping loadMapping(final String packageName, final ClassLoader classLoader)
82      throws MappingException {
83          URL url = classLoader.getResource(ResolveHelpers.getQualifiedFileName(
84                  XMLConstants.PKG_MAPPING_FILE, packageName));
85          if (url == null) { return null; }
86          try {
87              Mapping mapping = new Mapping(classLoader);
88              mapping.loadMapping(url);
89              return mapping;
90          } catch (java.io.IOException ioex) {
91              throw new MappingException(ioex);
92          }
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      protected Map<String, ClassDescriptor> internalResolve(final String packageName, final ClassLoader classLoader,
99              final Map properties) throws ResolverException {
100         
101         Map<String, ClassDescriptor> results = new HashMap<String, ClassDescriptor>();
102         if (!isEmptyPackageName(packageName) && _loadedPackages.contains(packageName)) {
103             if (LOG.isDebugEnabled()) {
104                 LOG.debug("Package: " + packageName + " has already been loaded.");
105             }
106             return results;
107         }
108         
109         if (!isEmptyPackageName(packageName)) {
110             _loadedPackages.add(packageName);
111         }
112         try {
113             final Mapping mapping = this.loadMapping(packageName, classLoader);
114             if (mapping != null) {
115                 MappingUnmarshaller unmarshaller = new MappingUnmarshaller();
116                 // TODO: Joachim 2007-09-07 the InternalContext should be set into the unmarshaller!
117                 MappingLoader mappingLoader = unmarshaller.getMappingLoader(mapping, BindingType.XML);
118                 for (ClassDescriptor classDescriptor : mappingLoader.getDescriptors()) {
119                     if (LOG.isDebugEnabled()) {
120                         LOG.debug("Found descriptor: " + classDescriptor);
121                     }
122                     results.put(classDescriptor.getJavaClass().getName(), classDescriptor);
123                 }
124             }
125         } catch (MappingException e) {
126             if (LOG.isDebugEnabled()) {
127                 LOG.debug("Ignored exception: " + e + " while loading mapping for package: "
128                         + packageName);
129             }
130         }
131         return results;
132     }
133 }