KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > BasicDataSource


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.logicalcobwebs.logging.Log;
9 import org.logicalcobwebs.logging.LogFactory;
10
11 import javax.sql.DataSource JavaDoc;
12 import javax.naming.NamingException JavaDoc;
13 import javax.naming.Reference JavaDoc;
14 import javax.naming.Context JavaDoc;
15 import javax.naming.Name JavaDoc;
16 import javax.naming.StringRefAddr JavaDoc;
17 import javax.naming.BinaryRefAddr JavaDoc;
18 import javax.naming.InitialContext JavaDoc;
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.ObjectOutputStream JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * Basic implementation of DataSource
31  * @version $Revision: 1.3 $, $Date: 2003/08/30 14:54:23 $
32  * @author bill
33  * @author $Author: billhorsman $ (current maintainer)
34  * @since Proxool 0.8
35  */

36 public class BasicDataSource implements DataSource JavaDoc {
37
38     private static final Log LOG = LogFactory.getLog(BasicDataSource.class);
39
40     private Properties JavaDoc jndiEnvironment;
41
42     // JNDI properties
43

44     private Context JavaDoc context;
45
46     private String JavaDoc contextFactory;
47
48     private String JavaDoc providerUrl;
49
50     private String JavaDoc securityAuthentication;
51
52     private String JavaDoc securityPrincipal;
53
54     private String JavaDoc securityCredentials;
55
56     // Main proxool properties
57

58     private String JavaDoc alias;
59
60     private String JavaDoc url;
61
62     private String JavaDoc user;
63
64     private String JavaDoc password;
65
66     private String JavaDoc driver;
67
68     private int maximumConnectionLifetime;;
69
70     private int prototypeCount;
71
72     private int minimumConnectionCount;
73
74     private int maximumConnectionCount;
75
76     private int houseKeepingSleepTime;
77
78     private int simultaneousBuildThrottle;
79
80     private int recentlyStartedThreshold;
81
82     private int overloadWithoutRefusalLifetime;
83
84     private int maximumActiveTime;
85
86     private boolean verbose;
87
88     private boolean trace;
89
90     private String JavaDoc statistics;
91
92     private String JavaDoc statisticsLogLevel;
93
94     /**
95      * A String of all the fatalSqlExceptions delimited by
96      * {@link org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#FATAL_SQL_EXCEPTIONS_DELIMITER}
97      */

98     private String JavaDoc fatalSqlExceptionsAsString;
99
100     private String JavaDoc houseKeepingTestSql;
101
102     public BasicDataSource() {
103         reset();
104     }
105
106     /**
107      * @see javax.sql.DataSource#getConnection()
108      */

109     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
110
111         ConnectionPool cp = null;
112         try {
113             if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) {
114                 registerPool();
115             }
116             cp = ConnectionPoolManager.getInstance().getConnectionPool(alias);
117             return cp.getConnection();
118
119         } catch (ProxoolException e) {
120             LOG.error("Problem getting connection", e);
121             throw new SQLException JavaDoc(e.toString());
122         } catch (NamingException JavaDoc e) {
123             LOG.error("JNDI Problem whilst getting connection", e);
124             throw new SQLException JavaDoc(e.toString());
125         }
126     }
127
128     /**
129      * Register a pool using the properties of this data source. (Check that it
130      * exists first)
131      * @throws org.logicalcobwebs.proxool.ProxoolException if the pool couldn't be registered
132      */

