Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

cleaned up package to use generics and java5 loops, removed unnecessary log guards, fixed indentation

Sami Sirencd00889fb593
diff --git a/src/java/org/apache/nutch/plugin/CircularDependencyException.java b/src/java/org/apache/nutch/plugin/CircularDependencyException.java
index 20fda7086..443e53f25 100644
--- a/src/java/org/apache/nutch/plugin/CircularDependencyException.java
+++ b/src/java/org/apache/nutch/plugin/CircularDependencyException.java
@@ -1,5 +1,4 @@
/*
-/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +24,8 @@ package org.apache.nutch.plugin;
*/
public class CircularDependencyException extends Exception {
+ private static final long serialVersionUID = 1L;
+
public CircularDependencyException(Throwable cause) {
super(cause);
}
diff --git a/src/java/org/apache/nutch/plugin/Extension.java b/src/java/org/apache/nutch/plugin/Extension.java
index 1b7667682..70f38d318 100644
--- a/src/java/org/apache/nutch/plugin/Extension.java
+++ b/src/java/org/apache/nutch/plugin/Extension.java
@@ -1,5 +1,4 @@
/*
-/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,10 +14,12 @@
* limitations under the License.
*/
package org.apache.nutch.plugin;
+
import java.util.HashMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configurable;
+
/**
* An <code>Extension</code> is a kind of listener descriptor that will be
* installed on a concrete <code>ExtensionPoint</code> that acts as kind of
@@ -31,20 +32,22 @@ public class Extension {
private String fId;
private String fTargetPoint;
private String fClazz;
- private HashMap fAttributes;
+ private HashMap<String, String> fAttributes;
private Configuration conf;
private PluginRepository pluginRepository;
+
/**
* @param pDescriptor
- * a plugin descriptor
+ * a plugin descriptor
* @param pExtensionPoint
- * an extension porin
+ * an extension porin
* @param pId
- * an unique id of the plugin
+ * an unique id of the plugin
*/
public Extension(PluginDescriptor pDescriptor, String pExtensionPoint,
- String pId, String pExtensionClass, Configuration conf, PluginRepository pluginRepository) {
- fAttributes = new HashMap();
+ String pId, String pExtensionClass, Configuration conf,
+ PluginRepository pluginRepository) {
+ fAttributes = new HashMap<String, String>();
setDescriptor(pDescriptor);
setExtensionPoint(pExtensionPoint);
setId(pId);
@@ -52,23 +55,26 @@ public class Extension {
this.conf = conf;
this.pluginRepository = pluginRepository;
}
+
/**
* @param point
*/
private void setExtensionPoint(String point) {
fTargetPoint = point;
}
+
/**
* Returns a attribute value, that is setuped in the manifest file and is
* definied by the extension point xml schema.
*
* @param pKey
- * a key
+ * a key
* @return String a value
*/
public String getAttribute(String pKey) {
- return (String) fAttributes.get(pKey);
+ return fAttributes.get(pKey);
}
+
/**
* Returns the full class name of the extension point implementation
*
@@ -77,6 +83,7 @@ public class Extension {
public String getClazz() {
return fClazz;
}
+
/**
* Return the unique id of the extension.
*
@@ -85,38 +92,38 @@ public class Extension {
public String getId() {
return fId;
}
+
/**
* Adds a attribute and is only used until model creation at plugin system
* start up.
*
- * @param pKey
- * a key
- * @param pValue
- * a value
+ * @param pKey a key
+ * @param pValue a value
*/
public void addAttribute(String pKey, String pValue) {
fAttributes.put(pKey, pValue);
}
+
/**
- * Sets the Class that implement the concret extension and is only used
- * until model creation at system start up.
+ * Sets the Class that implement the concret extension and is only used until
+ * model creation at system start up.
*
- * @param extensionClazz
- * The extensionClazz to set
+ * @param extensionClazz The extensionClasname to set
*/
public void setClazz(String extensionClazz) {
fClazz = extensionClazz;
}
+
/**
* Sets the unique extension Id and is only used until model creation at
* system start up.
*
- * @param extensionID
- * The extensionID to set
+ * @param extensionID The extensionID to set
*/
public void setId(String extensionID) {
fId = extensionID;
}
+
/**
* Returns the Id of the extension point, that is implemented by this
* extension.
@@ -124,14 +131,15 @@ public class Extension {
public String getTargetPoint() {
return fTargetPoint;
}
+
/**
* Return an instance of the extension implementatio. Before we create a
* extension instance we startup the plugin if it is not already done. The
* plugin instance and the extension instance use the same
- * <code>PluginClassLoader</code>. Each Plugin use its own classloader.
- * The PluginClassLoader knows only own <i>Plugin runtime libraries </i>
- * setuped in the plugin manifest file and exported libraries of the
- * depenedend plugins.
+ * <code>PluginClassLoader</code>. Each Plugin use its own classloader. The
+ * PluginClassLoader knows only own <i>Plugin runtime libraries </i> setuped
+ * in the plugin manifest file and exported libraries of the depenedend
+ * plugins.
*
* @return Object An instance of the extension implementation
*/
@@ -149,8 +157,8 @@ public class Extension {
// already.
this.pluginRepository.getPluginInstance(getDescriptor());
Object object = extensionClazz.newInstance();
- if(object instanceof Configurable) {
- ((Configurable)object).setConf(this.conf);
+ if (object instanceof Configurable) {
+ ((Configurable) object).setConf(this.conf);
}
return object;
} catch (ClassNotFoundException e) {
@@ -163,13 +171,6 @@ public class Extension {
}
}
- /**
- * @deprecated Use #{getDescriptor()} instead.
- */
- public PluginDescriptor getDiscriptor() {
- return getDescriptor();
- }
-
/**
* return the plugin descriptor.
*
@@ -178,6 +179,7 @@ public class Extension {
public PluginDescriptor getDescriptor() {
return fDescriptor;
}
+
/**
* Sets the plugin descriptor and is only used until model creation at system
* start up.
diff --git a/src/java/org/apache/nutch/plugin/ExtensionPoint.java b/src/java/org/apache/nutch/plugin/ExtensionPoint.java
index 2d7043c83..6bffea97c 100644
--- a/src/java/org/apache/nutch/plugin/ExtensionPoint.java
+++ b/src/java/org/apache/nutch/plugin/ExtensionPoint.java
@@ -1,5 +1,4 @@
/*
-/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +15,7 @@
*/
package org.apache.nutch.plugin;
import java.util.ArrayList;
+
/**
* The <code>ExtensionPoint</code> provide meta information of a extension
* point.
@@ -26,23 +26,25 @@ public class ExtensionPoint {
private String ftId;
private String fName;
private String fSchema;
- private ArrayList fExtensions;
+ private ArrayList<Extension> fExtensions;
+
/**
* Constructor
*
* @param pId
- * unique extension point Id
+ * unique extension point Id
* @param pName
- * name of the extension poin
+ * name of the extension poin
* @param pSchema
- * xml schema of the extension point
+ * xml schema of the extension point
*/
public ExtensionPoint(String pId, String pName, String pSchema) {
setId(pId);
setName(pName);
setSchema(pSchema);
- fExtensions = new ArrayList();
+ fExtensions = new ArrayList<Extension>();
}
+
/**
* Returns the unique id of the extension point.
*
@@ -51,6 +53,7 @@ public class ExtensionPoint {
public String getId() {
return ftId;
}
+
/**
* Returns the name of the extension point.
*
@@ -59,6 +62,7 @@ public class ExtensionPoint {
public String getName() {
return fName;
}
+
/**
* Returns a path to the xml schema of a extension point.
*
@@ -67,6 +71,7 @@ public class ExtensionPoint {
public String getSchema() {
return fSchema;
}
+
/**
* Sets the extensionPointId.
*
@@ -75,6 +80,7 @@ public class ExtensionPoint {
private void setId(String pId) {
ftId = pId;
}
+
/**
* Sets the extension point name.
*
@@ -83,6 +89,7 @@ public class ExtensionPoint {
private void setName(String pName) {
fName = pName;
}
+
/**
* Sets the schema.
*
@@ -91,31 +98,23 @@ public class ExtensionPoint {
private void setSchema(String pSchema) {
fSchema = pSchema;
}
+
/**
- * Install a coresponding extension to this extension point.
+ * Install a coresponding extension to this extension point.
*
* @param extension
*/
public void addExtension(Extension extension) {
fExtensions.add(extension);
}
+
/**
* Returns a array of extensions that lsiten to this extension point
*
* @return Extension[]
*/
public Extension[] getExtensions() {
- return (Extension[]) fExtensions.toArray(new Extension[fExtensions
- .size()]);
+ return fExtensions.toArray(new Extension[fExtensions.size()]);
}
- /**
- * Returns a array of extensions that lsiten to this extension point
- *
- * @return Extension[]
- * @deprecated Use the correctly spelled #{getExtensions} method instead.
- */
- public Extension[] getExtentens() {
- return getExtensions();
- }
}
diff --git a/src/java/org/apache/nutch/plugin/MissingDependencyException.java b/src/java/org/apache/nutch/plugin/MissingDependencyException.java
index 9fce5fe91..0f51569cf 100644
--- a/src/java/org/apache/nutch/plugin/MissingDependencyException.java
+++ b/src/java/org/apache/nutch/plugin/MissingDependencyException.java
@@ -1,5 +1,4 @@
/*
-/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +15,6 @@
*/
package org.apache.nutch.plugin;
-
/**
* <code>MissingDependencyException</code> will be thrown if a plugin
* dependency cannot be found.
@@ -25,6 +23,8 @@ package org.apache.nutch.plugin;
*/
public class MissingDependencyException extends Exception {
+ private static final long serialVersionUID = 1L;
+
public MissingDependencyException(Throwable cause) {
super(cause);
}
diff --git a/src/java/org/apache/nutch/plugin/Pluggable.java b/src/java/org/apache/nutch/plugin/Pluggable.java
index 094468c12..0711ded26 100644
--- a/src/java/org/apache/nutch/plugin/Pluggable.java
+++ b/src/java/org/apache/nutch/plugin/Pluggable.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
diff --git a/src/java/org/apache/nutch/plugin/Plugin.java b/src/java/org/apache/nutch/plugin/Plugin.java
index b066294a7..3c5ef4f12 100644
--- a/src/java/org/apache/nutch/plugin/Plugin.java
+++ b/src/java/org/apache/nutch/plugin/Plugin.java
@@ -1,5 +1,4 @@
/*
-/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -44,28 +43,33 @@ public class Plugin {
/**
* Constructor
- *
+ *
*/
public Plugin(PluginDescriptor pDescriptor, Configuration conf) {
setDescriptor(pDescriptor);
this.conf = conf;
}
+
/**
* Will be invoked until plugin start up. Since the nutch-plugin system use
* lazy loading the start up is invoked until the first time a extension is
* used.
*
* @throws PluginRuntimeException
- * If the startup was without successs.
+ * If the startup was without successs.
*/
- public void startUp() throws PluginRuntimeException {}
+ public void startUp() throws PluginRuntimeException {
+ }
+
/**
* Shutdown the plugin. This happens until nutch will be stopped.
*
* @throws PluginRuntimeException
- * if a problems occurs until shutdown the plugin.
+ * if a problems occurs until shutdown the plugin.
*/
- public void shutDown() throws PluginRuntimeException {}
+ public void shutDown() throws PluginRuntimeException {
+ }
+
/**
* Returns the plugin descriptor
*
@@ -74,18 +78,14 @@ public class Plugin {
public PluginDescriptor getDescriptor() {
return fDescriptor;
}
+
/**
- * @param descriptor
- * The descriptor to set
+ * @param descriptor The descriptor to set
*/
private void setDescriptor(PluginDescriptor descriptor) {
fDescriptor = descriptor;
}
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#finalize()
- */
+
protected void finalize() throws Throwable {
super.finalize();
shutDown();
diff --git a/src/java/org/apache/nutch/plugin/PluginClassLoader.java b/src/java/org/apache/nutch/plugin/PluginClassLoader.java
index b4908a8b9..f6e5e73dc 100644
--- a/src/java/org/apache/nutch/plugin/PluginClassLoader.java
+++ b/src/java/org/apache/nutch/plugin/PluginClassLoader.java
@@ -1,5 +1,4 @@
/*
-/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,8 +14,10 @@
* limitations under the License.
*/
package org.apache.nutch.plugin;
+
import java.net.URL;
import java.net.URLClassLoader;
+
/**
* The <code>PluginClassLoader</code> contains only classes of the runtime
* libraries setuped in the plugin manifest file and exported libraries of
@@ -32,8 +33,8 @@ public class PluginClassLoader extends URLClassLoader {
* Construtor
*
* @param urls
- * Array of urls with own libraries and all exported libraries of
- * plugins that are required to this plugin
+ * Array of urls with own libraries and all exported libraries of
+ * plugins that are required to this plugin
* @param parent
*/
public PluginClassLoader(URL[] urls, ClassLoader parent) {
diff --git a/src/java/org/apache/nutch/plugin/PluginDescriptor.java b/src/java/org/apache/nutch/plugin/PluginDescriptor.java
index 4b66fc258..43fdff693 100644
--- a/src/java/org/apache/nutch/plugin/PluginDescriptor.java
+++ b/src/java/org/apache/nutch/plugin/PluginDescriptor.java
@@ -1,5 +1,4 @@
/*
-/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +15,6 @@
*/
package org.apache.nutch.plugin;
-// JDK imports
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
@@ -26,16 +24,10 @@ import java.util.HashMap;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
-import java.util.logging.Logger;
-
-// Commons Logging imports
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-
-// Hadoop imports
import org.apache.hadoop.conf.Configuration;
-
/**
* The <code>PluginDescriptor</code> provide access to all meta information of
* a nutch-plugin, as well to the internationalizable resources and the plugin
@@ -54,15 +46,15 @@ public class PluginDescriptor {
private String fName;
private String fProviderName;
private HashMap fMessages = new HashMap();
- private ArrayList fExtensionPoints = new ArrayList();
- private ArrayList fDependencies = new ArrayList();
- private ArrayList fExportedLibs = new ArrayList();
- private ArrayList fNotExportedLibs = new ArrayList();
- private ArrayList fExtensions = new ArrayList();
+ private ArrayList<ExtensionPoint> fExtensionPoints = new ArrayList<ExtensionPoint>();
+ private ArrayList<String> fDependencies = new ArrayList<String>();
+ private ArrayList<URL> fExportedLibs = new ArrayList<URL>();
+ private ArrayList<URL> fNotExportedLibs = new ArrayList<URL>();
+ private ArrayList<Extension> fExtensions = new ArrayList<Extension>();
private PluginClassLoader fClassLoader;
public static final Log LOG = LogFactory.getLog(PluginDescriptor.class);
private Configuration fConf;
-
+
/**
* Constructor
*
@@ -74,7 +66,8 @@ public class PluginDescriptor {
* @param pPath
*/
public PluginDescriptor(String pId, String pVersion, String pName,
- String pProviderName, String pPluginclazz, String pPath, Configuration conf) {
+ String pProviderName, String pPluginclazz, String pPath,
+ Configuration conf) {
setPath(pPath);
setPluginId(pId);
setVersion(pVersion);
@@ -86,12 +79,14 @@ public class PluginDescriptor {
this.fConf = conf;
}
+
/**
* @param pPath
*/
private void setPath(String pPath) {
fPluginPath = pPath;
}
+
/**
* Returns the name of the plugin.
*
@@ -100,33 +95,38 @@ public class PluginDescriptor {
public String getName() {
return fName;
}
+
/**
* @param providerName
*/
private void setProvidername(String providerName) {
fProviderName = providerName;
}
+
/**
* @param name
*/
private void setName(String name) {
fName = name;
}
+
/**
* @param version
*/
private void setVersion(String version) {
fVersion = version;
}
+
/**
- * Returns the fully qualified name of the class which implements the
- * abstarct <code>Plugin</code> class.
+ * Returns the fully qualified name of the class which implements the abstarct
+ * <code>Plugin</code> class.
*
* @return the name of this plug-in's runtime class or <code>null</code>.
*/
public String getPluginClass() {
return fPluginClass;
}
+
/**
* Returns the unique identifier of the plug-in or <code>null</code>.
*
@@ -135,15 +135,16 @@ public class PluginDescriptor {
public String getPluginId() {
return fPluginId;
}
+
/**
* Returns an array of extensions.
*
* @return Exception[]
*/
public Extension[] getExtensions() {
- return (Extension[]) fExtensions.toArray(new Extension[fExtensions
- .size()]);
+ return fExtensions.toArray(new Extension[fExtensions.size()]);
}
+
/**
* Adds a extension.
*
@@ -152,24 +153,27 @@ public class PluginDescriptor {
public void addExtension(Extension pExtension) {
fExtensions.add(pExtension);
}
+
/**
* Sets the pluginClass.
*
* @param pluginClass
- * The pluginClass to set
+ * The pluginClass to set
*/
private void setPluginClass(String pluginClass) {
fPluginClass = pluginClass;
}
+
/**
* Sets the plugin Id.
*
* @param pluginId
- * The pluginId to set
+ * The pluginId to set
*/
private void setPluginId(String pluginId) {
fPluginId = pluginId;
}
+
/**
* Adds a extension point.
*
@@ -178,43 +182,46 @@ public class PluginDescriptor {
public void addExtensionPoint(ExtensionPoint extensionPoint) {
fExtensionPoints.add(extensionPoint);
}
+
/**
* Returns a array of extension points.
*
* @return ExtensionPoint[]
*/
public ExtensionPoint[] getExtenstionPoints() {
- return (ExtensionPoint[]) fExtensionPoints
- .toArray(new ExtensionPoint[fExtensionPoints.size()]);
+ return fExtensionPoints
+ .toArray(new ExtensionPoint[fExtensionPoints.size()]);
}
+
/**
* Returns a array of plugin ids.
*
* @return String[]
*/
public String[] getDependencies() {
- return (String[]) fDependencies
- .toArray(new String[fDependencies.size()]);
+ return fDependencies.toArray(new String[fDependencies.size()]);
}
+
/**
* Adds a dependency
*
- * @param pId
- * id of the dependent plugin
+ * @param pId id of the dependent plugin
*/
public void addDependency(String pId) {
fDependencies.add(pId);
}
+
/**
* Adds a exported library with a relative path to the plugin directory.
*
* @param pLibPath
*/
public void addExportedLibRelative(String pLibPath)
- throws MalformedURLException {
+ throws MalformedURLException {
URL url = new File(getPluginPath() + File.separator + pLibPath).toURL();
fExportedLibs.add(url);
}
+
/**
* Returns the directory path of the plugin.
*
@@ -223,94 +230,93 @@ public class PluginDescriptor {
public String getPluginPath() {
return fPluginPath;
}
+
/**
* Returns a array exported librareis as URLs
*
* @return URL[]
*/
public URL[] getExportedLibUrls() {
- return (URL[]) fExportedLibs.toArray(new URL[0]);
+ return fExportedLibs.toArray(new URL[0]);
}
+
/**
* Adds a not exported library with a plugin directory relative path.
*
* @param pLibPath
*/
public void addNotExportedLibRelative(String pLibPath)
- throws MalformedURLException {
+ throws MalformedURLException {
URL url = new File(getPluginPath() + File.separator + pLibPath).toURL();
fNotExportedLibs.add(url);
}
+
/**
* Returns a array of libraries as URLs that are not exported by the plugin.
*
* @return URL[]
*/
public URL[] getNotExportedLibUrls() {
- return (URL[]) fNotExportedLibs
- .toArray(new URL[fNotExportedLibs.size()]);
+ return fNotExportedLibs.toArray(new URL[fNotExportedLibs.size()]);
}
+
/**
* Returns a cached classloader for a plugin. Until classloader creation all
- * needed libraries are collected. A classloader use as first the plugins
- * own libraries and add then all exported libraries of dependend plugins.
+ * needed libraries are collected. A classloader use as first the plugins own
+ * libraries and add then all exported libraries of dependend plugins.
*
* @return PluginClassLoader the classloader for the plugin
*/
public PluginClassLoader getClassLoader() {
if (fClassLoader != null)
return fClassLoader;
- ArrayList arrayList = new ArrayList();
+ ArrayList<URL> arrayList = new ArrayList<URL>();
arrayList.addAll(fExportedLibs);
arrayList.addAll(fNotExportedLibs);
arrayList.addAll(getDependencyLibs());
File file = new File(getPluginPath());
- File[] files = file.listFiles();
try {
- for (int i = 0; i < files.length; i++) {
- File file2 = files[i];
+ for (File file2 : file.listFiles()) {
if (file2.getAbsolutePath().endsWith("properties"))
arrayList.add(file2.getParentFile().toURL());
}
} catch (MalformedURLException e) {
- if (LOG.isDebugEnabled()) {
- LOG.debug(getPluginId() + " " + e.toString());
- }
+ LOG.debug(getPluginId() + " " + e.toString());
}
- URL[] urls = (URL[]) arrayList.toArray(new URL[arrayList.size()]);
+ URL[] urls = arrayList.toArray(new URL[arrayList.size()]);
fClassLoader = new PluginClassLoader(urls, PluginDescriptor.class
- .getClassLoader());
+ .getClassLoader());
return fClassLoader;
}
+
/**
* @return Collection
*/
- private ArrayList getDependencyLibs() {
- ArrayList list = new ArrayList();
+ private ArrayList<URL> getDependencyLibs() {
+ ArrayList<URL> list = new ArrayList<URL>();
collectLibs(list, this);
return list;
}
+
/**
* @param pLibs
* @param pDescriptor
*/
- private void collectLibs(ArrayList pLibs, PluginDescriptor pDescriptor) {
- String[] pPluginIds = pDescriptor.getDependencies();
- for (int i = 0; i < pPluginIds.length; i++) {
- String id = pPluginIds[i];
- PluginDescriptor descriptor = PluginRepository.get(fConf).getPluginDescriptor(id);
- URL[] libs = descriptor.getExportedLibUrls();
- for (int j = 0; j < libs.length; j++) {
- URL url = libs[j];
+ private void collectLibs(ArrayList<URL> pLibs, PluginDescriptor pDescriptor) {
+
+ for (String id : pDescriptor.getDependencies()) {
+ PluginDescriptor descriptor = PluginRepository.get(fConf)
+ .getPluginDescriptor(id);
+ for (URL url: descriptor.getExportedLibUrls()) {
pLibs.add(url);
}
collectLibs(pLibs, descriptor);
}
}
+
/**
- * Returns a I18N'd resource string. The resource bundles could
- * be stored in root directory of a plugin in the well know i18n file name
- * conventions.
+ * Returns a I18N'd resource string. The resource bundles could be stored in
+ * root directory of a plugin in the well know i18n file name conventions.
*
* @param pKey
* @param pLocale
@@ -318,10 +324,10 @@ public class PluginDescriptor {
* @throws IOException
*/
public String getResourceString(String pKey, Locale pLocale)
- throws IOException {
+ throws IOException {
if (fMessages.containsKey(pLocale.toString())) {
- ResourceBundle bundle = (ResourceBundle) fMessages.get(pLocale
- .toString());
+ ResourceBundle bundle = (ResourceBundle) fMessages
+ .get(pLocale.toString());
try {
return bundle.getString(pKey);
} catch (MissingResourceException e) {
@@ -330,7 +336,7 @@ public class PluginDescriptor {
}
try {
ResourceBundle res = ResourceBundle.getBundle("messages", pLocale,
- getClassLoader());
+ getClassLoader());
return res.getString(pKey);
} catch (MissingResourceException x) {
return '!' + pKey + '!';
@@ -340,7 +346,7 @@ public class PluginDescriptor {
public String getProviderName() {
return fProviderName;
}
-
+
public String getVersion() {
return fVersion;
}
diff --git a/src/java/org/apache/nutch/plugin/PluginManifestParser.java b/src/java/org/apache/nutch/plugin/PluginManifestParser.java
index 57e9fb8dd..fd937d2a8 100644
--- a/src/java/org/apache/nutch/plugin/PluginManifestParser.java
+++ b/src/java/org/apache/nutch/plugin/PluginManifestParser.java
@@ -1,5 +1,4 @@
/*
- /**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -45,289 +44,261 @@ import org.xml.sax.SAXException;
* @author joa23
*/
public class PluginManifestParser {
- public static final Log LOG = PluginRepository.LOG;
+ private static final String ATTR_NAME = "name";
+ private static final String ATTR_CLASS = "class";
+ private static final String ATTR_ID = "id";
- private static final boolean WINDOWS = System.getProperty("os.name")
- .startsWith("Windows");
+ public static final Log LOG = PluginRepository.LOG;
- private Configuration conf;
+ private static final boolean WINDOWS = System.getProperty("os.name")
+ .startsWith("Windows");
- private PluginRepository pluginRepository;
+ private Configuration conf;
- public PluginManifestParser(Configuration conf, PluginRepository pluginRepository) {
- this.conf = conf;
- this.pluginRepository = pluginRepository;
- }
-
-
- /**
- * Returns a list with plugin descriptors.
- *
- * @return ArrayList
- *
- */
- public Map parsePluginFolder(String[] pluginFolders) {
- Map map = new HashMap();
+ private PluginRepository pluginRepository;
- if (pluginFolders == null) {
- throw new IllegalArgumentException("plugin.folders is not defined");
- }
-
- for (int i = 0; i < pluginFolders.length; i++) {
- String name = pluginFolders[i];
- File directory = getPluginFolder(name);
- if (directory == null) { continue; }
- if (LOG.isInfoEnabled()) {
- LOG.info("Plugins: looking in: " + directory.getAbsolutePath());
- }
- File[] files = directory.listFiles();
- if (files == null)
- continue;
- for (int j = 0; j < files.length; j++) {
- File oneSubFolder = files[j];
- if (oneSubFolder.isDirectory()) {
- String manifestPath = oneSubFolder.getAbsolutePath()
- + File.separator + "plugin.xml";
- try {
- if (LOG.isDebugEnabled()) {
- LOG.debug("parsing: " + manifestPath);
- }
- PluginDescriptor p = parseManifestFile(manifestPath);
- map.put(p.getPluginId(), p);
- } catch (MalformedURLException e) {
- if (LOG.isWarnEnabled()) { LOG.warn(e.toString()); }
- } catch (SAXException e) {
- if (LOG.isWarnEnabled()) { LOG.warn(e.toString()); }
- } catch (IOException e) {
- if (LOG.isWarnEnabled()) { LOG.warn(e.toString()); }
- } catch (ParserConfigurationException e) {
- if (LOG.isWarnEnabled()) { LOG.warn(e.toString()); }
- }
- }
- }
- }
- return map;
+ public PluginManifestParser(Configuration conf,
+ PluginRepository pluginRepository) {
+ this.conf = conf;
+ this.pluginRepository = pluginRepository;
+ }
+
+ /**
+ * Returns a list of all found plugin descriptors.
+ *
+ * @param pluginFolders
+ * folders to search plugins from
+ * @return
+ */
+ public Map<String, PluginDescriptor> parsePluginFolder(String[] pluginFolders) {
+ Map<String, PluginDescriptor> map = new HashMap<String, PluginDescriptor>();
+
+ if (pluginFolders == null) {
+ throw new IllegalArgumentException("plugin.folders is not defined");
}
- /**
- * Return the named plugin folder. If the name is absolute then it is
- * returned. Otherwise, for relative names, the classpath is scanned.
- */
- public File getPluginFolder(String name) {
- File directory = new File(name);
- if (!directory.isAbsolute()) {
- URL url = PluginManifestParser.class.getClassLoader().getResource(
- name);
- if (url == null && directory.exists()
- && directory.isDirectory() && directory.listFiles().length > 0) {
- return directory; // relative path that is not in the classpath
- } else if (url == null) {
- if (LOG.isWarnEnabled()) {
- LOG.warn("Plugins: directory not found: " + name);
- }
- return null;
- } else if (!"file".equals(url.getProtocol())) {
- if (LOG.isWarnEnabled()) {
- LOG.warn("Plugins: not a file: url. Can't load plugins from: "
- + url);
- }
- return null;
- }
- String path = url.getPath();
- if (WINDOWS && path.startsWith("/")) // patch a windows bug
- path = path.substring(1);
- try {
- path = URLDecoder.decode(path, "UTF-8"); // decode the url path
- } catch (UnsupportedEncodingException e) {
- }
- directory = new File(path);
+ for (String name : pluginFolders) {
+ File directory = getPluginFolder(name);
+ if (directory == null) {
+ continue;
+ }
+ LOG.info("Plugins: looking in: " + directory.getAbsolutePath());
+ for (File oneSubFolder : directory.listFiles()) {
+ if (oneSubFolder.isDirectory()) {
+ String manifestPath = oneSubFolder.getAbsolutePath() + File.separator
+ + "plugin.xml";
+ try {
+ LOG.debug("parsing: " + manifestPath);
+ PluginDescriptor p = parseManifestFile(manifestPath);
+ map.put(p.getPluginId(), p);
+ } catch (MalformedURLException e) {
+ LOG.warn(e.toString());
+ } catch (SAXException e) {
+ LOG.warn(e.toString());
+ } catch (IOException e) {
+ LOG.warn(e.toString());
+ } catch (ParserConfigurationException e) {
+ LOG.warn(e.toString());
+ }
}
- return directory;
+ }
}
+ return map;
+ }
- /**
- * @param manifestPath
- * @throws ParserConfigurationException
- * @throws IOException
- * @throws SAXException
- * @throws MalformedURLException
- */
- private PluginDescriptor parseManifestFile(String pManifestPath)
- throws MalformedURLException, SAXException, IOException,
- ParserConfigurationException {
- Document document = parseXML(new File(pManifestPath).toURL());
- String pPath = new File(pManifestPath).getParent();
- return parsePlugin(document, pPath);
+ /**
+ * Return the named plugin folder. If the name is absolute then it is
+ * returned. Otherwise, for relative names, the classpath is scanned.
+ */
+ public File getPluginFolder(String name) {
+ File directory = new File(name);
+ if (!directory.isAbsolute()) {
+ URL url = PluginManifestParser.class.getClassLoader().getResource(name);
+ if (url == null && directory.exists() && directory.isDirectory()
+ && directory.listFiles().length > 0) {
+ return directory; // relative path that is not in the classpath
+ } else if (url == null) {
+ LOG.warn("Plugins: directory not found: " + name);
+ return null;
+ } else if (!"file".equals(url.getProtocol())) {
+ LOG.warn("Plugins: not a file: url. Can't load plugins from: " + url);
+ return null;
+ }
+ String path = url.getPath();
+ if (WINDOWS && path.startsWith("/")) // patch a windows bug
+ path = path.substring(1);
+ try {
+ path = URLDecoder.decode(path, "UTF-8"); // decode the url path
+ } catch (UnsupportedEncodingException e) {
+ }
+ directory = new File(path);
}
+ return directory;
+ }
- /**
- * @param url
- * @return Document
- * @throws IOException
- * @throws SAXException
- * @throws ParserConfigurationException
- * @throws DocumentException
- */
- private Document parseXML(URL url) throws SAXException, IOException,
- ParserConfigurationException {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- return builder.parse(url.openStream());
- }
+ /**
+ * @param manifestPath
+ * @throws ParserConfigurationException
+ * @throws IOException
+ * @throws SAXException
+ * @throws MalformedURLException
+ */
+ private PluginDescriptor parseManifestFile(String pManifestPath)
+ throws MalformedURLException, SAXException, IOException,
+ ParserConfigurationException {
+ Document document = parseXML(new File(pManifestPath).toURL());
+ String pPath = new File(pManifestPath).getParent();
+ return parsePlugin(document, pPath);
+ }
- /**
- * @param pDocument
- * @throws MalformedURLException
- */
- private PluginDescriptor parsePlugin(Document pDocument, String pPath)
- throws MalformedURLException {
- Element rootElement = pDocument.getDocumentElement();
- String id = rootElement.getAttribute("id");
- String name = rootElement.getAttribute("name");
- String version = rootElement.getAttribute("version");
- String providerName = rootElement.getAttribute("provider-name");
- String pluginClazz = null;
- if (rootElement.getAttribute("class").trim().length() > 0) {
- pluginClazz = rootElement.getAttribute("class");
- }
- PluginDescriptor pluginDescriptor = new PluginDescriptor(id, version,
- name, providerName, pluginClazz, pPath, this.conf);
- if (LOG.isDebugEnabled()) {
- LOG.debug("plugin: id="+id+" name="+name+" version="+version
- +" provider="+providerName+"class="+pluginClazz);
- }
- parseExtension(rootElement, pluginDescriptor);
- parseExtensionPoints(rootElement, pluginDescriptor);
- parseLibraries(rootElement, pluginDescriptor);
- parseRequires(rootElement, pluginDescriptor);
- return pluginDescriptor;
+ /**
+ * @param url
+ * @return Document
+ * @throws IOException
+ * @throws SAXException
+ * @throws ParserConfigurationException
+ * @throws DocumentException
+ */
+ private Document parseXML(URL url) throws SAXException, IOException,
+ ParserConfigurationException {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ return builder.parse(url.openStream());
+ }
+
+ /**
+ * @param pDocument
+ * @throws MalformedURLException
+ */
+ private PluginDescriptor parsePlugin(Document pDocument, String pPath)
+ throws MalformedURLException {
+ Element rootElement = pDocument.getDocumentElement();
+ String id = rootElement.getAttribute(ATTR_ID);
+ String name = rootElement.getAttribute(ATTR_NAME);
+ String version = rootElement.getAttribute("version");
+ String providerName = rootElement.getAttribute("provider-name");
+ String pluginClazz = null;
+ if (rootElement.getAttribute(ATTR_CLASS).trim().length() > 0) {
+ pluginClazz = rootElement.getAttribute(ATTR_CLASS);
}
+ PluginDescriptor pluginDescriptor = new PluginDescriptor(id, version, name,
+ providerName, pluginClazz, pPath, this.conf);
+ LOG.debug("plugin: id=" + id + " name=" + name + " version=" + version
+ + " provider=" + providerName + "class=" + pluginClazz);
+ parseExtension(rootElement, pluginDescriptor);
+ parseExtensionPoints(rootElement, pluginDescriptor);
+ parseLibraries(rootElement, pluginDescriptor);
+ parseRequires(rootElement, pluginDescriptor);
+ return pluginDescriptor;
+ }
- /**
- * @param pRootElement
- * @param pDescriptor
- * @throws MalformedURLException
- */
- private void parseRequires(Element pRootElement,
- PluginDescriptor pDescriptor)
- throws MalformedURLException {
-
- NodeList nodelist = pRootElement.getElementsByTagName("requires");
- if (nodelist.getLength() > 0) {
+ /**
+ * @param pRootElement
+ * @param pDescriptor
+ * @throws MalformedURLException
+ */
+ private void parseRequires(Element pRootElement, PluginDescriptor pDescriptor)
+ throws MalformedURLException {
- Element requires = (Element) nodelist.item(0);
+ NodeList nodelist = pRootElement.getElementsByTagName("requires");
+ if (nodelist.getLength() > 0) {
- NodeList imports = requires.getElementsByTagName("import");
- for (int i=0; i<imports.getLength(); i++) {
- Element anImport = (Element) imports.item(i);
- String plugin = anImport.getAttribute("plugin");
- if (plugin != null) {
- pDescriptor.addDependency(plugin);
- }
- }
+ Element requires = (Element) nodelist.item(0);
+
+ NodeList imports = requires.getElementsByTagName("import");
+ for (int i = 0; i < imports.getLength(); i++) {
+ Element anImport = (Element) imports.item(i);
+ String plugin = anImport.getAttribute("plugin");
+ if (plugin != null) {
+ pDescriptor.addDependency(plugin);
}
+ }
}
+ }
- /**
- * @param pRootElement
- * @param pDescriptor
- * @throws MalformedURLException
- */
- private void parseLibraries(Element pRootElement,
- PluginDescriptor pDescriptor) throws MalformedURLException {
- NodeList nodelist = pRootElement.getElementsByTagName("runtime");
- if (nodelist.getLength() > 0) {
+ /**
+ * @param pRootElement
+ * @param pDescriptor
+ * @throws MalformedURLException
+ */
+ private void parseLibraries(Element pRootElement, PluginDescriptor pDescriptor)
+ throws MalformedURLException {
+ NodeList nodelist = pRootElement.getElementsByTagName("runtime");
+ if (nodelist.getLength() > 0) {
- Element runtime = (Element) nodelist.item(0);
+ Element runtime = (Element) nodelist.item(0);
- NodeList libraries = runtime.getElementsByTagName("library");
- for (int i = 0; i < libraries.getLength(); i++) {
- Element library = (Element) libraries.item(i);
- String libName = library.getAttribute("name");
- NodeList list = library.getElementsByTagName("export");
- Element exportElement = (Element) list.item(0);
- if (exportElement != null)
- pDescriptor.addExportedLibRelative(libName);
- else
- pDescriptor.addNotExportedLibRelative(libName);
- }
- }
+ NodeList libraries = runtime.getElementsByTagName("library");
+ for (int i = 0; i < libraries.getLength(); i++) {
+ Element library = (Element) libraries.item(i);
+ String libName = library.getAttribute(ATTR_NAME);
+ NodeList list = library.getElementsByTagName("export");
+ Element exportElement = (Element) list.item(0);
+ if (exportElement != null)
+ pDescriptor.addExportedLibRelative(libName);
+ else
+ pDescriptor.addNotExportedLibRelative(libName);
+ }
}
+ }
- /**
- * @param rootElement
- * @param pluginDescriptor
- */
- private void parseExtensionPoints(Element pRootElement,
- PluginDescriptor pPluginDescriptor) {
- NodeList list = pRootElement.getElementsByTagName("extension-point");
- if (list != null) {
- for (int i = 0; i < list.getLength(); i++) {
- Element oneExtensionPoint = (Element) list.item(i);
- String id = oneExtensionPoint.getAttribute("id");
- String name = oneExtensionPoint.getAttribute("name");
- String schema = oneExtensionPoint.getAttribute("schema");
- ExtensionPoint extensionPoint = new ExtensionPoint(id, name,
- schema);
- //if (LOG.isDebugEnabled()) { LOG.debug("plugin: point="+id); }
- pPluginDescriptor.addExtensionPoint(extensionPoint);
- }
- }
+ /**
+ * @param rootElement
+ * @param pluginDescriptor
+ */
+ private void parseExtensionPoints(Element pRootElement,
+ PluginDescriptor pPluginDescriptor) {
+ NodeList list = pRootElement.getElementsByTagName("extension-point");
+ if (list != null) {
+ for (int i = 0; i < list.getLength(); i++) {
+ Element oneExtensionPoint = (Element) list.item(i);
+ String id = oneExtensionPoint.getAttribute(ATTR_ID);
+ String name = oneExtensionPoint.getAttribute(ATTR_NAME);
+ String schema = oneExtensionPoint.getAttribute("schema");
+ ExtensionPoint extensionPoint = new ExtensionPoint(id, name, schema);
+ pPluginDescriptor.addExtensionPoint(extensionPoint);
+ }
}
+ }
- /**
- * @param rootElement
- * @param pluginDescriptor
- */
- private void parseExtension(Element pRootElement,
- PluginDescriptor pPluginDescriptor) {
- NodeList extensions = pRootElement.getElementsByTagName("extension");
- if (extensions != null) {
- for (int i = 0; i < extensions.getLength(); i++) {
- Element oneExtension = (Element) extensions.item(i);
- String pointId = oneExtension.getAttribute("point");
-
- NodeList extensionImplementations = oneExtension
- .getChildNodes();
- if (extensionImplementations != null) {
- for (int j = 0; j < extensionImplementations.getLength(); j++) {
- Node node = extensionImplementations.item(j);
- if (!node.getNodeName().equals("implementation")) {
- continue;
- }
- Element oneImplementation = (Element) node;
- String id = oneImplementation.getAttribute("id");
- String extensionClass = oneImplementation
- .getAttribute("class");
- if (LOG.isDebugEnabled()) {
- LOG.debug("impl: point=" + pointId + " class="
- + extensionClass);
- }
- Extension extension = new Extension(pPluginDescriptor,
- pointId, id, extensionClass, this.conf, this.pluginRepository);
- NodeList parameters = oneImplementation.getElementsByTagName("parameter");
- if (parameters != null) {
- for (int k=0; k<parameters.getLength(); k++) {
- Element param = (Element) parameters.item(k);
- extension.addAttribute(param.getAttribute("name"),
- param.getAttribute("value"));
- }
- }
- /*
- NamedNodeMap list = oneImplementation.getAttributes();
- for (int k = 0; k < list.getLength(); k++) {
- Node attribute = list.item(k);
- String name = attribute.getNodeName();
- if (name.equals("id") || name.equals("class"))
- continue;
- String value = attribute.getNodeValue();
- extension.addAttribute(name, value);
- }*/
- pPluginDescriptor.addExtension(extension);
- }
- }
+ /**
+ * @param rootElement
+ * @param pluginDescriptor
+ */
+ private void parseExtension(Element pRootElement,
+ PluginDescriptor pPluginDescriptor) {
+ NodeList extensions = pRootElement.getElementsByTagName("extension");
+ if (extensions != null) {
+ for (int i = 0; i < extensions.getLength(); i++) {
+ Element oneExtension = (Element) extensions.item(i);
+ String pointId = oneExtension.getAttribute("point");
+
+ NodeList extensionImplementations = oneExtension.getChildNodes();
+ if (extensionImplementations != null) {
+ for (int j = 0; j < extensionImplementations.getLength(); j++) {
+ Node node = extensionImplementations.item(j);
+ if (!node.getNodeName().equals("implementation")) {
+ continue;
+ }
+ Element oneImplementation = (Element) node;
+ String id = oneImplementation.getAttribute(ATTR_ID);
+ String extensionClass = oneImplementation.getAttribute(ATTR_CLASS);
+ LOG.debug("impl: point=" + pointId + " class=" + extensionClass);
+ Extension extension = new Extension(pPluginDescriptor, pointId, id,
+ extensionClass, this.conf, this.pluginRepository);
+ NodeList parameters = oneImplementation
+ .getElementsByTagName("parameter");
+ if (parameters != null) {
+ for (int k = 0; k < parameters.getLength(); k++) {
+ Element param = (Element) parameters.item(k);
+ extension.addAttribute(param.getAttribute(ATTR_NAME), param
+ .getAttribute("value"));
+ }
}
+ pPluginDescriptor.addExtension(extension);
+ }
}
+ }
}
-
+ }
}
diff --git a/src/java/org/apache/nutch/plugin/PluginRepository.java b/src/java/org/apache/nutch/plugin/PluginRepository.java
index a778d9bf4..aece0a4ab 100644
--- a/src/java/org/apache/nutch/plugin/PluginRepository.java
+++ b/src/java/org/apache/nutch/plugin/PluginRepository.java
@@ -1,5 +1,4 @@
/*
- /**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,29 +15,20 @@
*/
package org.apache.nutch.plugin;
-// JDK imports
import 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 imports
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-
-// Hadoop imports
import org.apache.hadoop.conf.Configuration;
-
-// Nutch imports
import org.apache.nutch.util.NutchConfiguration;
-
/**
* The plugin repositority is a registry of all plugins.
*
@@ -52,375 +42,383 @@ import org.apache.nutch.util.NutchConfiguration;
* @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();
+ private static final WeakHashMap<Configuration, PluginRepository> CACHE = new WeakHashMap<Configuration, PluginRepository>();
+
+ private boolean auto;
+
+ private List<PluginDescriptor> fRegisteredPlugins;
+
+ private HashMap<String, ExtensionPoint> fExtensionPoints;
+
+ private HashMap<String, Plugin> 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<String, Plugin>();
+ fExtensionPoints = new HashMap<String, ExtensionPoint>();
+ this.conf = conf;
+ this.auto = conf.getBoolean("plugin.auto-activation", true);
+ String[] pluginFolders = conf.getStrings("plugin.folders");
+ PluginManifestParser manifestParser = new PluginManifestParser(conf, this);
+ Map<String, PluginDescriptor> allPlugins = manifestParser
+ .parsePluginFolder(pluginFolders);
+ Pattern excludes = Pattern.compile(conf.get("plugin.excludes", ""));
+ Pattern includes = Pattern.compile(conf.get("plugin.includes", ""));
+ Map<String, PluginDescriptor> filteredPlugins = filter(excludes, includes,
+ allPlugins);
+ fRegisteredPlugins = getDependencyCheckedPlugins(filteredPlugins,
+ this.auto ? allPlugins : filteredPlugins);
+ installExtensionPoints(fRegisteredPlugins);
+ try {
+ installExtensions(fRegisteredPlugins);
+ } catch (PluginRuntimeException e) {
+ 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;
+ /**
+ * @return a cached instance of the plugin repository
+ */
+ public static synchronized PluginRepository get(Configuration conf) {
+ PluginRepository result = CACHE.get(conf);
+ if (result == null) {
+ result = new PluginRepository(conf);
+ CACHE.put(conf, result);
}
+ return result;
+ }
+ private void installExtensionPoints(List<PluginDescriptor> plugins) {
+ if (plugins == null) {
+ return;
+ }
- 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);
- }
+ for (PluginDescriptor plugin: plugins) {
+ for(ExtensionPoint point:plugin.getExtenstionPoints()) {
+ String xpId = point.getId();
+ LOG.debug("Adding extension point " + xpId);
+ fExtensionPoints.put(xpId, point);
}
}
-
- /**
- * @param pRegisteredPlugins
- */
- private void installExtensions(List pRegisteredPlugins)
+ }
+
+ /**
+ * @param pRegisteredPlugins
+ */
+ private void installExtensions(List<PluginDescriptor> 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());
+ for (PluginDescriptor descriptor : pRegisteredPlugins) {
+ for(Extension extension:descriptor.getExtensions()) {
+ String xpId = extension.getTargetPoint();
+ ExtensionPoint point = getExtensionPoint(xpId);
+ if (point == null) {
+ throw new PluginRuntimeException("Plugin ("
+ + descriptor.getPluginId() + "), " + "extension point: " + xpId
+ + " does not exist.");
}
- dependencies.put(id, dependency);
- getPluginCheckedDependencies((PluginDescriptor) plugins.get(id),
- plugins, dependencies, branch);
+ point.addExtension(extension);
}
-
- 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;
+ private void getPluginCheckedDependencies(PluginDescriptor plugin,
+ Map<String, PluginDescriptor> plugins,
+ Map<String, PluginDescriptor> dependencies,
+ Map<String, PluginDescriptor> branch) throws MissingDependencyException,
+ CircularDependencyException {
+
+ if (dependencies == null) {
+ dependencies = new HashMap<String, PluginDescriptor>();
}
-
- /**
- * @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
- if (LOG.isWarnEnabled()) { LOG.warn(mde.getMessage()); }
- } catch (CircularDependencyException cde) {
- // Simply ignore this plugin
- if (LOG.isWarnEnabled()) { LOG.warn(cde.getMessage()); }
- }
+ if (branch == null) {
+ branch = new HashMap<String, PluginDescriptor>();
+ }
+ branch.put(plugin.getPluginId(), plugin);
+
+ // Otherwise, checks each dependency
+ for(String id:plugin.getDependencies()) {
+ PluginDescriptor dependency = plugins.get(id);
+ if (dependency == null) {
+ throw new MissingDependencyException("Missing dependency " + id
+ + " for plugin " + plugin.getPluginId());
}
- return new ArrayList(checked.values());
+ if (branch.containsKey(id)) {
+ throw new CircularDependencyException("Circular dependency detected "
+ + id + " for plugin " + plugin.getPluginId());
+ }
+ dependencies.put(id, dependency);
+ getPluginCheckedDependencies(plugins.get(id), plugins, dependencies,
+ branch);
}
- /**
- * Returns all registed plugin descriptors.
- *
- * @return PluginDescriptor[]
- */
- public PluginDescriptor[] getPluginDescriptors() {
- return (PluginDescriptor[]) fRegisteredPlugins
- .toArray(new PluginDescriptor[fRegisteredPlugins.size()]);
+ branch.remove(plugin.getPluginId());
+ }
+
+ private Map<String, PluginDescriptor> getPluginCheckedDependencies(
+ PluginDescriptor plugin, Map<String, PluginDescriptor> plugins)
+ throws MissingDependencyException, CircularDependencyException {
+ Map<String, PluginDescriptor> dependencies = new HashMap<String, PluginDescriptor>();
+ Map<String, PluginDescriptor> branch = new HashMap<String, PluginDescriptor>();
+ 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<PluginDescriptor> getDependencyCheckedPlugins(
+ Map<String, PluginDescriptor> filtered, Map<String, PluginDescriptor> all) {
+ if (filtered == null) {
+ return null;
}
+ Map<String, PluginDescriptor> checked = new HashMap<String, PluginDescriptor>();
- /**
- * Returns the descriptor of one plugin identified by a plugin id.
- *
- * @param pPluginId
- * @return PluginDescriptor
- */
- public PluginDescriptor getPluginDescriptor(String pPluginId) {
- for (int i = 0; i < fRegisteredPlugins.size(); i++) {
- PluginDescriptor descriptor = (PluginDescriptor) fRegisteredPlugins
- .get(i);
- if (descriptor.getPluginId().equals(pPluginId))
- return descriptor;
+ for (PluginDescriptor plugin : filtered.values()) {
+ try {
+ checked.putAll(getPluginCheckedDependencies(plugin, all));
+ checked.put(plugin.getPluginId(), plugin);
+ } catch (MissingDependencyException mde) {
+ // Log exception and ignore plugin
+ LOG.warn(mde.getMessage());
+ } catch (CircularDependencyException cde) {
+ // Simply ignore this plugin
+ if (LOG.isWarnEnabled()) {
+ LOG.warn(cde.getMessage());
}
- return null;
+ }
}
+ return new ArrayList<PluginDescriptor>(checked.values());
+ }
- /**
- * Returns a extension point indentified by a extension point id.
- *
- * @param pXpId
- * @return a extentsion point
- */
- public ExtensionPoint getExtensionPoint(String pXpId) {
- return (ExtensionPoint) this.fExtensionPoints.get(pXpId);
- }
+ /**
+ * Returns all registed plugin descriptors.
+ *
+ * @return PluginDescriptor[]
+ */
+ public PluginDescriptor[] getPluginDescriptors() {
+ return fRegisteredPlugins.toArray(new PluginDescriptor[fRegisteredPlugins
+ .size()]);
+ }
- /**
- * Returns a instance of a plugin. Plugin instances are cached. So a plugin
- * exist only as one instance. This allow a central management of plugin own
- * resources.
- *
- * After creating the plugin instance the startUp() method is invoked. The
- * plugin use a own classloader that is used as well by all instance of
- * extensions of the same plugin. This class loader use all exported
- * libraries from the dependend plugins and all plugin libraries.
- *
- * @param pDescriptor
- * @return Plugin
- * @throws PluginRuntimeException
- */
- public Plugin getPluginInstance(PluginDescriptor pDescriptor)
- throws PluginRuntimeException {
- if (fActivatedPlugins.containsKey(pDescriptor.getPluginId()))
- return (Plugin) fActivatedPlugins.get(pDescriptor.getPluginId());
- try {
- // Must synchronize here to make sure creation and initialization
- // of a plugin instance are done by one and only one thread.
- // The same is in Extension.getExtensionInstance().
- // Suggested by Stefan Groschupf <[email protected]>
- synchronized (pDescriptor) {
- PluginClassLoader loader = pDescriptor.getClassLoader();
- Class pluginClass = loader.loadClass(pDescriptor
- .getPluginClass());
- Constructor constructor = pluginClass
- .getConstructor(new Class[] { PluginDescriptor.class, Configuration.class });
- Plugin plugin = (Plugin) constructor
- .newInstance(new Object[] { pDescriptor, this.conf });
- plugin.startUp();
- fActivatedPlugins.put(pDescriptor.getPluginId(), plugin);
- return plugin;
- }
- } catch (ClassNotFoundException e) {
- throw new PluginRuntimeException(e);
- } catch (InstantiationException e) {
- throw new PluginRuntimeException(e);
- } catch (IllegalAccessException e) {
- throw new PluginRuntimeException(e);
- } catch (NoSuchMethodException e) {
- throw new PluginRuntimeException(e);
- } catch (InvocationTargetException e) {
- throw new PluginRuntimeException(e);
- }
+ /**
+ * Returns the descriptor of one plugin identified by a plugin id.
+ *
+ * @param pPluginId
+ * @return PluginDescriptor
+ */
+ public PluginDescriptor getPluginDescriptor(String pPluginId) {
+
+ for (PluginDescriptor descriptor : fRegisteredPlugins) {
+ if (descriptor.getPluginId().equals(pPluginId))
+ return descriptor;
}
+ return null;
+ }
+
+ /**
+ * Returns a extension point indentified by a extension point id.
+ *
+ * @param pXpId
+ * @return a extentsion point
+ */
+ public ExtensionPoint getExtensionPoint(String pXpId) {
+ return this.fExtensionPoints.get(pXpId);
+ }
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#finalize()
- */
- public void finalize() throws Throwable {
- shotDownActivatedPlugins();
+ /**
+ * Returns a instance of a plugin. Plugin instances are cached. So a plugin
+ * exist only as one instance. This allow a central management of plugin own
+ * resources.
+ *
+ * After creating the plugin instance the startUp() method is invoked. The
+ * plugin use a own classloader that is used as well by all instance of
+ * extensions of the same plugin. This class loader use all exported libraries
+ * from the dependend plugins and all plugin libraries.
+ *
+ * @param pDescriptor
+ * @return Plugin
+ * @throws PluginRuntimeException
+ */
+ public Plugin getPluginInstance(PluginDescriptor pDescriptor)
+ throws PluginRuntimeException {
+ if (fActivatedPlugins.containsKey(pDescriptor.getPluginId()))
+ return fActivatedPlugins.get(pDescriptor.getPluginId());
+ try {
+ // Must synchronize here to make sure creation and initialization
+ // of a plugin instance are done by one and only one thread.
+ // The same is in Extension.getExtensionInstance().
+ // Suggested by Stefan Groschupf <[email protected]>
+ synchronized (pDescriptor) {
+ PluginClassLoader loader = pDescriptor.getClassLoader();
+ Class pluginClass = loader.loadClass(pDescriptor.getPluginClass());
+ Constructor constructor = pluginClass.getConstructor(new Class[] {
+ PluginDescriptor.class, Configuration.class });
+ Plugin plugin = (Plugin) constructor.newInstance(new Object[] {
+ pDescriptor, this.conf });
+ plugin.startUp();
+ fActivatedPlugins.put(pDescriptor.getPluginId(), plugin);
+ return plugin;
+ }
+ } catch (ClassNotFoundException e) {
+ throw new PluginRuntimeException(e);
+ } catch (InstantiationException e) {
+ throw new PluginRuntimeException(e);
+ } catch (IllegalAccessException e) {
+ throw new PluginRuntimeException(e);
+ } catch (NoSuchMethodException e) {
+ throw new PluginRuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new PluginRuntimeException(e);
}
+ }
- /**
- * @throws PluginRuntimeException
- */
- private void shotDownActivatedPlugins() throws PluginRuntimeException {
- Iterator iterator = fActivatedPlugins.keySet().iterator();
- while (iterator.hasNext()) {
- String pluginId = (String) iterator.next();
- Plugin object = (Plugin) fActivatedPlugins.get(pluginId);
- object.shutDown();
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#finalize()
+ */
+ public void finalize() throws Throwable {
+ shotDownActivatedPlugins();
+ }
+
+ /**
+ * Shuts down all plugins
+ *
+ * @throws PluginRuntimeException
+ */
+ private void shotDownActivatedPlugins() throws PluginRuntimeException {
+ for (Plugin plugin : fActivatedPlugins.values()) {
+ plugin.shutDown();
}
-
- private void displayStatus() {
-
- if (!LOG.isInfoEnabled()) { return; }
-
- LOG.info("Plugin Auto-activation mode: [" + this.auto + "]");
- LOG.info("Registered Plugins:");
-
- if ((fRegisteredPlugins == null) || (fRegisteredPlugins.size() == 0)) {
- LOG.info("\tNONE");
- } else {
- for (int i=0; i<fRegisteredPlugins.size(); i++) {
- PluginDescriptor plugin = (PluginDescriptor) fRegisteredPlugins.get(i);
- LOG.info("\t" + plugin.getName() + " (" + plugin.getPluginId() + ")");
- }
- }
+ }
- LOG.info("Registered Extension-Points:");
- if ((fExtensionPoints == null) || (fExtensionPoints.size() == 0)) {
- LOG.info("\tNONE");
- } else {
- Iterator iter = fExtensionPoints.values().iterator();
- while (iter.hasNext()) {
- ExtensionPoint ep = (ExtensionPoint) iter.next();
- LOG.info("\t" + ep.getName() + " (" + ep.getId() + ")");
- }
+ private void displayStatus() {
+ LOG.info("Plugin Auto-activation mode: [" + this.auto + "]");
+ LOG.info("Registered Plugins:");
+
+ if ((fRegisteredPlugins == null) || (fRegisteredPlugins.size() == 0)) {
+ LOG.info("\tNONE");
+ } else {
+ for (PluginDescriptor plugin : fRegisteredPlugins) {
+ LOG.info("\t" + plugin.getName() + " (" + plugin.getPluginId() + ")");
}
}
- /**
- * Filters a list of plugins.
- * The list of plugins is filtered regarding the configuration
- * properties <code>plugin.excludes</code> and <code>plugin.includes</code>.
- */
- private Map filter(Pattern excludes, Pattern includes, Map plugins) {
- Map map = new HashMap();
- if (plugins == null) { return map; }
-
- Iterator iter = plugins.values().iterator();
- while (iter.hasNext()) {
- PluginDescriptor plugin = (PluginDescriptor) iter.next();
- if (plugin == null) { continue; }
- String id = plugin.getPluginId();
- if (id == null) { continue; }
-
- if (!includes.matcher(id).matches()) {
- if (LOG.isDebugEnabled()) { LOG.debug("not including: " + id); }
- continue;
- }
- if (excludes.matcher(id).matches()) {
- if (LOG.isDebugEnabled()) { LOG.debug("excluding: " + id); }
- continue;
- }
- map.put(plugin.getPluginId(), plugin);
+
+ LOG.info("Registered Extension-Points:");
+ if ((fExtensionPoints == null) || (fExtensionPoints.size() == 0)) {
+ LOG.info("\tNONE");
+ } else {
+ for (ExtensionPoint ep : fExtensionPoints.values()) {
+ LOG.info("\t" + ep.getName() + " (" + ep.getId() + ")");
}
+ }
+ }
+
+ /**
+ * Filters a list of plugins. The list of plugins is filtered regarding the
+ * configuration properties <code>plugin.excludes</code> and
+ * <code>plugin.includes</code>.
+ *
+ * @param excludes
+ * @param includes
+ * @param plugins
+ * Map of plugins
+ * @return map of plugins matching the configuration
+ */
+ private Map<String, PluginDescriptor> filter(Pattern excludes,
+ Pattern includes, Map<String, PluginDescriptor> plugins) {
+
+ Map<String, PluginDescriptor> map = new HashMap<String, PluginDescriptor>();
+
+ if (plugins == null) {
return map;
}
- /**
- * Loads all necessary dependencies for a selected plugin, and then
- * runs one of the classes' main() method.
- * @param args plugin ID (needs to be activated in the configuration), and
- * the class name. The rest of arguments is passed to the main method of the
- * selected class.
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- if (args.length < 2) {
- System.err.println("Usage: PluginRepository pluginId className [arg1 arg2 ...]");
- return;
+ for (PluginDescriptor plugin : plugins.values()) {
+
+ if (plugin == null) {
+ continue;
}
- Configuration conf = NutchConfiguration.create();
- PluginRepository repo = new PluginRepository(conf);
- // args[0] - plugin ID
- PluginDescriptor d = repo.getPluginDescriptor(args[0]);
- if (d == null) {
- System.err.println("Plugin '" + args[0] + "' not present or inactive.");
- return;
+ String id = plugin.getPluginId();
+ if (id == null) {
+ continue;
}
- ClassLoader cl = d.getClassLoader();
- // args[1] - class name
- Class clazz = null;
- try {
- clazz = Class.forName(args[1], true, cl);
- } catch (Exception e) {
- System.err.println("Could not load the class '" + args[1] + ": " + e.getMessage());
- return;
+
+ if (!includes.matcher(id).matches()) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("not including: " + id);
+ }
+ continue;
}
- Method m = null;
- try {
- m = clazz.getMethod("main", new Class[]{args.getClass()});
- } catch (Exception e) {
- System.err.println("Could not find the 'main(String[])' method in class " + args[1] + ": " + e.getMessage());
- return;
+ if (excludes.matcher(id).matches()) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("excluding: " + id);
+ }
+ continue;
}
- String[] subargs = new String[args.length - 2];
- System.arraycopy(args, 2, subargs, 0, subargs.length);
- m.invoke(null, new Object[]{subargs});
+ map.put(plugin.getPluginId(), plugin);
+ }
+ return map;
+ }
+
+ /**
+ * Loads all necessary dependencies for a selected plugin, and then runs one
+ * of the classes' main() method.
+ *
+ * @param args
+ * plugin ID (needs to be activated in the configuration), and the
+ * class name. The rest of arguments is passed to the main method of
+ * the selected class.
+ * @throws Exception
+ */
+ public static void main(String[] args) throws Exception {
+ if (args.length < 2) {
+ System.err
+ .println("Usage: PluginRepository pluginId className [arg1 arg2 ...]");
+ return;
+ }
+ Configuration conf = NutchConfiguration.create();
+ PluginRepository repo = new PluginRepository(conf);
+ // args[0] - plugin ID
+ PluginDescriptor d = repo.getPluginDescriptor(args[0]);
+ if (d == null) {
+ System.err.println("Plugin '" + args[0] + "' not present or inactive.");
+ return;
+ }
+ ClassLoader cl = d.getClassLoader();
+ // args[1] - class name
+ Class clazz = null;
+ try {
+ clazz = Class.forName(args[1], true, cl);
+ } catch (Exception e) {
+ System.err.println("Could not load the class '" + args[1] + ": "
+ + e.getMessage());
+ return;
+ }
+ Method m = null;
+ try {
+ m = clazz.getMethod("main", new Class[] { args.getClass() });
+ } catch (Exception e) {
+ System.err.println("Could not find the 'main(String[])' method in class "
+ + args[1] + ": " + e.getMessage());
+ return;
}
+ String[] subargs = new String[args.length - 2];
+ System.arraycopy(args, 2, subargs, 0, subargs.length);
+ m.invoke(null, new Object[] { subargs });
+ }
}
diff --git a/src/java/org/apache/nutch/plugin/PluginRuntimeException.java b/src/java/org/apache/nutch/plugin/PluginRuntimeException.java
index ed4f82ad9..262822a4f 100644
--- a/src/java/org/apache/nutch/plugin/PluginRuntimeException.java
+++ b/src/java/org/apache/nutch/plugin/PluginRuntimeException.java
@@ -23,6 +23,8 @@ package org.apache.nutch.plugin;
*/
public class PluginRuntimeException extends Exception {
+ private static final long serialVersionUID = 1L;
+
public PluginRuntimeException(Throwable cause) {
super(cause);
}