You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nutch.apache.org by sn...@apache.org on 2018/11/15 10:34:00 UTC

[nutch] 01/14: NUTCH-2625 ProtocolFactory.getProtocol(url) may create multiple plugin instances - lock critical block (conditional creation of plugin instance) on object cache object

This is an automated email from the ASF dual-hosted git repository.

snagel pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nutch.git

commit a6f533dfecd688a6c43212b0e826be9a2da5b4ce
Author: Sebastian Nagel <sn...@apache.org>
AuthorDate: Tue Jul 24 16:19:04 2018 +0200

    NUTCH-2625 ProtocolFactory.getProtocol(url) may create multiple plugin instances
    - lock critical block (conditional creation of plugin instance)
      on object cache object
---
 .../org/apache/nutch/protocol/ProtocolFactory.java | 26 ++++++++++++----------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/src/java/org/apache/nutch/protocol/ProtocolFactory.java b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
index 87944a8..2d20ecd 100644
--- a/src/java/org/apache/nutch/protocol/ProtocolFactory.java
+++ b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
@@ -81,7 +81,7 @@ public class ProtocolFactory {
    * @throws ProtocolNotFound
    *           when Protocol can not be found for url
    */
-  public synchronized Protocol getProtocol(URL url)
+  public Protocol getProtocol(URL url)
       throws ProtocolNotFound {
     ObjectCache objectCache = ObjectCache.get(conf);
     try {
@@ -91,19 +91,21 @@ public class ProtocolFactory {
       }
 
       String cacheId = Protocol.X_POINT_ID + protocolName;
-      Protocol protocol = (Protocol) objectCache.getObject(cacheId);
-      if (protocol != null) {
+      synchronized (objectCache) {
+        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;
       }
-
-      Extension extension = findExtension(protocolName);
-      if (extension == null) {
-        throw new ProtocolNotFound(protocolName);
-      }
-
-      protocol = (Protocol) extension.getExtensionInstance();
-      objectCache.setObject(cacheId, protocol);
-      return protocol;
     } catch (PluginRuntimeException e) {
       throw new ProtocolNotFound(url.toString(), e.toString());
     }