KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > utils > DBConnectionManager


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.utils;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 /**
28  * <p>
29  * Manages a collection of ConnectionProviders, and provides transparent access
30  * to their connections.
31  * </p>
32  *
33  * @see ConnectionProvider
34  * @see PoolingConnectionProvider
35  * @see JNDIConnectionProvider
36  * @see org.quartz.utils.weblogic.WeblogicConnectionProvider
37  *
38  * @author James House
39  * @author Sharada Jambula
40  * @author Mohammad Rezaei
41  */

42 public class DBConnectionManager {
43
44     /*
45      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46      *
47      * Constants.
48      *
49      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50      */

51
52     public static final String JavaDoc DB_PROPS_PREFIX = "org.quartz.db.";
53
54     /*
55      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56      *
57      * Data members.
58      *
59      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60      */

61
62     private static DBConnectionManager instance = new DBConnectionManager();
63
64     private HashMap JavaDoc providers = new HashMap JavaDoc();
65
66     /*
67      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68      *
69      * Constructors.
70      *
71      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72      */

73
74     /**
75      * <p>
76      * Private constructor
77      * </p>
78      *
79      */

80     private DBConnectionManager() {
81     }
82
83     /*
84      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85      *
86      * Interface.
87      *
88      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89      */

90
91     public void addConnectionProvider(String JavaDoc dataSourceName,
92             ConnectionProvider provider) {
93         this.providers.put(dataSourceName, provider);
94     }
95
96     /**
97      * Get a database connection from the DataSource with the given name.
98      *
99      * @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 JavaDoc getConnection(String JavaDoc dsName) throws SQLException JavaDoc {
105         ConnectionProvider provider = (ConnectionProvider) providers
106                 .get(dsName);
107         if (provider == null) {
108             throw new SQLException JavaDoc("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 JavaDoc dsName) throws SQLException JavaDoc {
135
136         ConnectionProvider provider = (ConnectionProvider) providers
137         .get(dsName);
138         if (provider == null) {
139             throw new SQLException JavaDoc("There is no DataSource named '"
140                     + dsName + "'");
141         }
142
143         provider.shutdown();
144
145     }
146 }
147
Popular Tags