1 2 3 4 package net.nutch.protocol; 5 6 import java.util.Hashtable ; 7 import java.net.URL ; 8 import java.net.MalformedURLException ; 9 10 import net.nutch.plugin.*; 11 12 import java.util.logging.Logger ; 13 import net.nutch.util.LogFormatter; 14 15 18 public class ProtocolFactory { 19 20 public static final Logger LOG = LogFormatter 21 .getLogger(ProtocolFactory.class.getName()); 22 23 private final static ExtensionPoint X_POINT = PluginRepository.getInstance() 24 .getExtensionPoint(Protocol.X_POINT_ID); 25 26 static { 27 if (X_POINT == null) { 28 throw new RuntimeException ("x-point "+Protocol.X_POINT_ID+" not found."); 29 } 30 } 31 32 private static final Hashtable CACHE = new Hashtable (); 33 34 private ProtocolFactory() {} 36 37 public static Protocol getProtocol(String urlString) 38 throws ProtocolNotFound { 39 try { 40 URL url = new URL (urlString); 41 String protocolName = url.getProtocol(); 42 if (protocolName == null) 43 throw new ProtocolNotFound(urlString); 44 Extension extension = getExtension(protocolName); 45 if (extension == null) 46 throw new ProtocolNotFound(protocolName); 47 48 return (Protocol)extension.getExtensionInstance(); 49 50 } catch (MalformedURLException e) { 51 throw new ProtocolNotFound(urlString, e.toString()); 52 } catch (PluginRuntimeException e) { 53 throw new ProtocolNotFound(urlString, e.toString()); 54 } 55 } 56 57 private static Extension getExtension(String name) 58 throws PluginRuntimeException { 59 60 if (CACHE.containsKey(name)) 61 return (Extension)CACHE.get(name); 62 63 Extension extension = findExtension(name); 64 65 CACHE.put(name, extension); 66 67 return extension; 68 } 69 70 private static Extension findExtension(String name) 71 throws PluginRuntimeException { 72 73 Extension[] extensions = X_POINT.getExtentens(); 74 75 for (int i = 0; i < extensions.length; i++) { 76 Extension extension = extensions[i]; 77 78 if (name.equals(extension.getAttribute("protocolName"))) 79 return extension; 80 } 81 return null; 82 } 83 } 84 | Popular Tags |