17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils.protocol;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.utils.SystemUtils.PropertyList;
|
|
|
17 |
import org.openconcerto.utils.protocol.jarjar.Handler;
|
|
|
18 |
|
|
|
19 |
import java.net.MalformedURLException;
|
|
|
20 |
import java.net.URL;
|
|
|
21 |
import java.net.URLStreamHandler;
|
|
|
22 |
import java.net.URLStreamHandlerFactory;
|
|
|
23 |
|
|
|
24 |
public class Helper {
|
|
|
25 |
|
|
|
26 |
static private final PropertyList PL = new PropertyList("java.protocol.handler.pkgs", "|");
|
|
|
27 |
|
|
|
28 |
static final public void register() {
|
177 |
ilm |
29 |
// Works even if setURLStreamHandlerFactory() is called (as long as the factory returns null
|
17 |
ilm |
30 |
// for our protocols)
|
177 |
ilm |
31 |
// On Java 11, there's also URLStreamHandlerProvider
|
17 |
ilm |
32 |
PL.add(Helper.class.getPackage().getName());
|
|
|
33 |
}
|
|
|
34 |
|
177 |
ilm |
35 |
static final public void unregister() {
|
|
|
36 |
PL.remove(Helper.class.getPackage().getName());
|
|
|
37 |
}
|
|
|
38 |
|
17 |
ilm |
39 |
/**
|
|
|
40 |
* Set the {@link URL#setURLStreamHandlerFactory(URLStreamHandlerFactory) factory} to add our
|
|
|
41 |
* protocols. This is needed for example in web start when one of our url is embedded into a
|
|
|
42 |
* library supplied one. E.g. "jar:jarjar:file:/C:/mylibs/Outer.jar^/Inner.jar!/" will cause the
|
|
|
43 |
* jar Handler to try to create a jarjar URL but its classloader cannot access our classes
|
|
|
44 |
* (loaded by JNLPClassLoader).
|
|
|
45 |
*/
|
|
|
46 |
static final public void setURLStreamHandlerFactory() {
|
|
|
47 |
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
|
|
|
48 |
@Override
|
|
|
49 |
public URLStreamHandler createURLStreamHandler(String protocol) {
|
|
|
50 |
if (protocol.equals("jarjar"))
|
|
|
51 |
return new Handler();
|
|
|
52 |
else
|
|
|
53 |
return null;
|
|
|
54 |
}
|
|
|
55 |
});
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
180 |
ilm |
59 |
* Return a jar URL to the root of a jar file. Needed since the JRE cannot read files inside a
|
|
|
60 |
* jar inside a jar.
|
17 |
ilm |
61 |
*
|
|
|
62 |
* @param u the URL to wrap, e.g. "jar:file:/C:/mylibs/Outer.jar!/Inner.jar".
|
180 |
ilm |
63 |
* @return the wrapped URL, e.g. "jar:jarjar:file:/C:/mylibs/Outer.jar^/Inner.jar!/".
|
17 |
ilm |
64 |
*/
|
180 |
ilm |
65 |
public static final URL intoJar(final URL u) {
|
|
|
66 |
return intoJar(u, "");
|
177 |
ilm |
67 |
}
|
|
|
68 |
|
180 |
ilm |
69 |
public static final URL intoJar(final URL u, final String s) {
|
|
|
70 |
if (!u.getPath().endsWith(".jar"))
|
|
|
71 |
throw new IllegalArgumentException("Doesn't end with .jar :" + u);
|
|
|
72 |
|
|
|
73 |
final URL res;
|
17 |
ilm |
74 |
// if it's a jar inside another jar
|
180 |
ilm |
75 |
if ("jar".equals(u.getProtocol())) {
|
17 |
ilm |
76 |
try {
|
180 |
ilm |
77 |
res = new URL("jar:jar" + u.toExternalForm().replace('!', '^') + "!/" + s);
|
17 |
ilm |
78 |
} catch (MalformedURLException e) {
|
|
|
79 |
// shouldn't happen since we modify a valid URL
|
180 |
ilm |
80 |
throw new IllegalStateException("Couldn't transform to jarjar " + u, e);
|
17 |
ilm |
81 |
}
|
180 |
ilm |
82 |
} else {
|
|
|
83 |
try {
|
|
|
84 |
res = new URL("jar:" + u.toExternalForm() + "!/" + s);
|
|
|
85 |
} catch (MalformedURLException e) {
|
|
|
86 |
// shouldn't happen since constructed from a valid URL
|
|
|
87 |
throw new IllegalStateException("Couldn't transform to jar URL " + u, e);
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
return res;
|
17 |
ilm |
91 |
}
|
|
|
92 |
|
|
|
93 |
}
|