All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.plugin.integration.user.DefaultPluginUser Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.plugin.integration.user;

import org.pf4j.PluginManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.util.ObjectUtils;
import org.zodiac.commons.util.spring.Springs;
import org.zodiac.plugin.factory.process.pipe.PluginInfoContainers;

import java.lang.annotation.Annotation;
import java.util.*;

/**
 * 默认插件使用者。
 * 
 */
public class DefaultPluginUser implements PluginUser {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    protected final GenericApplicationContext parentApplicationContext;

    protected final PluginManager pluginManager;

    public DefaultPluginUser(ApplicationContext parentApplicationContext, PluginManager pluginManager) {
        Objects.requireNonNull(parentApplicationContext, "ApplicationContext can't be null");
        Objects.requireNonNull(pluginManager, "PluginManager can't be null");
        this.parentApplicationContext = (GenericApplicationContext)parentApplicationContext;
        this.pluginManager = pluginManager;
    }

    /**
     * 通过bean名称得到插件的bean。(Spring管理的bean)
     * 
     * @param name
     *            插件bean的名称。spring体系中的bean名称。可以通过注解定义,也可以自定义生成。具体可百度
     * @param 
     *            bean的类型
     * @return 返回bean
     */
    @Override
    public  T getBean(String name) {
        return getBean(name, true);
    }

    @Override
    public  T getBean(Class aClass) {
        return getBean(aClass, true);
    }

    @Override
    public  T getPluginBean(String name) {
        return getBean(name, false);
    }

    /**
     * 在主程序中定义的接口。插件或者主程序实现该接口。可以该方法获取到实现该接口的所有实现类。(Spring管理的bean)
     * 
     * @param aClass
     *            接口的类
     * @param 
     *            bean的类型
     * @return List
     */
    @Override
    public  List getBeans(Class aClass) {
        return getBeans(aClass, 3);
    }

    @Override
    public  List getMainBeans(Class aClass) {
        return getBeans(aClass, 1);
    }

    /**
     * 在主程序中定义的接口。获取插件中实现该接口的实现类。(Spring管理的bean)
     * 
     * @param aClass
     *            接口的类
     * @param 
     *            bean的类型
     * @return List
     */
    @Override
    public  List getPluginBeans(Class aClass) {
        return getBeans(aClass, 2);
    }

    @Override
    public  List getPluginBeans(String pluginId, Class aClass) {
        GenericApplicationContext pluginApplicationContext = PluginInfoContainers.getPluginApplicationContext(pluginId);
        if (pluginApplicationContext == null) {
            return Collections.emptyList();
        }
        return Springs.getBeanList(pluginApplicationContext, aClass);
    }

    @Override
    public List getPluginBeansWithAnnotation(Class annotationType) {
        List pluginApplicationContexts = PluginInfoContainers.getPluginApplicationContexts();
        List beans = new ArrayList<>();
        for (GenericApplicationContext pluginApplicationContext : pluginApplicationContexts) {
            Map beanMap = pluginApplicationContext.getBeansWithAnnotation(annotationType);
            if (!ObjectUtils.isEmpty(beanMap)) {
                beans.addAll(beanMap.values());
            }
        }
        return beans;
    }

    @Override
    public List getPluginBeansWithAnnotation(String pluginId, Class annotationType) {
        GenericApplicationContext genericApplicationContext =
            PluginInfoContainers.getPluginApplicationContext(pluginId);
        if (genericApplicationContext == null) {
            return Collections.emptyList();
        }
        Map beanMap = genericApplicationContext.getBeansWithAnnotation(annotationType);
        if (!ObjectUtils.isEmpty(beanMap)) {
            return new ArrayList<>(beanMap.values());
        } else {
            return Collections.emptyList();
        }
    }

    @Override
    public  T generateNewInstance(T object) {
        if (object == null) {
            return null;
        }
        List pluginApplicationContexts = PluginInfoContainers.getPluginApplicationContexts();
        pluginApplicationContexts.add(parentApplicationContext);
        Class aClass = object.getClass();
        for (GenericApplicationContext pluginApplicationContext : pluginApplicationContexts) {
            try {
                // 判断是否存在
                pluginApplicationContext.getBean(aClass);
                Object newBean = pluginApplicationContext.getBeanFactory().createBean(aClass);
                return (T)newBean;
            } catch (Exception e) {
                // 忽略
            }
        }
        return null;
    }

    /**
     * 得到插件扩展接口实现的bean。(非Spring管理)
     * 
     * @param tClass
     *            接口的类
     * @param 
     *            bean的类型
     * @return 返回bean
     */
    @Override
    public  List getPluginExtensions(Class tClass) {
        return pluginManager.getExtensions(tClass);
    }

    private  T getBean(String name, boolean haveParent) {
        List pluginApplicationContexts = PluginInfoContainers.getPluginApplicationContexts();
        if (haveParent) {
            pluginApplicationContexts.add(parentApplicationContext);
        }
        for (GenericApplicationContext pluginApplicationContext : pluginApplicationContexts) {
            if (pluginApplicationContext.containsBean(name)) {
                return (T)pluginApplicationContext.getBean(name);
            }
        }
        return null;
    }

    private  T getBean(Class aClass, boolean haveParent) {
        List pluginApplicationContexts = PluginInfoContainers.getPluginApplicationContexts();
        if (haveParent) {
            pluginApplicationContexts.add(parentApplicationContext);
        }
        for (GenericApplicationContext pluginApplicationContext : pluginApplicationContexts) {
            try {
                T bean = pluginApplicationContext.getBean(aClass);
                if (bean != null) {
                    return bean;
                }
            } catch (Exception e) {
                // 忽略
            }
        }
        return null;
    }

    /**
     * 获取多个bean.
     * 
     * @param aClass
     *            接口或者抽象类类类型
     * @param type
     *            1 获取主程序的, 2 获取插件中的, 3 获取所有的
     * @param 
     *            类类型
     * @return List
     */
    private  List getBeans(Class aClass, int type) {
        List pluginApplicationContexts = new ArrayList<>(1);

        if (type == 1) {
            pluginApplicationContexts.add(parentApplicationContext);
        } else if (type == 2) {
            pluginApplicationContexts.addAll(PluginInfoContainers.getPluginApplicationContexts());
        } else if (type == 3) {
            pluginApplicationContexts.add(parentApplicationContext);
            pluginApplicationContexts.addAll(PluginInfoContainers.getPluginApplicationContexts());
        } else {
            return Collections.emptyList();
        }

        List result = new ArrayList<>();
        for (GenericApplicationContext pluginApplicationContext : pluginApplicationContexts) {
            List pluginBeans = Springs.getBeanList(pluginApplicationContext, aClass);
            if (!pluginBeans.isEmpty()) {
                result.addAll(pluginBeans);
            }
        }
        return result;
    }

}