KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > moduleloader > ModuleURLConnection


1 /*
2  * ModuleLoader - A generic, policy-driven class loader.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.moduleloader;
37
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.InputStream JavaDoc;
41 import java.net.URL JavaDoc;
42 import java.net.URLConnection JavaDoc;
43 import java.security.Permission JavaDoc;
44
45 class ModuleURLConnection extends URLConnection JavaDoc
46 {
47     private ModuleManager m_mgr = null;
48     private int m_contentLength;
49     private long m_contentTime;
50     private String JavaDoc m_contentType;
51     private InputStream JavaDoc m_is;
52
53     public ModuleURLConnection(ModuleManager mgr, URL JavaDoc url)
54     {
55         super(url);
56         m_mgr = mgr;
57     }
58
59     public void connect() throws IOException JavaDoc
60     {
61         if (!connected)
62         {
63             // The URL is constructed like this:
64
// module://<module-id>/<source-idx>/<resource-path>
65
Module module = m_mgr.getModule(url.getHost());
66             if (module == null)
67             {
68                 throw new IOException JavaDoc("Unable to find bundle's module.");
69             }
70
71             String JavaDoc resource = url.getFile();
72             if (resource == null)
73             {
74                 throw new IOException JavaDoc("Unable to find resource: " + url.toString());
75             }
76             if (resource.startsWith("/"))
77             {
78                 resource = resource.substring(1);
79             }
80             int rsIdx = -1;
81             try
82             {
83                 rsIdx = Integer.parseInt(resource.substring(0, resource.indexOf("/")));
84             }
85             catch (NumberFormatException JavaDoc ex)
86             {
87                 new IOException JavaDoc("Error parsing resource index.");
88             }
89             resource = resource.substring(resource.indexOf("/") + 1);
90
91             // Get the resource bytes from the resource source.
92
byte[] bytes = null;
93             ResourceSource[] resSources = module.getResourceSources();
94             if ((resSources != null) && (rsIdx < resSources.length))
95             {
96                 if (resSources[rsIdx].hasResource(resource))
97                 {
98                     bytes = resSources[rsIdx].getBytes(resource);
99                 }
100             }
101
102             if (bytes == null)
103             {
104                 throw new IOException JavaDoc("Unable to find resource: " + url.toString());
105             }
106
107             m_is = new ByteArrayInputStream JavaDoc(bytes);
108             m_contentLength = bytes.length;
109             m_contentTime = 0L; // TODO: Change this.
110
m_contentType = URLConnection.guessContentTypeFromName(resource);
111             connected = true;
112         }
113     }
114
115     public InputStream JavaDoc getInputStream()
116         throws IOException JavaDoc
117     {
118         if (!connected)
119         {
120             connect();
121         }
122         return m_is;
123     }
124
125     public int getContentLength()
126     {
127         if (!connected)
128         {
129             try {
130                 connect();
131             } catch(IOException JavaDoc ex) {
132                 return -1;
133             }
134         }
135         return m_contentLength;
136     }
137
138     public long getLastModified()
139     {
140         if (!connected)
141         {
142             try {
143                 connect();
144             } catch(IOException JavaDoc ex) {
145                 return 0;
146             }
147         }
148         if (m_contentTime != -1L)
149         {
150             return m_contentTime;
151         }
152         else
153         {
154             return 0L;
155         }
156     }
157
158     public String JavaDoc getContentType()
159     {
160         if (!connected)
161         {
162             try {
163                 connect();
164             } catch(IOException JavaDoc ex) {
165                 return null;
166             }
167         }
168         return m_contentType;
169     }
170
171     public Permission JavaDoc getPermission()
172     {
173         // TODO: This should probably return a FilePermission
174
// to access the bundle JAR file, but we don't have the
175
// necessary information here to construct the absolute
176
// path of the JAR file...so it would take some
177
// re-arranging to get this to work.
178
return null;
179     }
180 }
Popular Tags