add configTool

This commit is contained in:
wenshao
2012-08-16 15:42:42 +08:00
parent 85d6db123d
commit fa5cc17022
2 changed files with 71 additions and 3 deletions
@@ -33,6 +33,7 @@ public class ConfigFilter extends FilterAdapter {
private Cipher cipher;
private String algorithm;
private String key;
private String encryptedPassword;
public ConfigFilter(){
this.setAlgorithm(DEFAULT_ALGORITHM);
@@ -51,6 +52,14 @@ public class ConfigFilter extends FilterAdapter {
}
}
public String getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
public String getKey() {
return key;
}
@@ -81,8 +90,21 @@ public class ConfigFilter extends FilterAdapter {
Properties properties = loadConfig(dataSource);
if (encryptedPassword != null) {
try {
byte[] passwordBytes = Base64.base64ToByteArray(encryptedPassword);
byte[] decryptedBytes = cipher.doFinal(passwordBytes);
String decryptedPassword = new String(decryptedBytes, "ISO-8859-1");
dataSource.setPassword(decryptedPassword);
} catch (Exception e) {
LOG.error("decrypt password error", e);
}
}
if (properties == null) {
LOG.error("load config error, return null");
if (encryptedPassword == null) {
LOG.error("load config error, return null");
}
return;
}
@@ -92,7 +114,7 @@ public class ConfigFilter extends FilterAdapter {
if (password != null && password.length() != 0) {
byte[] passwordBytes = Base64.base64ToByteArray(password);
byte[] decryptedBytes = cipher.doFinal(passwordBytes);
String decryptedPassword = new String(decryptedBytes, "ISO-8859-1");
properties.put(DruidDataSourceFactory.PROP_PASSWORD, decryptedPassword);
}
@@ -121,7 +143,9 @@ public class ConfigFilter extends FilterAdapter {
}
if (filePath == null) {
LOG.error("load config error, file is null");
if (this.encryptedPassword != null) {
LOG.error("load config error, file is null");
}
return null;
}
@@ -0,0 +1,44 @@
package com.alibaba.druid.bvt.filter.config;
import java.sql.Connection;
import com.alibaba.druid.filter.config.ConfigFilter;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.util.JdbcUtils;
import junit.framework.Assert;
import junit.framework.TestCase;
public class ConfigLoadFilterTest3 extends TestCase {
private DruidDataSource dataSource;
private ConfigFilter configFilter;
protected void setUp() throws Exception {
configFilter = new ConfigFilter();
configFilter.setEncryptedPassword("TFxB6eJcgxE1hxOgiwOC/L7zWR/9vnSIfpggI2PTfcvvRhSnCGCPwI9n03fiJiLmRdnDU2/KaVTJYwz8zzkBqg==");
configFilter.setKey("MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAMmVkLy+Cy/3rQ1OyH7nz1tT8Bu2KEU+z5LbeZ1yue/RN4KlcOEalj/K9Ev05Lpfu9cOdYnYM8ka1MiQdHLyJP0CAwEAAQ==");
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:config-3");
dataSource.setMaxActive(13);
dataSource.getProxyFilters().add(configFilter);
dataSource.setTestOnBorrow(false);
}
protected void tearDown() throws Exception {
JdbcUtils.close(dataSource);
}
public void test_config() throws Exception {
dataSource.init();
Assert.assertEquals(13, dataSource.getMaxActive());
Assert.assertEquals("jdbc:mock:config-3", dataSource.getUrl());
Assert.assertEquals(MockDriver.instance, dataSource.getDriver());
Assert.assertEquals("abc", dataSource.getPassword());
Connection conn = dataSource.getConnection();
conn.close();
}
}