You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/20 12:34:10 UTC

svn commit: r817004 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/java/org/apache/commons/runtime/util/ test/org/apache/commons/runtime/

Author: mturk
Date: Sun Sep 20 10:34:09 2009
New Revision: 817004

URL: http://svn.apache.org/viewvc?rev=817004&view=rev
Log:
Add temDir utils

Added:
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUtils.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java?rev=817004&r1=817003&r2=817004&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/SystemId.java Sun Sep 20 10:34:09 2009
@@ -29,14 +29,15 @@
  *
  * Example for Linux using 64-bit JVM on x86_64 cpu
  *
- * /META-LIB/200111.loader
- *      00 ? libcrypto.so
- *      01 ? libssl.so
- *      02 ! libacr.so
+ * org/apache/commons/runtime/resources/200111.loader
+ *      extension = so
+ *      dependencies = libcrypto, libssl
+ *      libcrypto = libcrypto.so.0.9.8 libcrypto.so.8
+ *      libssl = libssl.so.0.9.8 libssl.so.8
  *
- * /META-LIB/200111.00.bin  -> /tmp/.unique/libcrypto.so
- * /META-LIB/200111.01.bin  -> /tmp/.unique/libssl.so
- * /META-LIB/200111.02.bin  -> /tmp/.unique/libacr.so
+ * org/apache/commons/runtime/resources/200111.00.bin -> /tmp/.xyz/libacr.so
+ * org/apache/commons/runtime/resources/200111.01.bin -> /tmp/.xyz/libcrypto.so
+ * org/apache/commons/runtime/resources/200111.02.bin -> /tmp/.xyz/libssl.so
  *
  * </pre>
  * </p>
@@ -104,14 +105,14 @@
             data = "32";
         }
         if (arch.endsWith("86")) {
-            cpu = "ia32";
+            cpu = "x86";
             if (name.startsWith("Mac OS")) {
                 if (data.equals("64"))
                     cpu = "x86_64";
             }
         }
         else if (arch.startsWith("PA_RISC"))
-            cpu = "pa" + data;
+            cpu = "parisc" + data;
         else if (arch.startsWith("IA64"))
             cpu = "ia64";
         else if (arch.startsWith("sparc"))
