KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > database > DefaultConnectionProvider


1 /**
2  * $RCSfile: DefaultConnectionProvider.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/04/11 21:02:05 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.database;
13
14 import org.jivesoftware.util.JiveGlobals;
15 import org.jivesoftware.util.Log;
16
17 import java.io.IOException JavaDoc;
18 import java.sql.Connection JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 /**
22  * Default Jive connection provider, which uses an internal connection pool.<p>
23  *
24  * @author Jive Software
25  */

26 public class DefaultConnectionProvider implements ConnectionProvider {
27
28     private ConnectionPool connectionPool = null;
29
30     private String JavaDoc driver;
31     private String JavaDoc serverURL;
32     private String JavaDoc username;
33     private String JavaDoc password;
34     private int minConnections = 3;
35     private int maxConnections = 10;
36
37     /**
38      * Maximum time a connection can be open before it's reopened (in days)
39      */

40     private double connectionTimeout = 0.5;
41
42     /**
43      * MySQL doesn't currently support Unicode. However, a workaround is
44      * implemented in the mm.mysql JDBC driver. Setting the Jive property
45      * database.mysql.useUnicode to true will turn this feature on.
46      */

47     private boolean mysqlUseUnicode;
48
49     private Object JavaDoc initLock = new Object JavaDoc();
50
51     /**
52      * Creates a new DefaultConnectionProvider.
53      */

54     public DefaultConnectionProvider() {
55         loadProperties();
56     }
57
58     public boolean isPooled() {
59         return true;
60     }
61
62     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
63         if (connectionPool == null) {
64             // block until the init has been done
65
synchronized (initLock) {
66                 // if still null, something has gone wrong
67
if (connectionPool == null) {
68                     Log.error("Warning: DbConnectionDefaultPool.getConnection() was " +
69                             "called before the internal pool has been initialized.");
70                     return null;
71                 }
72             }
73         }
74
75         return connectionPool.getConnection();
76     }
77
78     public void start() {
79         // acquire lock so that no connections can be returned.
80
synchronized (initLock) {
81
82             try {
83                 connectionPool = new ConnectionPool(driver, serverURL, username,
84                         password, minConnections, maxConnections, connectionTimeout,
85                         mysqlUseUnicode);
86             }
87             catch (IOException JavaDoc e) {
88                 Log.error(e);
89             }
90         }
91     }
92
93     public void restart() {
94         // Kill off pool.
95
destroy();
96         // Reload properties.
97
loadProperties();
98         // Start a new pool.
99
start();
100     }
101
102     public void destroy() {
103         if (connectionPool != null) {
104             try {
105                 connectionPool.destroy();
106             }
107             catch (Exception JavaDoc e) {
108                 Log.error(e);
109             }
110         }
111         // Release reference to connectionPool
112
connectionPool = null;
113     }
114
115     public void finalize() {
116         destroy();
117     }
118
119     /**
120      * Returns the JDBC driver classname used to make database connections.
121      * For example: com.mysql.jdbc.Driver
122      *
123      * @return the JDBC driver classname.
124      */

125     public String JavaDoc getDriver() {
126         return driver;
127     }
128
129     /**
130      * Sets the JDBC driver classname used to make database connections.
131      * For example: com.mysql.jdbc.Driver
132      *
133      * @param driver the fully qualified JDBC driver name.
134      */

135     public void setDriver(String JavaDoc driver) {
136         this.driver = driver;
137         saveProperties();
138     }
139
140     /**
141      * Returns the JDBC connection URL used to make database connections.
142      *
143      * @return the JDBC connection URL.
144      */

145     public String JavaDoc getServerURL() {
146         return serverURL;
147     }
148
149     /**
150      * Sets the JDBC connection URL used to make database connections.
151      *
152      * @param serverURL the JDBC connection URL.
153      */

154     public void setServerURL(String JavaDoc serverURL) {
155         this.serverURL = serverURL;
156         saveProperties();
157     }
158
159     /**
160      * Returns the username used to connect to the database. In some cases,
161      * a username is not needed so this method will return null.
162      *
163      * @return the username used to connect to the datbase.
164      */

165     public String JavaDoc getUsername() {
166         return username;
167     }
168
169     /**
170      * Sets the username used to connect to the database. In some cases, a
171      * username is not needed so null should be passed in.
172      *
173      * @param username the username used to connect to the database.
174      */

175     public void setUsername(String JavaDoc username) {
176         this.username = username;
177         saveProperties();
178     }
179
180     /**
181      * Returns the password used to connect to the database. In some cases,
182      * a password is not needed so this method will return null.
183      *
184      * @return the password used to connect to the database.
185      */