133     private synchronized void registerPool() throws ProxoolException, NamingException JavaDoc {
134         if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) {
135             ConnectionPoolDefinition cpd = new ConnectionPoolDefinition();
136             cpd.setAlias(getAlias());
137             cpd.setDriver(getDriver());
138             cpd.setFatalSqlExceptionsAsString(getFatalSqlExceptionsAsString());
139             cpd.setHouseKeepingSleepTime(getHouseKeepingSleepTime());
140             cpd.setHouseKeepingTestSql(getHouseKeepingTestSql());
141             cpd.setMaximumActiveTime(getMaximumActiveTime());
142             cpd.setMaximumConnectionCount(getMaximumConnectionCount());
143             cpd.setMaximumConnectionLifetime(getMaximumConnectionLifetime());
144             cpd.setMinimumConnectionCount(getMinimumConnectionCount());
145             cpd.setOverloadWithoutRefusalLifetime(getOverloadWithoutRefusalLifetime());
146             cpd.setPassword(getPassword());
147             cpd.setPrototypeCount(getPrototypeCount());
148             cpd.setRecentlyStartedThreshold(getRecentlyStartedThreshold());
149             cpd.setSimultaneousBuildThrottle(getSimultaneousBuildThrottle());
150             cpd.setStatistics(getStatistics());
151             cpd.setStatisticsLogLevel(getStatisticsLogLevel());
152             cpd.setTrace(isTrace());
153             cpd.setUrl(getUrl());
154             cpd.setUser(getUser());
155             cpd.setVerbose(isVerbose());
156             ProxoolFacade.registerConnectionPool(cpd);
157
158         }
159
160         Hashtable JavaDoc env = new Hashtable JavaDoc();
161         env.put(Context.INITIAL_CONTEXT_FACTORY, getContextFactory());
162         env.put(Context.PROVIDER_URL, getProviderUrl());
163         env.put(Context.SECURITY_AUTHENTICATION, getSecurityAuthentication());
164         env.put(Context.SECURITY_PRINCIPAL, getSecurityPrincipal());
165         env.put(Context.SECURITY_CREDENTIALS, getSecurityCredentials());
166         context = new InitialContext JavaDoc(env);
167         context.bind("java:/comp/env/jdbc/proxool." + getAlias(), this);
168
169     }
170
171     public String JavaDoc getContextFactory() {
172         return contextFactory;
173     }
174
175     public void setContextFactory(String JavaDoc contextFactory) {
176         this.contextFactory = contextFactory;
177     }
178
179     public String JavaDoc getProviderUrl() {
180         return providerUrl;
181     }
182
183     public void setProviderUrl(String JavaDoc providerUrl) {
184         this.providerUrl = providerUrl;
185     }
186
187     public String JavaDoc getSecurityAuthentication() {
188         return securityAuthentication;
189     }
190
191     public void setSecurityAuthentication(String JavaDoc securityAuthentication) {
192         this.securityAuthentication = securityAuthentication;
193     }
194
195     public String JavaDoc getSecurityPrincipal() {
196         return securityPrincipal;
197     }
198
199     public void setSecurityPrincipal(String JavaDoc securityPrincipal) {
200         this.securityPrincipal = securityPrincipal;
201     }
202
203     public String JavaDoc getSecurityCredentials() {
204         return securityCredentials;
205     }
206
207     public void setSecurityCredentials(String JavaDoc securityCredentials) {
208         this.securityCredentials = securityCredentials;
209     }
210
211     /**
212      * Use {@link #getConnection()} instead
213      * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
214      */

215     public Connection JavaDoc getConnection(String JavaDoc username, String JavaDoc password)
216             throws SQLException JavaDoc {
217         throw new UnsupportedOperationException JavaDoc("You should configure the username and password "
218                 + "within the proxool configuration and just call getConnection() instead.");
219     }
220
221     /**
222      * Unsupported operation
223      * @see javax.sql.DataSource#getLoginTimeout
224      */

225     public int getLoginTimeout() {
226         throw new UnsupportedOperationException JavaDoc("login timeout is not supported");
227     }
228
229     /**
230      * Unsupported operation
231      * @see javax.sql.DataSource#setLoginTimeout
232      */

233     public void setLoginTimeout(int loginTimeout) {
234         throw new UnsupportedOperationException JavaDoc("login timeout is not supported");
235     }
236
237     /**
238      * Unsupported operation
239      * @see javax.sql.DataSource#getLogWriter
240      */

