You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hop.apache.org by ha...@apache.org on 2021/10/03 08:51:19 UTC

[incubator-hop] branch master updated: HOP-2229 HopURLClassLoader simplification

This is an automated email from the ASF dual-hosted git repository.

hansva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hop.git


The following commit(s) were added to refs/heads/master by this push:
     new 4ebfd0f  HOP-2229 HopURLClassLoader simplification
     new a20df7a  Merge pull request #1101 from nadment/HOP-2229
4ebfd0f is described below

commit 4ebfd0fcd29fa21d42abbeeffd1a315a13c7de5c
Author: Nicolas Adment <na...@gmail.com>
AuthorDate: Sat Oct 2 00:42:49 2021 +0200

    HOP-2229 HopURLClassLoader simplification
---
 .../apache/hop/core/plugins/BasePluginType.java    |   6 +-
 .../apache/hop/core/plugins/HopURLClassLoader.java | 146 +--------------------
 2 files changed, 7 insertions(+), 145 deletions(-)

diff --git a/core/src/main/java/org/apache/hop/core/plugins/BasePluginType.java b/core/src/main/java/org/apache/hop/core/plugins/BasePluginType.java
index 2b73e33..66ba596 100644
--- a/core/src/main/java/org/apache/hop/core/plugins/BasePluginType.java
+++ b/core/src/main/java/org/apache/hop/core/plugins/BasePluginType.java
@@ -552,11 +552,7 @@ public abstract class BasePluginType<T extends Annotation> implements IPluginTyp
         // Ignore for now, don't know if it's even possible.
         LogChannel.GENERAL.logError(
             "Unexpected error registering jar plugin file: " + pluginClassFile.getJarFile(), e);
-      } finally {
-        if (urlClassLoader instanceof HopURLClassLoader) {
-          ((HopURLClassLoader) urlClassLoader).closeClassLoader();
-        }
-      }
+      } 
     }
   }
 
diff --git a/core/src/main/java/org/apache/hop/core/plugins/HopURLClassLoader.java b/core/src/main/java/org/apache/hop/core/plugins/HopURLClassLoader.java
index cdd30d1..4ac458a 100644
--- a/core/src/main/java/org/apache/hop/core/plugins/HopURLClassLoader.java
+++ b/core/src/main/java/org/apache/hop/core/plugins/HopURLClassLoader.java
@@ -17,18 +17,12 @@
 
 package org.apache.hop.core.plugins;
 
-import java.io.IOException;
 import java.io.InputStream;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.security.ProtectionDomain;
