jjg@372: /* ohair@554: * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. jjg@372: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@372: * jjg@372: * This code is free software; you can redistribute it and/or modify it jjg@372: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@372: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. jjg@372: * jjg@372: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@372: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@372: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@372: * version 2 for more details (a copy is included in the LICENSE file that jjg@372: * accompanied this code). jjg@372: * jjg@372: * You should have received a copy of the GNU General Public License version jjg@372: * 2 along with this work; if not, write to the Free Software Foundation, jjg@372: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@372: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@372: */ jjg@372: jjg@450: package com.sun.tools.javac.util; jjg@372: jjg@372: import java.io.Closeable; jjg@372: import java.io.IOException; jjg@372: import java.lang.reflect.Field; jjg@372: import java.net.URL; jjg@372: import java.net.URLClassLoader; jjg@372: import java.util.ArrayList; jjg@372: import java.util.jar.JarFile; jjg@372: jjg@372: /** jjg@372: * A URLClassLoader that also implements Closeable. jjg@372: * Reflection is used to access internal data structures in the URLClassLoader, jjg@372: * since no public API exists for this purpose. Therefore this code is somewhat jjg@372: * fragile. Caveat emptor. jjg@372: * @throws Error if the internal data structures are not as expected. jjg@372: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. jjg@372: * This code and its internal interfaces are subject to change or jjg@372: * deletion without notice. jjg@372: */ jjg@450: public class CloseableURLClassLoader jjg@372: extends URLClassLoader implements Closeable { jjg@450: public CloseableURLClassLoader(URL[] urls, ClassLoader parent) throws Error { jjg@372: super(urls, parent); jjg@372: try { jjg@372: getLoaders(); //proactive check that URLClassLoader is as expected jjg@372: } catch (Throwable t) { jjg@372: throw new Error("cannot create CloseableURLClassLoader", t); jjg@372: } jjg@372: } jjg@372: jjg@372: /** jjg@372: * Close any jar files that may have been opened by the class loader. jjg@372: * Reflection is used to access the jar files in the URLClassLoader's jjg@372: * internal data structures. jjg@372: * @throws java.io.IOException if the jar files cannot be found for any jjg@372: * reson, or if closing the jar file itself causes an IOException. jjg@372: */ jjg@450: @Override jjg@372: public void close() throws IOException { jjg@372: try { jjg@372: for (Object l: getLoaders()) { jjg@372: if (l.getClass().getName().equals("sun.misc.URLClassPath$JarLoader")) { jjg@372: Field jarField = l.getClass().getDeclaredField("jar"); jjg@372: JarFile jar = (JarFile) getField(l, jarField); jjg@407: if (jar != null) { jjg@407: //System.err.println("CloseableURLClassLoader: closing " + jar); jjg@407: jar.close(); jjg@407: } jjg@372: } jjg@372: } jjg@372: } catch (Throwable t) { jjg@372: IOException e = new IOException("cannot close class loader"); jjg@372: e.initCause(t); jjg@372: throw e; jjg@372: } jjg@372: } jjg@372: jjg@372: private ArrayList getLoaders() jjg@372: throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException jjg@372: { jjg@372: Field ucpField = URLClassLoader.class.getDeclaredField("ucp"); jjg@372: Object urlClassPath = getField(this, ucpField); jjg@372: if (urlClassPath == null) jjg@372: throw new AssertionError("urlClassPath not set in URLClassLoader"); jjg@372: Field loadersField = urlClassPath.getClass().getDeclaredField("loaders"); jjg@372: return (ArrayList) getField(urlClassPath, loadersField); jjg@372: } jjg@372: jjg@372: private Object getField(Object o, Field f) jjg@372: throws IllegalArgumentException, IllegalAccessException { jjg@372: boolean prev = f.isAccessible(); jjg@372: try { jjg@372: f.setAccessible(true); jjg@372: return f.get(o); jjg@372: } finally { jjg@372: f.setAccessible(prev); jjg@372: } jjg@372: } jjg@372: jjg@372: }