241     public PrintWriter JavaDoc getLogWriter() {
242         throw new UnsupportedOperationException JavaDoc("Proxool uses Jakarta's Commons' Logging API");
243     }
244
245     /**
246      * Unsupported operation
247      * @see javax.sql.DataSource#setLogWriter
248      */

249     public void setLogWriter(PrintWriter JavaDoc logWriter) {
250         throw new UnsupportedOperationException JavaDoc("Proxool uses Jakarta's Commons' Logging API");
251     }
252
253     /**
254      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getAlias
255      */

256     public String JavaDoc getAlias() {
257         return alias;
258     }
259
260     /**
261      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getAlias
262      */

263     public void setAlias(String JavaDoc alias) {
264         this.alias = alias;
265     }
266
267     /**
268      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getUrl
269      */

270     public String JavaDoc getUrl() {
271         return url;
272     }
273
274     /**
275      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getUrl
276      */

277     public void setUrl(String JavaDoc url) {
278         this.url = url;
279     }
280
281     /**
282      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getDriver
283      */

284     public String JavaDoc getDriver() {
285         return driver;
286     }
287
288     /**
289      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getDriver
290      */

291     public void setDriver(String JavaDoc driver) {
292         this.driver = driver;
293     }
294
295     /**
296      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMaximumConnectionLifetime
297      */

298     public int getMaximumConnectionLifetime() {
299         return maximumConnectionLifetime;
300     }
301
302     /**
303      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMaximumConnectionLifetime
304      */

305     public void setMaximumConnectionLifetime(int maximumConnectionLifetime) {
306         this.maximumConnectionLifetime = maximumConnectionLifetime;
307     }
308
309     /**
310      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getPrototypeCount
311      */

312     public int getPrototypeCount() {
313         return prototypeCount;
314     }
315
316     /**
317      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getPrototypeCount
318      */

319     public void setPrototypeCount(int prototypeCount) {
320         this.prototypeCount = prototypeCount;
321     }
322
323     /**
324      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMinimumConnectionCount
325      */

326     public int getMinimumConnectionCount() {
327         return minimumConnectionCount;
328     }
329
330     /**
331      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMinimumConnectionCount
332      */

333     public void setMinimumConnectionCount(int minimumConnectionCount) {
334         this.minimumConnectionCount = minimumConnectionCount;
335     }
336
337     /**
338      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMaximumConnectionCount
339      */

340     public int getMaximumConnectionCount() {
341         return maximumConnectionCount;
342     }
343
344     /**
345      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMaximumConnectionCount
346      */

347     public void setMaximumConnectionCount(int maximumConnectionCount) {
348         this.maximumConnectionCount = maximumConnectionCount;
349     }
350
351     /**
352      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getHouseKeepingSleepTime
353      */

354     public int getHouseKeepingSleepTime() {
355         return houseKeepingSleepTime;
356     }
357
358     /**
359      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getHouseKeepingSleepTime
360      */

361     public void setHouseKeepingSleepTime(int houseKeepingSleepTime) {
362         this.houseKeepingSleepTime = houseKeepingSleepTime;
363     }
364
365     /**
366      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getSimultaneousBuildThrottle
367      */

368     public int getSimultaneousBuildThrottle() {
369         return simultaneousBuildThrottle;
370     }
371
372     /**
373      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getSimultaneousBuildThrottle
374      */

375     public void setSimultaneousBuildThrottle(int simultaneousBuildThrottle) {
376         this.simultaneousBuildThrottle = simultaneousBuildThrottle;
377     }
378
379     /**
380      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getRecentlyStartedThreshold
381      */

382     public int getRecentlyStartedThreshold() {
383         return recentlyStartedThreshold;
384     }
385
386     /**
387      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getRecentlyStartedThreshold
388      */

389     public void setRecentlyStartedThreshold(int recentlyStartedThreshold) {
390         this.recentlyStartedThreshold = recentlyStartedThreshold;
391     }
392
393     /**
394      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getOverloadWithoutRefusalLifetime
395      */

396     public int getOverloadWithoutRefusalLifetime() {
397         return overloadWithoutRefusalLifetime;
398     }
399
400     /**
401      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getOverloadWithoutRefusalLifetime
402      */

