You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2012/02/16 21:47:35 UTC

svn commit: r1245172 - /openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Files.java

Author: dblevins
Date: Thu Feb 16 20:47:35 2012
New Revision: 1245172

URL: http://svn.apache.org/viewvc?rev=1245172&view=rev
Log:
little utility for dealing with Files

Added:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Files.java
      - copied, changed from r1237134, openejb/trunk/sandbox/release-tools/src/main/java/org/apache/openejb/tools/release/util/Files.java

Copied: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Files.java (from r1237134, openejb/trunk/sandbox/release-tools/src/main/java/org/apache/openejb/tools/release/util/Files.java)
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Files.java?p2=openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Files.java&p1=openejb/trunk/sandbox/release-tools/src/main/java/org/apache/openejb/tools/release/util/Files.java&r1=1237134&r2=1245172&rev=1245172&view=diff
==============================================================================
--- openejb/trunk/sandbox/release-tools/src/main/java/org/apache/openejb/tools/release/util/Files.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Files.java Thu Feb 16 20:47:35 2012
@@ -14,24 +14,7 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
-package org.apache.openejb.tools.release.util;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- */
+package org.apache.openejb.util;
 
 import java.io.File;
 import java.io.FileFilter;
@@ -45,7 +28,7 @@ import java.util.regex.Pattern;
  */
 public class Files {
 
-    public static File file(String... parts) {
+    public static File path(String... parts) {
         File dir = null;
         for (String part : parts) {
             if (dir == null) {
@@ -58,7 +41,7 @@ public class Files {
         return dir;
     }
 
-    public static File file(File dir, String... parts) {
+    public static File path(File dir, String... parts) {
         for (String part : parts) {
             dir = new File(dir, part);
         }
@@ -122,6 +105,7 @@ public class Files {
             final File file = File.createTempFile("temp", "dir");
             if (!file.delete()) throw new IllegalStateException("Cannot make temp dir.  Delete failed");
             mkdir(file);
+            deleteOnExit(file);
             return file;
         } catch (IOException e) {
             throw new RuntimeException(e);
@@ -143,4 +127,40 @@ public class Files {
 
         assert file.isDirectory() : "not a directory" + file;
     }
+
+
+    // Shutdown hook for recursive delete on tmp directories
+    static final List<String> delete = new ArrayList<String>();
+
+    static {
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                delete();
+            }
+        });
+    }
+
+    public static void deleteOnExit(File file) {
+        delete.add(file.getAbsolutePath());
+    }
+
+    private static void delete() {
+        for (String path : delete) {
+            delete(new File(path));
+        }
+    }
+
+    public static void delete(File file) {
+        if (file.exists()) {
+            if (file.isDirectory()) {
+                for (File f : file.listFiles()) {
+                    delete(f);
+                }
+            }
+
+            file.delete();
+        }
+    }
+
 }