copyright messages updated in all java filees.
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / ExternalDatabase.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.Arrays;
19 import java.util.List;
20 import java.util.logging.Logger;
21
22 import javax.sql.DataSource;
23
24 import org.apache.commons.dbcp.ConnectionFactory;
25 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
26 import org.apache.commons.dbcp.PoolableConnectionFactory;
27 import org.apache.commons.dbcp.PoolingDataSource;
28 import org.apache.commons.pool.impl.GenericObjectPool;
29
30 /**
31  * Database that encapsulates connection to an external database. Database
32  * connection details can be configured through system properties and
33  * environment variables, see {@link #DB_URL_PROP}, {@link #DB_USER_PROP}, and
34  * {@link #DB_PASSWORD_PROP|.
35  * 
36  * This class assumes modern database drivers that work together with
37  * java.util.ServiceLoader so that explicitly doing a Class.forName() is not
38  * necessary to load the database driver.
39  */
40 public class ExternalDatabase extends AbstractDatabase {
41
42     private static final Logger LOGGER = Logger
43         .getLogger(ExternalDatabase.class.getName());
44
45     /**
46      * System property/environment variable that defines the database URL.
47      */
48     public static final String DB_URL_PROP = "TEST_DB_URL";
49
50     /**
51      * System property/environment variable that defines the database user.
52      */
53     public static final String DB_USER_PROP = "TEST_DB_USER";
54
55     /**
56      * System property/environment variable that defines the database password.
57      */
58     public static final String DB_PASSWORD_PROP = "TEST_DB_PASSWORD";
59
60     private String itsUrl;
61     private String itsUser;
62     private String itsPassword;
63
64     private DataSource itsDataSource;
65
66     public ExternalDatabase() {
67         // Empty
68     }
69
70     public String getExternalJdbcUrl() {
71         return itsUrl;
72     }
73
74     public String getJdbcUrl() {
75         return itsUrl;
76     }
77
78     public String getPassword() {
79         return itsPassword;
80     }
81
82     public String getUsername() {
83         return itsUser;
84     }
85
86     public void doStart() {
87         itsUrl = getProperty(DB_URL_PROP);
88         itsUser = getProperty(DB_USER_PROP);
89         itsPassword = getProperty(DB_PASSWORD_PROP);
90
91         createDataSource();
92     }
93
94     public void doStop() {
95         // Empty.
96     }
97
98 }