1 /* 2 * Copyright (c) 2007, 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.javac.util; 27 28 import java.io.Closeable; 29 import java.io.IOException; 30 import java.lang.reflect.Field; 31 import java.net.URL; 32 import java.net.URLClassLoader; 33 import java.util.ArrayList; 34 import java.util.jar.JarFile; 35 36 /** 37 * A URLClassLoader that also implements Closeable. 38 * Reflection is used to access internal data structures in the URLClassLoader, 39 * since no public API exists for this purpose. Therefore this code is somewhat 40 * fragile. Caveat emptor. 41 * @throws Error if the internal data structures are not as expected. 42 * 43 * <p><b>This is NOT part of any supported API. 44 * If you write code that depends on this, you do so at your own risk. 45 * This code and its internal interfaces are subject to change or 46 * deletion without notice.</b> 47 */ 48 public class CloseableURLClassLoader 49 extends URLClassLoader implements Closeable { 50 public CloseableURLClassLoader(URL[] urls, ClassLoader parent) throws Error { 51 super(urls, parent); 52 try { 53 getLoaders(); //proactive check that URLClassLoader is as expected 54 } catch (Throwable t) { 55 throw new Error("cannot create CloseableURLClassLoader", t); 56 } 57 } 58 59 /** 60 * Close any jar files that may have been opened by the class loader. 61 * Reflection is used to access the jar files in the URLClassLoader's 62 * internal data structures. 63 * @throws java.io.IOException if the jar files cannot be found for any 64 * reson, or if closing the jar file itself causes an IOException. 65 */ 66 @Override 67 public void close() throws IOException { 68 try { 69 for (Object l: getLoaders()) { 70 if (l.getClass().getName().equals("sun.misc.URLClassPath$JarLoader")) { 71 Field jarField = l.getClass().getDeclaredField("jar"); 72 JarFile jar = (JarFile) getField(l, jarField); 73 if (jar != null) { 74 //System.err.println("CloseableURLClassLoader: closing " + jar); 75 jar.close(); 76 } 77 } 78 } 79 } catch (Throwable t) { 80 IOException e = new IOException("cannot close class loader"); 81 e.initCause(t); 82 throw e; 83 } 84 } 85 86 private ArrayList<?> getLoaders() 87 throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException 88 { 89 Field ucpField = URLClassLoader.class.getDeclaredField("ucp"); 90 Object urlClassPath = getField(this, ucpField); 91 if (urlClassPath == null) 92 throw new AssertionError("urlClassPath not set in URLClassLoader"); 93 Field loadersField = urlClassPath.getClass().getDeclaredField("loaders"); 94 return (ArrayList<?>) getField(urlClassPath, loadersField); 95 } 96 97 private Object getField(Object o, Field f) 98 throws IllegalArgumentException, IllegalAccessException { 99 boolean prev = f.isAccessible(); 100 try { 101 f.setAccessible(true); 102 return f.get(o); 103 } finally { 104 f.setAccessible(prev); 105 } 106 } 107 108 }