KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > jdbcpool > JDBCPoolComponent


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JDBCPoolComponent.java 1127 2006-09-27 13:55:17Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.jdbcpool;
27
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import org.objectweb.easybeans.component.api.EZBComponent;
32 import org.objectweb.easybeans.component.api.EZBComponentException;
33 import org.objectweb.easybeans.log.JLog;
34 import org.objectweb.easybeans.log.JLogFactory;
35 import org.objectweb.easybeans.transaction.JTransactionManager;
36
37 /**
38  * Defines a component that creates a JDBC pool in order to use it in EasyBeans.
39  * @author Florent Benoit
40  */

41 public class JDBCPoolComponent implements EZBComponent {
42
43     /**
44      * Logger.
45      */

46     private static JLog logger = JLogFactory.getLog(JDBCPoolComponent.class);
47
48     /**
49      * Default username.
50      */

51     private static final String JavaDoc DEFAULT_USER = "";
52
53     /**
54      * Default password.
55      */

56     private static final String JavaDoc DEFAULT_PASSWORD = "";
57
58     /**
59      * Default min pool.
60      */

61     private static final int DEFAULT_MIN_POOL = 10;
62
63     /**
64      * Default max pool.
65      */

66     private static final int DEFAULT_MAX_POOL = 30;
67
68     /**
69      * Default prepared statement.
70      */

71     private static final int DEFAULT_PSTMT = 10;
72
73     /**
74      * Connection manager object.
75      */

76     private ConnectionManager connectionManager = null;
77
78     /**
79      * JNDI name.
80      */

81     private String JavaDoc jndiName = null;
82
83     /**
84      * Username.
85      */

86     private String JavaDoc username = DEFAULT_USER;
87
88     /**
89      * Password.
90      */

91     private String JavaDoc password = DEFAULT_PASSWORD;
92
93     /**
94      * URL for accessing to the database.
95      */

96     private String JavaDoc url = null;
97
98     /**
99      * Name of the driver class to use.
100      */

101     private String JavaDoc driver = null;
102
103     /**
104      * Use transaction or not ?
105      */

106     private boolean useTM = true;
107
108     /**
109      * Pool min.
110      */

111     private int poolMin = DEFAULT_MIN_POOL;
112
113     /**
114      * Pool max.
115      */

116     private int poolMax = DEFAULT_MAX_POOL;
117
118     /**
119      * Max of prepared statement.
120      */

121     private int pstmtMax = DEFAULT_PSTMT;
122
123     /**
124      * Default constructor.
125      */

126     public JDBCPoolComponent() {
127         connectionManager = new ConnectionManager();
128         connectionManager.setTransactionIsolation("default");
129     }
130
131     /**
132      * Init method.<br/> This method is called before the start method.
133      * @throws EZBComponentException if the initialization has failed.
134      */

135     public void init() throws EZBComponentException {
136         // Check that data are correct
137
validate();
138
139         connectionManager.setDatasourceName(jndiName);
140         connectionManager.setDSName(jndiName);
141         connectionManager.setUrl(url);
142         try {
143             connectionManager.setClassName(driver);
144         } catch (ClassNotFoundException JavaDoc e) {
145             throw new IllegalStateException JavaDoc("Cannot load jdbc driver '" + driver + "'.", e);
146         }
147         connectionManager.setUserName(username);
148         connectionManager.setPassword(password);
149         connectionManager.setTransactionIsolation("default");
150         connectionManager.setPstmtMax(pstmtMax);
151
152     }
153
154     /**
155      * Validate current data.
156      * @throws EZBComponentException if validation fails.
157      */

158     private void validate() throws EZBComponentException {
159         // check that there is a JNDI name
160
if (jndiName == null) {
161             throw new EZBComponentException("No JNDI name set");
162         }
163
164         // check that there is an URL
165
if (url == null) {
166             throw new EZBComponentException("No URL set");
167         }
168
169         // Check that there is a driver classname
170
if (driver == null) {
171             throw new EZBComponentException("No driver set");
172         }
173     }
174
175     /**
176      * Start method.<br/> This method is called after the init method.
177      * @throws EZBComponentException if the start has failed.
178      */

179     public void start() throws EZBComponentException {
180         // set settings
181
if (useTM) {
182             connectionManager.setTm(JTransactionManager.getTransactionManager());
183         }
184         connectionManager.setPoolMin(poolMin);
185         connectionManager.setPoolMax(poolMax);
186
187         // Something is there ?
188
try {
189             Object JavaDoc o = new InitialContext JavaDoc().lookup(jndiName);
190             if (o != null) {
191                 logger.warn("Entry with JNDI name {0} already exist", jndiName);
192             }
193         } catch (NamingException JavaDoc e) {
194             logger.debug("Nothing with JNDI name {0}", jndiName);
195         }
196
197         // Bind the resource.
198
try {
199             new InitialContext JavaDoc().rebind(jndiName, connectionManager);
200         } catch (NamingException JavaDoc e) {
201             throw new EZBComponentException("Cannot bind a JDBC Datasource with the jndi name '" + jndiName + "'.");
202         }
203
204         logger.info("DS ''{0}'', URL ''{1}'', Driver = ''{2}''.", jndiName, url, driver);
205     }
206
207     /**
208      * Stop method.<br/> This method is called when component needs to be
209      * stopped.
210      * @throws EZBComponentException if the stop is failing.
211      */

212     public void stop() throws EZBComponentException {
213         // Unbind the resource.
214
try {
215             new InitialContext JavaDoc().unbind(jndiName);
216         } catch (NamingException JavaDoc e) {
217             throw new EZBComponentException("Cannot unbind a JDBC Datasource with the jndi name '" + jndiName + "'.");
218         }
219     }
220
221     /**
222      * Sets the name of the JDBC driver.
223      * @param driver the driver's name
224      */

225     public void setDriver(final String JavaDoc driver) {
226         this.driver = driver;
227     }
228
229     /**
230      * Sets the JNDI name.
231      * @param jndiName the name to bind the datasource
232      */

233     public void setJndiName(final String JavaDoc jndiName) {
234         this.jndiName = jndiName;
235     }
236
237     /**
238      * Sets the password to use.
239      * @param password the password for the url connection.
240      */

241     public void setPassword(final String JavaDoc password) {
242         this.password = password;
243     }
244
245     /**
246      * The maximum size of the JDBC pool.
247      * @param poolMax the value of the pool's max.
248      */

249     public void setPoolMax(final int poolMax) {
250         this.poolMax = poolMax;
251     }
252
253     /**
254      * The minimum size of the JDBC pool.
255      * @param poolMin the value of the pool's min.
256      */

257     public void setPoolMin(final int poolMin) {
258         this.poolMin = poolMin;
259     }
260
261     /**
262      * Set the max cache of prepared statement.
263      * @param pstmtMax the max value for prepare statement.
264      */

265     public void setPstmtMax(final int pstmtMax) {
266         this.pstmtMax = pstmtMax;
267     }
268
269     /**
270      * Sets the connection's URL.
271      * @param url the URL used for the connection.
272      */

273     public void setUrl(final String JavaDoc url) {
274         this.url = url;
275     }
276
277     /**
278      * Sets the username that is used to get a connection.
279      * @param username the name of the user.
280      */

281     public void setUsername(final String JavaDoc username) {
282         this.username = username;
283     }
284
285     /**
286      * Is that the pool will use transaction or not.
287      * @param useTM the true/false value.
288      */

289     public void setUseTM(final boolean useTM) {
290         this.useTM = useTM;
291     }
292 }
293
Popular Tags