重构JDBC插件
This commit is contained in:
parent
0b47d1c44a
commit
a9e2a07e49
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.api.IBuriedPointType;
|
||||
import com.ai.cloud.skywalking.protocol.CallType;
|
||||
|
|
@ -11,7 +11,6 @@ import org.apache.logging.log4j.Logger;
|
|||
|
||||
import com.ai.cloud.skywalking.plugin.boot.BootException;
|
||||
import com.ai.cloud.skywalking.plugin.boot.BootPluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.driver.TracingDriver;
|
||||
|
||||
public class JDBCPluginDefine extends BootPluginDefine {
|
||||
private static Logger logger = LogManager.getLogger(JDBCPluginDefine.class);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Map;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.AuthDesc;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.sql.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class TracingDriver implements Driver {
|
||||
private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(TracingDriver.class);
|
||||
|
||||
private static final String TRACING_SIGN = "tracing:";
|
||||
|
||||
public static final void registerDriver() {
|
||||
try {
|
||||
DriverManager.registerDriver(new TracingDriver());
|
||||
} catch (SQLException e) {
|
||||
logger.error("register TracingDriver failure.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String convertConnectURLIfNecessary(String url) throws SQLException {
|
||||
if (url.toLowerCase().startsWith(TRACING_SIGN)) {
|
||||
return url.substring(TRACING_SIGN.length());
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
public java.sql.Connection connect(String url, Properties info)
|
||||
throws SQLException {
|
||||
Driver driver = DriverChooser.choose(convertConnectURLIfNecessary(url));
|
||||
if (driver == null) {
|
||||
throw new SQLException("Failed to choose driver by url[{}].", convertConnectURLIfNecessary(url));
|
||||
}
|
||||
|
||||
java.sql.Connection conn = driver.
|
||||
connect(convertConnectURLIfNecessary(url), info);
|
||||
|
||||
if (!AuthDesc.isAuth()) {
|
||||
return conn;
|
||||
} else {
|
||||
return new SWConnection(convertConnectURLIfNecessary(url), info, conn);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean acceptsURL(String url) throws SQLException {
|
||||
Driver driver = DriverChooser.choose(convertConnectURLIfNecessary(url));
|
||||
if (driver == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return driver.acceptsURL(convertConnectURLIfNecessary(url));
|
||||
}
|
||||
|
||||
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
|
||||
throws SQLException {
|
||||
return DriverChooser.choose(convertConnectURLIfNecessary(url)).
|
||||
getPropertyInfo(convertConnectURLIfNecessary(url), info);
|
||||
}
|
||||
|
||||
public int getMajorVersion() {
|
||||
return safeIntParse("1");
|
||||
}
|
||||
|
||||
public int getMinorVersion() {
|
||||
return safeIntParse("0");
|
||||
}
|
||||
|
||||
public boolean jdbcCompliant() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int safeIntParse(String intAsString) {
|
||||
try {
|
||||
return Integer.parseInt(intAsString);
|
||||
} catch (NumberFormatException nfe) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static class DriverChooser {
|
||||
private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(DriverChooser.class);
|
||||
|
||||
private static Map<String, String> urlDriverMapping = new HashMap<String, String>();
|
||||
|
||||
static {
|
||||
fetchUrlDriverMapping();
|
||||
}
|
||||
|
||||
public static Driver choose(String url) {
|
||||
String driverClassStr = chooseDriverClass(url);
|
||||
Driver driver = null;
|
||||
try {
|
||||
Class<?> driverClass = Class.forName(driverClassStr);
|
||||
driver = (Driver) driverClass.newInstance();
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to initial Driver class {}.", driverClassStr, e);
|
||||
}
|
||||
|
||||
return driver;
|
||||
}
|
||||
|
||||
private static String chooseDriverClass(String url) {
|
||||
Iterator<String> mapping = urlDriverMapping.keySet().iterator();
|
||||
while (mapping.hasNext()) {
|
||||
String urlPrefix = mapping.next();
|
||||
if (url.startsWith(urlPrefix)) {
|
||||
String driverClassStr = urlDriverMapping.get(urlPrefix);
|
||||
logger.debug("Success choose the driver class [{}] by connection url[{}]", driverClassStr, url);
|
||||
return driverClassStr;
|
||||
}
|
||||
}
|
||||
|
||||
logger.warn("Cannot match the driver class by connection url [{}].", url);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void fetchUrlDriverMapping() {
|
||||
BufferedReader bufferedReader = null;
|
||||
try {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(DriverChooser.class.
|
||||
getResourceAsStream("/driver-mapping-url.def")));
|
||||
String mappingString = null;
|
||||
while ((mappingString = bufferedReader.readLine()) != null) {
|
||||
fillMapping(mappingString);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to load driver-mapping-url.def.");
|
||||
} finally {
|
||||
if (bufferedReader != null) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to close driver-mapping-url.def.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void fillMapping(String mappingString) {
|
||||
String[] tmpMapping = mappingString.split("=");
|
||||
if (tmpMapping.length == 2) {
|
||||
urlDriverMapping.put(tmpMapping[0], tmpMapping[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.sql.Driver;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class DriverChooser {
|
||||
private static Logger logger = LogManager.getLogger(DriverChooser.class);
|
||||
|
||||
private static Properties urlDriverMapping = new Properties();
|
||||
|
||||
static {
|
||||
InputStream inputStream = DriverChooser.class.getResourceAsStream("/driver-mapping-url.properties");
|
||||
try {
|
||||
urlDriverMapping.load(inputStream);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to load driver-mapping-url.properties");
|
||||
}
|
||||
}
|
||||
|
||||
public static Driver choose(String url) throws ClassNotFoundException,
|
||||
IllegalAccessException,
|
||||
InstantiationException {
|
||||
Driver driver = null;
|
||||
for (Map.Entry<Object, Object> entry : urlDriverMapping.entrySet()) {
|
||||
if (url.startsWith(entry.getValue().toString())) {
|
||||
Class<?> driverClass = Class.forName(entry.getKey().toString());
|
||||
driver = (Driver) driverClass.newInstance();
|
||||
}
|
||||
}
|
||||
return driver;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,115 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.DriverPropertyInfo;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.AuthDesc;
|
||||
|
||||
public class TracingDriver implements Driver {
|
||||
private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(TracingDriver.class);
|
||||
|
||||
private static final String TRACING_SIGN = "tracing:";
|
||||
|
||||
private static boolean isOpenCompensation = false;
|
||||
|
||||
public static final void registerDriver() {
|
||||
try {
|
||||
DriverManager.registerDriver(new TracingDriver());
|
||||
isOpenCompensation = true;
|
||||
} catch (SQLException e) {
|
||||
logger.error("register TracingDriver failure.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Driver realDriver;
|
||||
|
||||
private Driver chooseDriver(String url) throws IllegalAccessException,
|
||||
InstantiationException, ClassNotFoundException, SQLException {
|
||||
if (realDriver == null) {
|
||||
this.realDriver = DriverChooser.choose(url);
|
||||
}
|
||||
return realDriver;
|
||||
}
|
||||
|
||||
private String getRealUrl(String url) throws SQLException {
|
||||
if (!isOpenCompensation) {
|
||||
return url;
|
||||
} else {
|
||||
if (url.toLowerCase().startsWith(TRACING_SIGN)) {
|
||||
return url.substring(TRACING_SIGN.length());
|
||||
} else {
|
||||
throw new SQLException(
|
||||
"tracing jdbc url must start with 'tracing:'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public java.sql.Connection connect(String url, Properties info)
|
||||
throws SQLException {
|
||||
java.sql.Connection conn = null;
|
||||
|
||||
try {
|
||||
conn = chooseDriver(getRealUrl(url)).connect(getRealUrl(url), info);
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
|
||||
if (!AuthDesc.isAuth()) {
|
||||
return conn;
|
||||
} else {
|
||||
return new SWConnection(getRealUrl(url), info, conn);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean acceptsURL(String url) throws SQLException {
|
||||
Driver driver = null;
|
||||
try {
|
||||
driver = chooseDriver(getRealUrl(url));
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
return driver.acceptsURL(getRealUrl(url));
|
||||
}
|
||||
|
||||
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
|
||||
throws SQLException {
|
||||
Driver driver = null;
|
||||
try {
|
||||
driver = chooseDriver(getRealUrl(url));
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
return driver.getPropertyInfo(getRealUrl(url), info);
|
||||
}
|
||||
|
||||
public int getMajorVersion() {
|
||||
return safeIntParse("1");
|
||||
}
|
||||
|
||||
public int getMinorVersion() {
|
||||
return safeIntParse("0");
|
||||
}
|
||||
|
||||
public boolean jdbcCompliant() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int safeIntParse(String intAsString) {
|
||||
try {
|
||||
return Integer.parseInt(intAsString);
|
||||
} catch (NumberFormatException nfe) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
jdbc:mysql=com.mysql.jdbc.Driver
|
||||
jdbc:oracle=oracle.jdbc.driver.OracleDriver
|
||||
jdbc:sybase=com.sybase.jdbc2.jdbc.SybDriver
|
||||
jdbc:microsoft:sqlserver=com.microsoft.jdbc.sqlserver.SQLServerDriver
|
||||
jdbc:sqlserver=com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
jdbc:jtds:sqlserver=net.sourceforge.jtds.jdbc.Driver
|
||||
jdbc:db2=com.ibm.db2.jcc.DB2Driver
|
||||
jdbc:informix-sqli=com.informix.jdbc.IfxDriver
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
#mysql
|
||||
com.mysql.jdbc.Driver=jdbc:mysql
|
||||
#oracle
|
||||
oracle.jdbc.driver.OracleDriver=jdbc:oracle
|
||||
#sysbase
|
||||
com.sybase.jdbc2.jdbc.SybDriver=jdbc:sybase
|
||||
# SQLServer 2000
|
||||
com.microsoft.jdbc.sqlserver.SQLServerDriver=jdbc:microsoft:sqlserver
|
||||
#SQLServer 2005
|
||||
com.microsoft.sqlserver.jdbc.SQLServerDriver=jdbc:sqlserver
|
||||
#SQLServer 7.0
|
||||
net.sourceforge.jtds.jdbc.Driver=jdbc:jtds:sqlserver
|
||||
#DB2
|
||||
com.ibm.db2.jcc.DB2Driver=jdbc:db2
|
||||
#Informix
|
||||
com.informix.jdbc.IfxDriver=jdbc:informix-sqli
|
||||
|
|
@ -23,7 +23,7 @@ public class MysqlJDBCTest {
|
|||
public static void main(String[] args) throws ClassNotFoundException,
|
||||
SQLException, InterruptedException {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
String url = "jdbc:mysql://10.1.241.20:31306/sw_db?user=sw_dbusr01&password=sw_dbusr01";
|
||||
String url = "tracing:jdbc:mysql://10.1.241.20:31306/sw_db?user=sw_dbusr01&password=sw_dbusr01";
|
||||
Connection con = DriverManager.getConnection(url);
|
||||
con.setAutoCommit(false);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue