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