403     public void setOverloadWithoutRefusalLifetime(int overloadWithoutRefusalLifetime) {
404         this.overloadWithoutRefusalLifetime = overloadWithoutRefusalLifetime;
405     }
406
407     /**
408      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMaximumActiveTime
409      */

410     public int getMaximumActiveTime() {
411         return maximumActiveTime;
412     }
413
414     /**
415      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getMaximumActiveTime
416      */

417     public void setMaximumActiveTime(int maximumActiveTime) {
418         this.maximumActiveTime = maximumActiveTime;
419     }
420
421     /**
422      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#isVerbose
423      */

424     public boolean isVerbose() {
425         return verbose;
426     }
427
428     /**
429      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#isVerbose
430      */

431     public void setVerbose(boolean verbose) {
432         this.verbose = verbose;
433     }
434
435     /**
436      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#isTrace
437      */

438     public boolean isTrace() {
439         return trace;
440     }
441
442     /**
443      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#isTrace
444      */

445     public void setTrace(boolean trace) {
446         this.trace = trace;
447     }
448
449     /**
450      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getStatistics
451      */

452     public String JavaDoc getStatistics() {
453         return statistics;
454     }
455
456     /**
457      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getStatistics
458      */

459     public void setStatistics(String JavaDoc statistics) {
460         this.statistics = statistics;
461     }
462
463     /**
464      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getStatisticsLogLevel
465      */

466     public String JavaDoc getStatisticsLogLevel() {
467         return statisticsLogLevel;
468     }
469
470     /**
471      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getStatisticsLogLevel
472      */

473     public void setStatisticsLogLevel(String JavaDoc statisticsLogLevel) {
474         this.statisticsLogLevel = statisticsLogLevel;
475     }
476
477     /**
478      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getFatalSqlExceptions
479      */

480     public String JavaDoc getFatalSqlExceptionsAsString() {
481         return fatalSqlExceptionsAsString;
482     }
483
484     /**
485      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getFatalSqlExceptions
486      */

487     public void setFatalSqlExceptionsAsString(String JavaDoc fatalSqlExceptionsAsString) {
488         this.fatalSqlExceptionsAsString = fatalSqlExceptionsAsString;
489     }
490
491     /**
492      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getHouseKeepingTestSql
493      */

494     public String JavaDoc getHouseKeepingTestSql() {
495         return houseKeepingTestSql;
496     }
497
498     /**
499      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getHouseKeepingTestSql
500      */

501     public void setHouseKeepingTestSql(String JavaDoc houseKeepingTestSql) {
502         this.houseKeepingTestSql = houseKeepingTestSql;
503     }
504
505     /**
506      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getUser
507      */

508     public String JavaDoc getUser() {
509         return user;
510     }
511
512     /**
513      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getUser
514      */

515     public void setUser(String JavaDoc user) {
516         this.user = user;
517     }
518
519     /**
520      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getPassword
521      */

522     public String JavaDoc getPassword() {
523         return password;
524     }
525
526     /**
527      * @see org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getPassword
528      */

529     public void setPassword(String JavaDoc password) {
530         this.password = password;
531     }
532
533     /**
534      * Reset all properties to their default values
535      */

