[cargo] r1685 - in core/trunk/api: container/src/main/java/org/codehaus/cargo/container/internal/util util/src/main/java/org/codehaus/cargo/util util/src/test/java/org/codehaus/cargo/util

View: New views
1 Messages — Rating Filter:   Alert me  

[cargo] r1685 - in core/trunk/api: container/src/main/java/org/codehaus/cargo/container/internal/util util/src/main/java/org/codehaus/cargo/util util/src/test/java/org/codehaus/cargo/util

by adriana-14 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Author: adriana
Date: 2008-08-21 08:57:14 -0500 (Thu, 21 Aug 2008)
New Revision: 1685

Modified:
   core/trunk/api/container/src/main/java/org/codehaus/cargo/container/internal/util/ResourceUtils.java
   core/trunk/api/util/src/main/java/org/codehaus/cargo/util/DefaultFileHandler.java
   core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandler.java
   core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandlerTest.java
Log:
Provide improvement proposed in
 http://jira.codehaus.org/browse/CARGO-597

Modified: core/trunk/api/container/src/main/java/org/codehaus/cargo/container/internal/util/ResourceUtils.java
===================================================================
--- core/trunk/api/container/src/main/java/org/codehaus/cargo/container/internal/util/ResourceUtils.java 2008-08-19 00:34:30 UTC (rev 1684)
+++ core/trunk/api/container/src/main/java/org/codehaus/cargo/container/internal/util/ResourceUtils.java 2008-08-21 13:57:14 UTC (rev 1685)
@@ -1,182 +1,221 @@
-/*
- * ========================================================================
- *
- * Copyright 2003-2006 The Apache Software Foundation. Code from this file
- * was originally imported from the Jakarta Cactus project.
- *
- * Copyright 2004 Vincent Massol.
- *
- * Licensed 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.codehaus.cargo.container.internal.util;
-
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.net.URL;
-import java.net.URLDecoder;
-import java.util.Vector;
-
-import org.apache.tools.ant.filters.util.ChainReaderHelper;
-import org.apache.tools.ant.types.FilterChain;
-import org.codehaus.cargo.util.log.LoggedObject;
-
-/**
- * Utility class that provides a couple of methods for extracting files stored as resource in a JAR.
- *
- * @version $Id$
- */
-public final class ResourceUtils extends LoggedObject
-{
-    /**
-     * Copies a container resource from the JAR into the specified file.
-     *
-     * @param resourceName The name of the resource
-     * @param destFile The file to which the contents of the resource should be copied
-     * @throws IOException If an I/O error occurs while copying the resource
-     */
-    public void copyResource(String resourceName, File destFile) throws IOException
-    {
-        InputStream in = ResourceUtils.class.getResourceAsStream(resourceName);
-        if (in == null)
-        {
-            throw new IOException("Resource [" + resourceName + "] not found");
-        }
-        
-        OutputStream out = null;
-        try
-        {
-            out = new FileOutputStream(destFile);
-            
-            byte[] buf = new byte[4096];
-            int numBytes;
-            while ((numBytes = in.read(buf)) > 0)
-            {
-                out.write(buf, 0, numBytes);
-            }
-        }
-        finally
-        {
-            in.close();
-            if (out != null)
-            {
-                out.close();
-            }
-        }
-    }
-    
-    /**
-     * Copies a container resource from the JAR into the specified file, thereby applying the
-     * specified filters.
-     *
-     * @param resourceName The name of the resource, relative to the
-     *        org.apache.cactus.integration.ant.container package
-     * @param destFile The file to which the contents of the resource should be copied
-     * @param filterChain The ordered list of filter readers that should be applied while copying
-     * @throws IOException If an I/O error occurs while copying the resource
-     */
-    public void copyResource(String resourceName, File destFile, FilterChain filterChain)
-        throws IOException
-    {
-        InputStream resource = ResourceUtils.class.getResourceAsStream(resourceName);
-        if (resource == null)
-        {
-            throw new IOException("Resource [" + resourceName + "] not found");
-        }
-        
-        BufferedReader in = null;
-        BufferedWriter out = null;
-        try
-        {
-            ChainReaderHelper helper = new ChainReaderHelper();
-            helper.setBufferSize(8192);
-            helper.setPrimaryReader(new BufferedReader(new InputStreamReader(resource)));
-            Vector filterChains = new Vector();
-            filterChains.add(filterChain);
-            helper.setFilterChains(filterChains);
-            in = new BufferedReader(helper.getAssembledReader());
-
-            out = new BufferedWriter(new FileWriter(destFile));
-
-            String line;
-            while ((line = in.readLine()) != null)
-            {
-                if (line.length() == 0)
-                {
-                    out.newLine();
-                }
-                else
-                {
-                    out.write(line);
-                    out.newLine();
-                }
-            }
-        }
-        finally
-        {
-            if (in != null)
-            {
-                in.close();
-            }
-            if (out != null)
-            {
-                out.close();
-            }
-        }
-    }
-    
-    /**
-     * Search for the given resource and return the directory or archive that contains it.
-     *
-     * <p>Doesn't work for archives in JDK 1.1 as the URL returned by getResource doesn't contain
-     * the name of the archive.</p>
-     *
-     * @param resourceName The name of the resource
-     * @return The directory or archive containing the specified resource
-     */
-    public File getResourceLocation(String resourceName)
-    {
-        File file = null;
-        URL url = ResourceUtils.class.getResource(resourceName);
-        if (url != null)
-        {
-            String urlString = url.toString();
-            if (urlString.startsWith("jar:file:"))
-            {
-                int pling = urlString.indexOf("!");
-                String jar = urlString.substring(9, pling);
-                file = new File(URLDecoder.decode(jar));
-            }
-            else if (urlString.startsWith("file:"))
-            {
-                int tail = urlString.indexOf(resourceName);
-                String dir = urlString.substring(5, tail);
-                file = new File(URLDecoder.decode(dir));
-            }
-        }
-
-        getLogger().debug("Location for [" + resourceName + "] is [" + file + "]",
-            this.getClass().getName());
-
-        return file;
-    }
-
-}
+/*
+ * ========================================================================
+ *
+ * Copyright 2003-2006 The Apache Software Foundation. Code from this file
+ * was originally imported from the Jakarta Cactus project.
+ *
+ * Copyright 2004 Vincent Massol.
+ *
+ * Licensed 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.codehaus.cargo.container.internal.util;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.Vector;
+
+import org.apache.tools.ant.filters.util.ChainReaderHelper;
+import org.apache.tools.ant.types.FilterChain;
+import org.codehaus.cargo.util.DefaultFileHandler;
+import org.codehaus.cargo.util.FileHandler;
+import org.codehaus.cargo.util.log.LoggedObject;
+
+/**
+ * Utility class that provides a couple of methods for extracting files stored as resource in a JAR.
+ *
+ * @version $Id$
+ */
+public final class ResourceUtils extends LoggedObject
+{
+    /**
+     * Default file handler for the @link{ResourceUtils#copyResource(String, File)} and
+     * @link{ResourceUtils#copyResource(String, File, FilterChain)} methods.
+     */
+    private static FileHandler defaultFileHandler = new DefaultFileHandler();
+
+    /**
+     * Copies a container resource from the JAR into the specified file.
+     *
+     * @param resourceName The name of the resource
+     * @param destFile The file to which the contents of the resource should be copied
+     * @throws IOException If an I/O error occurs while copying the resource
+     */
+    public void copyResource(String resourceName, File destFile) throws IOException
+    {
+        copyResource(resourceName, destFile.getPath(), defaultFileHandler);
+    }
+    
+    /**
+     * Copies a container resource from the JAR into the specified file using the specified
+     * file handler.
+     *
+     * @param resourceName The name of the resource
+     * @param destFile The file to which the contents of the resource should be copied
+     * @param handler The file handler to use
+     * @throws IOException If an I/O error occurs while copying the resource
+     */
+    public void copyResource(String resourceName, String destFile, FileHandler handler)
+        throws IOException
+    {
+        InputStream in = ResourceUtils.class.getResourceAsStream(resourceName);
+        if (in == null)
+        {
+            throw new IOException("Resource [" + resourceName + "] not found");
+        }
+        
+        OutputStream out = null;
+        try
+        {
+            out = handler.getOutputStream(destFile);
+            
+            byte[] buf = new byte[4096];
+            int numBytes;
+            while ((numBytes = in.read(buf)) > 0)
+            {
+                out.write(buf, 0, numBytes);
+            }
+        }
+        finally
+        {
+            in.close();
+            if (out != null)
+            {
+                out.close();
+            }
+        }
+    }
+    
+    /**
+     * Copies a container resource from the JAR into the specified file, thereby applying the
+     * specified filters.
+     *
+     * @param resourceName The name of the resource, relative to the
+     *        org.apache.cactus.integration.ant.container package
+     * @param destFile The file to which the contents of the resource should be copied
+     * @param filterChain The ordered list of filter readers that should be applied while copying
+     * @throws IOException If an I/O error occurs while copying the resource
+     */
+    public void copyResource(String resourceName, File destFile, FilterChain filterChain)
+        throws IOException
+    {
+        copyResource(resourceName, destFile.getPath(), defaultFileHandler, filterChain);
+    }
+    
+    /**
+     * Copies a container resource from the JAR into the specified file, using the specified
+     * file handler thereby applying the specified filters.
+     *
+     * @param resourceName The name of the resource, relative to the
+     *        org.apache.cactus.integration.ant.container package
+     * @param destFile The file to which the contents of the resource should be copied
+     * @param handler The file handler to be used for file copy
+     * @param filterChain The ordered list of filter readers that should be applied while copying
+     * @throws IOException If an I/O error occurs while copying the resource
+     */
+    public void copyResource(String resourceName, String destFile, FileHandler handler,
+        FilterChain filterChain) throws IOException
+    {
+        InputStream resource = ResourceUtils.class.getResourceAsStream(resourceName);
+        if (resource == null)
+        {
+            throw new IOException("Resource [" + resourceName + "] not found");
+        }
+        
+        BufferedReader in = null;
+        BufferedWriter out = null;
+        try
+        {
+            ChainReaderHelper helper = new ChainReaderHelper();
+            helper.setBufferSize(8192);
+            helper.setPrimaryReader(new BufferedReader(new InputStreamReader(resource)));
+            Vector filterChains = new Vector();
+            filterChains.add(filterChain);
+            helper.setFilterChains(filterChains);
+            in = new BufferedReader(helper.getAssembledReader());
+
+            out = new BufferedWriter(new OutputStreamWriter(handler.getOutputStream(destFile)));
+
+            String line;
+            while ((line = in.readLine()) != null)
+            {
+                if (line.length() == 0)
+                {
+                    out.newLine();
+                }
+                else
+                {
+                    out.write(line);
+                    out.newLine();
+                }
+            }
+        }
+        finally
+        {
+            if (in != null)
+            {
+                in.close();
+            }
+            if (out != null)
+            {
+                out.close();
+            }
+        }
+    }
+    
+    /**
+     * Search for the given resource and return the directory or archive that contains it.
+     *
+     * <p>Doesn't work for archives in JDK 1.1 as the URL returned by getResource doesn't contain
+     * the name of the archive.</p>
+     *
+     * @param resourceName The name of the resource
+     * @return The directory or archive containing the specified resource
+     */
+    public File getResourceLocation(String resourceName)
+    {
+        File file = null;
+        URL url = ResourceUtils.class.getResource(resourceName);
+        if (url != null)
+        {
+            String urlString = url.toString();
+            if (urlString.startsWith("jar:file:"))
+            {
+                int pling = urlString.indexOf("!");
+                String jar = urlString.substring(9, pling);
+                file = new File(URLDecoder.decode(jar));
+            }
+            else if (urlString.startsWith("file:"))
+            {
+                int tail = urlString.indexOf(resourceName);
+                String dir = urlString.substring(5, tail);
+                file = new File(URLDecoder.decode(dir));
+            }
+        }
+
+        getLogger().debug("Location for [" + resourceName + "] is [" + file + "]",
+            this.getClass().getName());
+
+        return file;
+    }
+
+}

