1 package org.apache.torque.dsfactory;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.Iterator;
20
21 import javax.sql.ConnectionPoolDataSource;
22
23 import org.apache.commons.beanutils.ConvertUtils;
24 import org.apache.commons.beanutils.MappedPropertyDescriptor;
25 import org.apache.commons.beanutils.PropertyUtils;
26
27 import org.apache.commons.configuration.Configuration;
28 import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import org.apache.torque.Torque;
34 import org.apache.torque.TorqueException;
35
36 /***
37 * A class that contains common functionality of the factories in this
38 * package.
39 *
40 * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
41 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
42 * @version $Id: AbstractDataSourceFactory.java,v 1.12.2.2 2004/05/20 04:35:14 seade Exp $
43 */
44 public abstract class AbstractDataSourceFactory
45 {
46 /*** "pool" Key for the configuration */
47 public static final String POOL_KEY = "pool";
48
49 /*** "connection" Key for the configuration */
50 public static final String CONNECTION_KEY = "connection";
51
52 /*** "default.pool" Key for the configuration */
53 public static final String DEFAULT_POOL_KEY = "defaults.pool";
54
55 /*** "default.connection" Key for the configuration */
56 public static final String DEFAULT_CONNECTION_KEY = "defaults.connection";
57
58 /*** The log */
59 private static Log log = LogFactory.getLog(AbstractDataSourceFactory.class);
60
61 /***
62 * Encapsulates setting configuration properties on
63 * <code>DataSource</code> objects.
64 *
65 * @param property the property to read from the configuration
66 * @param c the configuration to read the property from
67 * @param ds the <code>DataSource</code> instance to write the property to
68 * @throws Exception if anything goes wrong
69 */
70 protected void setProperty(String property, Configuration c, Object ds)
71 throws Exception
72 {
73 String key = property;
74 Class dsClass = ds.getClass();
75 int dot = property.indexOf('.');
76 try
77 {
78 if (dot > 0)
79 {
80 property = property.substring(0, dot);
81
82 MappedPropertyDescriptor mappedPD =
83 new MappedPropertyDescriptor(property, dsClass);
84 Class propertyType = mappedPD.getMappedPropertyType();
85 Configuration subProps = c.subset(property);
86
87 Iterator j = subProps.getKeys();
88 while (j.hasNext())
89 {
90 String subProp = (String) j.next();
91 String propVal = subProps.getString(subProp);
92 Object value = ConvertUtils.convert(propVal, propertyType);
93 PropertyUtils
94 .setMappedProperty(ds, property, subProp, value);
95
96 if (log.isDebugEnabled())
97 {
98 log.debug("setMappedProperty("
99 + ds + ", "
100 + property + ", "
101 + subProp + ", "
102 + value
103 + ")");
104 }
105 }
106 }
107 else
108 {
109 Class propertyType =
110 PropertyUtils.getPropertyType(ds, property);
111 Object value =
112 ConvertUtils.convert(c.getString(property), propertyType);
113 PropertyUtils.setSimpleProperty(ds, property, value);
114
115 if (log.isDebugEnabled())
116 {
117 log.debug("setSimpleProperty("
118 + ds + ", "
119 + property + ", "
120 + value
121 + ")");
122 }
123 }
124 }
125 catch (Exception e)
126 {
127 log.error(
128 "Property: "
129 + property
130 + " value: "
131 + c.getString(key)
132 + " is not supported by DataSource: "
133 + ds.getClass().getName());
134 }
135 }
136
137 /***
138 * Iterate over a Configuration subset and apply all
139 * properties to a passed object which must contain Bean
140 * setter and getter
141 *
142 * @param c The configuration subset
143 * @param o The object to apply the properties to
144 * @throws TorqueException if a property set fails
145 */
146 protected void applyConfiguration(Configuration c, Object o)
147 throws TorqueException
148 {
149 log.debug("applyConfiguration(" + c + ", " + o + ")");
150
151 if (c != null)
152 {
153 try
154 {
155 for (Iterator i = c.getKeys(); i.hasNext();)
156 {
157 String key = (String) i.next();
158 setProperty(key, c, o);
159 }
160 }
161 catch (Exception e)
162 {
163 log.error(e);
164 throw new TorqueException(e);
165 }
166 }
167 }
168
169 /***
170 * Initializes the ConnectionPoolDataSource.
171 *
172 * @param configuration where to read the settings from
173 * @throws TorqueException if a property set fails
174 * @return a configured <code>ConnectionPoolDataSource</code>
175 */
176 protected ConnectionPoolDataSource initCPDS(Configuration configuration)
177 throws TorqueException
178 {
179 log.debug("Starting initCPDS");
180 ConnectionPoolDataSource cpds = new DriverAdapterCPDS();
181 Configuration c = Torque.getConfiguration();
182
183 if (c == null)
184 {
185 log.warn("Global Configuration not set,"
186 + " no Default connection pool data source configured!");
187 }
188 else
189 {
190 Configuration conf = c.subset(DEFAULT_CONNECTION_KEY);
191 applyConfiguration(conf, cpds);
192 }
193
194 Configuration conf = configuration.subset(CONNECTION_KEY);
195 applyConfiguration(conf, cpds);
196
197 return cpds;
198 }
199 }