001: package org.apache.torque.dsfactory;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Iterator;
023:
024: import javax.sql.ConnectionPoolDataSource;
025: import javax.sql.DataSource;
026:
027: import org.apache.commons.beanutils.ConvertUtils;
028: import org.apache.commons.beanutils.MappedPropertyDescriptor;
029: import org.apache.commons.beanutils.PropertyUtils;
030: import org.apache.commons.configuration.Configuration;
031: import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
032: import org.apache.commons.lang.StringUtils;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.apache.torque.Torque;
036: import org.apache.torque.TorqueException;
037: import org.apache.torque.TorqueRuntimeException;
038:
039: /**
040: * A class that contains common functionality of the factories in this
041: * package.
042: *
043: * @author <a href="mailto:jmcnally@apache.org">John McNally</a>
044: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
045: * @version $Id: AbstractDataSourceFactory.java 473821 2006-11-11 22:37:25Z tv $
046: */
047: public abstract class AbstractDataSourceFactory implements
048: DataSourceFactory {
049: /** "pool" Key for the configuration */
050: public static final String POOL_KEY = "pool";
051:
052: /** "connection" Key for the configuration */
053: public static final String CONNECTION_KEY = "connection";
054:
055: /** "schema" Key for the configuration */
056: public static final String SCHEMA_KEY = "schema";
057:
058: /** "defaults" Key for the configuration */
059: public static final String DEFAULTS_KEY = "defaults";
060:
061: /** "defaults.pool" Key for the configuration */
062: public static final String DEFAULT_POOL_KEY = DEFAULTS_KEY + "."
063: + POOL_KEY;
064:
065: /** "defaults.connection" Key for the configuration */
066: public static final String DEFAULT_CONNECTION_KEY = DEFAULTS_KEY
067: + "." + CONNECTION_KEY;
068:
069: /** default schema name for the configuration */
070: public static final String DEFAULT_SCHEMA_KEY = DEFAULTS_KEY + "."
071: + SCHEMA_KEY;
072:
073: /** The log */
074: private static Log log = LogFactory
075: .getLog(AbstractDataSourceFactory.class);
076:
077: /** Internal Marker for the Schema name of this database connection */
078: private String schema = null;
079:
080: /**
081: * Encapsulates setting configuration properties on
082: * <code>DataSource</code> objects.
083: *
084: * @param property the property to read from the configuration
085: * @param c the configuration to read the property from
086: * @param ds the <code>DataSource</code> instance to write the property to
087: * @throws Exception if anything goes wrong
088: */
089: protected void setProperty(String property, Configuration c,
090: Object ds) throws Exception {
091: if (c == null || c.isEmpty()) {
092: return;
093: }
094:
095: String key = property;
096: Class dsClass = ds.getClass();
097: int dot = property.indexOf('.');
098: try {
099: if (dot > 0) {
100: property = property.substring(0, dot);
101:
102: MappedPropertyDescriptor mappedPD = new MappedPropertyDescriptor(
103: property, dsClass);
104: Class propertyType = mappedPD.getMappedPropertyType();
105: Configuration subProps = c.subset(property);
106: // use reflection to set properties
107: Iterator j = subProps.getKeys();
108: while (j.hasNext()) {
109: String subProp = (String) j.next();
110: String propVal = subProps.getString(subProp);
111: Object value = ConvertUtils.convert(propVal,
112: propertyType);
113: PropertyUtils.setMappedProperty(ds, property,
114: subProp, value);
115:
116: if (log.isDebugEnabled()) {
117: log.debug("setMappedProperty(" + ds + ", "
118: + property + ", " + subProp + ", "
119: + value + ")");
120: }
121: }
122: } else {
123: if ("password".equals(key)) {
124: // do not log value of password
125: // for this, ConvertUtils.convert cannot be used
126: // as it also logs the value of the converted property
127: // so it is assumed here that the password is a String
128: String value = c.getString(property);
129: PropertyUtils
130: .setSimpleProperty(ds, property, value);
131: if (log.isDebugEnabled()) {
132: log.debug("setSimpleProperty(" + ds + ", "
133: + property + ", "
134: + " (value not logged)" + ")");
135: }
136: } else {
137: Class propertyType = PropertyUtils.getPropertyType(
138: ds, property);
139: Object value = ConvertUtils.convert(c
140: .getString(property), propertyType);
141: PropertyUtils
142: .setSimpleProperty(ds, property, value);
143:
144: if (log.isDebugEnabled()) {
145: log.debug("setSimpleProperty(" + ds + ", "
146: + property + ", " + value + ")");
147: }
148: }
149: }
150: } catch (RuntimeException e) {
151: throw new TorqueRuntimeException(
152: "Runtime error setting property " + property, e);
153: } catch (Exception e) {
154: log.error("Property: " + property + " value: "
155: + c.getString(key)
156: + " is not supported by DataSource: "
157: + ds.getClass().getName());
158: }
159: }
160:
161: /**
162: * Iterate over a Configuration subset and apply all
163: * properties to a passed object which must contain Bean
164: * setter and getter
165: *
166: * @param c The configuration subset
167: * @param o The object to apply the properties to
168: * @throws TorqueException if a property set fails
169: */
170: protected void applyConfiguration(Configuration c, Object o)
171: throws TorqueException {
172: log.debug("applyConfiguration(" + c + ", " + o + ")");
173:
174: if (c != null) {
175: try {
176: for (Iterator i = c.getKeys(); i.hasNext();) {
177: String key = (String) i.next();
178: setProperty(key, c, o);
179: }
180: } catch (Exception e) {
181: log.error(e);
182: throw new TorqueException(e);
183: }
184: }
185: }
186:
187: /**
188: * Initializes the ConnectionPoolDataSource.
189: *
190: * @param configuration where to read the settings from
191: * @throws TorqueException if a property set fails
192: * @return a configured <code>ConnectionPoolDataSource</code>
193: */
194: protected ConnectionPoolDataSource initCPDS(
195: Configuration configuration) throws TorqueException {
196: log.debug("Starting initCPDS");
197: ConnectionPoolDataSource cpds = new DriverAdapterCPDS();
198: Configuration c = Torque.getConfiguration();
199:
200: if (c == null || c.isEmpty()) {
201: log
202: .warn("Global Configuration not set,"
203: + " no Default connection pool data source configured!");
204: } else {
205: Configuration conf = c.subset(DEFAULT_CONNECTION_KEY);
206: applyConfiguration(conf, cpds);
207: }
208:
209: Configuration conf = configuration.subset(CONNECTION_KEY);
210: applyConfiguration(conf, cpds);
211:
212: return cpds;
213: }
214:
215: /**
216: * Sets the current schema for the database connection
217: *
218: * @param schema The current schema name
219: */
220: public void setSchema(String schema) {
221: this .schema = schema;
222: }
223:
224: /**
225: * This method returns the current schema for the database connection
226: *
227: * @return The current schema name. Null means, no schema has been set.
228: * @throws TorqueException Any exceptions caught during processing will be
229: * rethrown wrapped into a TorqueException.
230: * @deprecated use DatabaseInfo.setSchema() instead. Will be removed
231: * in a future version of Torque.
232: */
233: public String getSchema() {
234: return schema;
235: }
236:
237: /**
238: * @return the <code>DataSource</code> configured by the factory.
239: * @throws TorqueException if the source can't be returned
240: */
241: public abstract DataSource getDataSource() throws TorqueException;
242:
243: /**
244: * Initialize the factory.
245: *
246: * @param configuration where to load the factory settings from
247: * @throws TorqueException Any exceptions caught during processing will be
248: * rethrown wrapped into a TorqueException.
249: */
250: public void initialize(Configuration configuration)
251: throws TorqueException {
252: if (configuration == null) {
253: throw new TorqueException(
254: "Torque cannot be initialized without "
255: + "a valid configuration. Please check the log files "
256: + "for further details.");
257: }
258:
259: schema = configuration.getString(SCHEMA_KEY, null);
260:
261: if (StringUtils.isEmpty(schema)) {
262: Configuration conf = Torque.getConfiguration();
263: schema = conf.getString(DEFAULT_SCHEMA_KEY, null);
264: }
265: }
266: }
|