Coverage Report - org.mule.module.launcher.GoodCitizenClassLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
GoodCitizenClassLoader
0%
0/36
0%
0/4
0
GoodCitizenClassLoader$NonCachingJarResourceURLStreamHandler
0%
0/5
N/A
0
GoodCitizenClassLoader$NonCachingURLStreamHandlerFactory
0%
0/2
N/A
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.module.launcher;
 8  
 
 9  
 import org.mule.util.ClassUtils;
 10  
 
 11  
 import java.io.IOException;
 12  
 import java.lang.reflect.Field;
 13  
 import java.lang.reflect.Method;
 14  
 import java.net.JarURLConnection;
 15  
 import java.net.URL;
 16  
 import java.net.URLClassLoader;
 17  
 import java.net.URLStreamHandler;
 18  
 import java.net.URLStreamHandlerFactory;
 19  
 import java.util.Collection;
 20  
 import java.util.Vector;
 21  
 import java.util.jar.JarFile;
 22  
 
 23  
 import sun.net.www.protocol.jar.Handler;
 24  
 
 25  
 /**
 26  
  * Fixes major classloader woes by:
 27  
  * <ol>
 28  
  *  <li>Providing a {@link #close()} method to release any connections to resources.</li>
 29  
  *  <li>Disabling caching of jar resources fix e.g. java.util.ResourceBundle 'tagging' the app
 30  
  *      and preventing it from being undeployed correctly (no leaving locked jars behind).</li>
 31  
  * </ol>
 32  
  */
 33  
 public class GoodCitizenClassLoader extends URLClassLoader
 34  
 {
 35  
 
 36  
     public GoodCitizenClassLoader(URL[] urls, ClassLoader parent)
 37  
     {
 38  0
         super(urls, parent, new NonCachingURLStreamHandlerFactory());
 39  0
     }
 40  
 
 41  
     /**
 42  
      * A workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5041014
 43  
      */
 44  
     public void close()
 45  
     {
 46  
         // jars
 47  
         try
 48  
         {
 49  0
             Class<URLClassLoader> clazz = URLClassLoader.class;
 50  0
             Field ucp = clazz.getDeclaredField("ucp");
 51  0
             ucp.setAccessible(true);
 52  0
             Object urlClassPath = ucp.get(this);
 53  0
             Field loaders = urlClassPath.getClass().getDeclaredField("loaders");
 54  0
             loaders.setAccessible(true);
 55  0
             Collection<?> jarLoaders = (Collection<?>) loaders.get(urlClassPath);
 56  0
             for (Object jarLoader : jarLoaders)
 57  
             {
 58  
                 try
 59  
                 {
 60  0
                     Field loader = jarLoader.getClass().getDeclaredField("jar");
 61  0
                     loader.setAccessible(true);
 62  0
                     Object jarFile = loader.get(jarLoader);
 63  0
                     ((JarFile) jarFile).close();
 64  
                 }
 65  0
                 catch (Throwable t)
 66  
                 {
 67  
                     // if we got this far, this is probably not a JAR loader so skip it
 68  0
                 }
 69  
             }
 70  
         }
 71  0
         catch (Throwable t)
 72  
         {
 73  
             // probably not a SUN VM
 74  0
         }
 75  
 
 76  
         try
 77  
         {
 78  
             // now native libs
 79  0
             Class<ClassLoader> clazz = ClassLoader.class;
 80  0
             Field nativeLibraries = clazz.getDeclaredField("nativeLibraries");
 81  0
             nativeLibraries.setAccessible(true);
 82  0
             Vector<?> nativelib = (Vector<?>) nativeLibraries.get(this);
 83  0
             for (Object lib : nativelib)
 84  
             {
 85  0
                 Method finalize = lib.getClass().getDeclaredMethod("finalize");
 86  0
                 finalize.setAccessible(true);
 87  0
                 finalize.invoke(lib);
 88  0
             }
 89  
         }
 90  0
         catch (Exception ex)
 91  
         {
 92  
             // ignore
 93  0
         }
 94  
 
 95  
         try
 96  
         {
 97  
             // fix groovy compiler leaks http://www.mulesoft.org/jira/browse/MULE-5125
 98  0
             final Class clazz = ClassUtils.loadClass("org.codehaus.groovy.transform.ASTTransformationVisitor", getClass());
 99  0
             final Field compUnit = clazz.getDeclaredField("compUnit");
 100  0
             compUnit.setAccessible(true);
 101  
             // static field
 102  0
             compUnit.set(null, null);
 103  
         }
 104  0
         catch (Throwable t)
 105  
         {
 106  
             // ignore
 107  0
         }
 108  0
     }
 109  
 
 110  0
     protected static class NonCachingURLStreamHandlerFactory implements URLStreamHandlerFactory
 111  
     {
 112  
         public URLStreamHandler createURLStreamHandler(String protocol)
 113  
         {
 114  0
             return new NonCachingJarResourceURLStreamHandler();
 115  
         }
 116  
     }
 117  
 
 118  
     /**
 119  
      * Prevents jar caching for this classloader, mainly to fix the static ResourceBundle mess/cache
 120  
      * that keeps connections open no matter what.
 121  
      */
 122  
     private static class NonCachingJarResourceURLStreamHandler extends Handler
 123  
     {
 124  
         public NonCachingJarResourceURLStreamHandler()
 125  
         {
 126  0
             super();
 127  0
         }
 128  
 
 129  
         @Override
 130  
         protected java.net.URLConnection openConnection(URL u) throws IOException
 131  
         {
 132  0
             JarURLConnection c = new sun.net.www.protocol.jar.JarURLConnection(u, this);
 133  0
             c.setUseCaches(false);
 134  0
             return c;
 135  
         }
 136  
     }
 137  
 }