org.zodiac.plugin.integration.user.DefaultPluginUser Maven / Gradle / Ivy
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