camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [8/8] git commit: CAMEL-6452: Added shutdown method to FileUtil to allow cleaning up temp dir and shutdown hook. Thanks to Sanjay Deshmukh for the patch
Date Wed, 17 Jul 2013 11:39:48 GMT
CAMEL-6452: Added shutdown method to FileUtil to allow cleaning up temp dir and shutdown hook.
Thanks to Sanjay Deshmukh for the patch

Conflicts:
	camel-core/src/main/java/org/apache/camel/util/FileUtil.java


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e9f2a1f8
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e9f2a1f8
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e9f2a1f8

Branch: refs/heads/camel-2.10.x
Commit: e9f2a1f86518d319c904283c04e90d57f243b823
Parents: 7c6b566
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Jul 17 12:11:12 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Jul 17 13:39:04 2013 +0200

----------------------------------------------------------------------
 .../java/org/apache/camel/util/FileUtil.java    | 22 +++++++++++++++++---
 .../org/apache/camel/util/FileUtilTest.java     |  9 ++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e9f2a1f8/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/FileUtil.java b/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
index 7b9e5e0..001fa1b 100644
--- a/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
+++ b/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
@@ -30,7 +30,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * File utilities
+ * File utilities.
  */
 public final class FileUtil {
     
@@ -39,8 +39,10 @@ public final class FileUtil {
     private static final transient Logger LOG = LoggerFactory.getLogger(FileUtil.class);
     private static final int RETRY_SLEEP_MILLIS = 10;
     private static File defaultTempDir;
+    private static Thread shutdownHook;
 
     private FileUtil() {
+        // Utils method
     }
 
     /**
@@ -296,17 +298,31 @@ public final class FileUtil {
         defaultTempDir = f;
 
         // create shutdown hook to remove the temp dir
-        Thread hook = new Thread() {
+        shutdownHook = new Thread() {
             @Override
             public void run() {
                 removeDir(defaultTempDir);
             }
         };
-        Runtime.getRuntime().addShutdownHook(hook);
+        Runtime.getRuntime().addShutdownHook(shutdownHook);
 
         return defaultTempDir;
     }
 
+    /**
+     * Shutdown and cleanup the temporary directory and removes any shutdown hooks in use.
+     */
+    public static synchronized void shutdown() {
+        if (defaultTempDir != null && defaultTempDir.exists()) {
+            removeDir(defaultTempDir);
+        }
+
+        if (shutdownHook != null) {
+            Runtime.getRuntime().removeShutdownHook(shutdownHook);
+            shutdownHook = null;
+        }
+    }
+
     private static void removeDir(File d) {
         String[] list = d.list();
         if (list == null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/e9f2a1f8/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java b/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
index 46adfdf..e9a6431 100644
--- a/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
@@ -204,4 +204,13 @@ public class FileUtilTest extends TestCase {
         assertTrue("A new file should be created " + file, FileUtil.createNewFile(file));
     }
 
+    public void testShutdown() throws Exception {
+        File tmpFile = FileUtil.createTempFile(null, null);
+        File tmpDir = tmpFile.getParentFile();
+        assertTrue(tmpDir.exists());
+
+        FileUtil.shutdown();
+        assertFalse(tmpDir.exists());
+    }
+
 }


Mime
View raw message