Search in sources :

Example 1 with IProxy

use of com.duangframework.core.interfaces.IProxy in project duangframework by tcrct.

the class BeanHelper method duang.

public static void duang() {
    // 扫描指定包路径下的类文件,类文件包含有指定的注解类或文件名以指定的字符串结尾的
    Map<String, List<Class<?>>> classMap = ClassScanKit.duang().annotations(InstanceFactory.MVC_ANNOTATION_SET).packages(ConfigKit.duang().key("base.package.path").asArray()).jarname(ConfigKit.duang().key("jar.prefix").asArray()).packages("com.duangframework.mvc").map();
    if (ToolsKit.isNotEmpty(classMap)) {
        String proxyKey = Proxy.class.getSimpleName();
        // 找出所有代理类进行实例化并缓存
        Map<Class<? extends Annotation>, IProxy> annotationMap = new HashMap<>();
        List<Class<?>> proxyList = classMap.get(proxyKey);
        if (ToolsKit.isNotEmpty(proxyList)) {
            for (Class<?> proxyClass : proxyList) {
                Proxy proxy = proxyClass.getAnnotation(Proxy.class);
                if (ToolsKit.isNotEmpty(proxy)) {
                    Class<? extends Annotation> aopClass = proxy.aop();
                    IProxy proxyObj = ClassUtils.newInstance(proxyClass);
                    annotationMap.put(aopClass, proxyObj);
                }
            }
        }
        // 将扫描后的Class进行实例化并缓存(Proxy除外)
        try {
            for (Iterator<Map.Entry<String, List<Class<?>>>> it = classMap.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry<String, List<Class<?>>> entry = it.next();
                String key = entry.getKey();
                if (proxyKey.equals(key)) {
                    continue;
                }
                List<Class<?>> classList = entry.getValue();
                if (ToolsKit.isEmpty(classList)) {
                    continue;
                }
                Map<Class<?>, Object> subBeanMap = BeanUtils.getAllBeanMaps().get(key);
                if (ToolsKit.isEmpty(subBeanMap)) {
                    subBeanMap = new HashMap<>(classList.size());
                }
                for (Class<?> cls : classList) {
                    Object clsObj = createBean(annotationMap, cls);
                    if (clsObj != null) {
                        // 实例化后,用类全名作key, 实例化对象作value缓存起来
                        subBeanMap.put(cls, clsObj);
                    }
                }
                BeanUtils.setAllBeanMaps(key, subBeanMap);
            }
        } catch (Exception e) {
            throw new MvcStartUpException(e.getMessage(), e);
        }
    }
    logger.warn("BeanHelper Success...");
}
Also used : IProxy(com.duangframework.core.interfaces.IProxy) Annotation(java.lang.annotation.Annotation) MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException) IProxy(com.duangframework.core.interfaces.IProxy) Proxy(com.duangframework.core.annotation.aop.Proxy) MvcStartUpException(com.duangframework.core.exceptions.MvcStartUpException)

Example 2 with IProxy

use of com.duangframework.core.interfaces.IProxy in project duangframework by tcrct.

the class BeanHelper method createBean.

private static Object createBean(Map<Class<? extends Annotation>, IProxy> annotationMap, Class<?> cls) throws Exception {
    List<IProxy> proxyList = new ArrayList<>();
    if (Modifier.isAbstract(cls.getModifiers()) || Modifier.isInterface(cls.getModifiers())) {
        return null;
    }
    Method[] methods = cls.getMethods();
    for (Method method : methods) {
        if (excludedMethodName.contains(method.getName())) {
            continue;
        }
        if (ToolsKit.isNotEmpty(annotationMap)) {
            for (Iterator<Map.Entry<Class<? extends Annotation>, IProxy>> it = annotationMap.entrySet().iterator(); it.hasNext(); ) {
                Map.Entry<Class<? extends Annotation>, IProxy> entry = it.next();
                if (method.isAnnotationPresent(entry.getKey())) {
                    proxyList.add(entry.getValue());
                }
            }
        }
    }
    Object instanceObj = null;
    // 实例化类,如果有代理注解则创建代理类
    if (ToolsKit.isNotEmpty(proxyList)) {
        // 代理类
        instanceObj = ProxyManager.createProxy(cls, proxyList);
    } else {
        instanceObj = ClassUtils.newInstance(cls);
    }
    return instanceObj;
}
Also used : IProxy(com.duangframework.core.interfaces.IProxy) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Example 3 with IProxy

use of com.duangframework.core.interfaces.IProxy in project duangframework by tcrct.

the class RpcFactory method initClient.

/**
 * 初始化RPC客户端,即消费者,生成代理类
 * @param classSet
 */
public static void initClient(Set<Class<?>> classSet) {
    if (ToolsKit.isEmpty(classSet)) {
        return;
    }
    try {
        for (Class<?> clientCls : classSet) {
            if (!clientCls.isInterface()) {
                continue;
            }
            List<IProxy> proxyList = new ArrayList<>();
            proxyList.add(new RpcClientProxy());
            Object proxyObj = ProxyManager.createProxy(clientCls, proxyList);
            if (null != proxyObj) {
                // 再将该代理类存放在BeanUtils里,让框架在执行IocHepler时注入到对应的Service
                BeanUtils.setBean2Map(clientCls, proxyObj);
                // 取出产品代号
                Rpc rpc = clientCls.getAnnotation(Rpc.class);
                watchNodePath.add(RpcUtils.getZookNotePath(rpc.productcode()));
            }
        }
    } catch (Exception e) {
        throw new RpcException("RpcFactory newProxyInstance is fail : " + e.getMessage(), e);
    }
}
Also used : Rpc(com.duangframework.core.annotation.rpc.Rpc) RpcException(com.duangframework.core.exceptions.RpcException) IProxy(com.duangframework.core.interfaces.IProxy) RpcException(com.duangframework.core.exceptions.RpcException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Aggregations

IProxy (com.duangframework.core.interfaces.IProxy)3 Annotation (java.lang.annotation.Annotation)2 Proxy (com.duangframework.core.annotation.aop.Proxy)1 Rpc (com.duangframework.core.annotation.rpc.Rpc)1 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)1 MvcStartUpException (com.duangframework.core.exceptions.MvcStartUpException)1 RpcException (com.duangframework.core.exceptions.RpcException)1 Method (java.lang.reflect.Method)1