Class Loader结合Security控制的一个使用范例

ClassLoader loader = new ClassLoader();

loader = AccessController.doPrivileged(
       new PrivilegedAction<T>(){
            public T run(){ 
               return new T();
}
}
)

// Usually the T is URLClassLoader or other user defined ClassLoader which implemented URLClassLoader
// return new URLClassLoader(new URL[]{});

Thread.getCurrentThread.setContextClassLoader(loader);
Class initialClass = loader.loadClass(“com.company.main.Entrance”);
Object instance = initialClass.newInstance();
Method startup = initialClass.getMethod(“start”, new Class[]{new String[0].getClass()});
Startup.invoke(*********);

User Defined ClassLoader Class By JMeter:

/**
 * This is a basic URL classloader for loading new resources
 * dynamically.
 *
 * It allows public access to the addURL() method.
 *
 * It also adds a convenience method to update the current thread classloader
 *
 */

publicclass DynamicClassLoader extends URLClassLoader {

    public DynamicClassLoader(URL[] urls) {
        super(urls);
    }

    public DynamicClassLoader(URL[] urls, ClassLoader parent) {
        super(urls, parent);
    }
 
    public DynamicClassLoader(URL[] urls, ClassLoader parent,
	     URLStreamHandlerFactory factory) {
        super(urls, parent, factory);
    }

    // Make the addURL method visible
    @Override
    publicvoid addURL(URL url) {
        super.addURL(url);
    }

    /**
     *
     * @param urls - list of URLs to add to the thread's classloader
     */
    publicstaticvoid updateLoader(URL [] urls) {
        DynamicClassLoader loader
            = (DynamicClassLoader) Thread.currentThread().getContextClassLoader();
        for(URL url : urls) {
            loader.addURL(url);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值