changeset 5:8018994ecabd

avoid creating unecessary nativedirs; clean them on exit 2010-10-20 Omair Majid <omajid@redhat.com> * netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java (JNLPClassLoader): Call installShutdownHooks. (installShutdownHooks): New method. Installs a shutdown hook to recursively delete the contents of nativeDir. (activateNative): Only create a nativeDir if there are native libraries.
author Omair Majid <omajid@redhat.com>
date Wed, 20 Oct 2010 09:49:01 -0400
parents 22bfb12deaf7
children 257297223569
files ChangeLog netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
diffstat 2 files changed, 49 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Oct 20 14:34:41 2010 +0100
+++ b/ChangeLog	Wed Oct 20 09:49:01 2010 -0400
@@ -1,3 +1,12 @@
+2010-10-20  Omair Majid  <omajid@redhat.com>
+
+	* netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
+	(JNLPClassLoader): Call installShutdownHooks.
+	(installShutdownHooks): New method. Installs a shutdown hook to
+	recursively delete the contents of nativeDir.
+	(activateNative): Only create a nativeDir if there are native
+	libraries.
+
 2010-10-19  Deepak Bhole  <dbhole@redhat.com>
 
 	* Makefile.am:
--- a/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Wed Oct 20 14:34:41 2010 +0100
+++ b/netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java	Wed Oct 20 09:49:01 2010 -0400
@@ -59,6 +59,7 @@
 import net.sourceforge.jnlp.cache.UpdatePolicy;
 import net.sourceforge.jnlp.security.SecurityWarningDialog;
 import net.sourceforge.jnlp.tools.JarSigner;
+import net.sourceforge.jnlp.util.FileUtils;
 import sun.misc.JarIndex;
 
 /**
@@ -169,6 +170,41 @@
 
         setSecurity();
 
+        installShutdownHooks();
+
+    }
+
+    /**
+     * Install JVM shutdown hooks to clean up resources allocated by this
+     * ClassLoader.
+     */
+    private void installShutdownHooks() {
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                if (JNLPRuntime.isDebug()) {
+                    System.out.println("Cleaning up native directory" + nativeDir.getAbsolutePath());
+                }
+
+                /*
+                 * Delete only the native dir created by this classloader (if
+                 * there is one). Other classloaders (parent, peers) will all
+                 * cleanup things they created
+                 */
+                if (nativeDir != null) {
+                    try {
+                        FileUtils.recursiveDelete(nativeDir,
+                                new File(System.getProperty("java.io.tmpdir")));
+                    } catch (IOException e) {
+                        /*
+                         * failed to delete a file in tmpdir, no big deal (not
+                         * to mention that the VM is shutting down at this
+                         * point so no much we can do)
+                         */
+                    }
+                }
+            }
+        });
     }
 
     private void setSecurity() throws LaunchException {
@@ -741,9 +777,6 @@
         if (localFile == null)
             return;
 
-        if (nativeDir == null)
-            nativeDir = getNativeDir();
-
         String[] librarySuffixes = { ".so", ".dylib", ".jnilib", ".framework", ".dll" };
 
         try {
@@ -770,10 +803,14 @@
                     continue;
                 }
 
+                if (nativeDir == null)
+                    nativeDir = getNativeDir();
+
                 File outFile = new File(nativeDir, name);
 
                 CacheUtil.streamCopy(jarFile.getInputStream(e),
                                      new FileOutputStream(outFile));
+
             }
         }
         catch (IOException ex) {