You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by ti...@apache.org on 2011/07/29 08:32:28 UTC

svn commit: r1152120 - in /aries/trunk/util/src: main/java/org/apache/aries/util/filesystem/ main/java/org/apache/aries/util/filesystem/impl/ test/java/org/apache/aries/util/filesystem/

Author: timothyjward
Date: Fri Jul 29 06:32:27 2011
New Revision: 1152120

URL: http://svn.apache.org/viewvc?rev=1152120&view=rev
Log:
ARIES-715 : Add InputStream based IDirectory support

Added:
    aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/InputStreamClosableDirectory.java
Modified:
    aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/FileSystem.java
    aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/FileSystemImpl.java
    aries/trunk/util/src/test/java/org/apache/aries/util/filesystem/FileSystemTest.java

Modified: aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/FileSystem.java
URL: http://svn.apache.org/viewvc/aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/FileSystem.java?rev=1152120&r1=1152119&r2=1152120&view=diff
==============================================================================
--- aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/FileSystem.java (original)
+++ aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/FileSystem.java Fri Jul 29 06:32:27 2011
@@ -20,8 +20,14 @@
 package org.apache.aries.util.filesystem;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.zip.ZipFile;
 
 import org.apache.aries.util.filesystem.impl.FileSystemImpl;
+import org.apache.aries.util.filesystem.impl.NestedZipDirectory;
+import org.apache.aries.util.io.IOUtils;
 
 /**
  * An abstraction of a file system. A file system can be a zip, or a directory.
@@ -39,4 +45,19 @@ public class FileSystem {
   {
 	  return FileSystemImpl.getFSRoot(fs, null);
   }
+  
+  /**
+   * This method gets an ICloseableDirectory that represents the root of a virtual file
+   * system. The provided InputStream should represent a zip file.
+   * 
+   * When this {@link ICloseableDirectory} is closed then backing resources will be
+   * cleaned up.
+   * 
+   * @param is An input stream to a zip file.
+   * @return   the root of the virtual FS.
+   */
+  public static ICloseableDirectory getFSRoot(InputStream is)
+  {
+    return FileSystemImpl.getFSRoot(is);
+  }
 }

