Search in sources :

Example 6 with JdbcDriverIT

use of com.creditease.uav.hook.jdbc.interceptors.JdbcDriverIT in project uavstack by uavorg.

the class JdbcHookProxy method injectDruid.

/**
 * injectDruid
 *
 * @param webapploader
 * @param appid
 */
private void injectDruid(ClassLoader webapploader, InterceptContext ic) {
    /**
     * check if there is druid, god, so sad we are in china, we have to support alibaba druid
     */
    try {
        webapploader.loadClass("com.alibaba.druid.Constants");
    } catch (ClassNotFoundException e) {
        return;
    }
    if (isHookEventDone("isInjectDruid")) {
        return;
    }
    final String appid = this.getAppID(ic);
    /**
     * set the webapploader is the target classloader
     */
    dpInstall.setTargetClassLoader(webapploader);
    /**
     * inject DruidDriver
     */
    dpInstall.installProxy("com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl", new String[] { "com.creditease.uav.hook.jdbc.interceptors" }, new DynamicProxyProcessor() {

        @Override
        public void process(DPMethod m) throws Exception {
            if ("connect".equals(m.getName())) {
                dpInstall.defineLocalVal(m, "mObj", JdbcDriverIT.class);
                m.insertBefore("{mObj=new JdbcDriverIT(\"" + appid + "\");}");
                m.insertAfter("{$_=mObj.doProxyConnection($_);}");
            }
        }
    }, false);
    /**
     * inject DruidDataSource
     */
    dpInstall.installProxy("com.alibaba.druid.util.JdbcUtils", new String[] { "com.creditease.uav.hook.jdbc.interceptors" }, new DynamicProxyProcessor() {

        @Override
        public void process(DPMethod m) throws Exception {
            if ("createDriver".equals(m.getName()) && m.getParameterTypes().length == 2) {
                dpInstall.defineLocalVal(m, "mObj", JdbcDriverIT.class);
                m.insertBefore("{mObj=new JdbcDriverIT(\"" + appid + "\");}");
                m.insertAfter("{$_=mObj.doRegisterDriver($_,false);}");
            }
        }
    }, false);
    /**
     * adapts:<br>
     * 1) FIX simulate the behaviour of Druid before injected: <br>
     * validateConnection use given Connection to ping DB, <br>
     * and throw exception if our $Proxy is given.<br>
     * <br>
     * 2) FIX simulate the behaviour of Druid before injected: <br>
     * these 'init' methods will new a Object to specified DB type
     */
    dpInstall.doAdapts(getAdapts());
    // release loader
    dpInstall.releaseTargetClassLoader();
}
Also used : DPMethod(com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod) DynamicProxyProcessor(com.creditease.uav.monitorframework.dproxy.DynamicProxyProcessor) JdbcDriverIT(com.creditease.uav.hook.jdbc.interceptors.JdbcDriverIT)

Aggregations

JdbcDriverIT (com.creditease.uav.hook.jdbc.interceptors.JdbcDriverIT)6 DynamicProxyProcessor (com.creditease.uav.monitorframework.dproxy.DynamicProxyProcessor)4 DPMethod (com.creditease.uav.monitorframework.dproxy.bytecode.DPMethod)4 ServerVendor (com.creditease.monitor.UAVServer.ServerVendor)1 Driver (java.sql.Driver)1