Apache Nutch is an extensible and scalable web crawler
fix NUTCH-421
Sami Siren25fbb1bb8f84
diff --git a/CHANGES.txt b/CHANGES.txtindex c4e4f6def..7e69cfb9e 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -119,6 +119,9 @@ Unreleased changes (0.9-dev)38. NUTCH-325 - UrlFilters.java throws NPE in case urlfilter.order containsFilters that are not in plugin.includes (Stefan Groschupf, siren)++39. NUTCH-421 - Allow predeterminate running order of indexing filters+ (Alan Tanaman, siren)Release 0.8 - 2006-07-25diff --git a/conf/nutch-default.xml b/conf/nutch-default.xmlindex a5436b658..1b2903ddb 100644--- a/conf/nutch-default.xml+++ b/conf/nutch-default.xml@@ -536,6 +536,24 @@</description></property>+<!-- indexingfilter plugin properties -->++<property>+ <name>indexingfilter.order</name>+ <value></value>+ <description>The order by which index filters are applied.+ If empty, all available index filters (as dictated by properties+ plugin-includes and plugin-excludes above) are loaded and applied in system+ defined order. If not empty, only named filters are loaded and applied+ in given order. For example, if this property has value:+ org.apache.nutch.indexer.basic.BasicIndexingFilter org.apache.nutch.indexer.more.MoreIndexingFilter+ then BasicIndexingFilter is applied first, and MoreIndexingFilter second.++ Filter ordering might have impact on result if one filter depends on output of+ another filter.+ </description>+</property>+<!-- analysis properties -->diff --git a/src/java/org/apache/nutch/indexer/IndexingFilters.java b/src/java/org/apache/nutch/indexer/IndexingFilters.javaindex 7fc4c2738..74fafefb6 100644--- a/src/java/org/apache/nutch/indexer/IndexingFilters.java+++ b/src/java/org/apache/nutch/indexer/IndexingFilters.java@@ -17,6 +17,7 @@package org.apache.nutch.indexer;+import java.util.ArrayList;import java.util.HashMap;// Commons Logging imports@@ -35,13 +36,22 @@ import org.apache.hadoop.io.Text;/** Creates and caches {@link IndexingFilter} implementing plugins.*/public class IndexingFilters {+ public static final String INDEXINGFILTER_ORDER = "indexingfilter.order";+public final static Log LOG = LogFactory.getLog(IndexingFilters.class);private IndexingFilter[] indexingFilters;public IndexingFilters(Configuration conf) {+ /* Get indexingfilter.order property */+ String order = conf.get(INDEXINGFILTER_ORDER);this.indexingFilters =(IndexingFilter[]) conf.getObject(IndexingFilter.class.getName());if (this.indexingFilters == null) {+ /* If ordered filters are required, prepare array of filters based on property */+ String[] orderedFilters = null;+ if (order != null && !order.trim().equals("")) {+ orderedFilters = order.split("\\s+");+ }try {ExtensionPoint point = PluginRepository.get(conf).getExtensionPoint(IndexingFilter.X_POINT_ID);if (point == null)@@ -58,7 +68,21 @@ public class IndexingFilters {filterMap.put(filter.getClass().getName(), filter);}}- conf.setObject(IndexingFilter.class.getName(), (IndexingFilter[]) filterMap.values().toArray(new IndexingFilter[0]));+ /* If no ordered filters required, just get the filters in an indeterminate order */+ if (orderedFilters == null) {+ conf.setObject(IndexingFilter.class.getName(), (IndexingFilter[]) filterMap.values().toArray(new IndexingFilter[0]));+ /* Otherwise run the filters in the required order */+ } else {+ ArrayList<IndexingFilter> filters = new ArrayList<IndexingFilter>();+ for (int i = 0; i < orderedFilters.length; i++) {+ IndexingFilter filter = (IndexingFilter) filterMap+ .get(orderedFilters[i]);+ if (filter != null) {+ filters.add(filter);+ }+ }+ conf.setObject(IndexingFilter.class.getName(), filters.toArray(new IndexingFilter[filters.size()]));+ }} catch (PluginRuntimeException e) {throw new RuntimeException(e);}diff --git a/src/test/org/apache/nutch/indexer/TestIndexingFilters.java b/src/test/org/apache/nutch/indexer/TestIndexingFilters.javanew file mode 100644index 000000000..dfc84ebbe--- /dev/null+++ b/src/test/org/apache/nutch/indexer/TestIndexingFilters.java@@ -0,0 +1,51 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You 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.indexer;++import org.apache.hadoop.conf.Configuration;+import org.apache.hadoop.io.Text;+import org.apache.lucene.document.Document;+import org.apache.nutch.crawl.CrawlDatum;+import org.apache.nutch.crawl.Inlinks;+import org.apache.nutch.metadata.Metadata;+import org.apache.nutch.parse.Outlink;+import org.apache.nutch.parse.ParseData;+import org.apache.nutch.parse.ParseImpl;+import org.apache.nutch.parse.ParseStatus;+import org.apache.nutch.util.NutchConfiguration;++import junit.framework.TestCase;++public class TestIndexingFilters extends TestCase {++ /**+ * Test behaviour when defined filter does not exist.+ * @throws IndexingException+ */+ public void testNonExistingIndexingFilter() throws IndexingException {+ Configuration conf = NutchConfiguration.create();+ String class1 = "NonExistingFilter";+ String class2 = "org.apache.nutch.indexer.basic.BasicIndexingFilter";+ conf.set(IndexingFilters.INDEXINGFILTER_ORDER, class1 + " " + class2);++ IndexingFilters filters = new IndexingFilters(conf);+ filters.filter(new Document(), new ParseImpl("text", new ParseData(+ new ParseStatus(), "title", new Outlink[0], new Metadata())), new Text(+ "http://www.example.com/"), new CrawlDatum(), new Inlinks());+ }++}