Skip to content

Commit

Permalink
sql server driver test & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 20, 2012
1 parent 67d77aa commit 39eac26
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 157 deletions.
Binary file not shown.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@
<version>10.2.0.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0.1301.101</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0.2206.100</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.nutz</groupId>
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/alibaba/druid/filter/FilterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -78,4 +80,68 @@ private static void loadFilterConfig(Properties filterProperties, ClassLoader cl
filterProperties.putAll(property);
}
}

public static void loadFilter(List<Filter> filters, String filterName) throws SQLException {
if (filterName.length() == 0) {
return;
}

String filterClassNames = getFilter(filterName);

if (filterClassNames != null) {
for (String filterClassName : filterClassNames.split(",")) {
if (existsFilter(filters, filterClassName)) {
continue;
}

Class<?> filterClass = JdbcUtils.loadDriverClass(filterClassName);

if (filterClass == null) {
LOG.error("load filter error, filter not found : " + filterClassName);
continue;
}

Filter filter;

try {
filter = (Filter) filterClass.newInstance();
} catch (InstantiationException e) {
throw new SQLException("load managed jdbc driver event listener error. " + filterName, e);
} catch (IllegalAccessException e) {
throw new SQLException("load managed jdbc driver event listener error. " + filterName, e);
}

filters.add(filter);
}

return;
}

if (existsFilter(filters, filterName)) {
return;
}

Class<?> filterClass = JdbcUtils.loadDriverClass(filterName);
if (filterClass == null) {
LOG.error("load filter error, filter not found : " + filterName);
return;
}

try {
Filter filter = (Filter) filterClass.newInstance();
filters.add(filter);
} catch (Exception e) {
throw new SQLException("load managed jdbc driver event listener error. " + filterName, e);
}
}

