KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > clazzy > ClazzyURLStreamHandlerFactory


1 package org.sapia.clazzy;
2
3 import java.net.URLStreamHandler JavaDoc;
4 import java.net.URLStreamHandlerFactory JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7
8 /**
9  * A <code>URLStreamHandlerFactory</code> implementation.
10  * <p>
11  * An instance of this class can be set on the <code>URL</code> class, by
12  * calling the <code>setURLStreamHandlerFactory()</code> static method:
13  *
14  * <pre>
15  * ClazzyURLStreamHandlerFactory fac = new ClazzyURLStreamHandlerFactory();
16  * URL.setURLStreamHandlerFactory(fac);
17  * </pre>
18  *
19  * <p>
20  * The above approach is not recommended: the URL class does not allow setting
21  * the factory more than once per VM. As a workaround, the JDK provides a mechanism
22  * based on package and class naming conventions for factory implementations
23  * (see <a HREF="http://java.sun.com/developer/onlineTraining/protocolhandlers/">this link</a>).
24  * <p>
25  * Thus, using this framework following the above convention involves setting the following system
26  * property:
27  * <pre>
28  * -Djava.protocol.handler.pkgs=org.sapia
29  * </pre>
30  *
31  *
32  * @author Yanick Duchesne
33  *
34  * @see org.sapia.clazzy.ClazzyURLConnection
35  * @see org.sapia.clazzy.loader.JarLoader
36  * @see org.sapia.clazzy.JarClassLoader
37  *
38  * <dl>
39  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
40  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
41  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
42  * </dl>
43  */

44 public class ClazzyURLStreamHandlerFactory implements URLStreamHandlerFactory JavaDoc{
45   
46   public static final String JavaDoc PROTOCOL = "clazzy";
47   
48   private Map JavaDoc _handlers = new HashMap JavaDoc();
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   /**
61    * @see java.net.URLStreamHandlerFactory#createURLStreamHandler(java.lang.String)
62    */

63   public URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
64     int i = protocol.indexOf(':');
65     if(i > -1){
66       protocol = protocol.substring(0, i);
67     }
68     Class JavaDoc handlerClass = (Class JavaDoc)_handlers.get(protocol);
69     if(handlerClass != null){
70       try{
71         return (URLStreamHandler JavaDoc)handlerClass.newInstance();
72       }catch(Exception JavaDoc e){
73         throw new RuntimeException JavaDoc("Could not create URLStreamHandler for: " + protocol, e);
74       }
75     }
76     throw new IllegalArgumentException JavaDoc("Unknown protocol");
77   }
78   
79 }
80
Popular Tags