-import java.util.*;
-import java.util.jar.JarFile;
 
 public class HopURLClassLoader extends URLClassLoader {
-  private static final Class<?> PKG = HopURLClassLoader.class; // For Translator
 
   private String name;
 
@@ -93,25 +87,14 @@ public class HopURLClassLoader extends URLClassLoader {
   @Override
   protected synchronized Class<?> loadClass(String name, boolean resolve)
       throws ClassNotFoundException {
-    Throwable thisLoaderException = null;
-
     try {
       return loadClassFromThisLoader(name, resolve);
-    } catch (ClassNotFoundException | NoClassDefFoundError e) {
-      thisLoaderException = e;
-    } catch (SecurityException e) {
-      thisLoaderException = e;
-    }
-
-    try {
-      return loadClassFromParent(name, resolve);
-    } catch (Exception e) {
-      if (thisLoaderException != null) {
+    } catch (ClassNotFoundException | NoClassDefFoundError | SecurityException exception) {
+      try {
+        return loadClassFromParent(name, resolve);
+      } catch (Exception parentException) {
         throw new ClassNotFoundException(
-            "Unable to load class '" + name + "' in this classloader or in the parent",
-            thisLoaderException);
-      } else {
-        throw e;
+            "Unable to load class '" + name + "' in this classloader or in the parent", exception);
       }
     }
   }
@@ -172,126 +155,9 @@ public class HopURLClassLoader extends URLClassLoader {
     }
   }
 
-  private static Object getFieldObject(Class<?> clazz, String name, Object obj) throws Exception {
-    Field field = clazz.getDeclaredField(name);
-    field.setAccessible(true);
-    return field.get(obj);
-  }
-
-  /** This method is designed to shutdown out classloader file locks in windows. */
-  public void closeClassLoader() {
-    HashSet<String> closedFiles = new HashSet<>();
-    try {
-      Object obj = getFieldObject(URLClassLoader.class, "ucp", this);
-      ArrayList<?> loaders = (ArrayList<?>) getFieldObject(obj.getClass(), "loaders", obj);
-      for (Object ldr : loaders) {
-        try {
-          JarFile file = (JarFile) getFieldObject(ldr.getClass(), "jar", ldr);
-          closedFiles.add(file.getName());
-          file.close();
-        } catch (Exception e) {
-          // skip
-        }
-      }
-    } catch (Exception e) {
-      // skip
-    }
-
-    try {
-      Vector<?> nativeLibArr =
-          (Vector<?>) getFieldObject(ClassLoader.class, "nativeLibraries", this);
-      for (Object lib : nativeLibArr) {
-        try {
-          Method fMethod = lib.getClass().getDeclaredMethod("finalize");
-          fMethod.setAccessible(true);
-          fMethod.invoke(lib);
-        } catch (Exception e) {
-          // skip
-        }
-      }
-    } catch (Exception e) {
-      // skip
-    }
-
-    HashMap<?, ?> uCache = null;
-    HashMap<?, ?> fCache = null;
-
-    try {
-      Class<?> jarUrlConnClass = null;
-      try {
-        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
-        jarUrlConnClass = contextClassLoader.loadClass("sun.net.www.protocol.jar.JarURLConnection");
-      } catch (Throwable skip) {
-        // skip
-      }
-      if (jarUrlConnClass == null) {
-        jarUrlConnClass = Class.forName("sun.net.www.protocol.jar.JarURLConnection");
-      }
-      Class<?> factory = getFieldObject(jarUrlConnClass, "factory", null).getClass();
-      try {
-        fCache = (HashMap<?, ?>) getFieldObject(factory, "fileCache", null);
-      } catch (Exception e) {
-        // skip
-      }
-      try {
-        uCache = (HashMap<?, ?>) getFieldObject(factory, "urlCache", null);
-      } catch (Exception e) {
-        // skip
-      }
-      if (uCache != null) {
-        Set<?> set = null;
-        while (set == null) {
-          try {
-            set = ((HashMap<?, ?>) uCache.clone()).keySet();
-          } catch (ConcurrentModificationException e) {
-            // Fix for BACKLOG-2149 - Do nothing - while loop will try again.
-          }
-        }
-
-        for (Object file : set) {
-          if (file instanceof JarFile) {
-            JarFile jar = (JarFile) file;
-            if (!closedFiles.contains(jar.getName())) {
-              continue;
-            }
-            try {
-              jar.close();
-            } catch (IOException e) {
-              // skip
-            }
-            if (fCache != null) {
-              fCache.remove(uCache.get(jar));
-            }
-            uCache.remove(jar);
-          }
-        }
-      } else if (fCache != null) {
-        for (Object key : ((HashMap<?, ?>) fCache.clone()).keySet()) {
-          Object file = fCache.get(key);
-          if (file instanceof JarFile) {
-            JarFile jar = (JarFile) file;
-            if (!closedFiles.contains(jar.getName())) {
-              continue;
-            }
-            try {
-              jar.close();
-            } catch (IOException e) {
-              // ignore
-            }
-            fCache.remove(key);
-          }
-        }
-      }
-    } catch (Exception e) {
-      // skip
-      e.printStackTrace();
-    }
-  }
-
   @Override
   public URL getResource(String name) {
-    URL url;
-    url = findResource(name);
+    URL url = findResource(name);
     if (url == null && getParent() != null) {
       url = getParent().getResource(name);
     }