Richard S. Hall | 930fecc | 2005-08-16 18:33:34 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | package org.apache.osgi.framework; |
| 18 | |
| 19 | import java.io.*; |
| 20 | import java.net.URL; |
| 21 | import java.net.URLConnection; |
| 22 | import java.security.Permission; |
| 23 | |
| 24 | import org.apache.osgi.moduleloader.*; |
| 25 | |
| 26 | class BundleURLConnection extends URLConnection |
| 27 | { |
| 28 | private ModuleManager m_mgr = null; |
| 29 | private int contentLength; |
| 30 | private long contentTime; |
| 31 | private String contentType; |
| 32 | private InputStream is; |
| 33 | |
| 34 | public BundleURLConnection(ModuleManager mgr, URL url) |
| 35 | { |
| 36 | super(url); |
| 37 | m_mgr = mgr; |
| 38 | } |
| 39 | |
| 40 | public void connect() throws IOException |
| 41 | { |
| 42 | if (!connected) |
| 43 | { |
| 44 | // The URL is constructed like this: |
| 45 | // bundle://<module-id>/<source-idx>/<resource-path> |
| 46 | |
| 47 | Module module = m_mgr.getModule(url.getHost()); |
| 48 | if (module == null) |
| 49 | { |
| 50 | throw new IOException("Unable to find bundle's module."); |
| 51 | } |
| 52 | |
| 53 | String resource = url.getFile(); |
| 54 | if (resource == null) |
| 55 | { |
| 56 | throw new IOException("Unable to find resource: " + url.toString()); |
| 57 | } |
| 58 | if (resource.startsWith("/")) |
| 59 | { |
| 60 | resource = resource.substring(1); |
| 61 | } |
| 62 | int rsIdx = -1; |
| 63 | try |
| 64 | { |
| 65 | rsIdx = Integer.parseInt(resource.substring(0, resource.indexOf("/"))); |
| 66 | } |
| 67 | catch (NumberFormatException ex) |
| 68 | { |
| 69 | new IOException("Error parsing resource index."); |
| 70 | } |
| 71 | resource = resource.substring(resource.indexOf("/") + 1); |
| 72 | |
| 73 | // Get the resource bytes from the resource source. |
| 74 | byte[] bytes = null; |
| 75 | ResourceSource[] resSources = module.getResourceSources(); |
| 76 | if ((resSources != null) && (rsIdx < resSources.length)) |
| 77 | { |
| 78 | if (resSources[rsIdx].hasResource(resource)) |
| 79 | { |
| 80 | bytes = resSources[rsIdx].getBytes(resource); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (bytes == null) |
| 85 | { |
| 86 | throw new IOException("Unable to find resource: " + url.toString()); |
| 87 | } |
| 88 | |
| 89 | is = new ByteArrayInputStream(bytes); |
| 90 | contentLength = bytes.length; |
| 91 | contentTime = 0L; // TODO: Change this. |
| 92 | contentType = URLConnection.guessContentTypeFromName(resource); |
| 93 | connected = true; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public InputStream getInputStream() |
| 98 | throws IOException |
| 99 | { |
| 100 | if (!connected) |
| 101 | { |
| 102 | connect(); |
| 103 | } |
| 104 | return is; |
| 105 | } |
| 106 | |
| 107 | public int getContentLength() |
| 108 | { |
| 109 | if (!connected) |
| 110 | { |
| 111 | try { |
| 112 | connect(); |
| 113 | } catch(IOException ex) { |
| 114 | return -1; |
| 115 | } |
| 116 | } |
| 117 | return contentLength; |
| 118 | } |
| 119 | |
| 120 | public long getLastModified() |
| 121 | { |
| 122 | if (!connected) |
| 123 | { |
| 124 | try { |
| 125 | connect(); |
| 126 | } catch(IOException ex) { |
| 127 | return 0; |
| 128 | } |
| 129 | } |
| 130 | if(contentTime != -1L) |
| 131 | return contentTime; |
| 132 | else |
| 133 | return 0L; |
| 134 | } |
| 135 | |
| 136 | public String getContentType() |
| 137 | { |
| 138 | if (!connected) |
| 139 | { |
| 140 | try { |
| 141 | connect(); |
| 142 | } catch(IOException ex) { |
| 143 | return null; |
| 144 | } |
| 145 | } |
| 146 | return contentType; |
| 147 | } |
| 148 | |
| 149 | public Permission getPermission() |
| 150 | { |
| 151 | // TODO: This should probably return a FilePermission |
| 152 | // to access the bundle JAR file, but we don't have the |
| 153 | // necessary information here to construct the absolute |
| 154 | // path of the JAR file...so it would take some |
| 155 | // re-arranging to get this to work. |
| 156 | return null; |
| 157 | } |
| 158 | } |