View Javadoc
1   /*
2    * Copyright 2005 Werner Guttmann
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.castor.mapping;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.exolab.castor.mapping.MappingException;
19  import org.exolab.castor.mapping.MappingLoader;
20  
21  /**
22   * Abstract base class for MappingLoaderFactory instances
23   * 
24   * @author me
25   * 
26   */
27  public abstract class AbstractMappingLoaderFactory implements MappingLoaderFactory {
28  
29    /**
30     * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons Logging </a> instance
31     * used for all logging.
32     */
33    public static final Log LOG = LogFactory.getLog(AbstractMappingLoaderFactory.class);
34  
35    /**
36     * Source type definition
37     */
38    private static final String SOURCE_TYPE = "CastorXmlMapping";
39  
40    /**
41     * @inheritDoc
42     * @see org.castor.mapping.MappingLoaderFactory#getSourceType()
43     */
44    public final String getSourceType() {
45      return SOURCE_TYPE;
46    }
47  
48    /**
49     * To obtain the class name of the MappingLoader to instantiate
50     * 
51     * @return The class name of the MappingLoader to instantiate
52     */
53    public abstract String getClassname();
54  
55    /**
56     * @inheritDoc
57     * @see org.castor.mapping.MappingLoaderFactory#getMappingLoader()
58     */
59    public final MappingLoader getMappingLoader() throws MappingException {
60      MappingLoader mappingLoader = null;
61      try {
62        ClassLoader loader = getClass().getClassLoader();
63        Class cls = loader.loadClass(getClassname());
64        Class[] types = new Class[] {ClassLoader.class};
65        Object[] args = new Object[] {loader};
66        mappingLoader = (MappingLoader) cls.getConstructor(types).newInstance(args);
67      } catch (Exception ex) {
68        LOG.error("Problem instantiating mapping loader factory implementation: " + getClassname(),
69            ex);
70      }
71      return mappingLoader;
72    }
73  
74  }