(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / support / persistence / ExternalDatabase.java
diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/ExternalDatabase.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/ExternalDatabase.java
new file mode 100644 (file)
index 0000000..2533df4
--- /dev/null
@@ -0,0 +1,82 @@
+package org.wamblee.support.persistence;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.logging.Logger;
+
+import javax.sql.DataSource;
+
+import org.apache.commons.dbcp.ConnectionFactory;
+import org.apache.commons.dbcp.DriverManagerConnectionFactory;
+import org.apache.commons.dbcp.PoolableConnectionFactory;
+import org.apache.commons.dbcp.PoolingDataSource;
+import org.apache.commons.pool.impl.GenericObjectPool;
+
+/**
+ * Database that encapsulates connection to an external database. 
+ * Database connection details can be configured through system properties
+ * and environment variables, see {@link #DB_URL_PROP}, {@link #DB_USER_PROP}, and 
+ * {@link #DB_PASSWORD_PROP|. 
+ * 
+ * This class assumes modern database drivers that work together with java.util.ServiceLoader
+ * so that explicitly doing a Class.forName() is not necessary to load the database driver. 
+ */
+public class ExternalDatabase extends AbstractDatabase {
+       
+       private static final Logger LOGGER = Logger.getLogger(ExternalDatabase.class.getName()); 
+       
+       /**
+        * System property/environment variable that defines the database URL. 
+        */
+       public static final String DB_URL_PROP = "TEST_DB_URL";
+       
+       /**
+        * System property/environment variable that defines the database user. 
+        */
+       public static final String DB_USER_PROP = "TEST_DB_USER";
+       
+       /**
+        * System property/environment variable that defines the database password. 
+        */
+       public static final String DB_PASSWORD_PROP = "TEST_DB_PASSWORD";
+
+
+       private String itsUrl; 
+       private String itsUser; 
+       private String itsPassword; 
+       
+       private DataSource itsDataSource; 
+       
+       public ExternalDatabase() { 
+               // Empty
+       }
+       
+       public String getExternalJdbcUrl() {
+               return itsUrl; 
+       }
+
+       public String getJdbcUrl() {
+               return itsUrl; 
+       }
+
+       public String getPassword() {
+               return itsPassword;
+       }
+
+       public String getUsername() {
+               return itsUser;
+       }
+
+       public void doStart() {
+               itsUrl = getProperty(DB_URL_PROP);
+               itsUser = getProperty(DB_USER_PROP);
+               itsPassword = getProperty(DB_PASSWORD_PROP);
+               
+               createDataSource();
+       }
+
+       public void doStop() {
+               // Empty. 
+       }
+
+}