1 package org.apache.ojb.broker.metadata.fieldaccess;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
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
38
39
40
41
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 }