copyright messages updated in all java filees.
[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         itsDataSource = new PoolingDataSource(connectionPool);
55     }
56
57     // / BELOW THIS LINE IS NOT OF INTEREST TO SUBCLASSES.
58
59     public final DataSource start() {
60         if (started) {
61             throw new RuntimeException("Database already started");
62         }
63         started = true;
64         doStart();
65         return getDatasource();
66     }
67
68     public final void stop() {
69         if (!started) {
70             return; // nothing to do.
71         }
72         started = false;
73         doStop();
74     }
75
76     private final DataSource getDatasource() {
77         if (!started) {
78             throw new RuntimeException("Database is not started!");
79         }
80         return itsDataSource;
81     }
82
83     protected String getProperty(String aName) {
84         String value = System.getProperty(aName);
85         if (value != null) {
86             return value;
87         }
88         value = System.getenv(aName);
89         if (value != null) {
90             return value;
91         }
92         throw new RuntimeException("This class expects the '" + aName +
93             "' property to be set");
94     }
95
96 }