001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.jdbc.conf;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.apache.openjpa.jdbc.meta.MetaDataPlusMappingFactory;
023: import org.apache.openjpa.lib.conf.Configuration;
024: import org.apache.openjpa.lib.conf.Configurations;
025: import org.apache.openjpa.lib.conf.PluginValue;
026: import org.apache.openjpa.lib.log.Log;
027: import org.apache.openjpa.lib.util.Localizer;
028: import org.apache.openjpa.meta.MetaDataFactory;
029:
030: /**
031: * Handles the complex logic of creating a {@link MetaDataFactory} for
032: * combined metadata and mapping.
033: *
034: * @author Abe White
035: * @nojavadoc
036: */
037: public class MappingFactoryValue extends PluginValue {
038:
039: private static final Localizer _loc = Localizer
040: .forPackage(MappingFactoryValue.class);
041:
042: private String[] _metaFactoryDefaults = null;
043: private String[] _mappedMetaFactoryDefaults = null;
044:
045: public MappingFactoryValue(String prop) {
046: super (prop, false);
047: }
048:
049: /**
050: * Default setting for a given <code>MetaDataFactory</code> alias setting.
051: * If a <code>MappingFactory</code> value is not supplied, we check these
052: * defaults against the <code>MetaDataFactory</code> setting. If the
053: * <code>MetaDataFactory</code> does not have a default, we assume it
054: * handles both metadata and mapping factory.
055: */
056: public void setMetaDataFactoryDefault(String metaAlias,
057: String mappingAlias) {
058: _metaFactoryDefaults = setAlias(metaAlias, mappingAlias,
059: _metaFactoryDefaults);
060: }
061:
062: /**
063: * If the <code>Mapping</code> property is set, we check these defaults
064: * before checking metadata factory defaults.
065: */
066: public void setMappedMetaDataFactoryDefault(String metaAlias,
067: String mappingAlias) {
068: _mappedMetaFactoryDefaults = setAlias(metaAlias, mappingAlias,
069: _mappedMetaFactoryDefaults);
070: }
071:
072: /**
073: * Intantiate a {@link MetaDataFactory} responsible for both metadata and
074: * mapping.
075: */
076: public MetaDataFactory instantiateMetaDataFactory(
077: Configuration conf, PluginValue metaPlugin, String mapping) {
078: return instantiateMetaDataFactory(conf, metaPlugin, mapping,
079: true);
080: }
081:
082: /**
083: * Intantiate a {@link MetaDataFactory} responsible for both metadata and
084: * mapping.
085: */
086: public MetaDataFactory instantiateMetaDataFactory(
087: Configuration conf, PluginValue metaPlugin, String mapping,
088: boolean fatal) {
089: String clsName = getClassName();
090: String props = getProperties();
091: String metaClsName = metaPlugin.getClassName();
092: String metaProps = metaPlugin.getProperties();
093:
094: // if no mapping factory set, check for default for this factory
095: if (StringUtils.isEmpty(clsName)) {
096: String def;
097: if (!StringUtils.isEmpty(mapping)) {
098: def = unalias(metaPlugin.alias(metaClsName),
099: _mappedMetaFactoryDefaults, true);
100: if (def != null)
101: clsName = unalias(def);
102: }
103: if (StringUtils.isEmpty(clsName)) {
104: def = unalias(metaPlugin.alias(metaClsName),
105: _metaFactoryDefaults, true);
106: if (def != null)
107: clsName = unalias(def);
108: }
109: }
110:
111: // if mapping factory and metadata factory the same, combine
112: // into metadata factory
113: if (clsName != null && clsName.equals(metaClsName)) {
114: if (props != null && metaProps == null)
115: metaProps = props;
116: else if (props != null)
117: metaProps += "," + props;
118: clsName = null;
119: props = null;
120: }
121:
122: // instantiate factories
123: MetaDataFactory map = (MetaDataFactory) newInstance(clsName,
124: MetaDataFactory.class, conf, fatal);
125: MetaDataFactory meta;
126: if (map != null
127: && map.getClass().getName().indexOf("Deprecated") != -1) {
128: // deprecated mapping factories take over metadata too, so we have
129: // to special-case them to treat them like metadata factory only
130: meta = map;
131: map = null;
132: } else {
133: meta = (MetaDataFactory) metaPlugin.newInstance(
134: metaClsName, MetaDataFactory.class, conf, fatal);
135: }
136:
137: // configure factories. if only meta factory, allow user to specify
138: // its mapping properties in the mapping factory setting
139: if (map == null && props != null) {
140: if (metaProps == null)
141: metaProps = props;
142: else
143: metaProps += ", " + props;
144: }
145: Configurations.configureInstance(map, conf, props,
146: (fatal) ? getProperty() : null);
147: Configurations.configureInstance(meta, conf, metaProps,
148: (fatal) ? metaPlugin.getProperty() : null);
149:
150: Log log = conf.getLog(JDBCConfiguration.LOG_METADATA);
151: if (log.isTraceEnabled()) {
152: log.trace(_loc.get("meta-factory", meta));
153: if (map != null)
154: log.trace(_loc.get("map-factory", map));
155: }
156:
157: // if no mapping setting, return meta factory alone, assuming it handles
158: // both metadata and mapping
159: MetaDataFactory ret = (map == null) ? meta
160: : new MetaDataPlusMappingFactory(meta, map);
161: return ret;
162: }
163: }
|