Author: jnioche
Date: Mon Jul  8 08:48:16 2013
New Revision: 1500609

URL: http://svn.apache.org/r1500609
Log:
NUTCH-1604 Protocol-factory not thread-safe

Modified:
    nutch/trunk/CHANGES.txt
    nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java

Modified: nutch/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/nutch/trunk/CHANGES.txt?rev=1500609&r1=1500608&r2=1500609&view=diff
==============================================================================
--- nutch/trunk/CHANGES.txt (original)
+++ nutch/trunk/CHANGES.txt Mon Jul  8 08:48:16 2013
@@ -2,6 +2,8 @@ Nutch Change Log
 
 Nutch 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)

Modified: nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java
URL: 
http://svn.apache.org/viewvc/nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java?rev=1500609&r1=1500608&r2=1500609&view=diff
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java 
(original)
+++ nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java Mon Jul 
 8 08:48:16 2013
@@ -20,7 +20,6 @@ package org.apache.nutch.protocol;
 import java.net.URL;
 import java.net.MalformedURLException;
 
-// Commons Logging imports
 import 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;
   }
-  
+
 }


Reply via email to