OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
    /**
59
     * Wrap the passed URL into a {@link Handler jarjar} one. Needed since the jre cannot read files
60
     * inside a jar inside a jar.
61
     *
62
     * @param u the URL to wrap, e.g. "jar:file:/C:/mylibs/Outer.jar!/Inner.jar".
63
     * @return the wrapped URL, if necessary, i.e. if <code>u</code> references a jar in a jar, e.g.
64
     *         "jar:jarjar:file:/C:/mylibs/Outer.jar^/Inner.jar!/".
65
     */
66
    public static final URL toJarJar(URL u) {
177 ilm 67
        return toJarJar(u, "");
68
    }
69
 
70
    public static final URL toJarJar(final URL u, final String s) {
17 ilm 71
        // if it's a jar inside another jar
72
        if ("jar".equals(u.getProtocol()) && u.getPath().endsWith(".jar")) {
73
            try {
177 ilm 74
                return new URL("jar:jar" + u.toString().replace('!', '^') + "!/" + s);
17 ilm 75
            } catch (MalformedURLException e) {
76
                // shouldn't happen since we modify a valid URL
77
                throw new IllegalStateException("Couldn't transform " + u, e);
78
            }
79
        } else
80
            return u;
81
    }
82
 
83
}