1 /*
   2  * Copyright 2007 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package com.sun.tools.javac.file;
  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 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()
  86             throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException
  87     {
  88         Field ucpField = URLClassLoader.class.getDeclaredField("ucp");
  89         Object urlClassPath = getField(this, ucpField);
  90         if (urlClassPath == null)
  91             throw new AssertionError("urlClassPath not set in URLClassLoader");
  92         Field loadersField = urlClassPath.getClass().getDeclaredField("loaders");
  93         return (ArrayList<?>) getField(urlClassPath, loadersField);
  94     }
  95 
  96     private Object getField(Object o, Field f)
  97             throws IllegalArgumentException, IllegalAccessException {
  98         boolean prev = f.isAccessible();
  99         try {
 100             f.setAccessible(true);
 101             return f.get(o);
 102         } finally {
 103             f.setAccessible(prev);
 104         }
 105     }
 106 
 107 }