author | redestad |
Tue, 03 May 2016 15:50:54 +0200 | |
changeset 37781 | 71ed5645f17c |
parent 37593 | 824750ada3d6 |
child 43712 | 5dfd0950317c |
permissions | -rw-r--r-- |
27565 | 1 |
/* |
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
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 |
||
36511 | 28 |
import java.io.ByteArrayInputStream; |
27565 | 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 |
||
36511 | 37 |
import jdk.internal.jimage.ImageLocation; |
38 |
import jdk.internal.jimage.ImageReader; |
|
39 |
import jdk.internal.jimage.ImageReaderFactory; |
|
40 |
||
36674
7ab530dd6f10
8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents:
36511
diff
changeset
|
41 |
import jdk.internal.loader.URLClassPath; |
7ab530dd6f10
8149122: Move sun.misc.URLClassPath and Resouce to an internal package
chegar
parents:
36511
diff
changeset
|
42 |
import jdk.internal.loader.Resource; |
27565 | 43 |
import sun.net.www.ParseUtil; |
44 |
import sun.net.www.URLConnection; |
|
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36674
diff
changeset
|
45 |
import sun.security.action.GetPropertyAction; |
27565 | 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 |
||
36511 | 53 |
// ImageReader to access resources in jimage |
54 |
private static final ImageReader reader = ImageReaderFactory.getImageReader(); |
|
27565 | 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 |
||
66 |
JavaRuntimeURLConnection(URL url) throws IOException { |
|
67 |
super(url); |
|
68 |
String path = url.getPath(); |
|
69 |
if (path.length() == 0 || path.charAt(0) != '/') |
|
70 |
throw new MalformedURLException(url + " missing path or /"); |
|
71 |
if (path.length() == 1) { |
|
72 |
this.module = null; |
|
73 |
this.name = null; |
|
74 |
} else { |
|
75 |
int pos = path.indexOf('/', 1); |
|
76 |
if (pos == -1) { |
|
77 |
this.module = path.substring(1); |
|
78 |
this.name = null; |
|
79 |
} else { |
|
80 |
this.module = path.substring(1, pos); |
|
81 |
this.name = ParseUtil.decode(path.substring(pos+1)); |
|
82 |
} |
|
83 |
} |
|
84 |
} |
|
85 |
||
36511 | 86 |
/** |
87 |
* Finds a resource in a module, returning {@code null} if the resource |
|
88 |
* is not found. |
|
89 |
*/ |
|
90 |
private static Resource findResource(String module, String name) { |
|
91 |
if (reader != null) { |
|
92 |
URL url = toJrtURL(module, name); |
|
93 |
ImageLocation location = reader.findLocation(module, name); |
|
94 |
if (location != null && URLClassPath.checkURL(url) != null) { |
|
95 |
return new Resource() { |
|
96 |
@Override |
|
97 |
public String getName() { |
|
98 |
return name; |
|
99 |
} |
|
100 |
@Override |
|
101 |
public URL getURL() { |
|
102 |
return url; |
|
103 |
} |
|
104 |
@Override |
|
105 |
public URL getCodeSourceURL() { |
|
106 |
return toJrtURL(module); |
|
107 |
} |
|
108 |
@Override |
|
109 |
public InputStream getInputStream() throws IOException { |
|
110 |
byte[] resource = reader.getResource(location); |
|
111 |
return new ByteArrayInputStream(resource); |
|
112 |
} |
|
113 |
@Override |
|
114 |
public int getContentLength() { |
|
115 |
long size = location.getUncompressedSize(); |
|
116 |
return (size > Integer.MAX_VALUE) ? -1 : (int) size; |
|
117 |
} |
|
118 |
}; |
|
119 |
} |
|
120 |
} |
|
121 |
return null; |
|
122 |
} |
|
123 |
||
27565 | 124 |
@Override |
125 |
public synchronized void connect() throws IOException { |
|
126 |
if (!connected) { |
|
127 |
if (name == null) { |
|
128 |
String s = (module == null) ? "" : module; |
|
129 |
throw new IOException("cannot connect to jrt:/" + s); |
|
130 |
} |
|
36511 | 131 |
resource = findResource(module, name); |
27565 | 132 |
if (resource == null) |
133 |
throw new IOException(module + "/" + name + " not found"); |
|
134 |
connected = true; |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
@Override |
|
139 |
public InputStream getInputStream() throws IOException { |
|
140 |
connect(); |
|
141 |
return resource.getInputStream(); |
|
142 |
} |
|
143 |
||
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) { |
|
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
164 |
String home = GetPropertyAction.privilegedGetProperty("java.home"); |
27565 | 165 |
p = new FilePermission(home + File.separator + "-", "read"); |
166 |
permission = p; |
|
167 |
} |
|
168 |
return p; |
|
169 |
} |
|
36511 | 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 |
*/ |
|
185 |
private static URL toJrtURL(String module) { |
|
186 |
try { |
|
187 |
return new URL("jrt:/" + module); |
|
188 |
} catch (MalformedURLException e) { |
|
189 |
throw new InternalError(e); |
|
190 |
} |
|
191 |
} |
|
27565 | 192 |
} |