Apache Nutch is an extensible and scalable web crawler
NUTCH-1604 Protocol-factory not thread-safe
Julien Nioche5bfd1952e2d7
diff --git a/CHANGES.txt b/CHANGES.txtindex 6fb5d2069..57328c53d 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Development Trunk+* NUTCH-1604 ProtocolFactory not thread-safe (jnioche)+* NUTCH-1595 Upgrade to Tika 1.4 (jnioche, markus)* NUTCH-1598 ElasticSearchIndexer to read ImmutableSettings from config (markus)diff --git a/src/java/org/apache/nutch/protocol/ProtocolFactory.java b/src/java/org/apache/nutch/protocol/ProtocolFactory.javaindex c7c11a9f2..578f626a7 100644--- a/src/java/org/apache/nutch/protocol/ProtocolFactory.java+++ b/src/java/org/apache/nutch/protocol/ProtocolFactory.java@@ -20,7 +20,6 @@ package org.apache.nutch.protocol;import java.net.URL;import java.net.MalformedURLException;-// Commons Logging importsimport org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -59,34 +58,34 @@ public class ProtocolFactory {** @param urlString* Url String- * @return The appropriate {@link Protocol} implementation for a given {@link URL}.+ * @return The appropriate {@link Protocol} implementation for a given+ * {@link URL}.* @throws ProtocolNotFound* when Protocol can not be found for urlString*/- public Protocol getProtocol(String urlString) throws ProtocolNotFound {+ public synchronized Protocol getProtocol(String urlString)+ throws ProtocolNotFound {ObjectCache objectCache = ObjectCache.get(conf);try {URL url = new URL(urlString);String protocolName = url.getProtocol();- String cacheId = Protocol.X_POINT_ID + protocolName;if (protocolName == null)throw new ProtocolNotFound(urlString);- if (objectCache.getObject(cacheId) != null) {- return (Protocol) objectCache.getObject(cacheId);- } else {- Extension extension = findExtension(protocolName);- if (extension == null) {- throw new ProtocolNotFound(protocolName);- }-- Protocol protocol = (Protocol) extension.getExtensionInstance();-- objectCache.setObject(cacheId, protocol);-+ String cacheId = Protocol.X_POINT_ID + protocolName;+ Protocol protocol = (Protocol) objectCache.getObject(cacheId);+ if (protocol != null) {return protocol;}+ Extension extension = findExtension(protocolName);+ if (extension == null) {+ throw new ProtocolNotFound(protocolName);+ }++ protocol = (Protocol) extension.getExtensionInstance();+ objectCache.setObject(cacheId, protocol);+ return protocol;} catch (MalformedURLException e) {throw new ProtocolNotFound(urlString, e.toString());} catch (PluginRuntimeException e) {@@ -106,13 +105,14 @@ public class ProtocolFactory {}return null;}-- boolean contains(String what, String where){- String parts[]=where.split("[, ]");- for(int i=0;i<parts.length;i++) {- if(parts[i].equals(what)) return true;++ boolean contains(String what, String where) {+ String parts[] = where.split("[, ]");+ for (int i = 0; i < parts.length; i++) {+ if (parts[i].equals(what))+ return true;}return false;}-+}