001: package net.xoetrope.optional.data.sql;
002:
003: import java.sql.Connection;
004: import java.sql.DriverManager;
005: import java.sql.SQLException;
006: import java.util.Vector;
007:
008: import net.xoetrope.optional.pool.PoolObject;
009:
010: /**
011: * <p>An extension of the ConnectionManager to manage connections to multiple
012: * databases</p>
013: * <p>Copyright: Copyright (c) 2003<br>
014: * License: see license.txt</p>
015: * $Revision: 1.4 $
016: */
017: public class NamedConnectionManager extends ConnectionManager {
018: /**
019: * Storage for the various connection parameters
020: */
021: protected Vector connectionParameters;
022:
023: /**
024: * Constructs a new ConnectionManager, called by getInstance
025: * @param aurl the database connection string
026: * @param auser the database user
027: * @param apassword the database password
028: * @param size the initial pool size
029: */
030: protected NamedConnectionManager(String aDriver, String aurl,
031: String auser, String apassword, int size) {
032: super (aDriver, aurl, auser, apassword, size);
033:
034: connectionParameters = new Vector();
035: addConnection("default", aDriver, aurl, auser, apassword);
036: }
037:
038: /**
039: * Get a new object from the pool. A lease is granted to the new object
040: * @param connParamName the name of the connection parameters
041: * @return a new PoolObject
042: * @throws Exception
043: */
044: public PoolObject getObject(String connParamName) throws Exception {
045: NamedConnectionObject c;
046:
047: String connectionName = "default";
048: if (connParamName != null)
049: connectionName = connParamName;
050:
051: for (int i = 0; i < objects.size(); i++) {
052: c = (NamedConnectionObject) objects.elementAt(i);
053: if (c.lease()
054: && (c.getName().compareTo(connectionName) == 0))
055: return c;
056: }
057:
058: c = (NamedConnectionObject) getNewObject(connectionName);
059: c.lease();
060: objects.addElement(c);
061:
062: return c;
063: }
064:
065: /**
066: * Creates a new ConnectionObject, loading the JDBC driver in the process if necessary
067: * @return
068: * @throws SQLException
069: */
070: public PoolObject getNewObject(String connParamName)
071: throws SQLException {
072: ConnectionParameters connectionParams = (ConnectionParameters) findConnectionParams(connParamName);
073: try {
074: Class.forName(connectionParams.driverName);
075: } catch (ClassNotFoundException e) {
076: e.printStackTrace();
077: }
078:
079: Connection conn = DriverManager.getConnection(
080: connectionParams.url, connectionParams.user,
081: connectionParams.password);
082: return new NamedConnectionObject(connectionParams.name, conn,
083: this );
084: }
085:
086: /**
087: * Gets an instance of the ConnectionManager
088: * @return
089: */
090: public static ConnectionManager getInstance() {
091: if (connMgr != null)
092: return connMgr;
093:
094: return connMgr = new NamedConnectionManager(defaultDriverName,
095: defaultDatabaseUrl, "sa", "", 10);
096: }
097:
098: /**
099: * Gets a connection.
100: * @param connectionName the name of the connection object
101: * @return the ConnectionObject
102: * @throws SQLException
103: */
104: public ConnectionObject getConnection(String connectionName)
105: throws SQLException {
106: try {
107: return (ConnectionObject) ConnectionManager.getInstance()
108: .getObject(connectionName);
109: } catch (Exception ex) {
110: throw new SQLException(ex.getMessage());
111: }
112: }
113:
114: /**
115: * Gets an instance of the ConnectionManager. Any open connections are closed
116: * prior to attempting the new connection.
117: * @param name the name by which the connection paramenters will be referenced
118: * @param driver the driver class name
119: * @param url the database URL
120: * @param userName the user name
121: * @param password the password
122: */
123: public void addConnection(String name, String driver, String url,
124: String userName, String password) {
125: // Check for the existance of a connection
126: ConnectionParameters connectionParams = (ConnectionParameters) findConnectionParams(name);
127: if (connectionParams != null)
128: return;
129:
130: try {
131: // Try to load the driver.
132: Class.forName(driver);
133:
134: // Attemp to add the connection details
135: connectionParams = new ConnectionParameters();
136: connectionParams.name = name;
137: connectionParams.driverName = driver;
138: connectionParams.url = url;
139: connectionParams.user = userName;
140: connectionParams.password = password;
141: connectionParameters.addElement(connectionParams);
142: } catch (ClassNotFoundException e) {
143: e.printStackTrace();
144: }
145: }
146:
147: /**
148: * Find a nameds set of connection parameters
149: * @param connParamName the name of the set of connection parameters
150: * @return the set or null if nothing is found
151: */
152: protected Object findConnectionParams(String connParamName) {
153: int numElements = connectionParameters.size();
154: for (int i = 0; i < numElements; i++) {
155: // Find the named element
156: if (((ConnectionParameters) connectionParameters
157: .elementAt(i)).name.compareTo(connParamName) == 0)
158: return connectionParameters.elementAt(i);
159: }
160:
161: return null;
162: }
163:
164: /**
165: * Gets an instance of the ConnectionManager. Any open connections are closed
166: * prior to attempting the new connection.
167: * @return
168: */
169: public ConnectionManager reset(String name, String driver,
170: String url, String userName, String password) {
171: if (connMgr != null)
172: connMgr.closeObjects();
173:
174: defaultDriverName = driver;
175:
176: try {
177: // Try to load the driver.
178: Class.forName(defaultDriverName);
179:
180: // Attempt to make the connection
181: return connMgr = new NamedConnectionManager(driver, url,
182: userName, password, 10);
183: } catch (ClassNotFoundException e) {
184: e.printStackTrace();
185: }
186:
187: return null;
188: }
189: }
190:
191: /**
192: * An internal class for storing the connection parameters
193: */
194: class ConnectionParameters {
195: public String name;
196: public String url;
197: public String user;
198: public String password;
199: public String driverName;
200: public String databaseUrl;
201: }
|