插件工厂类PluginFactory.java

/*** Constellio, Open Source Enterprise Search* Copyright (C) 2010 DocuLibre inc.** This copyrighted material is made available to anyone wishing to use, modify,* copy, or redistribute it subject to the terms and conditions of the GNU* Lesser General Public License, as published by the Free Software Foundation.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License* for more details.** You should have received a copy of the GNU Lesser General Public License* along with this distribution; if not, write to:* Free Software Foundation, Inc.* 51 Franklin Street, Fifth Floor* Boston, MA  02110-1301  USA*/
package com.doculibre.constellio.plugins;import java.io.File;
import java.io.FileFilter;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;import net.xeoh.plugins.base.PluginManager;
import net.xeoh.plugins.base.impl.PluginManagerFactory;
import net.xeoh.plugins.base.impl.PluginManagerImpl;
import net.xeoh.plugins.base.impl.classpath.ClassPathManager;
import net.xeoh.plugins.base.impl.classpath.ClassPathManagerUtils;
import net.xeoh.plugins.base.util.PluginManagerUtil;import org.apache.commons.io.FileUtils;import com.doculibre.constellio.plugins.api.ConstellioPlugin;
import com.doculibre.constellio.plugins.defaults.DefaultConstellioPlugin;
import com.doculibre.constellio.utils.ClasspathUtils;
import com.doculibre.constellio.utils.ConstellioSpringUtils;public class PluginFactory {private static Set<ClassLoader> classLoaders = new HashSet<ClassLoader>();private static PluginManager pm;@SuppressWarnings("unchecked")private static void initPluginManager() {if (pm == null) {pm = PluginManagerFactory.createPluginManager();File classesDir = ClasspathUtils.getClassesDir();pm.addPluginsFrom(classesDir.toURI());File pluginsDir = getPluginsDir();File[] pluginDirs = pluginsDir.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {boolean accept;if (pathname.isFile()) {accept = false;} else if (DefaultConstellioPlugin.NAME.equals(pathname)) {accept = true;} else {List<String> availablePluginNames = ConstellioSpringUtils.getAvailablePluginNames();accept = availablePluginNames.contains(pathname.getName());}return accept;}});for (File pluginDir : pluginDirs) {// Plugin root dir jarsCollection<File> pluginJarFiles = FileUtils.listFiles(pluginDir, new String[] { "jar" },false);// Accept only one root dir jarFile pluginJarFile = pluginJarFiles.isEmpty() ? null : pluginJarFiles.iterator().next();if (pluginJarFile != null) {URI pluginJarFileURI = pluginJarFile.toURI();pm.addPluginsFrom(pluginJarFileURI);PluginManagerImpl pmImpl = (PluginManagerImpl) pm;ClassPathManager classPathManager = pmImpl.getClassPathManager();ClassLoader classLoader = ClassPathManagerUtils.getClassLoader(classPathManager,pluginJarFile);classLoaders.add(classLoader);File pluginLibDir = new File(pluginDir, "lib");if (pluginLibDir.exists() && pluginLibDir.isDirectory()) {Collection<File> pluginDependencies = FileUtils.listFiles(pluginLibDir,new String[] { "jar" }, false);ClassPathManagerUtils.addJarDependencies(classPathManager, pluginJarFile,pluginDependencies);}}}}}public static <P extends ConstellioPlugin> P getPlugin(Class<P> pluginClass) {return getPlugin(pluginClass, false);}public static <P extends ConstellioPlugin> P getDefaultPlugin(Class<P> pluginClass) {return getPlugin(pluginClass, true);}private static <P extends ConstellioPlugin> P getPlugin(Class<P> pluginClass, boolean onlyDefault) {List<P> matches = getPlugins(pluginClass, onlyDefault);return !matches.isEmpty() ? matches.get(0) : null;}public static <P extends ConstellioPlugin> List<P> getPlugins(Class<P> pluginClass) {return getPlugins(pluginClass, false);}public static <P extends ConstellioPlugin> List<P> getDefaultPlugins(Class<P> pluginClass) {return getPlugins(pluginClass, true);}private static <P extends ConstellioPlugin> List<P> getPlugins(Class<P> pluginClass, boolean onlyDefault) {List<P> matches = new ArrayList<P>();P defaultPlugin = null;initPluginManager();PluginManagerUtil pmu = new PluginManagerUtil(pm);for (P impl : pmu.getPlugins(pluginClass)) {// ClassLoader classLoader = impl.getClass().getClassLoader();// classLoaders.add(classLoader);if (DefaultConstellioPlugin.NAME.equals(impl.getName())) {defaultPlugin = impl;} else if (!onlyDefault) {matches.add(impl);}}if (matches.isEmpty()) {if (defaultPlugin != null) {matches.add(defaultPlugin);}} else {// If many plugins are found, they are sorted in the order they are configured in constellio.xml// (the last has priority over the previous)Collections.sort(matches, new Comparator<ConstellioPlugin>() {@Overridepublic int compare(ConstellioPlugin o1, ConstellioPlugin o2) {List<String> availablePluginNames = ConstellioSpringUtils.getAvailablePluginNames();Integer indexOfPluginName1 = availablePluginNames.indexOf(o1.getName());Integer indexOfPluginName2 = availablePluginNames.indexOf(o2.getName());return indexOfPluginName1.compareTo(indexOfPluginName2);}});}return matches;}public static File getPluginsDir() {File webinfDir = ClasspathUtils.getWebinfDir();return new File(webinfDir, "plugins");}@SuppressWarnings("unchecked")public static boolean isValidPlugin(String name) {boolean validPlugin;File pluginsDir = getPluginsDir();File pluginDir = new File(pluginsDir, name);if (!pluginDir.exists() || pluginDir.isFile()) {validPlugin = false;} else {Collection<File> pluginJarFiles = FileUtils.listFiles(pluginDir, new String[] { "jar" }, false);// Accept only one root dir jarFile pluginJarFile = pluginJarFiles.isEmpty() ? null : pluginJarFiles.iterator().next();if (pluginJarFile != null) {validPlugin = true;} else {validPlugin = false;}}return validPlugin;}public static Set<ClassLoader> getClassLoaders() {initPluginManager();return classLoaders;}}

自定义类加载器PluginAwareClassLoader.java

/*** Constellio, Open Source Enterprise Search* Copyright (C) 2010 DocuLibre inc.** This copyrighted material is made available to anyone wishing to use, modify,* copy, or redistribute it subject to the terms and conditions of the GNU* Lesser General Public License, as published by the Free Software Foundation.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License* for more details.** You should have received a copy of the GNU Lesser General Public License* along with this distribution; if not, write to:* Free Software Foundation, Inc.* 51 Franklin Street, Fifth Floor* Boston, MA  02110-1301  USA*/
package com.doculibre.constellio.plugins;import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;public class PluginAwareClassLoader extends ClassLoader {private ClassLoader defaultClassLoader;public PluginAwareClassLoader() {defaultClassLoader = Thread.currentThread().getContextClassLoader();while (defaultClassLoader instanceof PluginAwareClassLoader) {PluginAwareClassLoader nested = (PluginAwareClassLoader) defaultClassLoader;defaultClassLoader = nested.getDefaultClassLoader();}}public ClassLoader getDefaultClassLoader() {return defaultClassLoader;}public void setDefaultClassLoader(ClassLoader defaultClassLoader) {this.defaultClassLoader = defaultClassLoader;}protected Set<ClassLoader> getExtraClassLoaders() {Set<ClassLoader> extraClassLoaders = PluginFactory.getClassLoaders();extraClassLoaders.add(PluginAwareClassLoader.class.getClassLoader());return extraClassLoaders;}@Overrideprotected Class<?> findClass(String name) throws ClassNotFoundException {Class<?> clazz;Throwable throwable;try {clazz = defaultClassLoader.loadClass(name);throwable = null;} catch (Throwable t) {clazz = null;throwable = t;}if (clazz == null) {for (ClassLoader extraClassLoader : getExtraClassLoaders()) {try {clazz = extraClassLoader.loadClass(name);if (clazz != null) {break;}} catch (ClassNotFoundException e) {// Ignore, already handled
                }}}if (clazz == null && throwable != null) {if (throwable instanceof ClassNotFoundException) {throw (ClassNotFoundException) throwable;} else if (throwable instanceof RuntimeException) {throw (RuntimeException) throwable;} else {throw new RuntimeException(throwable);}} return clazz;}@Overrideprotected URL findResource(String name) {URL resource;Throwable throwable;try {resource = defaultClassLoader.getResource(name);throwable = null;} catch (Throwable t) {resource = null;throwable = t;}if (resource == null) {for (ClassLoader extraClassLoader : getExtraClassLoaders()) {resource = extraClassLoader.getResource(name);if (resource != null) {break;}}}if (resource == null && throwable != null) {if (throwable instanceof RuntimeException) {throw (RuntimeException) throwable;} else {throw new RuntimeException(throwable);}} return resource;}@Overrideprotected Enumeration<URL> findResources(String name) throws IOException {Enumeration<URL> resources;Throwable throwable;try {resources = defaultClassLoader.getResources(name);throwable = null;} catch (Throwable t) {resources = null;throwable = t;}if (resources == null) {for (ClassLoader extraClassLoader : getExtraClassLoaders()) {resources = extraClassLoader.getResources(name);if (resources != null) {break;}}}if (resources == null && throwable != null) {if (throwable instanceof IOException) {throw (IOException) throwable;} else if (throwable instanceof RuntimeException) {throw (RuntimeException) throwable;} else {throw new RuntimeException(throwable);}} return resources;}}

插件框架参考 http://www.cnblogs.com/chenying99/archive/2013/04/01/2994529.html

转载于:https://www.cnblogs.com/chenying99/archive/2013/02/17/2914886.html

constellio——基于solr的开源搜索引擎系统源码研究(五)相关推荐

  1. 鸿蒙开源源码,基于鸿蒙系统开源项目OpenHarmony源码静态分析

    #ifndef __scc #define __scc(X) ((long) (X)) // 转为long类型 typedef long syscall_arg_t; #endif #define _ ...

  2. 二改注册登录版素材代下载搜索引擎系统源码,自带火车头采集

    简介: 二改注册登录版/素材代下载搜索引擎系统/自带火车头采集接口/源码素材付费下载系统 程序原创开发,对接易,码,百度搜狗关键词:源码代下,代下源码,均在首页 缺点:这个用户登录必须对接QQ互联登录 ...

  3. crm开源系统 tp框架_thinkphp6学习教程与源码 tp6开源CMS系统源码研究

    thinkphp6最新正式版框架上市已经有一段时间了,从官方的介绍来看,tp6的框架和tp5有很大的区别,完全重新改写了底层架构代码和逻辑,所以不支持thinkphp5的无缝升级,也就是说如果你之前的 ...

  4. thinkphp仿素材火教程_thinkphp6学习教程与源码 tp6开源CMS系统源码研究

    thinkphp6最新正式版框架上市已经有一段时间了,从官方的介绍来看,tp6的框架和tp5有很大的区别,完全重新改写了底层架构代码和逻辑,所以不支持thinkphp5的无缝升级,也就是说如果你之前的 ...

  5. thinkphp6学习教程与源码 tp6开源CMS系统源码研究

    thinkphp6最新正式版框架上市已经有一段时间了,从官方的介绍来看,tp6的框架和tp5有很大的区别,完全重新改写了底层架构代码和逻辑,所以不支持thinkphp5的无缝升级,也就是说如果你之前的 ...

  6. 20款开源搜索引擎系统

    20款开源搜索引擎系统 一些开源搜索引擎系统介绍,包含开源Web搜索引擎和开源桌面搜索引擎. Sphider Sphider是一个轻量级,采用PHP开发的web spider和搜索引擎,使用mysql ...

  7. 基于Solr的空间搜索学习笔记

    基于Solr的空间搜索学习笔记 在Solr中基于空间地址查询主要围绕2个概念实现: (1) Cartesian Tiers 笛卡尔层 Cartesian Tiers是通过将一个平面地图的根据设定的层次 ...

  8. HanLP自然语言处理包开源(包含源码)

    HanLP自然语言处理包开源(包含源码) 支持中文分词(N-最短路分词.CRF分词.索引分词.用户自定义词典.词性标注),命名实体识别(中国人名.音译人名.日本人名.地名.实体机构名识别),关键词提取 ...

  9. 真正开源的商城源码推荐

    **这里推荐一些真正开源的商城源码- ▲ phpShop phpShop是一个基于PHP的网上商店系统.phpShop虽然比其它网上购物系统简单,而且功能少.但是它非常灵活可让你按自己的个性化要求进行 ...

最新文章

  1. vs code 列编辑模式
  2. TaintDroid介绍
  3. tomcat架构分析(valve机制)
  4. VS2017编译可在Win2000上运行的程序
  5. TIS教程02-模型
  6. php+条件限定符,const 限定符
  7. 解决 avformat_alloc_context无法识别的问题
  8. 看到程序员坐在电脑前,如何判断程序员正在做什么?只需看这些
  9. 使用 Fiddler 分析网页加载过程
  10. 阿里云产品介绍(一):云服务器ECS
  11. axure文本框添加水印_Axure如何给元件添加注释?
  12. 8.字典操作。建立一个月份与天数的字典monthdays,月份为“Jan”“Feb”“Mar”“Apr”“May”“Jun” “Jul” “Aug” “Sep” “Oct” “Nov“ “Dec“
  13. 数据库 | MitoPhen 数据库:基于人体表型进行线粒体 DNA 疾病诊断
  14. 【Python报错】ComplexWarning: Casting complex values to real discards the imaginary part
  15. jmeter正则表达式提取器
  16. Json Web Token(JWT)
  17. 安装向日葵后,还是连不上问题
  18. WIN10安装CH340驱动出现感叹号的解决办法总结
  19. 5G核心网网络架构-什么是核心网?核心网的功能有哪些?
  20. TCP在线多用户聊天室

热门文章

  1. VUE 2 无法监听数组和对象的某些变化
  2. linux内核进程抢占,Re: Linux中进程能否被抢占
  3. 内涝预测过程的噪音_提高人工智能模型准确率的测试过程中需要注意什么?
  4. 霍夫变换检测圆c 语言,c++ 霍夫变换检测直线
  5. hht时频谱 matlab 乱序_MATLAB信号频谱分析FFT详解
  6. SQLServer left join 出现比左表多的数据
  7. springboot中mongodb自定义类型转换器
  8. java环境卡顿_解决Emacs在windows使用的问题,比如环境设置、卡顿……
  9. rtsp 获取视频流 java_OpenCV - 如何捕获rtsp视频流
  10. 纤程 java_JAVA协程 纤程 与Quasar 框架