1 package org.apache.torque.dsfactory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Iterator;
23
24 import javax.sql.ConnectionPoolDataSource;
25 import javax.sql.DataSource;
26
27 import org.apache.commons.beanutils.ConvertUtils;
28 import org.apache.commons.beanutils.MappedPropertyDescriptor;
29 import org.apache.commons.beanutils.PropertyUtils;
30 import org.apache.commons.configuration.Configuration;
31 import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.torque.Torque;
35 import org.apache.torque.TorqueException;
36 import org.apache.torque.TorqueRuntimeException;
37
38
39
40
41
42
43
44
45
46 public abstract class AbstractDataSourceFactory
47 implements DataSourceFactory
48 {
49
50 public static final String POOL_KEY = "pool";
51
52
53 public static final String CONNECTION_KEY = "connection";
54
55
56 public static final String DEFAULTS_KEY = "defaults";
57
58
59 public static final String DEFAULT_POOL_KEY
60 = DEFAULTS_KEY + "." + POOL_KEY;
61
62
63 public static final String DEFAULT_CONNECTION_KEY
64 = DEFAULTS_KEY + "." + CONNECTION_KEY;
65
66
67 private static Log log = LogFactory.getLog(AbstractDataSourceFactory.class);
68
69
70
71
72
73
74
75
76
77
78 protected void setProperty(String property, Configuration c, Object ds)
79 throws Exception
80 {
81 if (c == null || c.isEmpty())
82 {
83 return;
84 }
85
86 String key = property;
87 Class<?> dsClass = ds.getClass();
88 int dot = property.indexOf('.');
89 try
90 {
91 if (dot > 0)
92 {
93 property = property.substring(0, dot);
94
95 MappedPropertyDescriptor mappedPD =
96 new MappedPropertyDescriptor(property, dsClass);
97 Class<?> propertyType = mappedPD.getMappedPropertyType();
98 Configuration subProps = c.subset(property);
99
100 Iterator<?> j = subProps.getKeys();
101 while (j.hasNext())
102 {
103 String subProp = (String) j.next();
104 String propVal = subProps.getString(subProp);
105 Object value = ConvertUtils.convert(propVal, propertyType);
106 PropertyUtils
107 .setMappedProperty(ds, property, subProp, value);
108
109 if (log.isDebugEnabled())
110 {
111 log.debug("setMappedProperty("
112 + ds + ", "
113 + property + ", "
114 + subProp + ", "
115 + value
116 + ")");
117 }
118 }
119 }
120 else
121 {
122 if ("password".equals(key))
123 {
124
125
126
127
128 String value = c.getString(property);
129 PropertyUtils.setSimpleProperty(ds, property, value);
130 if (log.isDebugEnabled())
131 {
132 log.debug("setSimpleProperty("
133 + ds + ", "
134 + property + ", "
135 + " (value not logged)"
136 + ")");
137 }
138 }
139 else
140 {
141 Class<?> propertyType =
142 PropertyUtils.getPropertyType(ds, property);
143 Object value =
144 ConvertUtils.convert(c.getString(property), propertyType);
145 PropertyUtils.setSimpleProperty(ds, property, value);
146
147 if (log.isDebugEnabled())
148 {
149 log.debug("setSimpleProperty("
150 + ds + ", "
151 + property + ", "
152 + value
153 + ")");
154 }
155 }
156 }
157 }
158 catch (RuntimeException e)
159 {
160 throw new TorqueRuntimeException(
161 "Runtime error setting property " + property, e);
162 }
163 catch (Exception e)
164 {
165 log.error(
166 "Property: "
167 + property
168 + " value: "
169 + c.getString(key)
170 + " is not supported by DataSource: "
171 + ds.getClass().getName());
172 }
173 }
174
175
176
177
178
179
180
181
182
183
184 protected void applyConfiguration(Configuration c, Object o)
185 throws TorqueException
186 {
187 log.debug("applyConfiguration(" + c + ", " + o + ")");
188
189 if (c != null)
190 {
191 try
192 {
193 for (Iterator<?> i = c.getKeys(); i.hasNext();)
194 {
195 String key = (String) i.next();
196 setProperty(key, c, o);
197 }
198 }
199 catch (Exception e)
200 {
201 log.error(e);
202 throw new TorqueException(e);
203 }
204 }
205 }
206
207
208
209
210
211
212
213
214 protected ConnectionPoolDataSource initCPDS(Configuration configuration)
215 throws TorqueException
216 {
217 log.debug("Starting initCPDS");
218 ConnectionPoolDataSource cpds = new DriverAdapterCPDS();
219 Configuration c = Torque.getConfiguration();
220
221 if (c == null || c.isEmpty())
222 {
223 log.warn("Global Configuration not set,"
224 + " no Default connection pool data source configured!");
225 }
226 else
227 {
228 Configuration conf = c.subset(DEFAULT_CONNECTION_KEY);
229 applyConfiguration(conf, cpds);
230 }
231
232 Configuration conf = configuration.subset(CONNECTION_KEY);
233 applyConfiguration(conf, cpds);
234
235 return cpds;
236 }
237
238
239
240
241
242 public abstract DataSource getDataSource()
243 throws TorqueException;
244
245
246
247
248
249
250
251
252 public abstract void initialize(Configuration configuration)
253 throws TorqueException;
254 }