KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > sql > connection > StandaloneConnectionFactory


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.sql.connection;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.Driver JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.sape.carbon.core.component.ComponentConfiguration;
27 import org.sape.carbon.core.component.lifecycle.Configurable;
28 import org.sape.carbon.core.component.lifecycle.StateTransitionException;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * A Factory for JDBC Connections. Hides interaction with the JDBC driver.
35  * Maintains a reference to a JDBC Driver, plus connection info (userId,
36  * password, URL). Note that the Driver instance is discarded and recreated
37  * each time the component is configured.
38  *
39  * Copyright 2002 Sapient
40  * @see StandaloneConnectionFactoryConfiguration
41  *
42  * @since carbon 1.0
43  * @author Chris Herron, March 2002
44  * @version $Revision: 1.13 $($Author: araman $ / $Date: 2003/07/29 08:01:30 $)
45  */

46 public class StandaloneConnectionFactory
47         implements ConnectionFactory, Configurable {
48
49
50     /**
51      * The environment key for the username to authenticate a database
52      * connection with.
53      */

54     protected static final String JavaDoc USER_KEY = "user";
55
56     /**
57      * The environment key for the password to authenticate a database
58      * connection with.
59      */

60     protected static final String JavaDoc PASSWORD_KEY = "password";
61
62     /**
63      * A reference to the database driver.
64      * This gets reset after each call to <code>configure(...)</code>.
65      */

66     protected Driver JavaDoc driver = null;
67
68     /**
69      * The JDBC connection URL for the database.
70      */

71     protected String JavaDoc dbUrl = null;
72
73     /**
74      * The properties passed to the JDBC driver to obtain a connection.
75      * Uses the keys "user" and "password" for the user and password values.
76      * (Constants defined below).
77      */

78     protected Properties JavaDoc dbConnectionProperties;
79
80     /**
81      * Provides a handle to Apache-commons logger
82      */

83     private Log log = LogFactory.getLog(this.getClass());
84
85
86     /**
87      * Uses the configured JDBC Driver and connection parameters to return a
88      * JDBC connection.
89      *
90      * @return Connection specified by this factory
91      * @throws SQLException indicates an error creating the connection
92      * to the datasource
93      */

94     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
95         Connection JavaDoc con = driver.connect(dbUrl, dbConnectionProperties);
96         return con;
97     }
98
99     /**
100      * Configure the component. This is preceded and followed by the suspend and
101      * resume operations if they are available on the component.
102      *
103      * @param configuration A StandaloneConnectionFactoryConfiguration instance
104      */

105     public void configure(ComponentConfiguration configuration) {
106
107         try {
108             StandaloneConnectionFactoryConfiguration config =
109                 (StandaloneConnectionFactoryConfiguration) configuration;
110             Class JavaDoc driverClass = config.getDriverClass();
111             String JavaDoc dbUserId = config.getDbUserId();
112             String JavaDoc dbPassword = config.getDbPassword();
113             this.dbUrl = config.getDbUrl();
114             this.driver = (Driver JavaDoc) driverClass.newInstance();
115
116             if (null == driver) {
117                 throw new StateTransitionException(this.getClass(),
118                     "Configuration Failed: Could not instantiate a Driver");
119             }
120             Properties JavaDoc props = new Properties JavaDoc();
121
122             //Intentionally omitted check for empty string here since an error
123
//will be reported when the connection is attempted
124
if (null != dbUserId) {
125                 props.put(StandaloneConnectionFactory.USER_KEY,
126                           dbUserId);
127                 if (null != dbPassword) {
128                     props.put(StandaloneConnectionFactory.PASSWORD_KEY,
129                               dbPassword);
130                 }
131             } else {
132                 if (log.isInfoEnabled()) {
133                     log.info("No username provided. Logins will be anonymous. "
134                         + "([user] and [password] keys/entries omitted from "
135                         + "connection properties object");
136                 }
137             }
138             
139             //process additional properties set for the connection
140
Map JavaDoc connectionProperties = config.getConnectionProperties();
141             if ( connectionProperties == null || connectionProperties.size() == 0 ) {
142                 if( log.isInfoEnabled() ) {
143                     log.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() + " additional properties " +
150                                 connectionProperties+" for the connection has been set");
151                 }
152             }
153             
154             this.dbConnectionProperties = props;
155         } catch (InstantiationException JavaDoc ie) {
156             throw new StateTransitionException(this.getClass(),
157                 "Configuration Failed: Could not instantiate a Driver", ie);
158         } catch (IllegalAccessException JavaDoc ae) {
159             throw new StateTransitionException(this.getClass(),
160                 "Configuration Failed: Could not instantiate a Driver", ae);
161         }
162     }
163 }
Popular Tags