扩展点记录

来源:互联网 发布:淘宝儿童玩具电子琴 编辑:程序博客网 时间:2024/06/05 09:16
 /*** 扩展点处理器接口,各种扩展点的初始处理由实现该接口的特定处理器实现。* 在处理扩展点使用配置信息时,提供灵活的应对机制,IProcessSource代表* 处理源,对于处理源的处理分为两种情况:*/public interface IExtensionProcessor {/*** 实行扩展点解析处理处理,该方法将会在Workbench启动时,插件解析、部署后被调用。* * @param processSource 待处理的扩展点原* @throws ExtensionProcessException 处理失败时抛出该异常*/public void process(IProcessSource processSource) throws ExtensionException;/*** 资源释放,该方法将会在Workbench关闭时被调用。* * @throws ExtensionProcessException 释放资源失败时抛出该异常*/public void release(IPluginModule module) throws ExtensionException;}       public abstract class AbstractExtensionProcessor implements IExtensionProcessor {       //日志等通用操作} public class WebServiceProcessor extends AbstractExtensionProcessor implements IExtensionProcessor {    private Logger logger = LoggerManager.getLogger(WebServiceProcessor.class);    public static final String EXTENSION_ID = "com.agent.ws";    private static final String TAG_HANDLER = "webService";    public static final String ATTRIBUTE_CLASS = "class";     @Override    protected Logger getLogger() {        return logger;    }     /**     * 解析配置web service的extension,发布web service     */    public void process(IProcessSource processSource) throws ExtensionException {        Collection<Node> extensions = processSource.getProcessNodes();        if (extensions == null || extensions.size() <= 0) {            return;        }        //        IPluginModule module = processSource.getPluginModule();        ClassLoader loader = module.getPluginLoader();        WebServiceManager serviceManager = ServiceFactory.getService(WebServiceManager.class);        try {            for (Node extension : extensions) {                List<Node> list = W3CDomUtil.getChildNodes(extension, TAG_HANDLER);                for (Node child : list) {                    String cls = W3CDomUtil.getAttributeValue(child, ATTRIBUTE_CLASS);                    Assert.isNotNull(cls);                    Object ws = loader.loadClass(cls).newInstance();                    String url = serviceManager.generateURL(module.getPluginName() + "/", ws.getClass().getInterfaces()[0].getSimpleName());                    serviceManager.publish(url, ws);                }            }        } catch (Exception e) {            logger.log(Level.SEVERE, "parse WebServiceProcessor config error.", e);        }     }     public void release(IPluginModule module) throws ExtensionException {     } }  <extension-point id="com.agent.ws" name="webService" processor="com.agent.extension.ws.WebServiceProcessor"></extension-point><extension point="com.agent.ws">   <webService  class="com.agent.plugin.ws.impl.DomainAdminServiceImpl" />   <webService  class="com.agent.plugin.ws.impl.ScriptAdminServiceImpl" /></extension>  /*** 插件执行器接口*/public interface IPluginActuator {        /**     * 插件执行器初始化     * @param context 应用程序上下文     * @throws ActuatorException 操作失败时抛出该异常     */    public void init(ApplicationContext context) throws ActuatorException;        /**     * 插件执行器根据一个插件定义模型执行相应的运行操作     * @param definition 插件定义模型     * @throws ActuatorException 操作失败时抛出该异常     */    public void start(IPluginModule module) throws ActuatorException;        /**     * 插件执行器根据一个插件定义模型执行相应的停止操作     * @param definition 插件定义模型     * @throws ActuatorException 操作失败时抛出该异常     */    public void stop(IPluginModule module) throws ActuatorException;        /**     * 插件执行器执行释放操作     * @param context 应用程序上下文     * @throws ActuatorException 操作失败时抛出该异常     */    public void dispose(ApplicationContext context) throws ActuatorException;}    *** 扩展点执行器,维护扩展点的的生命周期,如Workbench应用启动时 加载扩展点资源和Workbench应用关闭时的销毁扩展点资源。*/public class ExtensionActuator implements IPluginActuator {private static final Logger logger = LoggerManager.getLogger(ExtensionActuator.class);; private static final String TAG_PLUGIN     = "plugin";private static final String TAG_EXTENSION  = "extension";private static final String ATTR_POINT     = "point";private static final String TAG_EXTENSION_POINT = "extension-point";private static final String ATTR_ID        = "id";private static final String ATTR_NAME      = "name";private static final String ATTR_PROCESSOR = "processor"; private ApplicationContext context;private LinkedSetMap<String, ProcessorInfo> processors = new LinkedSetMap<String, ProcessorInfo>();public void init(ApplicationContext context) throws ActuatorException {this.context = context;ExtensionContext extensionContext = ExtensionContext.getInstance();extensionContext.setApplicationContext(context);extensionContext.setExtensionRegistry(new ExtensionRegistry());}public void start(IPluginModule module) throws ActuatorException {IPluginDefinition definition = module.getPluginDefinition();if (definition == null) {return;}try {Document doc = DocumentDescriptor.INSTANCE.loadPluginXML(definition.getPath()); Map<String, IProcessSource> cache = new HashMap<String, IProcessSource>();// 处理扩展点资源processExtensions(module, doc, cache);// 处理扩展点processExtensionPoints(module, doc, cache);} catch (Exception e) {throw new ActuatorException("", e);}} public void stop(IPluginModule module) throws ActuatorException {String pluginName = module.getPluginName();Collection<ProcessorInfo> infos = processors.get(pluginName); if (infos != null) {infos = new ArrayList<ProcessorInfo>(infos); // array safe copyCollections.reverse((ArrayList) infos);for (ProcessorInfo info : infos) {try {info.getProcessor().release(module);logger.info(String.format("Extension-[%s] release finish", info.getId()));} finally {processors.remove(pluginName, info);}}}} public void dispose(ApplicationContext context) throws ActuatorException {ExtensionContext extensionContext = ExtensionContext.getInstance();if (extensionContext != null) {logger.info("Excute extension context dispose");extensionContext.dispose();}processors = null;} /*** 将对应扩展点的扩展点配置集合分类*/protected void processExtensions(IPluginModule module, Document doc, Map<String, IProcessSource> cache) throws ExtensionLoadException {if (doc == null) {return;} NodeList extensions = null;try {extensions = W3CDomUtil.selectNodes(doc, "/" + TAG_PLUGIN + "/" + TAG_EXTENSION);} catch (XPathExpressionException e) {throw new ExtensionLoadException(e);} for (int i = 0, size = extensions == null ? 0 : extensions.getLength(); i < size; i++) {Node extension = extensions.item(i);String point = W3CDomUtil.getAttributeValueTrim(extension, ATTR_POINT);System.out.println("~~~扩展点~~~~~~~~"+point);if (point != null) {ProcessSource ps = (ProcessSource) cache.get(point);if (ps == null) {ps = new ProcessSource();cache.put(point, ps);}ps.addNode(extension);}}} protected void processExtensionPoints(IPluginModule module, Document doc, Map<String, IProcessSource> elements) throws ActuatorException {if (doc == null) {return;}NodeList extensionPoints = null;try {extensionPoints = W3CDomUtil.selectNodes(doc, "/" + TAG_PLUGIN + "/" + TAG_EXTENSION_POINT);} catch (XPathExpressionException e) {throw new ActuatorException(e);} for (int i = 0, size = extensionPoints == null ? 0 : extensionPoints.getLength(); i < size; i++) {Node extensionPoint = extensionPoints.item(i);String id        = W3CDomUtil.getAttributeValueTrim(extensionPoint, ATTR_ID);String name      = W3CDomUtil.getAttributeValueTrim(extensionPoint, ATTR_NAME);String processor = W3CDomUtil.getAttributeValueTrim(extensionPoint, ATTR_PROCESSOR);System.out.println("~~扩展点~~~id ~~"+id+"~~name~~"+name+"~~processor~~"+processor);if (id != null && processor == null) {throw new ActuatorException(String.format("Could not find any extension processor for extension[%s]", id));} else if (id != null && processor != null) {IExtensionProcessor extProc = null;try {Class<?> cls = ClassUtil.loadClass(processor, IExtensionProcessor.class, module.getPluginLoader());extProc = (IExtensionProcessor) cls.newInstance();} catch (Exception e) {throw new ActuatorException(String.format("Fail in creating extension processor of extension[%s]", id), e);}IProcessSource ps = elements.get(id);if (ps == null) {ps = new EmptyProcessSource();((EmptyProcessSource) ps).setPluginModule(module);((EmptyProcessSource) ps).setName(name);} else {((ProcessSource) ps).setPluginModule(module);((ProcessSource) ps).setName(name);}extProc.process(ps);// 只有正常完成加载的扩展点才会被缓存processors.put(module.getPluginName(), new ProcessorInfo(id, extProc));logger.info(String.format("Extension-[%s] process finish", id));}}} private static class ProcessorInfo {String id;IExtensionProcessor processor; public ProcessorInfo(String id, IExtensionProcessor processor) {this.id = id;this.processor = processor;} public String getId() {return id;} public IExtensionProcessor getProcessor() {return processor;}} private static class EmptyProcessSource implements IProcessSource {private String name;private IPluginModule module; void setPluginModule(IPluginModule module) {this.module = module;} public IPluginModule getPluginModule() {return module;} public void setName(String name) {this.name = name;} public String getName() {return name;} public Collection<Node> getProcessNodes() {return Collections.EMPTY_LIST;}}} /*** 该类负责启动和停止插件的激活器IPluginActivator* @author Administrator**/public class ActivateActuator implements IPluginActuator {private static final Logger logger = LoggerManager.getLogger(ActivateActuator.class);  public void init(ApplicationContext context) throws ActuatorException {//do nothing}/*** 启动插件的激活器,调用插件的配置的IPluginActivator的实现*/public void start(IPluginModule module) throws ActuatorException {IPluginActivator activator = module.getPluginActivator();if (activator == null) {return;}try {activator.start();logger.info(String.format("Plugin-[%s] start finish", module.getPluginName()));} catch (Exception e) {throw new ActuatorException(String.format("Excute plugin[%s] activator start fail", module.getPluginName()),e);}} public void stop(IPluginModule module) throws ActuatorException {IPluginActivator activator = module.getPluginActivator();if (activator == null) {return;}try {activator.stop();logger.info(String.format("Plugin-[%s] stop finish", module.getPluginName()));} catch (Exception e) {throw new ActuatorException(String.format("Excute plugin[%s] activator stop fail", module.getPluginName()), e);}} public void dispose(ApplicationContext context) throws ActuatorException {// do nothing}}

原创粉丝点击