📄 pluginrepository.java
字号:
/* /** * Copyright 2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.nutch.plugin;// JDK importsimport java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.WeakHashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.regex.Pattern;// Commons Logging importsimport org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;// Hadoop importsimport org.apache.hadoop.conf.Configuration;// Nutch importsimport org.apache.nutch.util.NutchConfiguration;/** * The plugin repositority is a registry of all plugins. * * At system boot up a repositority is builded by parsing the mainifest files of * all plugins. Plugins that require not existing other plugins are not * registed. For each plugin a plugin descriptor instance will be created. The * descriptor represents all meta information about a plugin. So a plugin * instance will be created later when it is required, this allow lazy plugin * loading. * * @author joa23 */public class PluginRepository { private static final WeakHashMap CACHE = new WeakHashMap(); private boolean auto; private List fRegisteredPlugins; private HashMap fExtensionPoints; private HashMap fActivatedPlugins; private Configuration conf; public static final Log LOG = LogFactory.getLog(PluginRepository.class); /** * @throws PluginRuntimeException * @see java.lang.Object#Object() */ public PluginRepository(Configuration conf) throws RuntimeException { fActivatedPlugins = new HashMap(); fExtensionPoints = new HashMap(); this.conf = conf; this.auto = conf.getBoolean("plugin.auto-activation", true); String[] pluginFolders = conf.getStrings("plugin.folders"); PluginManifestParser manifestParser = new PluginManifestParser(conf, this); Map allPlugins =manifestParser.parsePluginFolder(pluginFolders); Pattern excludes = Pattern.compile(conf.get( "plugin.excludes", "")); Pattern includes = Pattern.compile(conf.get( "plugin.includes", "")); Map filteredPlugins = filter(excludes, includes, allPlugins); fRegisteredPlugins = getDependencyCheckedPlugins( filteredPlugins, this.auto ? allPlugins : filteredPlugins); installExtensionPoints(fRegisteredPlugins); try { installExtensions(fRegisteredPlugins); } catch (PluginRuntimeException e) { if (LOG.isFatalEnabled()) { LOG.fatal(e.toString()); } throw new RuntimeException(e.getMessage()); } displayStatus(); } /** * @return a cached instance of the plugin repository */ public static synchronized PluginRepository get(Configuration conf) { PluginRepository result = (PluginRepository)CACHE.get(conf); if (result == null) { result = new PluginRepository(conf); CACHE.put(conf, result); } return result; } private void installExtensionPoints(List plugins) { if (plugins == null) { return; } for (int i=0; i<plugins.size(); i++) { PluginDescriptor plugin = (PluginDescriptor) plugins.get(i); ExtensionPoint[] points = plugin.getExtenstionPoints(); for (int j=0; j<points.length; j++) { ExtensionPoint point = points[j]; String xpId = point.getId(); if (LOG.isDebugEnabled()) { LOG.debug("Adding extension point " + xpId); } fExtensionPoints.put(xpId, point); } } } /** * @param pRegisteredPlugins */ private void installExtensions(List pRegisteredPlugins) throws PluginRuntimeException { for (int i = 0; i < pRegisteredPlugins.size(); i++) { PluginDescriptor descriptor = (PluginDescriptor) pRegisteredPlugins.get(i); Extension[] extensions = descriptor.getExtensions(); for (int j = 0; j < extensions.length; j++) { Extension extension = extensions[j]; String xpId = extension.getTargetPoint(); ExtensionPoint point = getExtensionPoint(xpId); if (point == null) { throw new PluginRuntimeException( "Plugin (" + descriptor.getPluginId() + "), " + "extension point: " + xpId + " does not exist."); } point.addExtension(extension); } } } private void getPluginCheckedDependencies(PluginDescriptor plugin, Map plugins, Map dependencies, Map branch) throws MissingDependencyException, CircularDependencyException { if (dependencies == null) { dependencies = new HashMap(); } if (branch == null) { branch = new HashMap(); } branch.put(plugin.getPluginId(), plugin); // Get the plugin dependencies String[] ids = plugin.getDependencies(); // Otherwise, checks each dependency for (int i=0; i<ids.length; i++) { String id = ids[i]; PluginDescriptor dependency = (PluginDescriptor) plugins.get(id); if (dependency == null) { throw new MissingDependencyException( "Missing dependency " + id + " for plugin " + plugin.getPluginId()); } if (branch.containsKey(id)) { throw new CircularDependencyException( "Circular dependency detected " + id + " for plugin " + plugin.getPluginId()); } dependencies.put(id, dependency); getPluginCheckedDependencies((PluginDescriptor) plugins.get(id), plugins, dependencies, branch); } branch.remove(plugin.getPluginId()); } private Map getPluginCheckedDependencies(PluginDescriptor plugin, Map plugins) throws MissingDependencyException, CircularDependencyException { Map dependencies = new HashMap(); Map branch = new HashMap(); getPluginCheckedDependencies(plugin, plugins, dependencies, branch); return dependencies; } /** * @param filtered is the list of plugin filtred * @param all is the list of all plugins found. * @return List */ private List getDependencyCheckedPlugins(Map filtered, Map all) { if (filtered == null) { return null; } Map checked = new HashMap(); Iterator iter = filtered.values().iterator(); while (iter.hasNext()) { PluginDescriptor plugin = (PluginDescriptor) iter.next(); try { checked.putAll(getPluginCheckedDependencies(plugin, all)); checked.put(plugin.getPluginId(), plugin); } catch (MissingDependencyException mde) { // Simply ignore this plugin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -