src/share/jaxws_classes/com/sun/istack/internal/tools/ParallelWorldClassLoader.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.istack.internal.tools;
aoqi@0 27
aoqi@0 28 import java.io.InputStream;
aoqi@0 29 import java.io.ByteArrayOutputStream;
aoqi@0 30 import java.io.Closeable;
aoqi@0 31 import java.io.File;
aoqi@0 32 import java.io.IOException;
aoqi@0 33 import java.net.JarURLConnection;
aoqi@0 34 import java.net.URISyntaxException;
aoqi@0 35 import java.net.URL;
aoqi@0 36 import java.net.MalformedURLException;
aoqi@0 37 import java.net.URLConnection;
aoqi@0 38 import java.util.Collections;
aoqi@0 39 import java.util.Enumeration;
aoqi@0 40 import java.util.HashSet;
aoqi@0 41 import java.util.Set;
aoqi@0 42 import java.util.jar.JarFile;
aoqi@0 43 import java.util.logging.Level;
aoqi@0 44 import java.util.logging.Logger;
aoqi@0 45
aoqi@0 46 /**
aoqi@0 47 * Load classes/resources from a side folder, so that
aoqi@0 48 * classes of the same package can live in a single jar file.
aoqi@0 49 *
aoqi@0 50 * <p>
aoqi@0 51 * For example, with the following jar file:
aoqi@0 52 * <pre>
aoqi@0 53 * /
aoqi@0 54 * +- foo
aoqi@0 55 * +- X.class
aoqi@0 56 * +- bar
aoqi@0 57 * +- X.class
aoqi@0 58 * </pre>
aoqi@0 59 * <p>
aoqi@0 60 * {@link ParallelWorldClassLoader}("foo/") would load <tt>X.class<tt> from
aoqi@0 61 * <tt>/foo/X.class</tt> (note that X is defined in the root package, not
aoqi@0 62 * <tt>foo.X</tt>.
aoqi@0 63 *
aoqi@0 64 * <p>
aoqi@0 65 * This can be combined with {@link MaskingClassLoader} to mask classes which are loaded by the parent
aoqi@0 66 * class loader so that the child class loader
aoqi@0 67 * classes living in different folders are loaded
aoqi@0 68 * before the parent class loader loads classes living the jar file publicly
aoqi@0 69 * visible
aoqi@0 70 * For example, with the following jar file:
aoqi@0 71 * <pre>
aoqi@0 72 * /
aoqi@0 73 * +- foo
aoqi@0 74 * +- X.class
aoqi@0 75 * +- bar
aoqi@0 76 * +-foo
aoqi@0 77 * +- X.class
aoqi@0 78 * </pre>
aoqi@0 79 * <p>
aoqi@0 80 * {@link ParallelWorldClassLoader}(MaskingClassLoader.class.getClassLoader()) would load <tt>foo.X.class<tt> from
aoqi@0 81 * <tt>/bar/foo.X.class</tt> not the <tt>foo.X.class<tt> in the publicly visible place in the jar file, thus
aoqi@0 82 * masking the parent classLoader from loading the class from <tt>foo.X.class<tt>
aoqi@0 83 * (note that X is defined in the package foo, not
aoqi@0 84 * <tt>bar.foo.X</tt>.
aoqi@0 85 *
aoqi@0 86 * @author Kohsuke Kawaguchi
aoqi@0 87 */
aoqi@0 88 public class ParallelWorldClassLoader extends ClassLoader implements Closeable {
aoqi@0 89
aoqi@0 90 /**
aoqi@0 91 * Strings like "prefix/", "abc/", or "" to indicate
aoqi@0 92 * classes should be loaded normally.
aoqi@0 93 */
aoqi@0 94 private final String prefix;
aoqi@0 95 private final Set<JarFile> jars;
aoqi@0 96
aoqi@0 97 public ParallelWorldClassLoader(ClassLoader parent,String prefix) {
aoqi@0 98 super(parent);
aoqi@0 99 this.prefix = prefix;
aoqi@0 100 jars = Collections.synchronizedSet(new HashSet<JarFile>());
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 protected Class findClass(String name) throws ClassNotFoundException {
aoqi@0 104
aoqi@0 105 StringBuffer sb = new StringBuffer(name.length()+prefix.length()+6);
aoqi@0 106 sb.append(prefix).append(name.replace('.','/')).append(".class");
aoqi@0 107
aoqi@0 108 URL u = getParent().getResource(sb.toString());
aoqi@0 109 if (u == null) {
aoqi@0 110 throw new ClassNotFoundException(name);
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 InputStream is = null;
aoqi@0 114 URLConnection con = null;
aoqi@0 115
aoqi@0 116 try {
aoqi@0 117 con = u.openConnection();
aoqi@0 118 is = con.getInputStream();
aoqi@0 119 } catch (IOException ioe) {
aoqi@0 120 throw new ClassNotFoundException(name);
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 if (is==null)
aoqi@0 124 throw new ClassNotFoundException(name);
aoqi@0 125
aoqi@0 126 try {
aoqi@0 127 ByteArrayOutputStream baos = new ByteArrayOutputStream();
aoqi@0 128 byte[] buf = new byte[1024];
aoqi@0 129 int len;
aoqi@0 130 while((len=is.read(buf))>=0)
aoqi@0 131 baos.write(buf,0,len);
aoqi@0 132
aoqi@0 133 buf = baos.toByteArray();
aoqi@0 134 int packIndex = name.lastIndexOf('.');
aoqi@0 135 if (packIndex != -1) {
aoqi@0 136 String pkgname = name.substring(0, packIndex);
aoqi@0 137 // Check if package already loaded.
aoqi@0 138 Package pkg = getPackage(pkgname);
aoqi@0 139 if (pkg == null) {
aoqi@0 140 definePackage(pkgname, null, null, null, null, null, null, null);
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 return defineClass(name,buf,0,buf.length);
aoqi@0 144 } catch (IOException e) {
aoqi@0 145 throw new ClassNotFoundException(name,e);
aoqi@0 146 } finally {
aoqi@0 147 try {
aoqi@0 148 if (con != null && con instanceof JarURLConnection) {
aoqi@0 149 jars.add(((JarURLConnection) con).getJarFile());
aoqi@0 150 }
aoqi@0 151 } catch (IOException ioe) {
aoqi@0 152 //ignore
aoqi@0 153 }
aoqi@0 154 if (is != null) {
aoqi@0 155 try {
aoqi@0 156 is.close();
aoqi@0 157 } catch (IOException ioe) {
aoqi@0 158 //ignore
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161 }
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 @Override
aoqi@0 165 protected URL findResource(String name) {
aoqi@0 166 URL u = getParent().getResource(prefix + name);
aoqi@0 167 if (u != null) {
aoqi@0 168 try {
aoqi@0 169 jars.add(new JarFile(new File(toJarUrl(u).toURI())));
aoqi@0 170 } catch (URISyntaxException ex) {
aoqi@0 171 Logger.getLogger(ParallelWorldClassLoader.class.getName()).log(Level.WARNING, null, ex);
aoqi@0 172 } catch (IOException ex) {
aoqi@0 173 Logger.getLogger(ParallelWorldClassLoader.class.getName()).log(Level.WARNING, null, ex);
aoqi@0 174 } catch (ClassNotFoundException ex) {
aoqi@0 175 //ignore - not a jar
aoqi@0 176 }
aoqi@0 177 }
aoqi@0 178 return u;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 @Override
aoqi@0 182 protected Enumeration<URL> findResources(String name) throws IOException {
aoqi@0 183 Enumeration<URL> en = getParent().getResources(prefix + name);
aoqi@0 184 while (en.hasMoreElements()) {
aoqi@0 185 try {
aoqi@0 186 jars.add(new JarFile(new File(toJarUrl(en.nextElement()).toURI())));
aoqi@0 187 } catch (URISyntaxException ex) {
aoqi@0 188 //should not happen
aoqi@0 189 Logger.getLogger(ParallelWorldClassLoader.class.getName()).log(Level.WARNING, null, ex);
aoqi@0 190 } catch (IOException ex) {
aoqi@0 191 Logger.getLogger(ParallelWorldClassLoader.class.getName()).log(Level.WARNING, null, ex);
aoqi@0 192 } catch (ClassNotFoundException ex) {
aoqi@0 193 //ignore - not a jar
aoqi@0 194 }
aoqi@0 195 }
aoqi@0 196 return en;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 public synchronized void close() throws IOException {
aoqi@0 200 for (JarFile jar : jars) {
aoqi@0 201 jar.close();
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 /**
aoqi@0 206 * Given the URL inside jar, returns the URL to the jar itself.
aoqi@0 207 */
aoqi@0 208 public static URL toJarUrl(URL res) throws ClassNotFoundException, MalformedURLException {
aoqi@0 209 String url = res.toExternalForm();
aoqi@0 210 if(!url.startsWith("jar:"))
aoqi@0 211 throw new ClassNotFoundException("Loaded outside a jar "+url);
aoqi@0 212 url = url.substring(4); // cut off jar:
aoqi@0 213 url = url.substring(0,url.lastIndexOf('!')); // cut off everything after '!'
aoqi@0 214 url = url.replaceAll(" ", "%20"); // support white spaces in path
aoqi@0 215 return new URL(url);
aoqi@0 216 }
aoqi@0 217 }

mercurial