[jboss-cvs] JBossAS SVN: r78121 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/protocol and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Sep 7 18:10:37 EDT 2008


Author: mstruk
Date: 2008-09-07 18:10:37 -0400 (Sun, 07 Sep 2008)
New Revision: 78121

Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARVFSContextUnitTestCase.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java
Log:
JBVFS-57 JarInputStream generation for directory entries + URL handler query propagation fix

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java	2008-09-07 12:09:12 UTC (rev 78120)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipFileWrapper.java	2008-09-07 22:10:37 UTC (rev 78121)
@@ -26,12 +26,14 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.net.URI;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Enumeration;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
+import java.util.zip.ZipOutputStream;
 
 import org.jboss.logging.Logger;
 import org.jboss.virtual.VFSUtils;
@@ -204,6 +206,10 @@
     */
    synchronized InputStream openStream(ZipEntry ent) throws IOException
    {
+      // JBVFS-57 JarInputStream composition
+      if (ent.isDirectory())
+         return recomposeZipAsInputStream(ent.getName());
+
       ensureZipFile();
       InputStream is = zipFile.getInputStream(ent);
       if (is == null)
@@ -329,6 +335,35 @@
       return file.exists() == false;
    }
 
+   protected synchronized void recomposeZip(OutputStream baos, String path) throws IOException
+   {
+      ZipOutputStream zout = new ZipOutputStream(baos);
+      zout.setMethod(ZipOutputStream.STORED);
+
+      ensureZipFile();
+      Enumeration<? extends ZipEntry> entries = zipFile.entries();
+      while(entries.hasMoreElements())
+      {
+         ZipEntry oldEntry = entries.nextElement();
+         if (oldEntry.getName().startsWith(path))
+         {
+            String newName = oldEntry.getName().substring(path.length());
+            if(newName.length() == 0)
+               continue;
+
+            ZipEntry newEntry = new ZipEntry(newName);
+            newEntry.setComment(oldEntry.getComment());
+            newEntry.setTime(oldEntry.getTime());
+            newEntry.setSize(oldEntry.getSize());
+            newEntry.setCrc(oldEntry.getCrc());
+            zout.putNextEntry(newEntry);
+            if (oldEntry.isDirectory() == false)
+               VFSUtils.copyStream(zipFile.getInputStream(oldEntry), zout);
+         }
+      }
+      zout.close();
+   }
+
    /**
     * toString
     *

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java	2008-09-07 12:09:12 UTC (rev 78120)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipStreamWrapper.java	2008-09-07 22:10:37 UTC (rev 78121)
@@ -29,6 +29,7 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.Enumeration;
@@ -160,7 +161,7 @@
       recomposeZip(baos, "");
    }
 
-   private void recomposeZip(ByteArrayOutputStream baos, String path) throws IOException
+   protected void recomposeZip(OutputStream baos, String path) throws IOException
    {
       ZipOutputStream zout = new ZipOutputStream(baos);
       zout.setMethod(ZipOutputStream.STORED);
@@ -194,22 +195,6 @@
       return recomposeZipAsInputStream("");
    }
 
-   private InputStream recomposeZipAsInputStream(String path) throws FileNotFoundException
-   {
-      ByteArrayOutputStream baos = new ByteArrayOutputStream();
-      try
-      {
-         recomposeZip(baos, path);
-         return new ByteArrayInputStream(baos.toByteArray());
-      }
-      catch (IOException ex)
-      {
-         FileNotFoundException e = new FileNotFoundException("Failed to recompose inflated nested archive " + getName());
-         e.initCause(ex);
-         throw e;
-      }
-   }
-
    static class InMemoryFile
    {
       ZipEntry entry;

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java	2008-09-07 12:09:12 UTC (rev 78120)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipWrapper.java	2008-09-07 22:10:37 UTC (rev 78121)
@@ -21,9 +21,12 @@
 */
 package org.jboss.virtual.plugins.context.zip;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Enumeration;
 import java.util.zip.ZipEntry;
 
@@ -189,5 +192,38 @@
     * @throws IOException for any error
     */
    abstract boolean delete(int gracePeriod) throws IOException;
-   
+
+   /**
+    * Recompose an input stream as having <em>path</em> entry for its root.
+    * All entries' names have to be shifted accordingly.
+    *
+    * @param path root path
+    * @return InputStream containing a new ZIP archive
+    * @throws FileNotFoundException for any error that prevented recomposition
+    */
+   protected InputStream recomposeZipAsInputStream(String path) throws FileNotFoundException
+   {
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      try
+      {
+         recomposeZip(baos, path);
+         return new ByteArrayInputStream(baos.toByteArray());
+      }
+      catch (IOException ex)
+      {
+         FileNotFoundException e = new FileNotFoundException("Failed to recompose inflated nested archive " + getName());
+         e.initCause(ex);
+         throw e;
+      }
+   }
+
+   /**
+    * Recompose an input streamas having <em>path</em> entry for its root, and write it to a given OutputStream.
+    * All entries' names have to be shifted accordingly.
+    *
+    * @param os OutputStream that will contain newly create archive
+    * @param path root path
+    * @throws IOException for any error
+    */
+   abstract protected void recomposeZip(OutputStream os, String path) throws IOException;   
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java	2008-09-07 12:09:12 UTC (rev 78120)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java	2008-09-07 22:10:37 UTC (rev 78121)
@@ -68,6 +68,10 @@
       String file = URLDecoder.decode(url.toExternalForm(), "UTF-8").substring(getProtocolNameLength() + 1); // strip out vfs protocol + :
       URL vfsurl = null;
       String relative;
