2533df427ebebffc836767dc7cb5e49493c88031
[utils] /
1 package org.wamblee.support.persistence;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.logging.Logger;
6
7 import javax.sql.DataSource;
8
9 import org.apache.commons.dbcp.ConnectionFactory;
10 import org.apache.commons.dbcp.DriverManagerConnectionFactory;
11 import org.apache.commons.dbcp.PoolableConnectionFactory;
12 import org.apache.commons.dbcp.PoolingDataSource;
13 import org.apache.commons.pool.impl.GenericObjectPool;
14
15 /**
16  * Database that encapsulates connection to an external database. 
17  * Database connection details can be configured through system properties
18  * and environment variables, see {@link #DB_URL_PROP}, {@link #DB_USER_PROP}, and 
19  * {@link #DB_PASSWORD_PROP|. 
20  * 
21  * This class assumes modern database drivers that work together with java.util.ServiceLoader
22  * so that explicitly doing a Class.forName() is not necessary to load the database driver. 
23  */
24 public class ExternalDatabase extends AbstractDatabase {
25         
26         private static final Logger LOGGER = Logger.getLogger(ExternalDatabase.class.getName()); 
27         
28         /**
29          * System property/environment variable that defines the database URL. 
30          */
31         public static final String DB_URL_PROP = "TEST_DB_URL";
32         
33         /**
34          * System property/environment variable that defines the database user. 
35          */
36         public static final String DB_USER_PROP = "TEST_DB_USER";
37         
38         /**
39          * System property/environment variable that defines the database password. 
40          */
41         public static final String DB_PASSWORD_PROP = "TEST_DB_PASSWORD";
42
43
44         private String itsUrl; 
45         private String itsUser; 
46         private String itsPassword; 
47         
48         private DataSource itsDataSource; 
49         
50         public ExternalDatabase() { 
51                 // Empty
52         }
53         
54         public String getExternalJdbcUrl() {
55                 return itsUrl; 
56         }
57
58         public String getJdbcUrl() {
59                 return itsUrl; 
60         }
61
62         public String getPassword() {
63                 return itsPassword;
64         }
65
66         public String getUsername() {
67                 return itsUser;
68         }
69
70         public void doStart() {
71                 itsUrl = getProperty(DB_URL_PROP);
72                 itsUser = getProperty(DB_USER_PROP);
73                 itsPassword = getProperty(DB_PASSWORD_PROP);
74                 
75                 createDataSource();
76         }
77
78         public void doStop() {
79                 // Empty. 
80         }
81
82 }