Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2625 ProtocolFactory.getProtocol(url) may create multiple plugin instances - lock critical block (conditional creation of plugin instance) on object cache object

Sebastian Nagela6f533dfecd6
diff --git a/src/java/org/apache/nutch/protocol/ProtocolFactory.java b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
index 87944a8f0..2d20ecd6b 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());
}