2e8f8d8335e0070530bd9c042f8e699181429a97
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / AbstractDatabase.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.support.persistence;
17
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20
21 import javax.sql.DataSource;
22
23 import org.apache.commons.dbcp.ConnectionFactory;
24 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
25 import org.apache.commons.dbcp.PoolableConnectionFactory;
26 import org.apache.commons.dbcp.PoolingDataSource;
27 import org.apache.commons.pool.impl.GenericObjectPool;
28
29 public abstract class AbstractDatabase implements Database {
30
31     private static final Logger LOGGER = Logger
32         .getLogger(AbstractDatabase.class.getName());
33
34     private static final int CONNECTION_POOL_SIZE = 16;
35
36     private DataSource itsDataSource;
37
38     private GenericObjectPool connectionPool;
39
40     private boolean started;
41
42     protected AbstractDatabase() {
43         started = false;
44     }
45
46     protected abstract void doStart();
47
48     protected abstract void doStop();
49
50     /**
51      * This method must be called from the start method.
52      */
53     protected final void createDataSource() {
54         connectionPool = new GenericObjectPool(null);
55         connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
56         ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
57             getJdbcUrl(), getUsername(), getPassword());
58         // The following line must be kept in although it does not appear to be
59         // used, the constructor regsiters the
60         // constructed object at the connection pool.
61         PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
62             connectionFactory, connectionPool, null, null, false, true);
63         ingoredVariable(poolableConnectionFactory);
64         itsDataSource = new PoolingDataSource(connectionPool);
65     }
66
67     private static void ingoredVariable(PoolableConnectionFactory aFactory) {
68         // Empty
69     }
70
71     // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
72
73     public final DataSource start() {
74         if (started) {
75             throw new RuntimeException("Database already started");
76         }
77         started = true;
78         doStart();
79         return getDatasource();
80     }
81
82     public final void stop() {
83         if (!started) {
84             return; // nothing to do.
85         }
86         started = false;
87         try {
88             connectionPool.close();
89             connectionPool.close();
90         } catch (Exception e) {
91             LOGGER.log(Level.WARNING, "Could not close pool", e);
92         }
93         doStop();
94     }
95
96     private final DataSource getDatasource() {
97         if (!started) {
98             throw new RuntimeException("Database is not started!");
99         }
100         return itsDataSource;
101     }
102
103     protected String getProperty(String aName) {
104         String value = System.getProperty(aName);
105         if (value != null) {
106             return value;
107         }
108         value = System.getenv(aName);
109         if (value != null) {
110             return value;
111         }
112         throw new RuntimeException("This class expects the '" + aName +
113             "' property to be set");
114     }
115
116 }