01: /**********************************************************************
02: Copyright (c) 2003 Erik Bengtson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15:
16: Contributors:
17: 2003 Andy Jefferson - converted to use Reflection
18: 2004 Andy Jefferson - changed to give targetException on Invocation error
19: ...
20: **********************************************************************/package org.jpox.store.mapping;
21:
22: import java.lang.reflect.Constructor;
23: import java.lang.reflect.InvocationTargetException;
24: import java.util.HashMap;
25: import java.util.Map;
26:
27: import org.jpox.ClassConstants;
28: import org.jpox.exceptions.JPOXException;
29: import org.jpox.store.DatastoreField;
30: import org.jpox.store.StoreManager;
31: import org.jpox.util.Localiser;
32:
33: /**
34: * Factory class for creating Mapping instances
35: *
36: * @version $Revision: 1.3 $
37: */
38: public final class DatastoreMappingFactory {
39: private static final Localiser LOCALISER = Localiser
40: .getInstance("org.jpox.store.Localisation");
41:
42: /** Private constructor to prevent instantiation. */
43: private DatastoreMappingFactory() {
44: }
45:
46: /** cache of constructors keyed by mapping class **/
47: private static Map mappingConstructors = new HashMap();
48:
49: /** constructor arguments **/
50: private static final Class[] ctr_args_classes = new Class[] {
51: ClassConstants.JAVA_TYPE_MAPPING,
52: ClassConstants.STORE_MANAGER,
53: ClassConstants.DATASTORE_FIELD };
54:
55: /**
56: * Get a new instance of the Mapping using the Store Manager, type and field.
57: * @param mappingClass the Mapping class to be created
58: * @param mapping The java mapping type
59: * @param storeMgr The Store Manager
60: * @param column The column to map
61: * @return The Mapping
62: */
63: public static DatastoreMapping createMapping(Class mappingClass,
64: JavaTypeMapping mapping, StoreManager storeMgr,
65: DatastoreField column) {
66: Object obj = null;
67: try {
68: Object[] args = new Object[] { mapping, storeMgr, column };
69: Constructor ctr = (Constructor) mappingConstructors
70: .get(mappingClass);
71: if (ctr == null) {
72: ctr = mappingClass.getConstructor(ctr_args_classes);
73: mappingConstructors.put(mappingClass, ctr);
74: }
75: try {
76: obj = ctr.newInstance(args);
77: } catch (InvocationTargetException e) {
78: throw new JPOXException(
79: LOCALISER.msg("041009", mappingClass.getName(),
80: e.getTargetException()), e
81: .getTargetException()).setFatal();
82: } catch (Exception e) {
83: throw new JPOXException(LOCALISER.msg("041009",
84: mappingClass.getName(), e), e).setFatal();
85: }
86: } catch (NoSuchMethodException nsme) {
87: throw new JPOXException(LOCALISER.msg("041007",
88: JavaTypeMapping.class, StoreManager.class,
89: DatastoreField.class, mappingClass.getName()))
90: .setFatal();
91: }
92: return (DatastoreMapping) obj;
93: }
94: }
|