src/share/classes/com/sun/tools/javac/file/CloseableURLClassLoader.java

Print this page




  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 API supported by Sun Microsystems.  If
  44  *  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 class CloseableURLClassLoader
  49         extends URLClassLoader implements Closeable {
  50     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     public void close() throws IOException {
  67         try {
  68             for (Object l: getLoaders()) {
  69                 if (l.getClass().getName().equals("sun.misc.URLClassPath$JarLoader")) {
  70                     Field jarField = l.getClass().getDeclaredField("jar");
  71                     JarFile jar = (JarFile) getField(l, jarField);
  72                     if (jar != null) {
  73                         //System.err.println("CloseableURLClassLoader: closing " + jar);
  74                         jar.close();
  75                     }
  76                 }
  77             }
  78         } catch (Throwable t) {
  79             IOException e = new IOException("cannot close class loader");
  80             e.initCause(t);
  81             throw e;
  82         }
  83     }
  84 
  85     private ArrayList<?> getLoaders()




  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 API supported by Sun Microsystems.  If
  44  *  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()