536     private void reset() {
537         url = null;
538         driver = null;
539         maximumConnectionLifetime = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_CONNECTION_LIFETIME;
540         prototypeCount = ConnectionPoolDefinitionIF.DEFAULT_PROTOTYPE_COUNT;
541         minimumConnectionCount = ConnectionPoolDefinitionIF.DEFAULT_MINIMUM_CONNECTION_COUNT;
542         maximumConnectionCount = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_CONNECTION_COUNT;
543         houseKeepingSleepTime = ConnectionPoolDefinitionIF.DEFAULT_HOUSE_KEEPING_SLEEP_TIME;
544         houseKeepingTestSql = null;
545         simultaneousBuildThrottle = ConnectionPoolDefinitionIF.DEFAULT_SIMULTANEOUS_BUILD_THROTTLE;
546         ;
547         recentlyStartedThreshold = ConnectionPoolDefinitionIF.DEFAULT_RECENTLY_STARTED_THRESHOLD;
548         overloadWithoutRefusalLifetime = ConnectionPoolDefinitionIF.DEFAULT_OVERLOAD_WITHOUT_REFUSAL_THRESHOLD;
549         maximumActiveTime = ConnectionPoolDefinitionIF.DEFAULT_MAXIMUM_ACTIVE_TIME;
550         verbose = false;
551         trace = false;
552         statistics = null;
553         statisticsLogLevel = null;
554     }
555
556     /**
557      * <CODE>Referenceable</CODE> implementation prepares object for
558      * binding in jndi.
559      */

560     public Reference JavaDoc getReference()
561             throws NamingException JavaDoc {
562         // this class implements its own factory
563
String JavaDoc factory = getClass().getName();
564         Reference JavaDoc ref = new Reference JavaDoc(getClass().getName(), factory, null);
565
566         ref.add(new StringRefAddr JavaDoc("url", getUrl()));
567
568         byte[] ser = null;
569         // BinaryRefAddr does not allow null byte[].
570
if (jndiEnvironment != null) {
571             try {
572                 ser = serialize(jndiEnvironment);
573                 ref.add(new BinaryRefAddr JavaDoc("jndiEnvironment", ser));
574             } catch (IOException JavaDoc ioe) {
575                 throw new NamingException JavaDoc("An IOException prevented "
576                    + "serializing the jndiEnvironment properties.");
577             }
578         }
579
580         // TODO
581
return null;
582     }
583
584     /**
585      * implements ObjectFactory to create an instance of this class
586      */

587     public Object JavaDoc getObjectInstance(Object JavaDoc refObj, Name JavaDoc name,
588             Context JavaDoc context, Hashtable JavaDoc env)
589             throws Exception JavaDoc {
590         // TODO
591
return null;
592     }
593
594     /**
595      * Get the value of jndiEnvironment which is used when instantiating
596      * a jndi InitialContext. This InitialContext is used to locate the
597      * backend ConnectionPoolDataSource.
598      *
599      * @return value of jndiEnvironment.
600      */

601     public String JavaDoc getJndiEnvironment(String JavaDoc key) {
602         String JavaDoc value = null;
603         if (jndiEnvironment != null) {
604             value = jndiEnvironment.getProperty(key);
605         }
606         return value;
607     }
608
609     /**
610      * Set the value of jndiEnvironment which is used when instantiating
611      * a jndi InitialContext. This InitialContext is used to locate the
612      * backend ConnectionPoolDataSource.
613      *
614      * @param key property key
615      * @param value to assign to jndiEnvironment.
616      */

617     public void setJndiEnvironment(String JavaDoc key, String JavaDoc value) {
618         if (jndiEnvironment == null) {
619             jndiEnvironment = new Properties JavaDoc();
620         }
621         jndiEnvironment.setProperty(key, value);
622     }
623
624     /**
625      * Converts a object to a byte array for storage/serialization.
626      *
627      * @param obj The Serializable to convert.
628      * @return A byte[] with the converted Serializable.
629      * @exception java.io.IOException if conversion to a byte[] fails.
630      */

631     private static byte[] serialize(Serializable JavaDoc obj)
632             throws IOException JavaDoc {
633         byte[] byteArray = null;
634         ByteArrayOutputStream JavaDoc baos = null;
635         ObjectOutputStream JavaDoc out = null;
636         try {
637             // These objects are closed in the finally.
638
baos = new ByteArrayOutputStream JavaDoc();
639             out = new ObjectOutputStream JavaDoc(baos);
640
641             out.writeObject(obj);
642             byteArray = baos.toByteArray();
643         } finally {
644             if (out != null) {
645                 out.close();
646             }
647         }
648         return byteArray;
649     }
650
651 }
652
653 /*
654  Revision history:
655  $Log:
656  */
Popular Tags