+      String queryStr = url.getQuery();
+      if (queryStr != null)
+         file = file.substring(0, file.lastIndexOf('?'));
+
       File fp = new File(file);
       if (fp.exists())
       {
@@ -94,7 +98,9 @@
 
       if (vfsurl == null)
          throw new IOException("VFS file does not exist: " + url);
-
+      if (queryStr != null)
+         vfsurl = new URL(vfsurl + "?" + queryStr);
+      
       return new VirtualFileURLConnection(url, vfsurl, relative);
    }
 }
\ No newline at end of file

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARVFSContextUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARVFSContextUnitTestCase.java	2008-09-07 12:09:12 UTC (rev 78120)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARVFSContextUnitTestCase.java	2008-09-07 22:10:37 UTC (rev 78121)
@@ -152,10 +152,19 @@
    /**
     * Handler representing a directory must return a zero length stream
     *
+    * This behavior has changed with vfszip - where directory entry within
+    * zip archive when opened as stream returns a recomposed zip archive
+    * that has requested entry as its root
+    *
     * @throws Exception for any error
     */
    public void testDirectoryZipEntryOpenStream() throws Exception
    {
+      doDirectoryZipEntryOpenStream(true);
+   }
+
+   protected void doDirectoryZipEntryOpenStream(boolean expectEmpty) throws Exception
+   {
       URL url = getResource("/vfs/context/jar/complex.jar");
       VFSContext ctx = createVSFContext(url);
 
@@ -163,7 +172,13 @@
       InputStream is = sub.openStream();
       try
       {
-         assertTrue("input stream closed", is.read() == -1);
+         // JBVFS-57 JarInputStream composition - recomposes directory as a
+         // jar equivalent to jar:complex.jar!/subfolder
+         // NOT IMPLEMENTED for vfsjar - vfsjar is deprecated
+         if (expectEmpty)
+            assertTrue("input stream should be closed", is.read() == -1);
+         else
+            assertTrue("input stream should not be empty", is.read() != -1);
       }
       finally
       {
@@ -257,14 +272,12 @@
     */
    public void testWarClassesJarInputStream() throws Exception
    {
-      System.setProperty(VFSUtils.FORCE_COPY_KEY, "true");
       URL rootURL = getResource("/vfs/test/web_pkg_scope.ear");
       VFS vfs = VFS.getVFS(rootURL);
       VirtualFile file = vfs.getChild("web_pkg_scope_web.war/WEB-INF/classes/META-INF/persistence.xml");
       assertNotNull(file);
       VirtualFile classes = file.getParent().getParent();
       // Access the classes contents as a jar file
-      URL classesURL = classes.toURL();
       String[] entryNames = {
             "web_pkg_scope/",
             "web_pkg_scope/entity/",
@@ -275,19 +288,29 @@
             // "META-INF/",
             "META-INF/persistence.xml"
       };
-      JarInputStream jis = new JarInputStream( classesURL.openStream() );
-      HashSet<String> missingEntries = new HashSet<String>(Arrays.asList(entryNames));
-      Set<String> excess = new HashSet<String>();
-      JarEntry jarEntry;
-      while((jarEntry = jis.getNextJarEntry()) != null)
+
+      URL [] classesURLs = new URL []
       {
-         String name = jarEntry.getName();
-         boolean removed = missingEntries.remove(name);
-         if(!removed)
-            excess.add(name);
+         classes.toURL(),
+         new URL(classes.toURL() + "?" + VFSUtils.USE_COPY_QUERY + "=true")
+      };
+
+      for (URL url: classesURLs)
+      {
+         JarInputStream jis = new JarInputStream( url.openStream() );
+         HashSet<String> missingEntries = new HashSet<String>(Arrays.asList(entryNames));
+         Set<String> excess = new HashSet<String>();
+         JarEntry jarEntry;
+         while((jarEntry = jis.getNextJarEntry()) != null)
+         {
+            String name = jarEntry.getName();
+            boolean removed = missingEntries.remove(name);
+            if(!removed)
+               excess.add(name);
+         }
+         assertEquals("No missing entries: "+missingEntries, 0, missingEntries.size());
+         assertEquals("Excess entries: " + excess, 0, excess.size());
       }
-      assertEquals("No missing entries: "+missingEntries, 0, missingEntries.size());
-      assertEquals("Excess entries: " + excess, 0, excess.size());
       classes.closeStreams();
    }
 

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java	2008-09-07 12:09:12 UTC (rev 78120)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java	2008-09-07 22:10:37 UTC (rev 78121)
@@ -151,6 +151,19 @@
       assertTrue("read archive content", memOut.size() == handler.getSize());
    }
 
+   /**
+    * Handler representing a directory must return a recomposed zip archive
+    * that has requested entry as its root.
+    *
+    * This behavior is new to vfszip. vfsjar returns an empty stream.
+    *
+    * @throws Exception for any error
+    */
+   public void testDirectoryZipEntryOpenStream() throws Exception
+   {
+      doDirectoryZipEntryOpenStream(false);
+   }
+
    // we need to make sure this doesn't get touched before
    protected String getNestedName()
    {




More information about the jboss-cvs-commits mailing list