View Javadoc

1   package org.apache.ojb.broker.metadata.fieldaccess;
2   
3   /* Copyright 2002-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import org.apache.ojb.broker.core.PersistenceBrokerConfiguration;
19  import org.apache.ojb.broker.metadata.MetadataException;
20  import org.apache.ojb.broker.util.ClassHelper;
21  import org.apache.ojb.broker.util.configuration.ConfigurationException;
22  import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
23  import org.apache.ojb.broker.util.logging.Logger;
24  import org.apache.ojb.broker.util.logging.LoggerFactory;
25  
26  /**
27   * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
28   * @version $Id: PersistentFieldFactory.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $
29   */
30  
31  public class PersistentFieldFactory
32  {
33      private static Logger log = LoggerFactory.getLogger(PersistentFieldFactory.class);
34      private static final Class DEFAULT_PERSISTENT_FIELD_IMPL = PersistentFieldDirectImpl.class;
35      private static final Class[] METHOD_PARAMETER_TYPES = {Class.class, String.class};
36  
37  //    private static boolean usesAccessorsAndMutators = false;
38  //    private static boolean usesAccessorsAndMutatorsCheck = false;
39  
40      /**
41       * @throws MetadataException if an erros occours when creating the PersistenteField
42       */
43  	public static PersistentField createPersistentField(Class attributeType, String attributeName)
44  	{
45  		return createPersistentField(null,attributeType,attributeName);
46  	}
47      
48      public static PersistentField createPersistentField(String persistentFieldClassName, Class attributeType, String attributeName)
49      {
50          try
51          {
52              if (persistentFieldClassName == null)
53              {
54                  synchronized (PersistentFieldFactory.class)
55                  {
56                      persistentFieldClassName = getDefaultPersistentFieldClassName();
57                  }
58              }
59              Object[] args = {attributeType, attributeName};
60              return (PersistentField) ClassHelper.newInstance(persistentFieldClassName, METHOD_PARAMETER_TYPES, args);
61              
62          }
63          catch (Exception ex)
64          {
65              throw new MetadataException("Error creating PersistentField: " +
66                      attributeType.getName() + ", " + attributeName, ex);
67          }
68      }
69  
70  //    public static boolean usesAccessorsAndMutators()
71  //    {
72  //        boolean retval = false;
73  //        if (usesAccessorsAndMutatorsCheck)
74  //            retval = usesAccessorsAndMutators;
75  //        else
76  //        {
77  //            String className = getDefaultPersistentFieldClassName();
78  //            PersistentField field = null;
79  //            try
80  //            {
81  //                field = (PersistentField) ClassHelper.newInstance(className);
82  //                usesAccessorsAndMutators = field.usesAccessorsAndMutators();
83  //                retval = usesAccessorsAndMutators;
84  //            }
85  //            catch (Exception e)
86  //            {
87  //                log.error("Cannot verify 'usesAccessorsAndMutators' attribute for class " + className, e);
88  //            }
89  //            finally
90  //            {
91  //                usesAccessorsAndMutatorsCheck = true;
92  //            }
93  //        }
94  //        return retval;
95  //    }
96  
97      private static String getDefaultPersistentFieldClassName()
98      {
99          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 }