Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

changed cache key to avoid conflict in namespace, also added a testcase to enforce the (changed) contract

Sami Sirenc5df4b7daa10
diff --git a/src/java/org/apache/nutch/protocol/ProtocolFactory.java b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
index cc1678ba8..d0f553388 100644
--- a/src/java/org/apache/nutch/protocol/ProtocolFactory.java
+++ b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
@@ -27,9 +27,13 @@ import org.apache.nutch.plugin.*;
import org.apache.hadoop.conf.Configuration;
-/** Creates and caches {@link Protocol} plugins. Protocol plugins should
+/**
+ * Creates and caches {@link Protocol} plugins. Protocol plugins should
* define the attribute "protocolName" with the name of the protocol that they
- * implement. */
+ * implement. Configuration object is used for caching. Cache key is
+ * constructed from appending protocol name (eg. http) to
+ * constant {@link Protocol#X_POINT_ID).
+ */
public class ProtocolFactory {
public static final Log LOG = LogFactory.getLog(ProtocolFactory.class);
@@ -46,16 +50,22 @@ public class ProtocolFactory {
}
}
- /** Returns the appropriate {@link Protocol} implementation for a url. */
+ /**
+ * Returns the appropriate {@link Protocol} implementation for a url.
+ * @param urlString Url String
+ * @return
+ * @throws ProtocolNotFound when Protocol can not be found for urlString
+ */
public Protocol getProtocol(String urlString) throws ProtocolNotFound {
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 (conf.getObject(protocolName) != null) {
- return (Protocol) conf.getObject(protocolName);
+ if (conf.getObject(cacheId) != null) {
+ return (Protocol) conf.getObject(cacheId);
} else {
Extension extension = findExtension(protocolName);
if (extension == null) {
@@ -64,7 +74,7 @@ public class ProtocolFactory {
Protocol protocol = (Protocol) extension.getExtensionInstance();
- conf.setObject(protocolName, protocol);
+ conf.setObject(cacheId, protocol);
return protocol;
}
diff --git a/src/test/org/apache/nutch/protocol/TestProtocolFactory.java b/src/test/org/apache/nutch/protocol/TestProtocolFactory.java
new file mode 100644
index 000000000..3e209c517
--- /dev/null
+++ b/src/test/org/apache/nutch/protocol/TestProtocolFactory.java
@@ -0,0 +1,50 @@
+package org.apache.nutch.protocol;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.nutch.util.NutchConfiguration;
+
+import junit.framework.TestCase;
+
+public class TestProtocolFactory extends TestCase {
+
+ public void testGetProtocol(){
+ Configuration conf=NutchConfiguration.create();
+
+ ProtocolFactory factory=new ProtocolFactory(conf);
+
+ //non existing protocol
+ try {
+ factory.getProtocol("xyzxyz://somehost");
+ fail("Must throw ProtocolNotFound");
+ } catch (ProtocolNotFound e) {
+ //all is ok
+ } catch (Exception ex){
+ fail("Must not throw any other exception");
+ }
+
+ Protocol httpProtocol=null;
+
+ //existing protocol
+ try {
+ httpProtocol=factory.getProtocol("http://somehost");
+ assertNotNull(httpProtocol);
+ } catch (Exception ex){
+ fail("Must not throw any other exception");
+ }
+
+ //cache key
+ Object protocol=conf.getObject(Protocol.X_POINT_ID + "http");
+ assertNotNull(protocol);
+ assertEquals(httpProtocol, protocol);
+
+ //test same object instance
+ try {
+ System.out.println(httpProtocol);
+ System.out.println(factory.getProtocol("http://somehost"));
+ assertTrue(httpProtocol==factory.getProtocol("http://somehost"));
+ } catch (ProtocolNotFound e) {
+ fail("Must not throw any exception");
+ }
+ }
+
+}