Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-210, Add an xsl that generates a basic ServletContext XML file for the nutch webapp and make use of the ServletContext init parameters to override the properties in nutch-default and nutch-site properties. Contributed by Chris Mattmann.

Jerome Charron1f2af77496ab
diff --git a/conf/context.xsl b/conf/context.xsl
new file mode 100644
index 000000000..198ab84a4
--- /dev/null
+++ b/conf/context.xsl
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+ Copyright 2006 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.
+
+ Author : Chris Mattmann
+ Auhtor : J�r�me Charron
+ Description: This xsl file is used to transform dynamic properties out of the
+ nutch-default.xml file into a deployable Context.xml file for configuring
+ the Nutch war file in a servlet container.
+-->
+
+<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+
+<xsl:template match="/">
+<Context path="/nutch" docBase="nutch-0.8-dev.war"
+ debug="5" reloadable="true" crossContext="true">
+
+<xsl:for-each select="configuration/property">
+
+<!-- "searcher." properties -->
+<xsl:call-template name="parameter">
+ <xsl:with-param name="property" select="current()"/>
+ <xsl:with-param name="filter" select="'searcher.'"/>
+</xsl:call-template>
+
+<!-- "plugin." properties -->
+<xsl:call-template name="parameter">
+ <xsl:with-param name="property" select="current()"/>
+ <xsl:with-param name="filter" select="'plugin.'"/>
+</xsl:call-template>
+
+<!-- "extension.clustering." properties -->
+<xsl:call-template name="parameter">
+ <xsl:with-param name="property" select="current()"/>
+ <xsl:with-param name="filter" select="'extension.clustering.'"/>
+</xsl:call-template>
+
+<!-- "extension.ontology." properties -->
+<xsl:call-template name="parameter">
+ <xsl:with-param name="property" select="current()"/>
+ <xsl:with-param name="filter" select="'extension.ontology.'"/>
+</xsl:call-template>
+
+<!-- "query." properties -->
+<xsl:call-template name="parameter">
+ <xsl:with-param name="property" select="current()"/>
+ <xsl:with-param name="filter" select="'query.'"/>
+</xsl:call-template>
+
+</xsl:for-each>
+
+</Context>
+</xsl:template>
+
+
+<!--
+ ! Template used to write out a parameter if the property's
+ ! name contains the specified filter string.
+ !-->
+<xsl:template name="parameter">
+ <xsl:param name="property"/>
+ <xsl:param name="filter"/>
+ <xsl:if test="contains(name, $filter)">
+ <Parameter override="false">
+ <xsl:attribute name="name">
+ <xsl:value-of select="name"/>
+ </xsl:attribute>
+ <xsl:attribute name="value">
+ <xsl:value-of select="value"/>
+ </xsl:attribute>
+ </Parameter>
+ </xsl:if>
+</xsl:template>
+
+</xsl:stylesheet>
diff --git a/src/java/org/apache/nutch/util/NutchConfiguration.java b/src/java/org/apache/nutch/util/NutchConfiguration.java
index 33e47675b..b0e53a7c6 100644
--- a/src/java/org/apache/nutch/util/NutchConfiguration.java
+++ b/src/java/org/apache/nutch/util/NutchConfiguration.java
@@ -16,12 +16,23 @@
package org.apache.nutch.util;
+// JDK imports
+import java.util.Enumeration;
+
+// Servlet imports
+import javax.servlet.ServletContext;
+
+// Hadoop imports
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.WritableName;
+
/** Utility to create Hadoop {@link Configuration}s that include Nutch-specific
* resources. */
public class NutchConfiguration {
+
+ private final static String KEY = NutchConfiguration.class.getName();
+
private NutchConfiguration() {} // singleton
// for back-compatibility, add old aliases for these Writable classes
@@ -41,11 +52,40 @@ public class NutchConfiguration {
return conf;
}
+ /**
+ * Create a {@link Configuration} for Nutch front-end.
+ *
+ * If a {@link Configuration} is found in the
+ * {@link javax.servlet.ServletContext} it is simply returned, otherwise,
+ * a new {@link Configuration} is created using the {@link #create()} method,
+ * and then all the init parameters found in the
+ * {@link javax.servlet.ServletContext} are added to the {@link Configuration}
+ * (the created {@link Configuration} is then saved into the
+ * {@link javax.servlet.ServletContext}).
+ *
+ * @param application is the ServletContext whose init parameters
+ * must override those of Nutch.
+ */
+ public static Configuration get(ServletContext application) {
+ Configuration conf = (Configuration) application.getAttribute(KEY);
+ if (conf == null) {
+ conf = create();
+ Enumeration e = application.getInitParameterNames();
+ while (e.hasMoreElements()) {
+ String name = (String) e.nextElement();
+ conf.set(name, application.getInitParameter(name));
+ }
+ application.setAttribute(KEY, conf);
+ }
+ return conf;
+ }
+
/** Add the standard Nutch resources to {@link Configuration}. */
public static Configuration addNutchResources(Configuration conf) {
conf.addDefaultResource("nutch-default.xml");
conf.addFinalResource("nutch-site.xml");
return conf;
}
+
}
diff --git a/src/web/jsp/anchors.jsp b/src/web/jsp/anchors.jsp
index 3a094f7ff..349be088f 100644
--- a/src/web/jsp/anchors.jsp
+++ b/src/web/jsp/anchors.jsp
@@ -11,11 +11,7 @@
import="org.apache.hadoop.conf.Configuration"
import="org.apache.nutch.util.NutchConfiguration"
%><%
- Configuration nutchConf = (Configuration) application.getAttribute(Configuration.class.getName());
- if (nutchConf == null) {
- nutchConf = NutchConfiguration.create();
- application.setAttribute(Configuration.class.getName(), nutchConf);
- }
+ Configuration nutchConf = NutchConfiguration.get(application);
NutchBean bean = NutchBean.get(application, nutchConf);
// set the character encoding to use when interpreting request values
request.setCharacterEncoding("UTF-8");
diff --git a/src/web/jsp/cached.jsp b/src/web/jsp/cached.jsp
index 62e0f9a23..cdf66e4c6 100644
--- a/src/web/jsp/cached.jsp
+++ b/src/web/jsp/cached.jsp
@@ -10,11 +10,7 @@
import="org.apache.hadoop.conf.Configuration"
import="org.apache.nutch.util.NutchConfiguration"
%><%
- Configuration nutchConf = (Configuration) application.getAttribute(Configuration.class.getName());
- if (nutchConf == null) {
- nutchConf = NutchConfiguration.create();
- application.setAttribute(Configuration.class.getName(), nutchConf);
- }
+ Configuration nutchConf = NutchConfiguration.get(application);
NutchBean bean = NutchBean.get(application, nutchConf);
bean.LOG.info("cache request from " + request.getRemoteAddr());
Hit hit = new Hit(Integer.parseInt(request.getParameter("idx")),
diff --git a/src/web/jsp/explain.jsp b/src/web/jsp/explain.jsp
index 3802b27fc..2accc8073 100644
--- a/src/web/jsp/explain.jsp
+++ b/src/web/jsp/explain.jsp
@@ -9,11 +9,7 @@
import="org.apache.hadoop.conf.Configuration"
import="org.apache.nutch.util.NutchConfiguration"
%><%
- Configuration nutchConf = (Configuration) application.getAttribute(Configuration.class.getName());
- if (nutchConf == null) {
- nutchConf = NutchConfiguration.create();
- application.setAttribute(Configuration.class.getName(), nutchConf);
- }
+ Configuration nutchConf = NutchConfiguration.get(application);
NutchBean bean = NutchBean.get(application, nutchConf);
// set the character encoding to use when interpreting request values
request.setCharacterEncoding("UTF-8");
diff --git a/src/web/jsp/refine-query-init.jsp b/src/web/jsp/refine-query-init.jsp
index e701707b9..597d37497 100644
--- a/src/web/jsp/refine-query-init.jsp
+++ b/src/web/jsp/refine-query-init.jsp
@@ -13,11 +13,7 @@ org.apache.nutch.ontology.Ontology ontology = null;
// being loaded into the JVM. Need improvement in future.
try {
- Configuration nutchConf = (Configuration) application.getAttribute(Configuration.class.getName());
- if (nutchConf == null) {
- nutchConf = NutchConfiguration.create();
- application.setAttribute(Configuration.class.getName(), nutchConf);
- }
+ Configuration nutchConf = NutchConfiguration.get(application);
String urls = nutchConf.get("extension.ontology.urls");
ontology = new org.apache.nutch.ontology.OntologyFactory(nutchConf).getOntology();
if (urls==null || urls.trim().equals("")) {
diff --git a/src/web/jsp/search.jsp b/src/web/jsp/search.jsp
index 390c649fb..1fdd61271 100644
--- a/src/web/jsp/search.jsp
+++ b/src/web/jsp/search.jsp
@@ -15,11 +15,8 @@
import="org.apache.nutch.util.NutchConfiguration"
%><%
- Configuration nutchConf = (Configuration) application.getAttribute(Configuration.class.getName());
- if (nutchConf == null) {
- nutchConf = NutchConfiguration.create();
- application.setAttribute(Configuration.class.getName(), nutchConf);
- }
+ Configuration nutchConf = NutchConfiguration.get(application);
+
/**
* Number of hits to retrieve and cluster if clustering extension is available
* and clustering is on. By default, 100. Configurable via nutch-conf.xml.
diff --git a/src/web/jsp/text.jsp b/src/web/jsp/text.jsp
index 630f4d096..2759bc14b 100644
--- a/src/web/jsp/text.jsp
+++ b/src/web/jsp/text.jsp
@@ -12,11 +12,7 @@
%><%
// show the content of a hit as plain text
- Configuration nutchConf = (Configuration) application.getAttribute(Configuration.class.getName());
- if (nutchConf == null) {
- nutchConf = NutchConfiguration.create();
- application.setAttribute(Configuration.class.getName(), nutchConf);
- }
+ Configuration nutchConf = NutchConfiguration.get(application);
NutchBean bean = NutchBean.get(application, nutchConf);
bean.LOG.info("text request from " + request.getRemoteAddr());