checkstyle
[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 javax.sql.DataSource;
19
20 import org.apache.commons.dbcp.ConnectionFactory;
21 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
22 import org.apache.commons.dbcp.PoolableConnectionFactory;
23 import org.apache.commons.dbcp.PoolingDataSource;
24 import org.apache.commons.pool.impl.GenericObjectPool;
25
26 public abstract class AbstractDatabase implements Database {
27     private static final int CONNECTION_POOL_SIZE = 16;
28
29     private DataSource itsDataSource;
30
31     private boolean started;
32
33     protected AbstractDatabase() {
34         started = false;
35     }
36
37     protected abstract void doStart();
38
39     protected abstract void doStop();
40
41     /**
42      * This method must be called from the start method.
43      */
44     protected final void createDataSource() {
45         GenericObjectPool connectionPool = new GenericObjectPool(null);
46         connectionPool.setMaxActive(CONNECTION_POOL_SIZE);
47         ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
48             getJdbcUrl(), getUsername(), getPassword());
49         // The following line must be kept in although it does not appear to be
50         // used, the constructor regsiters the
51         // constructed object at the connection pool.
52         PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
53             connectionFactory, connectionPool, null, null, false, true);
54         ingoredVariable(poolableConnectionFactory);
55         itsDataSource = new PoolingDataSource(connectionPool);
56     }
57     
58     private static void ingoredVariable(PoolableConnectionFactory aFactory) {
59         // Empty
60     }
61
62     // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
63
64     public final DataSource start() {
65         if (started) {
66             throw new RuntimeException("Database already started");
67         }
68         started = true;
69         doStart();
70         return getDatasource();
71     }
72
73     public final void stop() {
74         if (!started) {
75             return; // nothing to do.
76         }
77         started = false;
78         doStop();
79     }
80
81     private final DataSource getDatasource() {
82         if (!started) {
83             throw new RuntimeException("Database is not started!");
84         }
85         return itsDataSource;
86     }
87
88     protected String getProperty(String aName) {
89         String value = System.getProperty(aName);
90         if (value != null) {
91             return value;
92         }
93         value = System.getenv(aName);
94         if (value != null) {
95             return value;
96         }
97         throw new RuntimeException("This class expects the '" + aName +
98             "' property to be set");
99     }
100
101 }