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.1 2007-08-24 22:17:35 ewestfal Exp $
029 */
030
031 public class PersistentFieldFactory
032 {
033 private static Logger log = LoggerFactory.getLogger(PersistentFieldFactory.class);
034 private static final Class DEFAULT_PERSISTENT_FIELD_IMPL = PersistentFieldDirectImpl.class;
035 private static final Class[] METHOD_PARAMETER_TYPES = {Class.class, String.class};
036
037 // private static boolean usesAccessorsAndMutators = false;
038 // private static boolean usesAccessorsAndMutatorsCheck = false;
039
040 /**
041 * @throws MetadataException if an erros occours when creating the PersistenteField
042 */
043 public static PersistentField createPersistentField(Class attributeType, String attributeName)
044 {
045 return createPersistentField(null,attributeType,attributeName);
046 }
047
048 public static PersistentField createPersistentField(String persistentFieldClassName, Class attributeType, String attributeName)
049 {
050 try
051 {
052 if (persistentFieldClassName == null)
053 {
054 synchronized (PersistentFieldFactory.class)
055 {
056 persistentFieldClassName = getDefaultPersistentFieldClassName();
057 }
058 }
059 Object[] args = {attributeType, attributeName};
060 return (PersistentField) ClassHelper.newInstance(persistentFieldClassName, METHOD_PARAMETER_TYPES, args);
061
062 }
063 catch (Exception ex)
064 {
065 throw new MetadataException("Error creating PersistentField: " +
066 attributeType.getName() + ", " + attributeName, ex);
067 }
068 }
069
070 // public static boolean usesAccessorsAndMutators()
071 // {
072 // boolean retval = false;
073 // if (usesAccessorsAndMutatorsCheck)
074 // retval = usesAccessorsAndMutators;
075 // else
076 // {
077 // String className = getDefaultPersistentFieldClassName();
078 // PersistentField field = null;
079 // try
080 // {
081 // field = (PersistentField) ClassHelper.newInstance(className);
082 // usesAccessorsAndMutators = field.usesAccessorsAndMutators();
083 // retval = usesAccessorsAndMutators;
084 // }
085 // catch (Exception e)
086 // {
087 // log.error("Cannot verify 'usesAccessorsAndMutators' attribute for class " + className, e);
088 // }
089 // finally
090 // {
091 // usesAccessorsAndMutatorsCheck = true;
092 // }
093 // }
094 // return retval;
095 // }
096
097 private static String getDefaultPersistentFieldClassName()
098 {
099 try
100 {
101 PersistenceBrokerConfiguration config =
102 (PersistenceBrokerConfiguration) OjbConfigurator.getInstance().getConfigurationFor(
103 null);
104
105 Class clazz = config.getPersistentFieldClass();
106 return clazz.getName();
107 }
108 catch (ConfigurationException e)
109 {
110 log.error("Cannot look-up PersistentField class, use default implementation instead", e);
111 return DEFAULT_PERSISTENT_FIELD_IMPL.getName();
112 }
113 }
114
115 }