KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.clazzy;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.net.URLConnection JavaDoc;
9
10
11 /**
12  * An instance of this class deals with "clazzy" URLs, of the form:
13  * <pre>
14  * clazzy:path_to_jar?file_name.
15  * </pre>
16  * <p>
17  * An instance of this class in fact interacts with a <code>JarClassLoader</code>
18  * to retrieve resources corresponding to the given URLs (respecting the above
19  * format).
20  * <p>
21  * In order for the URLs handled by this class to work properly, the following
22  * system property must be set:
23  * <pre>
24  * -Djava.protocol.handler.pkgs=org.sapia
25  * </pre>
26  *
27  * @see org.sapia.clazzy.ClazzyURLStreamHandlerFactory
28  * @see org.sapia.clazzy.loader.JarLoader
29  * @see org.sapia.clazzy.JarClassLoader
30  *
31  * @author Yanick Duchesne
32  *
33  * <dl>
34  * <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>
35  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
36  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
37  * </dl>
38  */

39 public class ClazzyURLConnection extends URLConnection JavaDoc{
40   
41   
42   public ClazzyURLConnection(URL JavaDoc url) {
43     super(url);
44   }
45   
46   /**
47    * @see java.net.URLConnection#connect()
48    */

49   public void connect() throws IOException JavaDoc {
50   }
51   
52   /**
53    * @see java.net.URLConnection#getInputStream()
54    */

55   public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
56     String JavaDoc path = getURL().toExternalForm();
57     int i = path.indexOf("?");
58     if(i < 0){
59       throw new IOException JavaDoc("URL should have format: protocol://path_to_jar?file_name. Got: " + path);
60     }
61     JarClassLoader loader = new JarClassLoader(new File JavaDoc(getURL().getPath()));
62     try{
63       return loader.findResourceAsStream(path.substring(i+1));
64     }finally{
65       loader.close();
66     }
67   }
68   
69   /**
70    * @see java.net.URLConnection#getOutputStream()
71    */

72   public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
73     return super.getOutputStream();
74   }
75   
76   /**
77    * @see java.net.URLConnection#getAllowUserInteraction()
78    */

79   public boolean getAllowUserInteraction() {
80     return false;
81   }
82   
83 }
84
Popular Tags