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