1 package org.sapia.clazzy; 2 3 import java.net.URLStreamHandler ; 4 import java.net.URLStreamHandlerFactory ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 8 44 public class ClazzyURLStreamHandlerFactory implements URLStreamHandlerFactory { 45 46 public static final String PROTOCOL = "clazzy"; 47 48 private Map _handlers = new HashMap (); 49 50 public ClazzyURLStreamHandlerFactory(){ 51 _handlers.put("http", sun.net.www.protocol.http.Handler.class); 52 _handlers.put("ftp", sun.net.www.protocol.ftp.Handler.class); 53 _handlers.put("gopher", sun.net.www.protocol.gopher.Handler.class); 54 _handlers.put("mailto", sun.net.www.protocol.mailto.Handler.class); 55 _handlers.put("file", sun.net.www.protocol.file.Handler.class); 56 _handlers.put("jar", Handler.class); 57 _handlers.put(PROTOCOL, Handler.class); 58 } 59 60 63 public URLStreamHandler createURLStreamHandler(String protocol) { 64 int i = protocol.indexOf(':'); 65 if(i > -1){ 66 protocol = protocol.substring(0, i); 67 } 68 Class handlerClass = (Class )_handlers.get(protocol); 69 if(handlerClass != null){ 70 try{ 71 return (URLStreamHandler )handlerClass.newInstance(); 72 }catch(Exception e){ 73 throw new RuntimeException ("Could not create URLStreamHandler for: " + protocol, e); 74 } 75 } 76 throw new IllegalArgumentException ("Unknown protocol"); 77 } 78 79 } 80 | Popular Tags |