|
1 | 1 | package io.quarkus.bootstrap.runner;
|
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
3 | 6 | import java.io.IOException;
|
4 | 7 | import java.io.InputStream;
|
5 | 8 | import java.net.MalformedURLException;
|
6 | 9 | import java.net.URI;
|
7 | 10 | import java.net.URISyntaxException;
|
8 | 11 | import java.net.URL;
|
| 12 | +import java.net.URLConnection; |
| 13 | +import java.net.URLStreamHandler; |
9 | 14 | import java.nio.file.Path;
|
10 | 15 | import java.security.CodeSource;
|
11 | 16 | import java.security.ProtectionDomain;
|
@@ -44,7 +49,7 @@ public void init() {
|
44 | 49 | path = '/' + path;
|
45 | 50 | }
|
46 | 51 | URI uri = new URI("file", null, path, null);
|
47 |
| - url = uri.toURL(); |
| 52 | + url = new URL((URL) null, uri.toString(), new JarUrlStreamHandler(uri)); |
48 | 53 | } catch (URISyntaxException | MalformedURLException e) {
|
49 | 54 | throw new RuntimeException("Unable to create protection domain for " + jarPath, e);
|
50 | 55 | }
|
@@ -201,4 +206,73 @@ public boolean equals(Object o) {
|
201 | 206 | public int hashCode() {
|
202 | 207 | return Objects.hashCode(jarPath);
|
203 | 208 | }
|
| 209 | + |
| 210 | + /** |
| 211 | + * This URLStreamHandler is designed to handle only one jar, which is the one passed in the constructor. |
| 212 | + * The goal here is to cache the external form of the URL. |
| 213 | + * <p> |
| 214 | + * Do not use this class outside of this extremely specific purpose. |
| 215 | + */ |
| 216 | + private static class JarUrlStreamHandler extends URLStreamHandler { |
| 217 | + |
| 218 | + private final String externalForm; |
| 219 | + |
| 220 | + private JarUrlStreamHandler(URI uri) { |
| 221 | + this.externalForm = "file:".concat(uri.getRawPath()); |
| 222 | + // while it would be more optimized to store the URI here for when we open connections |
| 223 | + // opening a connection for ProtectionDomains is actually extremely rare |
| 224 | + // and never done in production at runtime so we favored reducing memory allocations for the common case |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + protected URLConnection openConnection(URL u) throws IOException { |
| 229 | + return new JarURLConnection(u); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + protected String toExternalForm(URL u) { |
| 234 | + return externalForm; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + private static class JarURLConnection extends URLConnection { |
| 239 | + |
| 240 | + private final File file; |
| 241 | + |
| 242 | + private JarURLConnection(URL url) throws IOException { |
| 243 | + super(url); |
| 244 | + try { |
| 245 | + this.file = new File(url.toURI()); |
| 246 | + } catch (URISyntaxException e) { |
| 247 | + throw new IOException(e); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public void connect() throws IOException { |
| 253 | + if (!file.exists()) { |
| 254 | + throw new FileNotFoundException(file.getAbsolutePath()); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public InputStream getInputStream() throws IOException { |
| 260 | + return new FileInputStream(file); |
| 261 | + } |
| 262 | + |
| 263 | + @Override |
| 264 | + public int getContentLength() { |
| 265 | + return (int) file.length(); |
| 266 | + } |
| 267 | + |
| 268 | + @Override |
| 269 | + public long getContentLengthLong() { |
| 270 | + return file.length(); |
| 271 | + } |
| 272 | + |
| 273 | + @Override |
| 274 | + public String getContentType() { |
| 275 | + return "application/java-archive"; |
| 276 | + } |
| 277 | + } |
204 | 278 | }
|
0 commit comments