Removed DOCUMENT ME comments that were generated and applied source code
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / AbstractDatabase.java
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(
33             getJdbcUrl(), getUsername(), getPassword());
34         // The following line must be kept in although it does not appear to be
35         // used, the constructor regsiters the
36         // constructed object at the connection pool.
37         PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
38             connectionFactory, connectionPool, null, null, false, true);
39         itsDataSource = new PoolingDataSource(connectionPool);
40     }
41
42     // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
43
44     public final DataSource start() {
45         if (started) {
46             throw new RuntimeException("Database already started");
47         }
48         started = true;
49         doStart();
50         return getDatasource();
51     }
52
53     public final void stop() {
54         if (!started) {
55             return; // nothing to do.
56         }
57         started = false;
58         doStop();
59     }
60
61     private final DataSource getDatasource() {
62         if (!started) {
63             throw new RuntimeException("Database is not started!");
64         }
65         return itsDataSource;
66     }
67
68     protected String getProperty(String aName) {
69         String value = System.getProperty(aName);
70         if (value != null) {
71             return value;
72         }
73         value = System.getenv(aName);
74         if (value != null) {
75             return value;
76         }
77         throw new RuntimeException("This class expects the '" + aName +
78             "' property to be set");
79     }
80
81 }