src/share/jaxws_classes/com/sun/tools/internal/xjc/ClassLoaderBuilder.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/ClassLoaderBuilder.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,122 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.xjc;
    1.30 +
    1.31 +import java.net.MalformedURLException;
    1.32 +import java.net.URL;
    1.33 +import java.net.URLClassLoader;
    1.34 +import java.util.ArrayList;
    1.35 +import java.util.Arrays;
    1.36 +import java.util.List;
    1.37 +
    1.38 +import javax.xml.bind.JAXBContext;
    1.39 +
    1.40 +import com.sun.istack.internal.tools.MaskingClassLoader;
    1.41 +import com.sun.istack.internal.tools.ParallelWorldClassLoader;
    1.42 +
    1.43 +/**
    1.44 + * Creates a class loader configured to run XJC 1.0/2.0 safely without
    1.45 + * interference with JAXB 2.0 API in Mustang.
    1.46 + *
    1.47 + * @author Kohsuke Kawaguchi
    1.48 + */
    1.49 +class ClassLoaderBuilder {
    1.50 +
    1.51 +    /**
    1.52 +     * Creates a new class loader that eventually delegates to the given {@link ClassLoader}
    1.53 +     * such that XJC can be loaded by using this classloader.
    1.54 +     *
    1.55 +     * @param v
    1.56 +     *      Either "1.0" or "2.0", indicating the version of the -source value.
    1.57 +     */
    1.58 +    protected static ClassLoader createProtectiveClassLoader(ClassLoader cl, String v) throws ClassNotFoundException, MalformedURLException {
    1.59 +        if(noHack)  return cl;  // provide an escape hatch
    1.60 +
    1.61 +        boolean mustang = false;
    1.62 +
    1.63 +        if (SecureLoader.getClassClassLoader(JAXBContext.class) == null) {
    1.64 +            // JAXB API is loaded from the bootstrap. We need to override one with ours
    1.65 +            mustang = true;
    1.66 +
    1.67 +            List<String> mask = new ArrayList<String>(Arrays.asList(maskedPackages));
    1.68 +            mask.add("javax.xml.bind.");
    1.69 +
    1.70 +            cl = new MaskingClassLoader(cl,mask);
    1.71 +
    1.72 +            URL apiUrl = cl.getResource("javax/xml/bind/JAXBPermission.class");
    1.73 +            if(apiUrl==null)
    1.74 +                throw new ClassNotFoundException("There's no JAXB 2.2 API in the classpath");
    1.75 +
    1.76 +            cl = new URLClassLoader(new URL[]{ParallelWorldClassLoader.toJarUrl(apiUrl)},cl);
    1.77 +        }
    1.78 +
    1.79 +        //Leave XJC2 in the publicly visible place
    1.80 +        // and then isolate XJC1 in a child class loader,
    1.81 +        // then use a MaskingClassLoader
    1.82 +        // so that the XJC2 classes in the parent class loader
    1.83 +        //  won't interfere with loading XJC1 classes in a child class loader
    1.84 +
    1.85 +        if ("1.0".equals(v)) {
    1.86 +            if(!mustang)
    1.87 +                // if we haven't used Masking ClassLoader, do so now.
    1.88 +                cl = new MaskingClassLoader(cl,toolPackages);
    1.89 +            cl = new ParallelWorldClassLoader(cl,"1.0/");
    1.90 +        } else {
    1.91 +            if(mustang)
    1.92 +                // the whole RI needs to be loaded in a separate class loader
    1.93 +                cl = new ParallelWorldClassLoader(cl,"");
    1.94 +        }
    1.95 +
    1.96 +        return cl;
    1.97 +    }
    1.98 +
    1.99 +
   1.100 +    /**
   1.101 +     * The list of package prefixes we want the
   1.102 +     * {@link MaskingClassLoader} to prevent the parent
   1.103 +     * classLoader from loading
   1.104 +     */
   1.105 +    private static String[] maskedPackages = new String[]{
   1.106 +        // toolPackages + alpha
   1.107 +        "com.sun.tools.",
   1.108 +        "com.sun.codemodel.internal.",
   1.109 +        "com.sun.relaxng.",
   1.110 +        "com.sun.xml.internal.xsom.",
   1.111 +        "com.sun.xml.internal.bind.",
   1.112 +    };
   1.113 +
   1.114 +    private static String[] toolPackages = new String[]{
   1.115 +        "com.sun.tools.",
   1.116 +        "com.sun.codemodel.internal.",
   1.117 +        "com.sun.relaxng.",
   1.118 +        "com.sun.xml.internal.xsom."
   1.119 +    };
   1.120 +
   1.121 +    /**
   1.122 +     * Escape hatch in case this class loader hack breaks.
   1.123 +     */
   1.124 +    public static final boolean noHack = Boolean.getBoolean(XJCFacade.class.getName()+".nohack");
   1.125 +}

mercurial