Modified: aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/FileSystemImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/FileSystemImpl.java?rev=1152120&r1=1152119&r2=1152120&view=diff
==============================================================================
--- aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/FileSystemImpl.java (original)
+++ aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/FileSystemImpl.java Fri Jul 29 06:32:27 2011
@@ -1,11 +1,32 @@
+/*
+ * 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 WARRANTIESOR 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.aries.util.filesystem.impl;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipInputStream;
 
+import org.apache.aries.util.filesystem.ICloseableDirectory;
 import org.apache.aries.util.filesystem.IDirectory;
 import org.apache.aries.util.filesystem.IFile;
 import org.apache.aries.util.io.IOUtils;
@@ -79,4 +100,31 @@ public class FileSystemImpl {
 			IOUtils.close(zis);
 		}
 	}
+
+  public static ICloseableDirectory getFSRoot(InputStream is) {
+    File tempFile = null;
+    try {
+      tempFile = File.createTempFile("inputStreamExtract", ".zip");
+    } catch (IOException e1) {
+      _logger.error ("IOException in IDirectory.getFSRoot", e1);
+      return null;
+    }
+    FileOutputStream fos = null; 
+    try {
+      fos = new FileOutputStream(tempFile);
+      IOUtils.copy(is, fos);  
+    } catch (IOException e) {
+      return null;
+    } finally {
+      IOUtils.close(fos);
+    }
+    
+    IDirectory dir = getFSRoot(tempFile, null);
+    
+    if(dir == null)
+      return null;
+    else
+      return new InputStreamClosableDirectory(dir, tempFile);
+
+  }
 }

Added: aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/InputStreamClosableDirectory.java
URL: http://svn.apache.org/viewvc/aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/InputStreamClosableDirectory.java?rev=1152120&view=auto
==============================================================================
--- aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/InputStreamClosableDirectory.java (added)
+++ aries/trunk/util/src/main/java/org/apache/aries/util/filesystem/impl/InputStreamClosableDirectory.java Fri Jul 29 06:32:27 2011
@@ -0,0 +1,38 @@
+/*
+ * 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 WARRANTIESOR 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.aries.util.filesystem.impl;
+
+import java.io.File;
+
+import org.apache.aries.util.filesystem.IDirectory;
+
+public class InputStreamClosableDirectory extends CloseableDirectory {
+
+  private final File tempFile;
+  
+  public InputStreamClosableDirectory(IDirectory delegate, File temp) {
+    super(delegate);
+    tempFile = temp;
+  }
+
+  @Override
+  protected void cleanup() {
+      tempFile.delete();
+  }
+}

Modified: aries/trunk/util/src/test/java/org/apache/aries/util/filesystem/FileSystemTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/util/src/test/java/org/apache/aries/util/filesystem/FileSystemTest.java?rev=1152120&r1=1152119&r2=1152120&view=diff
==============================================================================
--- aries/trunk/util/src/test/java/org/apache/aries/util/filesystem/FileSystemTest.java (original)
+++ aries/trunk/util/src/test/java/org/apache/aries/util/filesystem/FileSystemTest.java Fri Jul 29 06:32:27 2011
@@ -30,6 +30,7 @@ import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Field;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
@@ -38,12 +39,7 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
-
 import org.apache.aries.unittest.junit.Assert;
-import org.apache.aries.util.filesystem.FileSystem;
-import org.apache.aries.util.filesystem.ICloseableDirectory;
-import org.apache.aries.util.filesystem.IDirectory;
-import org.apache.aries.util.filesystem.IFile;
 import org.apache.aries.util.io.IOUtils;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
@@ -106,6 +102,26 @@ public class FileSystemTest
     runBasicRootDirTests(dir, baseDir.length(), baseDir.lastModified());
   }
   
+  /**
+   * Make sure we correctly understand the content of the application when the
+   * application is a zip. This test just checks that the
+   * root directory returns the expected information.
+   * 
+   * @throws IOException
+   */
+  @Test(expected=UnsupportedOperationException.class)
+  public void basicRootDirTestsWithZipInputStream() throws IOException
+  {
+    File baseDir = new File("fileSystemTest/app2.zip");
+    ICloseableDirectory dir = FileSystem.getFSRoot(new FileInputStream(baseDir));
+    
+    try {
+      runBasicRootDirTests(dir, baseDir.length(), baseDir.lastModified());
+    } finally {
+      dir.close();
+    }
+  }
+  
   @Test
   public void testInvalidFSRoot() throws IOException
   {
@@ -177,6 +193,35 @@ public class FileSystemTest
   }
   
   /**
+   * Make sure that the operations work with zip files inside other zip files. Performance is not going to be great though :)
+   */
+  @Test
+  public void nestedZipInZipInputStream() throws Exception
+  {
+    ICloseableDirectory outer = FileSystem.getFSRoot(new FileInputStream("fileSystemTest/outer.zip"));
+    try {
+      IFile innerFile = outer.getFile("app2.zip");
+      assertNotNull(innerFile);
+
+      IDirectory inner = innerFile.convertNested();
+      assertNotNull(inner);
+      
+      File desiredFile = new File(new File(getTestResourceDir(), "/app1"), "META-INF/APPLICATION.MF");  
+      
+      // no size information when stream reading :(
+      runBasicDirTest(inner, "app2.zip/", -1, desiredFile.lastModified());
+      runBasicDirTest(inner.toCloseable(), "app2.zip/", desiredFile.length(), desiredFile.lastModified());
+    } finally {
+      outer.close();
+      
+      Field f = outer.getClass().getDeclaredField("tempFile");
+      
+      f.setAccessible(true);
+      assertFalse(((File)f.get(outer)).exists());
+    }
+  }
+  
+  /**
    * Make sure we correctly understand the directory structure for zips.
    * 
    * @throws IOException
@@ -195,6 +240,27 @@ public class FileSystemTest
     runBasicDirTest(dir.toCloseable(), desiredFile.length(), desiredFile.lastModified());
   }
   
+  /**
+   * Make sure we correctly understand the directory structure for zips.
+   * 
+   * @throws IOException
+   */
+  @Test
+  public void basicDirTestsWithZipInputStream() throws IOException
+  {
+    File baseDir = new File("fileSystemTest/app2.zip");
+    ICloseableDirectory dir = FileSystem.getFSRoot(new FileInputStream(baseDir));
+
+    try {
+      File desiredFile = new File(new File(getTestResourceDir(), "/app1"), "META-INF/APPLICATION.MF");
+    
+      runBasicDirTest(dir, desiredFile.length(), desiredFile.lastModified());
+      runBasicDirTest(dir.toCloseable(), desiredFile.length(), desiredFile.lastModified());
+    } finally {
+      dir.close();
+    }
+  }
+  
   @Test
   public void zipCloseableZipSimplePerformanceTest() throws IOException
   {