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