001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JDBCPoolComponent.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.jdbcpool;
025:
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028:
029: import org.ow2.easybeans.component.api.EZBComponent;
030: import org.ow2.easybeans.component.api.EZBComponentException;
031: import org.ow2.easybeans.transaction.JTransactionManager;
032: import org.ow2.util.log.Log;
033: import org.ow2.util.log.LogFactory;
034:
035: /**
036: * Defines a component that creates a JDBC pool in order to use it in EasyBeans.
037: * @author Florent Benoit
038: */
039: public class JDBCPoolComponent implements EZBComponent {
040:
041: /**
042: * Logger.
043: */
044: private static Log logger = LogFactory
045: .getLog(JDBCPoolComponent.class);
046:
047: /**
048: * Default username.
049: */
050: private static final String DEFAULT_USER = "";
051:
052: /**
053: * Default password.
054: */
055: private static final String DEFAULT_PASSWORD = "";
056:
057: /**
058: * Default min pool.
059: */
060: private static final int DEFAULT_MIN_POOL = 10;
061:
062: /**
063: * Default max pool.
064: */
065: private static final int DEFAULT_MAX_POOL = 30;
066:
067: /**
068: * Default prepared statement.
069: */
070: private static final int DEFAULT_PSTMT = 10;
071:
072: /**
073: * Default prepared statement.
074: */
075: private static final int DEFAULT_CHECK_LEVEL = 0;
076:
077: /**
078: * Level of checking on connections when got from the pool. this avoids
079: * reusing bad connections because too old, for example when database was
080: * restarted... 0 = no checking 1 = check that still physically opened. 2 =
081: * try a null statement.
082: */
083: private int checkLevel = DEFAULT_CHECK_LEVEL;
084:
085: /**
086: * Connection manager object.
087: */
088: private ConnectionManager connectionManager = null;
089:
090: /**
091: * JNDI name.
092: */
093: private String jndiName = null;
094:
095: /**
096: * Username.
097: */
098: private String username = DEFAULT_USER;
099:
100: /**
101: * Password.
102: */
103: private String password = DEFAULT_PASSWORD;
104:
105: /**
106: * URL for accessing to the database.
107: */
108: private String url = null;
109:
110: /**
111: * Name of the driver class to use.
112: */
113: private String driver = null;
114:
115: /**
116: * Use transaction or not ?
117: */
118: private boolean useTM = true;
119:
120: /**
121: * Pool min.
122: */
123: private int poolMin = DEFAULT_MIN_POOL;
124:
125: /**
126: * Pool max.
127: */
128: private int poolMax = DEFAULT_MAX_POOL;
129:
130: /**
131: * Max of prepared statement.
132: */
133: private int pstmtMax = DEFAULT_PSTMT;
134:
135: /**
136: * Default constructor.
137: */
138: public JDBCPoolComponent() {
139: connectionManager = new ConnectionManager();
140: connectionManager.setTransactionIsolation("default");
141: }
142:
143: /**
144: * Init method.<br/> This method is called before the start method.
145: * @throws EZBComponentException if the initialization has failed.
146: */
147: public void init() throws EZBComponentException {
148: // Check that data are correct
149: validate();
150:
151: connectionManager.setDatasourceName(jndiName);
152: connectionManager.setDSName(jndiName);
153: connectionManager.setUrl(url);
154: try {
155: connectionManager.setClassName(driver);
156: } catch (ClassNotFoundException e) {
157: throw new IllegalStateException("Cannot load jdbc driver '"
158: + driver + "'.", e);
159: }
160: connectionManager.setUserName(username);
161: connectionManager.setPassword(password);
162: connectionManager.setTransactionIsolation("default");
163: connectionManager.setPstmtMax(pstmtMax);
164: connectionManager.setCheckLevel(checkLevel);
165:
166: }
167:
168: /**
169: * Validate current data.
170: * @throws EZBComponentException if validation fails.
171: */
172: private void validate() throws EZBComponentException {
173: // check that there is a JNDI name
174: if (jndiName == null) {
175: throw new EZBComponentException("No JNDI name set");
176: }
177:
178: // check that there is an URL
179: if (url == null) {
180: throw new EZBComponentException("No URL set");
181: }
182:
183: // Check that there is a driver classname
184: if (driver == null) {
185: throw new EZBComponentException("No driver set");
186: }
187: }
188:
189: /**
190: * Start method.<br/> This method is called after the init method.
191: * @throws EZBComponentException if the start has failed.
192: */
193: public void start() throws EZBComponentException {
194: // set settings
195: if (useTM) {
196: connectionManager.setTm(JTransactionManager
197: .getTransactionManager());
198: }
199: connectionManager.setPoolMin(poolMin);
200: connectionManager.setPoolMax(poolMax);
201:
202: // Something is there ?
203: try {
204: Object o = new InitialContext().lookup(jndiName);
205: if (o != null) {
206: logger.warn("Entry with JNDI name {0} already exist",
207: jndiName);
208: }
209: } catch (NamingException e) {
210: logger.debug("Nothing with JNDI name {0}", jndiName);
211: }
212:
213: // Bind the resource.
214: try {
215: new InitialContext().rebind(jndiName, connectionManager);
216: } catch (NamingException e) {
217: throw new EZBComponentException(
218: "Cannot bind a JDBC Datasource with the jndi name '"
219: + jndiName + "'.");
220: }
221:
222: logger.info("DS ''{0}'', URL ''{1}'', Driver = ''{2}''.",
223: jndiName, url, driver);
224: }
225:
226: /**
227: * Stop method.<br/> This method is called when component needs to be
228: * stopped.
229: * @throws EZBComponentException if the stop is failing.
230: */
231: public void stop() throws EZBComponentException {
232: // Unbind the resource.
233: try {
234: new InitialContext().unbind(jndiName);
235: } catch (NamingException e) {
236: throw new EZBComponentException(
237: "Cannot unbind a JDBC Datasource with the jndi name '"
238: + jndiName + "'.");
239: }
240: }
241:
242: /**
243: * Sets the name of the JDBC driver.
244: * @param driver the driver's name
245: */
246: public void setDriver(final String driver) {
247: this .driver = driver;
248: }
249:
250: /**
251: * @return the name of the JDBC driver.
252: */
253: public String getDriver() {
254: return this .driver;
255: }
256:
257: /**
258: * Sets the JNDI name.
259: * @param jndiName the name to bind the datasource
260: */
261: public void setJndiName(final String jndiName) {
262: this .jndiName = jndiName;
263: }
264:
265: /**
266: * @return the name that is bound in the datasource
267: */
268: public String getJndiName() {
269: return this .jndiName;
270: }
271:
272: /**
273: * Sets the password to use.
274: * @param password the password for the url connection.
275: */
276: public void setPassword(final String password) {
277: this .password = password;
278: }
279:
280: /**
281: * The maximum size of the JDBC pool.
282: * @param poolMax the value of the pool's max.
283: */
284: public void setPoolMax(final int poolMax) {
285: this .poolMax = poolMax;
286: }
287:
288: /**
289: * @return the maximum size of the JDBC pool.
290: */
291: public int getPoolMax() {
292: return this .poolMax;
293: }
294:
295: /**
296: * The minimum size of the JDBC pool.
297: * @param poolMin the value of the pool's min.
298: */
299: public void setPoolMin(final int poolMin) {
300: this .poolMin = poolMin;
301: }
302:
303: /**
304: * @return the minimum size of the JDBC pool.
305: */
306: public int getPoolMin() {
307: return this .poolMax;
308: }
309:
310: /**
311: * Set the max cache of prepared statement.
312: * @param pstmtMax the max value for prepare statement.
313: */
314: public void setPstmtMax(final int pstmtMax) {
315: this .pstmtMax = pstmtMax;
316: }
317:
318: /**
319: * @return the minimum size of the JDBC pool.
320: */
321: public int getPstmtMax() {
322: return this .poolMax;
323: }
324:
325: /**
326: * Sets the connection's URL.
327: * @param url the URL used for the connection.
328: */
329: public void setUrl(final String url) {
330: this .url = url;
331: }
332:
333: /**
334: * @return the URL used for the connection.
335: */
336: public String getUrl() {
337: return this .url;
338: }
339:
340: /**
341: * Sets the username that is used to get a connection.
342: * @param username the name of the user.
343: */
344: public void setUsername(final String username) {
345: this .username = username;
346: }
347:
348: /**
349: * @return the username that is used to get a connection.
350: */
351: public String getUsername() {
352: return this .username;
353: }
354:
355: /**
356: * Is that the pool will use transaction or not.
357: * @param useTM the true/false value.
358: */
359: public void setUseTM(final boolean useTM) {
360: this .useTM = useTM;
361: }
362:
363: /**
364: * @return true if this pool will use transaction (else false).
365: */
366: public boolean isUseTM() {
367: return this .useTM;
368: }
369:
370: /**
371: * @return connection checking level
372: */
373: public int getCheckLevel() {
374: return this .checkLevel;
375: }
376:
377: /**
378: * Sets the JDBC check level.
379: * @param checkLevel jdbc connection checking level.
380: */
381: public void setCheckLevel(final int checkLevel) {
382: this.checkLevel = checkLevel;
383: }
384:
385: }
|