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 - output targetException when Invocation error
19: ...
20: **********************************************************************/package org.jpox.store.mapping;
21:
22: import org.jpox.ClassLoaderResolver;
23: import org.jpox.exceptions.JPOXException;
24: import org.jpox.metadata.AbstractMemberMetaData;
25: import org.jpox.store.DatastoreAdapter;
26: import org.jpox.store.DatastoreContainerObject;
27: import org.jpox.util.Localiser;
28:
29: /**
30: * Factory class for creating Mapping instances.
31: * This is called to generate field mappings for the classes to be persisted.
32: *
33: * @version $Revision: 1.18 $
34: */
35: public final class MappingFactory {
36: private static final Localiser LOCALISER = Localiser
37: .getInstance("org.jpox.store.Localisation");
38:
39: /** Private constructor to prevent instantiation. */
40: private MappingFactory() {
41: }
42:
43: /**
44: * Get a new instance of the Mapping using the DBA and type.
45: * @param mappingClass the Mapping class to be created
46: * @param dba The Database Adapter
47: * @param type The type
48: * @return The Mapping
49: */
50: public static JavaTypeMapping createMapping(Class mappingClass,
51: DatastoreAdapter dba, String type) {
52: JavaTypeMapping mapping = null;
53: try {
54: mapping = (JavaTypeMapping) mappingClass.newInstance();
55: } catch (Exception e) {
56: throw new JPOXException(LOCALISER.msg("041009",
57: mappingClass.getName(), e), e).setFatal();
58: }
59: mapping.initialize(dba, type);
60: return mapping;
61: }
62:
63: /**
64: * Get a new instance of the Mapping using the the DBA, field metadata, and the table managing the field.
65: * @param mappingClass the Mapping class to be created
66: * @param dba Datastore Adapter
67: * @param fmd FieldMetaData for the field to be mapped
68: * @param datastoreContainer The Table
69: * @param clr The ClassLoaderResolver
70: * @return The Mapping
71: */
72: public static JavaTypeMapping createMapping(Class mappingClass,
73: DatastoreAdapter dba, AbstractMemberMetaData fmd,
74: int roleForField,
75: DatastoreContainerObject datastoreContainer,
76: ClassLoaderResolver clr) {
77: JavaTypeMapping mapping = null;
78: try {
79: mapping = (JavaTypeMapping) mappingClass.newInstance();
80: } catch (Exception e) {
81: throw new JPOXException(LOCALISER.msg("041009",
82: mappingClass.getName(), e), e).setFatal();
83: }
84:
85: if (roleForField >= 0) {
86: mapping.setRoleForField(roleForField);
87: }
88: mapping.initialize(dba, fmd, datastoreContainer, clr);
89:
90: return mapping;
91: }
92: }
|