private static boolean existsFilter(List<Filter> filterList, String filterClassName) {
for (Filter filter : filterList) {
String itemFilterClassName = filter.getClass().getName();
if (itemFilterClassName.equalsIgnoreCase(filterClassName)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.FilterChainImpl;
import com.alibaba.druid.filter.FilterManager;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource.ActiveConnectionTraceInfo;
import com.alibaba.druid.pool.vendor.NullExceptionSorter;
Expand All @@ -59,7 +60,6 @@
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.ConcurrentIdentityHashMap;
import com.alibaba.druid.util.DruidFilterUtils;
import com.alibaba.druid.util.Histogram;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.JdbcUtils;
Expand Down Expand Up @@ -485,7 +485,7 @@ public String getValidConnectionCheckerClassName() {
}

public void setValidConnectionCheckerClassName(String validConnectionCheckerClass) throws Exception {
Class<?> clazz = DruidFilterUtils.loadClass(validConnectionCheckerClass);
Class<?> clazz = JdbcUtils.loadDriverClass(validConnectionCheckerClass);
ValidConnectionChecker validConnectionChecker = null;
if (clazz != null) {
validConnectionChecker = (ValidConnectionChecker) clazz.newInstance();
Expand Down Expand Up @@ -713,7 +713,7 @@ public void setPasswordCallback(PasswordCallback passwordCallback) {
}

public void setPasswordCallbackClassName(String passwordCallbackClassName) throws Exception {
Class<?> clazz = DruidFilterUtils.loadClass(passwordCallbackClassName);
Class<?> clazz = JdbcUtils.loadDriverClass(passwordCallbackClassName);
if (clazz != null) {
this.passwordCallback = (PasswordCallback) clazz.newInstance();
} else {
Expand Down Expand Up @@ -1012,7 +1012,7 @@ public void setExceptionSorter(String exceptionSorter) throws Exception {
return;
}

Class<?> clazz = DruidFilterUtils.loadClass(exceptionSorter);
Class<?> clazz = JdbcUtils.loadDriverClass(exceptionSorter);
if (clazz == null) {
LOG.error("load exceptionSorter error : " + exceptionSorter);
} else {
Expand Down Expand Up @@ -1050,7 +1050,7 @@ public void setFilters(String filters) throws SQLException {
String[] filterArray = filters.split("\\,");

for (String item : filterArray) {
DruidFilterUtils.loadFilter(this.filters, item);
FilterManager.loadFilter(this.filters, item);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.DruidFilterUtils;
import com.alibaba.druid.util.JdbcUtils;

public class MySqlValidConnectionChecker implements ValidConnectionChecker, Serializable {
Expand All @@ -26,7 +25,7 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri

public MySqlValidConnectionChecker(){
try {
clazz = DruidFilterUtils.loadClass("com.mysql.jdbc.Connection");
clazz = JdbcUtils.loadDriverClass("com.mysql.jdbc.Connection");
ping = clazz.getMethod("ping");
if (ping != null) {
driverHasPingMethod = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.DruidFilterUtils;
import com.alibaba.druid.util.JdbcUtils;

public class OracleValidConnectionChecker implements ValidConnectionChecker, Serializable {
Expand All @@ -48,7 +47,7 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser

public OracleValidConnectionChecker(){
try {
clazz = DruidFilterUtils.loadClass("oracle.jdbc.driver.OracleConnection");
clazz = JdbcUtils.loadDriverClass("oracle.jdbc.driver.OracleConnection");
if (clazz != null) {
ping = clazz.getMethod("pingDatabase", new Class[] { Integer.TYPE });
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/alibaba/druid/proxy/DruidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@

import com.alibaba.druid.VERSION;
import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.FilterManager;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig;
import com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl;
import com.alibaba.druid.stat.JdbcStatManager;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.DruidFilterUtils;
import com.alibaba.druid.util.JMXUtils;
import com.alibaba.druid.util.JdbcUtils;

Expand Down Expand Up @@ -160,7 +160,7 @@ private DataSourceProxyImpl getDataSource(String url, Properties info) throws SQ
String property = System.getProperty("druid.filters");
if (property != null && property.length() > 0) {
for (String filterItem : property.split(",")) {
DruidFilterUtils.loadFilter(config.getFilters(), filterItem);
FilterManager.loadFilter(config.getFilters(), filterItem);
}
}
}
Expand Down Expand Up @@ -203,7 +203,7 @@ public static DataSourceProxyConfig parseConfig(String url, Properties info) thr
int pos = restUrl.indexOf(':', FILTERS_PREFIX.length());
String filtersText = restUrl.substring(FILTERS_PREFIX.length(), pos);
for (String filterItem : filtersText.split(",")) {
DruidFilterUtils.loadFilter(config.getFilters(), filterItem);
FilterManager.loadFilter(config.getFilters(), filterItem);
}
restUrl = restUrl.substring(pos + 1);
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public static DataSourceProxyConfig parseConfig(String url, Properties info) thr
}

public Driver createDriver(String className) throws SQLException {
Class<?> rawDriverClass = DruidFilterUtils.loadClass(className);
Class<?> rawDriverClass = JdbcUtils.loadDriverClass(className);

if (rawDriverClass == null) {
throw new SQLException("jdbc-driver's class not found. '" + className + "'");
Expand Down
129 changes: 0 additions & 129 deletions src/main/java/com/alibaba/druid/util/DruidFilterUtils.java

This file was deleted.

26 changes: 25 additions & 1 deletion src/main/java/com/alibaba/druid/util/JdbcUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public static String getDriverClassName(String rawUrl) throws SQLException {
} else if (rawUrl.startsWith("jdbc:microsoft:")) {
return "com.microsoft.jdbc.sqlserver.SQLServerDriver";
} else if (rawUrl.startsWith("jdbc:sqlserver:")) {
return "com.microsoft.jdbc.sqlserver.SQLServerDriver";
return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
} else if (rawUrl.startsWith("jdbc:sybase:Tds:")) {
return "com.sybase.jdbc2.jdbc.SybDriver";
} else if (rawUrl.startsWith("jdbc:jtds:")) {
Expand Down Expand Up @@ -610,4 +610,28 @@ private static void setParameters(PreparedStatement stmt, List<Object> parameter
stmt.setObject(i + 1, parameters.get(i));
}
}

public static Class<?> loadDriverClass(String className) {
Class<?> clazz = null;

if (className == null) {
return null;
}

try {
clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {

}

if (clazz != null) {
return clazz;
}

try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}
}
Loading

0 comments on commit 39eac26

Please sign in to comment.