1 /*
   2  * Copyright (c) 1997, 2011, 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 com.sun.tools.internal.xjc;
  27 
  28 import java.net.MalformedURLException;
  29 import java.net.URL;
  30 import java.net.URLClassLoader;
  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.List;
  34 
  35 import javax.xml.bind.JAXBContext;
  36 
  37 import com.sun.istack.internal.tools.MaskingClassLoader;
  38 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
  39 import com.sun.tools.internal.xjc.SecureLoader;
  40 
  41 /**
  42  * Creates a class loader configured to run XJC 1.0/2.0 safely without
  43  * interference with JAXB 2.0 API in Mustang.
  44  *
  45  * @author Kohsuke Kawaguchi
  46  */
  47 class ClassLoaderBuilder {
  48 
  49     /**
  50      * Creates a new class loader that eventually delegates to the given {@link ClassLoader}
  51      * such that XJC can be loaded by using this classloader.
  52      *
  53      * @param v
  54      *      Either "1.0" or "2.0", indicating the version of the -source value.
  55      */
  56     protected static ClassLoader createProtectiveClassLoader(ClassLoader cl, String v) throws ClassNotFoundException, MalformedURLException {
  57         if(noHack)  return cl;  // provide an escape hatch
  58 
  59         boolean mustang = false;
  60 
  61         if (SecureLoader.getClassClassLoader(JAXBContext.class) == null) {
  62             // JAXB API is loaded from the bootstrap. We need to override one with ours
  63             mustang = true;
  64 
  65             List mask = new ArrayList(Arrays.asList(maskedPackages));
  66             mask.add("javax.xml.bind.");
  67 
  68             cl = new MaskingClassLoader(cl,mask);
  69 
  70             URL apiUrl = cl.getResource("javax/xml/bind/JAXBPermission.class");
  71             if(apiUrl==null)
  72                 throw new ClassNotFoundException("There's no JAXB 2.2 API in the classpath");
  73 
  74             cl = new URLClassLoader(new URL[]{ParallelWorldClassLoader.toJarUrl(apiUrl)},cl);
  75         }
  76 
  77         //Leave XJC2 in the publicly visible place
  78         // and then isolate XJC1 in a child class loader,
  79         // then use a MaskingClassLoader
  80         // so that the XJC2 classes in the parent class loader
  81         //  won't interfere with loading XJC1 classes in a child class loader
  82 
  83         if (v.equals("1.0")) {
  84             if(!mustang)
  85                 // if we haven't used Masking ClassLoader, do so now.
  86                 cl = new MaskingClassLoader(cl,toolPackages);
  87             cl = new ParallelWorldClassLoader(cl,"1.0/");
  88         } else {
  89             if(mustang)
  90                 // the whole RI needs to be loaded in a separate class loader
  91                 cl = new ParallelWorldClassLoader(cl,"");
  92         }
  93 
  94         return cl;
  95     }
  96 
  97 
  98     /**
  99      * The list of package prefixes we want the
 100      * {@link MaskingClassLoader} to prevent the parent
 101      * classLoader from loading
 102      */
 103     private static String[] maskedPackages = new String[]{
 104         // toolPackages + alpha
 105         "com.sun.tools.",
 106         "com.sun.codemodel.internal.",
 107         "com.sun.relaxng.",
 108         "com.sun.xml.internal.xsom.",
 109         "com.sun.xml.internal.bind.",
 110     };
 111 
 112     private static String[] toolPackages = new String[]{
 113         "com.sun.tools.",
 114         "com.sun.codemodel.internal.",
 115         "com.sun.relaxng.",
 116         "com.sun.xml.internal.xsom."
 117     };
 118 
 119     /**
 120      * Escape hatch in case this class loader hack breaks.
 121      */
 122     public static final boolean noHack = Boolean.getBoolean(XJCFacade.class.getName()+".nohack");
 123 }