1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
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
36 extends StrategyInstaller {
37
38 private static final Localizer _loc = Localizer.forPackage
39 (RuntimeStrategyInstaller.class);
40
41 /**
42 * Constructor; supply configuration.
43 */
44 public RuntimeStrategyInstaller(MappingRepository repos) {
45 super(repos);
46 }
47
48 public void installStrategy(ClassMapping cls) {
49 if ((cls.getSourceMode() & cls.MODE_MAPPING) == 0)
50 throw new MetaDataException(_loc.get("no-mapping", cls));
51
52 ClassStrategy strat = repos.namedStrategy(cls);
53 if (strat == null)
54 strat = repos.defaultStrategy(cls, false);
55 cls.setStrategy(strat, Boolean.FALSE);
56 }
57
58 public void installStrategy(FieldMapping field) {
59 FieldStrategy strategy = repos.namedStrategy(field, true);
60 if (strategy == null) {
61 try {
62 strategy = repos.defaultStrategy(field, true, false);
63 } catch (MetaDataException mde) {
64 // if the parent class is abstract and field is unmapped,
65 // allow it to pass (assume subclasses map the field)
66 Class cls = field.getDefiningMetaData().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 }