设为首页 收藏本站
查看: 901|回复: 0

[经验分享] constellio——基于solr的开源搜索引擎系统源码研究(五)

[复制链接]

尚未签到

发表于 2015-7-18 09:08:57 | 显示全部楼层 |阅读模式
  插件工厂类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 classLoaders = new HashSet();
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() {
@Override
public boolean accept(File pathname) {
boolean accept;
if (pathname.isFile()) {
accept = false;
} else if (DefaultConstellioPlugin.NAME.equals(pathname)) {
accept = true;
} else {
List availablePluginNames = ConstellioSpringUtils.getAvailablePluginNames();
accept = availablePluginNames.contains(pathname.getName());
}
return accept;
}
});
for (File pluginDir : pluginDirs) {
// Plugin root dir jars
Collection pluginJarFiles = FileUtils.listFiles(pluginDir, new String[] { "jar" },
false);
// Accept only one root dir jar
File 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 pluginDependencies = FileUtils.listFiles(pluginLibDir,
new String[] { "jar" }, false);
ClassPathManagerUtils.addJarDependencies(classPathManager, pluginJarFile,
pluginDependencies);
}
}
}
}
}
public static  P getPlugin(Class pluginClass) {
return getPlugin(pluginClass, false);
}
public static  P getDefaultPlugin(Class pluginClass) {
return getPlugin(pluginClass, true);
}
private static  P getPlugin(Class pluginClass, boolean onlyDefault) {
List matches = getPlugins(pluginClass, onlyDefault);
return !matches.isEmpty() ? matches.get(0) : null;
}
public static  List getPlugins(Class pluginClass) {
return getPlugins(pluginClass, false);
}
public static  List getDefaultPlugins(Class pluginClass) {
return getPlugins(pluginClass, true);
}
private static  List getPlugins(Class pluginClass, boolean onlyDefault) {
List matches = new ArrayList();
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() {
@Override
public int compare(ConstellioPlugin o1, ConstellioPlugin o2) {
List 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 pluginJarFiles = FileUtils.listFiles(pluginDir, new String[] { "jar" }, false);
// Accept only one root dir jar
File pluginJarFile = pluginJarFiles.isEmpty() ? null : pluginJarFiles.iterator().next();
if (pluginJarFile != null) {
validPlugin = true;
} else {
validPlugin = false;
}
}
return validPlugin;
}
public static Set 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 getExtraClassLoaders() {
Set extraClassLoaders = PluginFactory.getClassLoaders();
extraClassLoaders.add(PluginAwareClassLoader.class.getClassLoader());
return extraClassLoaders;
}
@Override
protected 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;
}
@Override
protected 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;
}
@Override
protected Enumeration findResources(String name) throws IOException {
Enumeration 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.iyunv.com/chenying99/archive/2013/04/01/2994529.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-87866-1-1.html 上篇帖子: Solr服务器配置自动索引 下篇帖子: 将数据库的数据导入solr索引库中
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表