Author: siren
Date: Sun Jun 18 01:40:10 2006
New Revision: 415108

URL: http://svn.apache.org/viewvc?rev=415108&view=rev
Log:
changed cache key to avoid conflict in namespace, also added a testcase to 
enforce the (changed) contract

Added:
    
lucene/nutch/trunk/src/test/org/apache/nutch/protocol/TestProtocolFactory.java
Modified:
    lucene/nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java

Modified: 
lucene/nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java
URL: 
http://svn.apache.org/viewvc/lucene/nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java?rev=415108&r1=415107&r2=415108&view=diff
==============================================================================
--- lucene/nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java 
(original)
+++ lucene/nutch/trunk/src/java/org/apache/nutch/protocol/ProtocolFactory.java 
Sun Jun 18 01:40:10 2006
@@ -27,9 +27,13 @@
 
 import org.apache.hadoop.conf.Configuration;
 
-/** Creates and caches [EMAIL PROTECTED] Protocol} plugins.  Protocol plugins 
should
+/** 
+ * Creates and caches [EMAIL PROTECTED] 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 [EMAIL PROTECTED] Protocol#X_POINT_ID).
+ */
 public class ProtocolFactory {
 
   public static final Log LOG = LogFactory.getLog(ProtocolFactory.class);
@@ -46,16 +50,22 @@
         }
   }                      
 
-  /** Returns the appropriate [EMAIL PROTECTED] Protocol} implementation for a 
url. */
+  /**
+   * Returns the appropriate [EMAIL PROTECTED] 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 @@
 
         Protocol protocol = (Protocol) extension.getExtensionInstance();
 
-        conf.setObject(protocolName, protocol);
+        conf.setObject(cacheId, protocol);
 
         return protocol;
       }

Added: 
lucene/nutch/trunk/src/test/org/apache/nutch/protocol/TestProtocolFactory.java
URL: 
http://svn.apache.org/viewvc/lucene/nutch/trunk/src/test/org/apache/nutch/protocol/TestProtocolFactory.java?rev=415108&view=auto
==============================================================================
--- 
lucene/nutch/trunk/src/test/org/apache/nutch/protocol/TestProtocolFactory.java 
(added)
+++ 
lucene/nutch/trunk/src/test/org/apache/nutch/protocol/TestProtocolFactory.java 
Sun Jun 18 01:40:10 2006
@@ -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");
+    }
+  }
+  
+}


Reply via email to