Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-471 - Fix synchronization in NutchBean creation.

Tacettin Guney629209fa3f53
diff --git a/CHANGES.txt b/CHANGES.txt
index 2c6426707..9adc00df5 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -54,6 +54,9 @@ Unreleased changes (1.0-dev)
There was discussion to remove parse-rss, in light of the feed plugin,
however, this patch does not explicitly remove parse-rss. (dogacan, mattmann)
+17. NUTCH-471 - Fix synchronization in NutchBean creation.
+ (Enis Soztutar via dogacan)
+
Release 0.9 - 2007-04-02
1. Changed log4j confiquration to log to stdout on commandline
diff --git a/src/java/org/apache/nutch/searcher/NutchBean.java b/src/java/org/apache/nutch/searcher/NutchBean.java
index ea99b30f1..904a93682 100644
--- a/src/java/org/apache/nutch/searcher/NutchBean.java
+++ b/src/java/org/apache/nutch/searcher/NutchBean.java
@@ -19,7 +19,7 @@ package org.apache.nutch.searcher;
import java.io.*;
import java.util.*;
-import javax.servlet.ServletContext;
+import javax.servlet.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -27,6 +27,7 @@ import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.Closeable;
import org.apache.hadoop.conf.*;
+import org.apache.hadoop.util.StringUtils;
import org.apache.nutch.parse.*;
import org.apache.nutch.indexer.*;
import org.apache.nutch.crawl.Inlinks;
@@ -41,6 +42,7 @@ public class NutchBean
DistributedSearch.Protocol, Closeable {
public static final Log LOG = LogFactory.getLog(NutchBean.class);
+ public static final String KEY = "nutchBean";
// static {
// LogFormatter.setShowThreadIDs(true);
@@ -63,14 +65,10 @@ public class NutchBean
private FileSystem fs;
- /** Cache in servlet context. */
+ /** Returns the cached instance in the servlet context.
+ * @see NutchBeanConstructor*/
public static NutchBean get(ServletContext app, Configuration conf) throws IOException {
- NutchBean bean = (NutchBean)app.getAttribute("nutchBean");
- if (bean == null) {
- if (LOG.isInfoEnabled()) { LOG.info("creating new bean"); }
- bean = new NutchBean(conf);
- app.setAttribute("nutchBean", bean);
- }
+ NutchBean bean = (NutchBean)app.getAttribute(KEY);
return bean;
}
@@ -407,6 +405,28 @@ public class NutchBean
}
}
+ /** Responsible for constructing a NutchBean singleton instance and
+ * caching it in the servlet context. This class should be registered in
+ * the deployment descriptor as a listener
+ */
+ public static class NutchBeanConstructor implements ServletContextListener {
+
+ public void contextDestroyed(ServletContextEvent sce) { }
+ public void contextInitialized(ServletContextEvent sce) {
+ ServletContext app = sce.getServletContext();
+ Configuration conf = NutchConfiguration.get(app);
+
+ LOG.info("creating new bean");
+ NutchBean bean = null;
+ try {
+ bean = new NutchBean(conf);
+ app.setAttribute(KEY, bean);
+ }
+ catch (IOException ex) {
+ LOG.error(StringUtils.stringifyException(ex));
+ }
+ }
+ }
}
diff --git a/src/web/web.xml b/src/web/web.xml
index 20971c8b5..b3c288ad0 100755
--- a/src/web/web.xml
+++ b/src/web/web.xml
@@ -22,6 +22,10 @@
<!-- order is very important here -->
+<listener>
+ <listener-class>org.apache.nutch.searcher.NutchBean$NutchBeanConstructor</listener-class>
+</listener>
+
<servlet>
<servlet-name>Cached</servlet-name>
<servlet-class>org.apache.nutch.servlet.Cached</servlet-class>