/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package org.hibernate.console; import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Vector; import java.util.jar.JarFile; import org.hibernate.util.ReflectHelper; /** * Workaround for jdk disgrace with open jar files & native libs, * which is a reason of locked, "undelete" files. * * @author Vitali Yemialyanchyk */ protected HashSet<String> setJarFileNames2Close = new HashSet<String>(); super(urls, parent); } public void close() { setJarFileNames2Close.clear(); closeClassLoader(this); finalizeNativeLibs(this); cleanupJarFileFactory(); } /** * cleanup jar file factory cache */ @SuppressWarnings({ "nls", "unchecked" }) public boolean cleanupJarFileFactory() { boolean res = false; Class classJarURLConnection = null; try { classJarURLConnection = ReflectHelper.classForName("sun.net.www.protocol.jar.JarURLConnection"); //ignore } if (classJarURLConnection == null) { return res; } try { f = classJarURLConnection.getDeclaredField("factory"); //ignore } if (f == null) { return res; } f.setAccessible(true); try { obj = f.get(null); //ignore } if (obj == null) { return res; } Class classJarFileFactory = obj.getClass(); // try { f = classJarFileFactory.getDeclaredField("fileCache"); f.setAccessible(true); obj = f.get(null); } //ignore } try { f = classJarFileFactory.getDeclaredField("urlCache"); f.setAccessible(true); obj = f.get(null); } //ignore } if (urlCache != null) { while (it.hasNext()) { obj = it.next(); continue; } if (setJarFileNames2Close.contains(jarFile.getName())) { try { jarFile.close(); //ignore } if (fileCache != null) { fileCache.remove(urlCache.get(jarFile)); } urlCache.remove(jarFile); } } res = true; } else if (fileCache != null) { // urlCache := null while (it.hasNext()) { obj = fileCache.get(key); continue; } if (setJarFileNames2Close.contains(jarFile.getName())) { try { jarFile.close(); //ignore } fileCache.remove(key); } } res = true; } setJarFileNames2Close.clear(); return res; } /** * close jar files of cl * @param cl * @return */ @SuppressWarnings( { "nls", "unchecked" }) boolean res = false; if (cl == null) { return res; } try { f = classURLClassLoader.getDeclaredField("ucp"); //ignore } if (f != null) { f.setAccessible(true); try { obj = f.get(cl); //ignore } if (obj != null) { f = null; try { f = ucp.getClass().getDeclaredField("loaders"); //ignore } if (f != null) { f.setAccessible(true); try { res = true; //ignore } for (int i = 0; loaders != null && i < loaders.size(); i++) { obj = loaders.get(i); f = null; try { f = obj.getClass().getDeclaredField("jar"); //ignore } if (f != null) { f.setAccessible(true); try { obj = f.get(obj); // ignore } setJarFileNames2Close.add(jarFile.getName()); //try { // jarFile.getManifest().clear(); //} catch (IOException e) { // // ignore //} try { jarFile.close(); // ignore } } } } } } } return res; } /** * finalize native libraries * @param cl * @return */ @SuppressWarnings({ "nls", "unchecked" }) boolean res = false; try { nativeLibraries = classClassLoader.getDeclaredField("nativeLibraries"); //ignore } if (nativeLibraries == null) { return res; } nativeLibraries.setAccessible(true); try { obj = nativeLibraries.get(cl); //ignore } return res; } res = true; try { finalize = lib.getClass().getDeclaredMethod("finalize", new Class[0]); //ignore } if (finalize != null) { finalize.setAccessible(true); try { //ignore } } } return res; } }
URL: http://www.stopka.us