You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/07/17 13:39:46 UTC
[6/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
CAMEL-6452: Added shutdown method to FileUtil to allow cleaning up temp dir and shutdown hook. Thanks to Sanjay Deshmukh for the patch
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8bae7e64
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8bae7e64
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8bae7e64
Branch: refs/heads/camel-2.11.x
Commit: 8bae7e6450658c1197324b55787a1a5b73ded3b8
Parents: 31a27b1
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Jul 17 12:11:12 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Jul 17 13:34:40 2013 +0200
----------------------------------------------------------------------
.../java/org/apache/camel/util/FileUtil.java | 26 ++++++++++++++------
.../org/apache/camel/util/FileUtilTest.java | 9 +++++++
2 files changed, 28 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/8bae7e64/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 39952e5..339a5da 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
@@ -16,7 +16,6 @@
*/
package org.apache.camel.util;
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -31,7 +30,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * File utilities
+ * File utilities.
*/
public final class FileUtil {
@@ -45,12 +44,11 @@ public final class FileUtil {
private static final String USER_DIR_KEY = "user.dir";
private static final File USER_DIR = new File(System.getProperty(USER_DIR_KEY));
private static File defaultTempDir;
-
-
+ private static Thread shutdownHook;
+
private FileUtil() {
// Utils method
}
-
public static File getUserDir() {
return USER_DIR;
@@ -309,17 +307,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/8bae7e64/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());
+ }
+
}