001: package org.apache.ojb.broker.metadata.fieldaccess;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.core.PersistenceBrokerConfiguration;
019: import org.apache.ojb.broker.metadata.MetadataException;
020: import org.apache.ojb.broker.util.ClassHelper;
021: import org.apache.ojb.broker.util.configuration.ConfigurationException;
022: import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
023: import org.apache.ojb.broker.util.logging.Logger;
024: import org.apache.ojb.broker.util.logging.LoggerFactory;
025:
026: /**
027: * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
028: * @version $Id: PersistentFieldFactory.java,v 1.11.2.2 2005/12/21 22:26:41 tomdz Exp $
029: */
030:
031: public class PersistentFieldFactory {
032: private static Logger log = LoggerFactory
033: .getLogger(PersistentFieldFactory.class);
034: private static final Class DEFAULT_PERSISTENT_FIELD_IMPL = PersistentFieldDirectImpl.class;
035: private static final Class[] METHOD_PARAMETER_TYPES = {
036: Class.class, String.class };
037:
038: // private static boolean usesAccessorsAndMutators = false;
039: // private static boolean usesAccessorsAndMutatorsCheck = false;
040:
041: /**
042: * @throws MetadataException if an erros occours when creating the PersistenteField
043: */
044: public static PersistentField createPersistentField(
045: Class attributeType, String attributeName) {
046: return createPersistentField(null, attributeType, attributeName);
047: }
048:
049: public static PersistentField createPersistentField(
050: String persistentFieldClassName, Class attributeType,
051: String attributeName) {
052: try {
053: if (persistentFieldClassName == null) {
054: synchronized (PersistentFieldFactory.class) {
055: persistentFieldClassName = getDefaultPersistentFieldClassName();
056: }
057: }
058: Object[] args = { attributeType, attributeName };
059: return (PersistentField) ClassHelper.newInstance(
060: persistentFieldClassName, METHOD_PARAMETER_TYPES,
061: args);
062:
063: } catch (Exception ex) {
064: throw new MetadataException(
065: "Error creating PersistentField: "
066: + attributeType.getName() + ", "
067: + attributeName, ex);
068: }
069: }
070:
071: // public static boolean usesAccessorsAndMutators()
072: // {
073: // boolean retval = false;
074: // if (usesAccessorsAndMutatorsCheck)
075: // retval = usesAccessorsAndMutators;
076: // else
077: // {
078: // String className = getDefaultPersistentFieldClassName();
079: // PersistentField field = null;
080: // try
081: // {
082: // field = (PersistentField) ClassHelper.newInstance(className);
083: // usesAccessorsAndMutators = field.usesAccessorsAndMutators();
084: // retval = usesAccessorsAndMutators;
085: // }
086: // catch (Exception e)
087: // {
088: // log.error("Cannot verify 'usesAccessorsAndMutators' attribute for class " + className, e);
089: // }
090: // finally
091: // {
092: // usesAccessorsAndMutatorsCheck = true;
093: // }
094: // }
095: // return retval;
096: // }
097:
098: private static String getDefaultPersistentFieldClassName() {
099: try {
100: PersistenceBrokerConfiguration config = (PersistenceBrokerConfiguration) OjbConfigurator
101: .getInstance().getConfigurationFor(null);
102:
103: Class clazz = config.getPersistentFieldClass();
104: return clazz.getName();
105: } catch (ConfigurationException e) {
106: log
107: .error(
108: "Cannot look-up PersistentField class, use default implementation instead",
109: e);
110: return DEFAULT_PERSISTENT_FIELD_IMPL.getName();
111: }
112: }
113:
114: }
|