Created
May 23, 2012 18:35
-
-
Save serac/2776913 to your computer and use it in GitHub Desktop.
Externalize jdbc-pool Datasource Credentials to Properties File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceFactory.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceFactory.java | |
index 4b91e4a..59a6d60 100644 | |
--- a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceFactory.java | |
+++ b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/DataSourceFactory.java | |
@@ -17,6 +17,9 @@ | |
package org.apache.tomcat.jdbc.pool; | |
+import java.io.File; | |
+import java.io.FileInputStream; | |
+import java.io.IOException; | |
import java.sql.Connection; | |
import java.util.Hashtable; | |
import java.util.Properties; | |
@@ -67,6 +70,7 @@ public class DataSourceFactory implements ObjectFactory { | |
protected static final String PROP_PASSWORD = "password"; | |
protected static final String PROP_URL = "url"; | |
protected static final String PROP_USERNAME = "username"; | |
+ protected static final String PROP_CREDENTIALFILE = "credentialFile"; | |
protected static final String PROP_MAXACTIVE = "maxActive"; | |
protected static final String PROP_MAXIDLE = "maxIdle"; | |
@@ -140,6 +144,7 @@ public class DataSourceFactory implements ObjectFactory { | |
PROP_PASSWORD, | |
PROP_URL, | |
PROP_USERNAME, | |
+ PROP_CREDENTIALFILE, | |
PROP_VALIDATIONQUERY, | |
PROP_VALIDATOR_CLASS_NAME, | |
PROP_VALIDATIONINTERVAL, | |
@@ -334,14 +339,32 @@ public class DataSourceFactory implements ObjectFactory { | |
poolProperties.setTestWhileIdle(Boolean.valueOf(value).booleanValue()); | |
} | |
- value = properties.getProperty(PROP_PASSWORD); | |
+ value = properties.getProperty(PROP_URL); | |
if (value != null) { | |
- poolProperties.setPassword(value); | |
+ poolProperties.setUrl(value); | |
} | |
- value = properties.getProperty(PROP_URL); | |
+ value = properties.getProperty(PROP_CREDENTIALFILE); | |
if (value != null) { | |
- poolProperties.setUrl(value); | |
+ File credFile = new File(value); | |
+ if (credFile.exists()) { | |
+ Properties credentials = new Properties(); | |
+ try { | |
+ credentials.load(new FileInputStream(credFile)); | |
+ poolProperties.setUsername(credentials.getProperty(PROP_USERNAME)); | |
+ poolProperties.setPassword(credentials.getProperty(PROP_PASSWORD)); | |
+ } catch (IOException e) { | |
+ System.err.println("Error reading credential file " + value); | |
+ } | |
+ } else { | |
+ System.err.println(value + " does not exist."); | |
+ } | |
+ } | |
+ | |
+ // Explicit username and password supersede values in credential file | |
+ value = properties.getProperty(PROP_PASSWORD); | |
+ if (value != null) { | |
+ poolProperties.setPassword(value); | |
} | |
value = properties.getProperty(PROP_USERNAME); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment