001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc;
038:
039: import java.net.MalformedURLException;
040: import java.net.URL;
041: import java.net.URLClassLoader;
042: import java.util.ArrayList;
043: import java.util.Arrays;
044: import java.util.List;
045:
046: import javax.xml.bind.JAXBContext;
047:
048: import com.sun.istack.tools.MaskingClassLoader;
049: import com.sun.istack.tools.ParallelWorldClassLoader;
050:
051: /**
052: * Creates a class loader configured to run XJC 1.0/2.0 safely without
053: * interference with JAXB 2.0 API in Mustang.
054: *
055: * @author Kohsuke Kawaguchi
056: */
057: class ClassLoaderBuilder {
058:
059: /**
060: * Creates a new class loader that eventually delegates to the given {@link ClassLoader}
061: * such that XJC can be loaded by using this classloader.
062: *
063: * @param v
064: * Either "1.0" or "2.0", indicating the version of the -source value.
065: */
066: protected static ClassLoader createProtectiveClassLoader(
067: ClassLoader cl, String v) throws ClassNotFoundException,
068: MalformedURLException {
069: if (noHack)
070: return cl; // provide an escape hatch
071:
072: boolean mustang = false;
073:
074: if (JAXBContext.class.getClassLoader() == null) {
075: // JAXB API is loaded from the bootstrap. We need to override one with ours
076: mustang = true;
077:
078: List mask = new ArrayList(Arrays.asList(maskedPackages));
079: mask.add("javax.xml.bind.");
080:
081: cl = new MaskingClassLoader(cl, mask);
082:
083: URL apiUrl = cl
084: .getResource("javax/xml/bind/annotation/XmlSeeAlso.class");
085: if (apiUrl == null)
086: throw new ClassNotFoundException(
087: "There's no JAXB 2.1 API in the classpath");
088:
089: cl = new URLClassLoader(
090: new URL[] { ParallelWorldClassLoader
091: .toJarUrl(apiUrl) }, cl);
092: }
093:
094: //Leave XJC2 in the publicly visible place
095: // and then isolate XJC1 in a child class loader,
096: // then use a MaskingClassLoader
097: // so that the XJC2 classes in the parent class loader
098: // won't interfere with loading XJC1 classes in a child class loader
099:
100: if (v.equals("1.0")) {
101: if (!mustang)
102: // if we haven't used Masking ClassLoader, do so now.
103: cl = new MaskingClassLoader(cl, toolPackages);
104: cl = new ParallelWorldClassLoader(cl, "1.0/");
105: } else {
106: if (mustang)
107: // the whole RI needs to be loaded in a separate class loader
108: cl = new ParallelWorldClassLoader(cl, "");
109: }
110:
111: return cl;
112: }
113:
114: /**
115: * The list of package prefixes we want the
116: * {@link MaskingClassLoader} to prevent the parent
117: * classLoader from loading
118: */
119: private static String[] maskedPackages = new String[] {
120: // toolPackages + alpha
121: "com.sun.tools.", "com.sun.codemodel.", "com.sun.relaxng.",
122: "com.sun.xml.xsom.", "com.sun.xml.bind.", };
123:
124: private static String[] toolPackages = new String[] {
125: "com.sun.tools.", "com.sun.codemodel.", "com.sun.relaxng.",
126: "com.sun.xml.xsom." };
127:
128: /**
129: * Escape hatch in case this class loader hack breaks.
130: */
131: public static final boolean noHack = Boolean
132: .getBoolean(XJCFacade.class.getName() + ".nohack");
133: }
|