Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleDataSourceDriverLoaderUtils
linenumberstrue
collapsetrue
package com.fr.stable.fun.impl;

import com.fr.plugin.ExtraClassManager;
import com.fr.plugin.manage.PluginManager;
import com.fr.stable.fun.DatasourceDriverLoader;

import java.util.Set;

/**
 *具体调用接口的工具类
 */
public class DataSourceDriverLoaderUtils {

    public static ClassLoader getClassLoader(String driverName) {
    Set<DataSourceDriverLoader> dataSourceDriverLoaders = getDataSourceDriverLoader();
    //是空的话说明插件没有成功加载
    if (dataSourceDriverLoaders == null || dataSourceDriverLoaders.isEmpty()) {
        FineLoggerFactory.getLogger().info("====== not loading classloader plugin ======");
    } else {
        for (DataSourceDriverLoader datasourceDriverLoader : dataSourceDriverLoaders) {
            //判断是否是目标driver
            if (datasourceDriverLoader.isInterceptAllowed(driverName)) {
                ClassLoader classLoader = datasourceDriverLoader.getClassLoader();
                return classLoader;
            }
        }
    }
    return null;
}

private static Set<DataSourceDriverLoader> getDataSourceDriverLoader() {
    return ExtraClassManager.getInstance().getArray(DataSourceDriverLoader.XML_TAG);
}
}

 


接口接入

Code Block
languagexml
<extra-core>
    <DataSourceDriverLoader class="com.fr.plugin.dialect.classloader.ClassLoaderHandler"/>
</extra-core>

...

demo里在成功命中目标驱动后设置了日志信息输出:

 


示例代码:

https://gitcode.fanruan.com/fanruan/demo-driver-loader

注意事项

1、仅当实现的ClassLoader不为空时才会set进datasource中