ExtClasspathLoader

/* 
 * Copyright (c) 2018,
 */
package com.rpc.jmeter.utils;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;

public final class ExtClasspathLoader {

    private static Method addURL = initAddMethod();

    private static ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
    private static ClassLoader webappClassLoader = ExtClasspathLoader.class.getClassLoader();

    /**
     * 初始化addUrl 方法.
     * 
     * @return 可访问addUrl方法的Method对象
     */
    private static Method initAddMethod() {
        try {
            Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
            add.setAccessible(true);
            return add;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void loadClasspath(String filepath) {
        File file = new File(filepath);
        loopFiles(file);
    }

    /**
     * 循环遍历目录,找出所有的jar包。
     * 
     * @param file
     *            当前遍历文件
     */
    private static void loopFiles(File file) {
        if (file.isDirectory()) {
            File[] tmps = file.listFiles();
            for (File tmp : tmps) {
                loopFiles(tmp);
            }
        } else {
            if (file.getAbsolutePath().endsWith(".jar") || file.getAbsolutePath().endsWith(".zip")) {
                addURL(file, false);
            }
        }
    }

    /**
     * 通过filepath加载文件到classpath。
     * 
     * @param filePath
     *            文件路径
     * @return URL
     * @throws Exception
     *             异常
     */
    public static void addURL(File file, boolean system) {
        try {
            if (system) {
                addURL.invoke(systemClassLoader, new Object[] { file.toURI().toURL() });
            } else {
                addURL.invoke(webappClassLoader, new Object[] { file.toURI().toURL() });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String path = "D:\\*-resource-client-1.0.6-SNAPSHOT.jar";
        ExtClasspathLoader.loadClasspath(path);

        ApplicationConfig application = new ApplicationConfig();
        application.setName("username");
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("zookeeper://10.202.35.91:2181?backup=10.202.35.92:2181,10.202.35.96:2181");

        ReferenceConfig<?> reference = new ReferenceConfig<>();
        reference.setApplication(application);
        reference.setRegistry(registry);
        reference.setInterface("com.*.*.match.client.rpc.IMatchEmpService");
        reference.setGroup("ENV3_1");
        try {
            Object bean = reference.get();
            Class<?> paramClass = Class.forName("com.*.*.match.client.dto.MatchEmpReq");
            Object param = paramClass.newInstance();
            param = JSON.parse(
                    "{\"cityCode\":\"755\",\"intertype\":\"1\",\"orderTime\":null,\"orderType\":\"D\",\"parcelWeight\":10,\"unitAreaCode\":\"755Q001\"}",
                    paramClass);
            Method targetMethod = bean.getClass().getMethod("getEmpIdForDdsDelivery", paramClass);
            Object rpcResult = targetMethod.invoke(bean, param);
            //System.out.println("结果 = " + JSONUtils.toObject(rpcResult).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // File file = new File(path);
        // Map<String, List<String>> result = MyFileUtils.parseJar(file);
        // System.out.println("解析结果: ");
        // Iterator<Entry<String, List<String>>> iterator =
        // result.entrySet().iterator();
        // while (iterator.hasNext()) {
        // Entry<String, List<String>> entry = iterator.next();
        // System.out.print("接口名: " + entry.getKey() + "\t");
        // System.out.println("拥有方法: " + entry.getValue());
        // }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值