01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.jdbc.meta;
20:
21: import java.lang.reflect.Modifier;
22:
23: import org.apache.openjpa.jdbc.meta.strats.NoneFieldStrategy;
24: import org.apache.openjpa.lib.util.Localizer;
25: import org.apache.openjpa.util.MetaDataException;
26:
27: /**
28: * Installer that attempts to use the given mapping information, and
29: * fails if it does not work.
30: *
31: * @author Abe White
32: * @nojavadoc
33: * @since 0.4.0
34: */
35: public class RuntimeStrategyInstaller extends StrategyInstaller {
36:
37: private static final Localizer _loc = Localizer
38: .forPackage(RuntimeStrategyInstaller.class);
39:
40: /**
41: * Constructor; supply configuration.
42: */
43: public RuntimeStrategyInstaller(MappingRepository repos) {
44: super (repos);
45: }
46:
47: public void installStrategy(ClassMapping cls) {
48: if ((cls.getSourceMode() & cls.MODE_MAPPING) == 0)
49: throw new MetaDataException(_loc.get("no-mapping", cls));
50:
51: ClassStrategy strat = repos.namedStrategy(cls);
52: if (strat == null)
53: strat = repos.defaultStrategy(cls, false);
54: cls.setStrategy(strat, Boolean.FALSE);
55: }
56:
57: public void installStrategy(FieldMapping field) {
58: FieldStrategy strategy = repos.namedStrategy(field, true);
59: if (strategy == null) {
60: try {
61: strategy = repos.defaultStrategy(field, true, false);
62: } catch (MetaDataException mde) {
63: // if the parent class is abstract and field is unmapped,
64: // allow it to pass (assume subclasses map the field)
65: Class cls = field.getDefiningMetaData()
66: .getDescribedType();
67: if (!Modifier.isAbstract(cls.getModifiers())
68: || field.getMappedBy() != null
69: || field.getMappingInfo().hasSchemaComponents()
70: || field.getValueInfo().hasSchemaComponents()
71: || field.getElementMapping().getValueInfo()
72: .hasSchemaComponents()
73: || field.getKeyMapping().getValueInfo()
74: .hasSchemaComponents())
75: throw mde;
76:
77: strategy = NoneFieldStrategy.getInstance();
78: }
79: }
80: field.setStrategy(strategy, Boolean.FALSE);
81: }
82:
83: public void installStrategy(Version version) {
84: VersionStrategy strat = repos.namedStrategy(version);
85: if (strat == null)
86: strat = repos.defaultStrategy(version, false);
87: version.setStrategy(strat, Boolean.FALSE);
88: }
89:
90: public void installStrategy(Discriminator discrim) {
91: DiscriminatorStrategy strat = repos.namedStrategy(discrim);
92: if (strat == null)
93: strat = repos.defaultStrategy(discrim, false);
94: discrim.setStrategy(strat, Boolean.FALSE);
95: }
96: }
|