KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > BundleURLConnection


1 /*
2  * Oscar - An implementation of the OSGi framework.
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.oscar;
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 import org.ungoverned.moduleloader.Module;
46 import org.ungoverned.moduleloader.ModuleManager;
47 import org.ungoverned.moduleloader.ResourceSource;
48
49 class BundleURLConnection extends URLConnection JavaDoc
50 {
51     private ModuleManager m_mgr = null;
52     private long id;
53     private int contentLength;
54     private long contentTime;
55     private String JavaDoc contentType;
56     private InputStream JavaDoc is;
57
58     public BundleURLConnection(ModuleManager mgr, URL JavaDoc url)
59     {
60         super(url);
61         m_mgr = mgr;
62     }
63
64     public void connect() throws IOException JavaDoc
65     {
66         if (!connected)
67         {
68             // The URL is constructed like this:
69
// bundle://<module-id>/<source-idx>/<resource-path>
70

71             Module module = m_mgr.getModule(url.getHost());
72             if (module == null)
73             {
74                 throw new IOException JavaDoc("Unable to find bundle's module.");
75             }
76
77             String JavaDoc resource = url.getFile();
78             if (resource == null)
79             {
80                 throw new IOException JavaDoc("Unable to find resource: " + url.toString());
81             }
82             if (resource.startsWith("/"))
83             {
84                 resource = resource.substring(1);
85             }
86             int rsIdx = -1;
87             try
88             {
89                 rsIdx = Integer.parseInt(resource.substring(0, resource.indexOf("/")));
90             }
91             catch (NumberFormatException JavaDoc ex)
92             {
93                 new IOException JavaDoc("Error parsing resource index.");
94             }
95             resource = resource.substring(resource.indexOf("/") + 1);
96
97             // Get the resource bytes from the resource source.
98
byte[] bytes = null;
99             ResourceSource[] resSources = module.getResourceSources();
100             if ((resSources != null) && (rsIdx < resSources.length))
101             {
102                 if (resSources[rsIdx].hasResource(resource))
103                 {
104                     bytes = resSources[rsIdx].getBytes(resource);
105                 }
106             }
107
108             if (bytes == null)
109             {
110                 throw new IOException JavaDoc("Unable to find resource: " + url.toString());
111             }
112
113             is = new ByteArrayInputStream JavaDoc(bytes);
114             contentLength = bytes.length;
115             contentTime = 0L; // TODO: Change this.
116
contentType = URLConnection.guessContentTypeFromName(resource);
117             connected = true;
118         }
119     }
120
121     public InputStream JavaDoc getInputStream()
122         throws IOException JavaDoc
123     {
124         if (!connected)
125         {
126             connect();
127         }
128         return is;
129     }
130
131     public int getContentLength()
132     {
133         if (!connected)
134         {
135             try {
136                 connect();
137             } catch(IOException JavaDoc ex) {
138                 return -1;
139             }
140         }
141         return contentLength;
142     }
143
144     public long getLastModified()
145     {
146         if (!connected)
147         {
148             try {
149                 connect();
150             } catch(IOException JavaDoc ex) {
151                 return 0;
152             }
153         }
154         if(contentTime != -1L)
155             return contentTime;
156         else
157             return 0L;
158     }
159
160     public String JavaDoc getContentType()
161     {
162         if (!connected)
163         {
164             try {
165                 connect();
166             } catch(IOException JavaDoc ex) {
167                 return null;
168             }
169         }
170         return contentType;
171     }
172
173     public Permission JavaDoc getPermission()
174     {
175         // TODO: This should probably return a FilePermission
176
// to access the bundle JAR file, but we don't have the
177
// necessary information here to construct the absolute
178
// path of the JAR file...so it would take some
179
// re-arranging to get this to work.
180
return null;
181     }
182 }
Free Books   Free Magazines  
Popular Tags