001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz.utils;
022:
023: import java.sql.Connection;
024: import java.sql.SQLException;
025: import java.util.HashMap;
026:
027: /**
028: * <p>
029: * Manages a collection of ConnectionProviders, and provides transparent access
030: * to their connections.
031: * </p>
032: *
033: * @see ConnectionProvider
034: * @see PoolingConnectionProvider
035: * @see JNDIConnectionProvider
036: * @see org.quartz.utils.weblogic.WeblogicConnectionProvider
037: *
038: * @author James House
039: * @author Sharada Jambula
040: * @author Mohammad Rezaei
041: */
042: public class DBConnectionManager {
043:
044: /*
045: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
046: *
047: * Constants.
048: *
049: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
050: */
051:
052: public static final String DB_PROPS_PREFIX = "org.quartz.db.";
053:
054: /*
055: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
056: *
057: * Data members.
058: *
059: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
060: */
061:
062: private static DBConnectionManager instance = new DBConnectionManager();
063:
064: private HashMap providers = new HashMap();
065:
066: /*
067: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
068: *
069: * Constructors.
070: *
071: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
072: */
073:
074: /**
075: * <p>
076: * Private constructor
077: * </p>
078: *
079: */
080: private DBConnectionManager() {
081: }
082:
083: /*
084: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
085: *
086: * Interface.
087: *
088: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
089: */
090:
091: public void addConnectionProvider(String dataSourceName,
092: ConnectionProvider provider) {
093: this .providers.put(dataSourceName, provider);
094: }
095:
096: /**
097: * Get a database connection from the DataSource with the given name.
098: *
099: * @return a database connection
100: * @exception SQLException
101: * if an error occurs, or there is no DataSource with the
102: * given name.
103: */
104: public Connection getConnection(String dsName) throws SQLException {
105: ConnectionProvider provider = (ConnectionProvider) providers
106: .get(dsName);
107: if (provider == null) {
108: throw new SQLException("There is no DataSource named '"
109: + dsName + "'");
110: }
111:
112: return provider.getConnection();
113: }
114:
115: /**
116: * Get the class instance.
117: *
118: * @return an instance of this class
119: */
120: public static DBConnectionManager getInstance() {
121: // since the instance variable is initialized at class loading time,
122: // it's not necessary to synchronize this method */
123: return instance;
124: }
125:
126: /**
127: * Shuts down database connections from the DataSource with the given name,
128: * if applicable for the underlying provider.
129: *
130: * @exception SQLException
131: * if an error occurs, or there is no DataSource with the
132: * given name.
133: */
134: public void shutdown(String dsName) throws SQLException {
135:
136: ConnectionProvider provider = (ConnectionProvider) providers
137: .get(dsName);
138: if (provider == null) {
139: throw new SQLException("There is no DataSource named '"
140: + dsName + "'");
141: }
142:
143: provider.shutdown();
144:
145: }
146: }
|