Apache Nutch is an extensible and scalable web crawler
Add functionality to run individual classes in plugins.
Andrzej Bialeckiddcdad641a90
diff --git a/bin/nutch b/bin/nutchindex add7f44a8..a083eaee8 100755--- a/bin/nutch+++ b/bin/nutch@@ -41,6 +41,7 @@ if [ $# = 0 ]; thenecho " index run the indexer on parsed segments and linkdb"echo " merge merge several segment indexes"echo " dedup remove duplicates from a set of segment indexes"+ echo " plugin load a plugin and run one of its classes main()"echo " server run a search server"echo " namenode run the NDFS namenode"echo " datanode run an NDFS datanode"@@ -150,6 +151,8 @@ elif [ "$COMMAND" = "dedup" ] ; thenCLASS=org.apache.nutch.indexer.DeleteDuplicateselif [ "$COMMAND" = "merge" ] ; thenCLASS=org.apache.nutch.indexer.IndexMerger+elif [ "$COMMAND" = "plugin" ] ; then+ CLASS=org.apache.nutch.plugin.PluginRepositoryelif [ "$COMMAND" = "server" ] ; thenCLASS='org.apache.nutch.searcher.DistributedSearch$Server'elif [ "$COMMAND" = "namenode" ] ; thendiff --git a/src/java/org/apache/nutch/plugin/PluginRepository.java b/src/java/org/apache/nutch/plugin/PluginRepository.javaindex f78c15378..1e971504a 100644--- a/src/java/org/apache/nutch/plugin/PluginRepository.java+++ b/src/java/org/apache/nutch/plugin/PluginRepository.java@@ -19,6 +19,7 @@ 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.Iterator;@@ -377,4 +378,46 @@ public class PluginRepository {}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;+ }+ NutchConf conf = new NutchConf();+ 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/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/Http.java b/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/Http.javaindex d9eed3176..4c07fa2ba 100755--- a/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/Http.java+++ b/src/plugin/protocol-http/src/java/org/apache/nutch/protocol/http/Http.java@@ -50,7 +50,9 @@ public class Http extends HttpBase {}public static void main(String[] args) throws Exception {- main(new Http(), args);+ Http http = new Http();+ http.setConf(new NutchConf());+ main(http, args);}protected Response getResponse(URL url, CrawlDatum datum, boolean redirect)diff --git a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.javaindex f3089022f..49268db0b 100644--- a/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java+++ b/src/plugin/protocol-httpclient/src/java/org/apache/nutch/protocol/httpclient/Http.java@@ -86,7 +86,9 @@ public class Http extends HttpBase {}public static void main(String[] args) throws Exception {- main(new Http(), args);+ Http http = new Http();+ http.setConf(new NutchConf());+ main(http, args);}protected Response getResponse(URL url, CrawlDatum datum, boolean redirect)