@@ -143,7 +144,7 @@
         String pid;
 
         if (sys.equals("windows")) {
-            if (cpu.equals("ia32"))
+            if (cpu.equals("x86"))
                 pid = "100000";
             else if (cpu.equals("ia64"))
                 pid = "100011";
@@ -151,7 +152,7 @@
                 pid = "100010";
         }
         else if (sys.equals("linux")) {
-            if (cpu.equals("ia32"))
+            if (cpu.equals("x86"))
                 pid = "200100";
             else if (cpu.equals("ia64"))
                 pid = "200112";
@@ -159,7 +160,7 @@
                 pid = "200111";
         }
         else if (sys.equals("solaris")) {
-            if (cpu.equals("ia32"))
+            if (cpu.equals("x86"))
                 pid = "200200";
             else if (cpu.equals("sparc32"))
                 pid = "200201";
@@ -169,17 +170,17 @@
                 pid = "200210";
         }
         else if (sys.equals("darwin")) {
-            if (cpu.equals("ia32"))
+            if (cpu.equals("x86"))
                 pid = "200400";
             else
                 pid = "200201";
         }
         else if (sys.equals("hpux")) {
-            if (cpu.equals("ia32"))
+            if (cpu.equals("x86"))
                 pid = "200800";
             else if (cpu.equals("ia64"))
                 pid = "200811";
-            else if (cpu.equals("pa32"))
+            else if (cpu.equals("parisc32"))
                 pid = "200802";
             else
                 pid = "200812";

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java?rev=817004&r1=817003&r2=817004&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java Sun Sep 20 10:34:09 2009
@@ -16,6 +16,8 @@
 
 package org.apache.commons.runtime.util;
 
+import java.io.IOException;
+import java.io.File;
 import java.io.PrintStream;
 import java.util.Random;
 
@@ -36,7 +38,7 @@
     private static long     timer                  = System.currentTimeMillis();
     private static Random   rnd                    = new Random(timer);
 
-    private static final char[] hc = { 
+    private static final char[] hc = {
                 '0', '1', '2', '3', '4', '5', '6', '7',
                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
 
@@ -150,5 +152,91 @@
         out.println(dumpBuffer(data));
     }
 
+    /**
+     * Create temporary directory.
+     * <p>
+     * Directory is created inside system dependant default temorary directory.
+     * This directory is specified by the system property
+     * {@code java.io.tmpdir}.
+     * </p>
+     * @params prefix The prefix string to be used in generating directory name.
+     *                Must be at least three characters long.
+     * @returns An abstract pathname denoting newly-created empty directory.
+     */
+    public static File createTempDirectory(String prefix)
+        throws IllegalArgumentException, IOException
+    {
+        int tries = 0;
+        while(true) {
+            File tmpf = File.createTempFile(prefix, ".acr");
+            try {
+                if (tmpf.delete()) {
+                    /* Create the directory with the same name
+                     * as the temp file was.
+                     */
+                    if (tmpf.mkdir()) {
+                        /* Directory is ceated
+                         */
+                        return tmpf;
+                    }
+                }
+            } catch (Exception ex) {
+                // Ignore
+            }
+            if (tries++ > 5) {
+                if (tries > 10)
+                    break;
+                try {
+                    Thread.sleep(10 + 2 * tries);
+                } catch (InterruptedException ix) {
+                    // Ignore
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Recursively delete a directory.
+     * <p>
+     * This method behaves like {@code java.io.File#delete} method
+     * but allows deletion of non-empty directories.
+     * </p>
+     *
+     * @params dir The directory abstract pathname.
+     * @returns {@code true} if the directory is successfully deleted;
+     *          {@code false} otherwise.
+     */
+    public static boolean deleteDirectory(File dir)
+    {
+        try {
+            if (dir.delete()) {
+                /* We are done */
+                return true;
+            }
+            else if (dir.isDirectory()) {
+                File[] entries = dir.listFiles();
+                if (entries != null) {
+                    for (int i = 0; i < entries.length; i++) {
+                        if (entries[i].isDirectory()) {
+                            /* Recursively delete a child directory
+                             */
+                            deleteDirectory(entries[i]);
+                        }
+                        else {
+                            /* Regular file
+                             */
+                            entries[i].delete();
+                        }
+                    }
+                }
+                return dir.delete();
+            }
+        } catch (Exception ex) {
+            // Ignore
+        }
+        return false;
+    }
+
 }
 

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=817004&r1=817003&r2=817004&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Sun Sep 20 10:34:09 2009
@@ -49,6 +49,7 @@
         suite.addTest(TestMemoryMap.suite());
         suite.addTest(TestCallback.suite());
         suite.addTest(TestObserver.suite());
+        suite.addTest(TestUtils.suite());
 
         // On Sun JVM/Linux this gives wired results.
         // On first call the exception is thrown, but then

Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUtils.java?rev=817004&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUtils.java (added)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUtils.java Sun Sep 20 10:34:09 2009
@@ -0,0 +1,93 @@
+/* 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.commons.runtime;
+
+import org.apache.commons.runtime.util.*;
+import java.io.File;
+import junit.framework.*;
+
+/**
+ * Utils test task.
+ */
+public class TestUtils extends TestCase
+{
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(TestUtils.class);
+        return suite;
+    }
+
+    public void testTempDir()
+        throws Exception
+    {
+        File td = Utils.createTempDirectory("test-");
+        assertFalse("Create temp directory", td == null);
+        boolean rc = Utils.deleteDirectory(td);
+        assertTrue("Delete temp directory", rc);
+    }
+
+    public void testTempDirWithFiles()
+        throws Exception
+    {
+        boolean rc;
+        File td = Utils.createTempDirectory("test-");
+        assertFalse("Create temp directory", td == null);
+        for (int i = 0; i < 10; i++) {
+            File f = new File(td, "file" + i);
+            rc = f.createNewFile();
+            assertTrue("Create file", rc);
+        }
+        rc = Utils.deleteDirectory(td);
+        assertTrue("Delete temp directory", rc);
+    }
+
+    public void testTempDirWithDirs()
+        throws Exception
+    {
+        boolean rc;
+        File td = Utils.createTempDirectory("test-");
+        assertFalse("Create temp directory", td == null);
+        for (int i = 0; i < 10; i++) {
+            File d = new File(td, "dir" + i);
+            rc = d.mkdir();
+            assertTrue("Create directory", rc);
+        }
+        rc = Utils.deleteDirectory(td);
+        assertTrue("Delete temp directory", rc);
+    }
+
+    public void testTempDirWithDirsAndFiles()
+        throws Exception
+    {
+        boolean rc;
+        File td = Utils.createTempDirectory("test-");
+        assertFalse("Create temp directory", td == null);
+        for (int i = 0; i < 10; i++) {
+            File d = new File(td, "dir" + i);
+            rc = d.mkdir();
+            assertTrue("Create directory", rc);
+            for (int j = 0; j < 10; j++) {
+                File f = new File(d.getPath(), "file" + j);
+                rc = f.createNewFile();
+                assertTrue("Create file", rc);
+            }
+        }
+        rc = Utils.deleteDirectory(td);
+        assertTrue("Delete temp directory", rc);
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native