Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/ClassLoaderUtil.java
38829 views
/*1* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.misc;2627/**28* Provides utility functions related to URLClassLoaders or subclasses of it.29*30* W A R N I N G31*32* This class uses undocumented, unpublished, private data structures inside33* java.net.URLClassLoader and sun.misc.URLClassPath. Use with extreme caution.34*35* @author tjquinn36*/373839import java.io.IOException;40import java.net.URLClassLoader;41import java.util.*;42import java.util.jar.JarFile;4344public class ClassLoaderUtil {4546/**47* Releases resources held by a URLClassLoader. A new classloader must48* be created before the underlying resources can be accessed again.49* @param classLoader the instance of URLClassLoader (or a subclass)50*/51public static void releaseLoader(URLClassLoader classLoader) {52releaseLoader(classLoader, null);53}5455/**56* Releases resources held by a URLClassLoader. Notably, close the jars57* opened by the loader. Initializes and updates the List of58* jars that have been successfully closed.59* <p>60* @param classLoader the instance of URLClassLoader (or a subclass)61* @param jarsClosed a List of Strings that will contain the names of jars62* successfully closed; can be null if the caller does not need the information returned63* @return a List of IOExceptions reporting jars that failed to close; null64* indicates that an error other than an IOException occurred attempting to65* release the loader; empty indicates a successful release; non-empty66* indicates at least one error attempting to close an open jar.67*/68public static List<IOException> releaseLoader(URLClassLoader classLoader, List<String> jarsClosed) {6970List<IOException> ioExceptions = new LinkedList<IOException>();7172try {73/* Records all IOExceptions thrown while closing jar files. */7475if (jarsClosed != null) {76jarsClosed.clear();77}7879URLClassPath ucp = SharedSecrets.getJavaNetAccess()80.getURLClassPath(classLoader);81ArrayList<?> loaders = ucp.loaders;82Stack<?> urls = ucp.urls;83HashMap<?,?> lmap = ucp.lmap;8485/*86*The urls variable in the URLClassPath object holds URLs that have not yet87*been used to resolve a resource or load a class and, therefore, do88*not yet have a loader associated with them. Clear the stack so any89*future requests that might incorrectly reach the loader cannot be90*resolved and cannot open a jar file after we think we've closed91*them all.92*/93synchronized(urls) {94urls.clear();95}9697/*98*Also clear the map of URLs to loaders so the class loader cannot use99*previously-opened jar files - they are about to be closed.100*/101synchronized(lmap) {102lmap.clear();103}104105/*106*The URLClassPath object's path variable records the list of all URLs that are on107*the URLClassPath's class path. Leave that unchanged. This might108*help someone trying to debug why a released class loader is still used.109*Because the stack and lmap are now clear, code that incorrectly uses a110*the released class loader will trigger an exception if the111*class or resource would have been resolved by the class112*loader (and no other) if it had not been released.113*114*The list of URLs might provide some hints to the person as to where115*in the code the class loader was set up, which might in turn suggest116*where in the code the class loader needs to stop being used.117*The URLClassPath does not use the path variable to open new jar118*files - it uses the urls Stack for that - so leaving the path variable119*will not by itself allow the class loader to continue handling requests.120*/121122/*123*For each loader, close the jar file associated with that loader.124*125*The URLClassPath's use of loaders is sync-ed on the entire URLClassPath126*object.127*/128synchronized (ucp) {129for (Object o : loaders) {130if (o != null) {131/*132*If the loader is a JarLoader inner class and its jarFile133*field is non-null then try to close that jar file. Add134*it to the list of closed files if successful.135*/136if (o instanceof URLClassPath.JarLoader) {137URLClassPath.JarLoader jl = (URLClassPath.JarLoader)o;138JarFile jarFile = jl.getJarFile();139try {140if (jarFile != null) {141jarFile.close();142if (jarsClosed != null) {143jarsClosed.add(jarFile.getName());144}145}146} catch (IOException ioe) {147/*148*Wrap the IOException to identify which jar149*could not be closed and add it to the list150*of IOExceptions to be returned to the caller.151*/152String jarFileName = (jarFile == null) ? "filename not available":jarFile.getName();153String msg = "Error closing JAR file: " + jarFileName;154IOException newIOE = new IOException(msg);155newIOE.initCause(ioe);156ioExceptions.add(newIOE);157}158}159}160}161/*162*Now clear the loaders ArrayList.163*/164loaders.clear();165}166} catch (Throwable t) {167throw new RuntimeException (t);168}169return ioExceptions;170}171}172173174