186     public String JavaDoc getPassword() {
187         return password;
188     }
189
190     /**
191      * Sets the password used to connect to the database. In some cases, a
192      * password is not needed so null should be passed in.
193      *
194      * @param password the password used to connect to the database.
195      */

196     public void setPassword(String JavaDoc password) {
197         this.password = password;
198         saveProperties();
199     }
200
201     /**
202      * Returns the minimum number of connections that the pool will use. This
203      * should probably be at least three.
204      *
205      * @return the minimum number of connections in the pool.
206      */

207     public int getMinConnections() {
208         return minConnections;
209     }
210
211     /**
212      * Sets the minimum number of connections that the pool will use. This
213      * should probably be at least three.
214      *
215      * @param minConnections the minimum number of connections in the pool.
216      */

217     public void setMinConnections(int minConnections) {
218         this.minConnections = minConnections;
219         saveProperties();
220     }
221
222     /**
223      * Returns the maximum number of connections that the pool will use. The
224      * actual number of connections in the pool will vary between this value
225      * and the minimum based on the current load.
226      *
227      * @return the max possible number of connections in the pool.
228      */

229     public int getMaxConnections() {
230         return maxConnections;
231     }
232
233     /**
234      * Sets the maximum number of connections that the pool will use. The
235      * actual number of connections in the pool will vary between this value
236      * and the minimum based on the current load.
237      *
238      * @param maxConnections the max possible number of connections in the pool.
239      */

240     public void setMaxConnections(int maxConnections) {
241         this.maxConnections = maxConnections;
242         saveProperties();
243     }
244
245     /**
246      * Returns the amount of time between connection recycles in days. For
247      * example, a value of .5 would correspond to recycling the connections
248      * in the pool once every half day.
249      *
250      * @return the amount of time in days between connection recycles.
251      */

252     public double getConnectionTimeout() {
253         return connectionTimeout;
254     }
255
256     /**
257      * Sets the amount of time between connection recycles in days. For
258      * example, a value of .5 would correspond to recycling the connections
259      * in the pool once every half day.
260      *
261      * @param connectionTimeout the amount of time in days between connection
262      * recycles.
263      */

264     public void setConnectionTimeout(double connectionTimeout) {
265         this.connectionTimeout = connectionTimeout;
266         saveProperties();
267     }
268
269     public boolean isMysqlUseUnicode() {
270         return mysqlUseUnicode;
271     }
272
273     /**
274      * Load properties that already exist from Jive properties.
275      */

276     private void loadProperties() {
277         driver = JiveGlobals.getXMLProperty("database.defaultProvider.driver");
278         serverURL = JiveGlobals.getXMLProperty("database.defaultProvider.serverURL");
279         username = JiveGlobals.getXMLProperty("database.defaultProvider.username");
280         password = JiveGlobals.getXMLProperty("database.defaultProvider.password");
281         String JavaDoc minCons = JiveGlobals.getXMLProperty("database.defaultProvider.minConnections");
282         String JavaDoc maxCons = JiveGlobals.getXMLProperty("database.defaultProvider.maxConnections");
283         String JavaDoc conTimeout = JiveGlobals.getXMLProperty("database.defaultProvider.connectionTimeout");
284         // See if we should use Unicode under MySQL
285
mysqlUseUnicode = Boolean.valueOf(JiveGlobals.getXMLProperty("database.mysql.useUnicode")).booleanValue();
286         try {
287             if (minCons != null) {
288                 minConnections = Integer.parseInt(minCons);
289             }
290             if (maxCons != null) {
291                 maxConnections = Integer.parseInt(maxCons);
292             }
293             if (conTimeout != null) {
294                 connectionTimeout = Double.parseDouble(conTimeout);
295             }
296         }
297         catch (Exception JavaDoc e) {
298             Log.error("Error: could not parse default pool properties. " +
299                     "Make sure the values exist and are correct.", e);
300         }
301     }
302
303     /**
304      * Save properties as Jive properties.
305      */

306     private void saveProperties() {
307
308         JiveGlobals.setXMLProperty("database.defaultProvider.driver", driver);
309         JiveGlobals.setXMLProperty("database.defaultProvider.serverURL", serverURL);
310         JiveGlobals.setXMLProperty("database.defaultProvider.username", username);
311         JiveGlobals.setXMLProperty("database.defaultProvider.password", password);
312
313         JiveGlobals.setXMLProperty("database.defaultProvider.minConnections",
314                 Integer.toString(minConnections));
315         JiveGlobals.setXMLProperty("database.defaultProvider.maxConnections",
316                 Integer.toString(maxConnections));
317         JiveGlobals.setXMLProperty("database.defaultProvider.connectionTimeout",
318                 Double.toString(connectionTimeout));
319     }
320 }
321
Popular Tags