59f3d7762ce4967aed99422160d42bf682a9dda9
[utils] /
1 package org.wamblee.support.persistence;
2
3 import javax.sql.DataSource;
4
5 import org.apache.commons.dbcp.ConnectionFactory;
6 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
7 import org.apache.commons.dbcp.PoolableConnectionFactory;
8 import org.apache.commons.dbcp.PoolingDataSource;
9 import org.apache.commons.pool.impl.GenericObjectPool;
10
11 public abstract class AbstractDatabase implements Database {
12         private static final int CONNECTION_POOL_SIZE = 16;
13
14     private DataSource itsDataSource; 
15         
16         private boolean started; 
17         
18         protected AbstractDatabase() { 
19                 started = false; 
20         }
21         
22         protected abstract void doStart();
23         
24         protected abstract void doStop();
25         
26         /**
27          * This method must be called from the start method. 
28          */
29         protected final void createDataSource() { 
30                 GenericObjectPool connectionPool = new GenericObjectPool(null);
31                 connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
32                 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(getJdbcUrl(), getUsername(), getPassword());
33                 // The following line must be kept in although it does not appear to be used, the constructor regsiters the
34                 // constructed object at the connection pool. 
35                 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
36                 itsDataSource = new PoolingDataSource(connectionPool);
37         }
38         
39         
40         /// BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
41         
42         public final DataSource start() {
43                 if ( started ) { 
44                         throw new RuntimeException("Database already started"); 
45                 }
46                 started = true; 
47                 doStart();
48                 return getDatasource();
49         }
50         
51         public final void stop() { 
52                 if ( ! started ) { 
53                         return; // nothing to do. 
54                 }
55                 started = false;
56                 doStop(); 
57         }
58
59         private final DataSource getDatasource() {
60                 if ( !started) { 
61                         throw new RuntimeException("Database is not started!");
62                 }
63                 return itsDataSource;
64         }
65         
66         protected String getProperty(String aName) { 
67                 String value = System.getProperty(aName); 
68                 if ( value != null ) { 
69                         return value;
70                 }
71                 value = System.getenv(aName); 
72                 if ( value != null ) { 
73                         return value; 
74                 }
75                 throw new RuntimeException("This class expects the '" + aName + "' property to be set");
76         }
77
78 }