001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.sql.connection;
019:
020: import java.sql.Connection;
021: import java.sql.Driver;
022: import java.sql.SQLException;
023: import java.util.Map;
024: import java.util.Properties;
025:
026: import org.sape.carbon.core.component.ComponentConfiguration;
027: import org.sape.carbon.core.component.lifecycle.Configurable;
028: import org.sape.carbon.core.component.lifecycle.StateTransitionException;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * A Factory for JDBC Connections. Hides interaction with the JDBC driver.
035: * Maintains a reference to a JDBC Driver, plus connection info (userId,
036: * password, URL). Note that the Driver instance is discarded and recreated
037: * each time the component is configured.
038: *
039: * Copyright 2002 Sapient
040: * @see StandaloneConnectionFactoryConfiguration
041: *
042: * @since carbon 1.0
043: * @author Chris Herron, March 2002
044: * @version $Revision: 1.13 $($Author: araman $ / $Date: 2003/07/29 08:01:30 $)
045: */
046: public class StandaloneConnectionFactory implements ConnectionFactory,
047: Configurable {
048:
049: /**
050: * The environment key for the username to authenticate a database
051: * connection with.
052: */
053: protected static final String USER_KEY = "user";
054:
055: /**
056: * The environment key for the password to authenticate a database
057: * connection with.
058: */
059: protected static final String PASSWORD_KEY = "password";
060:
061: /**
062: * A reference to the database driver.
063: * This gets reset after each call to <code>configure(...)</code>.
064: */
065: protected Driver driver = null;
066:
067: /**
068: * The JDBC connection URL for the database.
069: */
070: protected String dbUrl = null;
071:
072: /**
073: * The properties passed to the JDBC driver to obtain a connection.
074: * Uses the keys "user" and "password" for the user and password values.
075: * (Constants defined below).
076: */
077: protected Properties dbConnectionProperties;
078:
079: /**
080: * Provides a handle to Apache-commons logger
081: */
082: private Log log = LogFactory.getLog(this .getClass());
083:
084: /**
085: * Uses the configured JDBC Driver and connection parameters to return a
086: * JDBC connection.
087: *
088: * @return Connection specified by this factory
089: * @throws SQLException indicates an error creating the connection
090: * to the datasource
091: */
092: public Connection getConnection() throws SQLException {
093: Connection con = driver.connect(dbUrl, dbConnectionProperties);
094: return con;
095: }
096:
097: /**
098: * Configure the component. This is preceded and followed by the suspend and
099: * resume operations if they are available on the component.
100: *
101: * @param configuration A StandaloneConnectionFactoryConfiguration instance
102: */
103: public void configure(ComponentConfiguration configuration) {
104:
105: try {
106: StandaloneConnectionFactoryConfiguration config = (StandaloneConnectionFactoryConfiguration) configuration;
107: Class driverClass = config.getDriverClass();
108: String dbUserId = config.getDbUserId();
109: String dbPassword = config.getDbPassword();
110: this .dbUrl = config.getDbUrl();
111: this .driver = (Driver) driverClass.newInstance();
112:
113: if (null == driver) {
114: throw new StateTransitionException(this .getClass(),
115: "Configuration Failed: Could not instantiate a Driver");
116: }
117: Properties props = new Properties();
118:
119: //Intentionally omitted check for empty string here since an error
120: //will be reported when the connection is attempted
121: if (null != dbUserId) {
122: props.put(StandaloneConnectionFactory.USER_KEY,
123: dbUserId);
124: if (null != dbPassword) {
125: props.put(StandaloneConnectionFactory.PASSWORD_KEY,
126: dbPassword);
127: }
128: } else {
129: if (log.isInfoEnabled()) {
130: log
131: .info("No username provided. Logins will be anonymous. "
132: + "([user] and [password] keys/entries omitted from "
133: + "connection properties object");
134: }
135: }
136:
137: //process additional properties set for the connection
138: Map connectionProperties = config.getConnectionProperties();
139: if (connectionProperties == null
140: || connectionProperties.size() == 0) {
141: if (log.isInfoEnabled()) {
142: log
143: .info("No additional properties for the connection have been specified");
144: }
145: } else {
146: //properties have been specified, iterate and add it to the list
147: props.putAll(connectionProperties);
148: if (log.isInfoEnabled()) {
149: log.info(connectionProperties.size()
150: + " additional properties "
151: + connectionProperties
152: + " for the connection has been set");
153: }
154: }
155:
156: this .dbConnectionProperties = props;
157: } catch (InstantiationException ie) {
158: throw new StateTransitionException(
159: this .getClass(),
160: "Configuration Failed: Could not instantiate a Driver",
161: ie);
162: } catch (IllegalAccessException ae) {
163: throw new StateTransitionException(
164: this .getClass(),
165: "Configuration Failed: Could not instantiate a Driver",
166: ae);
167: }
168: }
169: }
|