Modified: core/trunk/api/util/src/main/java/org/codehaus/cargo/util/DefaultFileHandler.java
===================================================================
--- core/trunk/api/util/src/main/java/org/codehaus/cargo/util/DefaultFileHandler.java 2008-08-19 00:34:30 UTC (rev 1684)
+++ core/trunk/api/util/src/main/java/org/codehaus/cargo/util/DefaultFileHandler.java 2008-08-21 13:57:14 UTC (rev 1685)
@@ -227,8 +227,8 @@
     public String createDirectory(String parentDir, String name)
     {
         File dir = new File(parentDir, name);
-        dir.mkdirs();
-        if (!dir.isDirectory())
+        boolean created = dir.mkdirs();
+        if (!created || !dir.isDirectory())
         {
             throw new CargoException("Couldn't create directory " + dir.getAbsolutePath());
         }

Modified: core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandler.java
===================================================================
--- core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandler.java 2008-08-19 00:34:30 UTC (rev 1684)
+++ core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandler.java 2008-08-21 13:57:14 UTC (rev 1685)
@@ -109,6 +109,32 @@
         }
     }
 
+    public String createDirectory(String parent, String file)
+    {
+        if (file == null)
+        {
+            file = "";
+        }
+
+        if (parent!= null && !parent.endsWith("/") &&
+            !file.startsWith("/"))
+        {
+            parent += "/";
+        }
+
+        String filename = parent == null ? file : parent + file;
+        try
+        {
+            FileObject fileObject = getFileSystemManager().resolveFile(filename);
+            fileObject.createFolder();
+            return fileObject.toString();
+        }
+        catch (FileSystemException e)
+        {
+            throw new CargoException("Failed to create folder [" + filename + "]", e);
+        }
+    }
+
     public boolean exists(String path)
     {
         boolean result;

Modified: core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandlerTest.java
===================================================================
--- core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandlerTest.java 2008-08-19 00:34:30 UTC (rev 1684)
+++ core/trunk/api/util/src/test/java/org/codehaus/cargo/util/VFSFileHandlerTest.java 2008-08-21 13:57:14 UTC (rev 1685)
@@ -20,6 +20,7 @@
 package org.codehaus.cargo.util;
 
 import junit.framework.TestCase;
+
 import org.apache.commons.vfs.FileObject;
 import org.apache.commons.vfs.impl.StandardFileSystemManager;
 
@@ -104,4 +105,27 @@
         assertEquals(1, children.length);
         assertEquals("ram:///some/directory/file.txt", children[0]);
     }
+
+    public void testCreateDirectory()
+    {
+        this.fileHandler.createDirectory("ram://test", "test");
+        assertTrue(this.fileHandler.exists("ram:///test/test"));
+        assertTrue(this.fileHandler.isDirectory("ram:///test/test"));
+
+        this.fileHandler.createDirectory("ram://test2/", "test");
+        assertTrue(this.fileHandler.exists("ram:///test2/test"));
+        assertTrue(this.fileHandler.isDirectory("ram:///test2/test"));
+
+        this.fileHandler.createDirectory("ram://test3", "/test");
+        assertTrue(this.fileHandler.exists("ram:///test3/test"));
+        assertTrue(this.fileHandler.isDirectory("ram:///test3/test"));
+
+        this.fileHandler.createDirectory(null, "ram://test4");
+        assertTrue(this.fileHandler.exists("ram:///test4"));
+        assertTrue(this.fileHandler.isDirectory("ram:///test4"));
+
+        this.fileHandler.createDirectory("ram://test5", null);
+        assertTrue(this.fileHandler.exists("ram:///test5"));
+        assertTrue(this.fileHandler.isDirectory("ram:///test5"));
+    }
 }


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price!