15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.net.www.protocol.jrt;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.FilePermission;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.MalformedURLException;
34 import java.net.URL;
35 import java.security.AccessController;
36 import java.security.Permission;
37 import java.security.PrivilegedAction;
38 import java.util.List;
39
40 import jdk.internal.jimage.ImageLocation;
41 import jdk.internal.jimage.ImageReader;
42 import jdk.internal.jimage.ImageReaderFactory;
43
44 import jdk.internal.loader.URLClassPath;
45 import jdk.internal.loader.Resource;
46 import sun.net.www.ParseUtil;
47 import sun.net.www.URLConnection;
48
49 /**
50 * URLConnection implementation that can be used to connect to resources
51 * contained in the runtime image.
52 */
53 public class JavaRuntimeURLConnection extends URLConnection {
54
55 // ImageReader to access resources in jimage
56 private static final ImageReader reader = ImageReaderFactory.getImageReader();
57
58 // the module and resource name in the URL
59 private final String module;
60 private final String name;
61
62 // the Resource when connected
63 private volatile Resource resource;
64
65 // the permission to access resources in the runtime image, created lazily
66 private static volatile Permission permission;
67
146 @Override
147 public long getContentLengthLong() {
148 try {
149 connect();
150 return resource.getContentLength();
151 } catch (IOException ioe) {
152 return -1L;
153 }
154 }
155
156 @Override
157 public int getContentLength() {
158 long len = getContentLengthLong();
159 return len > Integer.MAX_VALUE ? -1 : (int)len;
160 }
161
162 @Override
163 public Permission getPermission() throws IOException {
164 Permission p = permission;
165 if (p == null) {
166 // using lambda expression here leads to recursive initialization
167 PrivilegedAction<String> pa = new PrivilegedAction<String>() {
168 public String run() { return System.getProperty("java.home"); }
169 };
170 String home = AccessController.doPrivileged(pa);
171 p = new FilePermission(home + File.separator + "-", "read");
172 permission = p;
173 }
174 return p;
175 }
176
177 /**
178 * Returns a jrt URL for the given module and resource name.
179 */
180 private static URL toJrtURL(String module, String name) {
181 try {
182 return new URL("jrt:/" + module + "/" + name);
183 } catch (MalformedURLException e) {
184 throw new InternalError(e);
185 }
186 }
187
188 /**
189 * Returns a jrt URL for the given module.
190 */
|
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.net.www.protocol.jrt;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.FilePermission;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.MalformedURLException;
34 import java.net.URL;
35 import java.security.Permission;
36
37 import jdk.internal.jimage.ImageLocation;
38 import jdk.internal.jimage.ImageReader;
39 import jdk.internal.jimage.ImageReaderFactory;
40
41 import jdk.internal.loader.URLClassPath;
42 import jdk.internal.loader.Resource;
43 import sun.net.www.ParseUtil;
44 import sun.net.www.URLConnection;
45 import sun.security.action.GetPropertyAction;
46
47 /**
48 * URLConnection implementation that can be used to connect to resources
49 * contained in the runtime image.
50 */
51 public class JavaRuntimeURLConnection extends URLConnection {
52
53 // ImageReader to access resources in jimage
54 private static final ImageReader reader = ImageReaderFactory.getImageReader();
55
56 // the module and resource name in the URL
57 private final String module;
58 private final String name;
59
60 // the Resource when connected
61 private volatile Resource resource;
62
63 // the permission to access resources in the runtime image, created lazily
64 private static volatile Permission permission;
65
144 @Override
145 public long getContentLengthLong() {
146 try {
147 connect();
148 return resource.getContentLength();
149 } catch (IOException ioe) {
150 return -1L;
151 }
152 }
153
154 @Override
155 public int getContentLength() {
156 long len = getContentLengthLong();
157 return len > Integer.MAX_VALUE ? -1 : (int)len;
158 }
159
160 @Override
161 public Permission getPermission() throws IOException {
162 Permission p = permission;
163 if (p == null) {
164 String home = GetPropertyAction.getProperty("java.home");
165 p = new FilePermission(home + File.separator + "-", "read");
166 permission = p;
167 }
168 return p;
169 }
170
171 /**
172 * Returns a jrt URL for the given module and resource name.
173 */
174 private static URL toJrtURL(String module, String name) {
175 try {
176 return new URL("jrt:/" + module + "/" + name);
177 } catch (MalformedURLException e) {
178 throw new InternalError(e);
179 }
180 }
181
182 /**
183 * Returns a jrt URL for the given module.
184 */
|