[jboss-cvs] JBossAS SVN: r74540 - in projects/vfs/trunk/src: test/java/org/jboss/test/virtual/test and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Jun 13 12:41:36 EDT 2008


Author: alesj
Date: 2008-06-13 12:41:36 -0400 (Fri, 13 Jun 2008)
New Revision: 74540

Added:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryWrapper.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/SizeLimitedInputStream.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARVFSContextUnitTestCase.java
Log:
Fix zip context creation.

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/SizeLimitedInputStream.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/SizeLimitedInputStream.java	2008-06-13 16:14:10 UTC (rev 74539)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/SizeLimitedInputStream.java	2008-06-13 16:41:36 UTC (rev 74540)
@@ -39,9 +39,9 @@
 
 	private InputStream in;
 
-   private int togo;
+   private long togo;
 
-	public SizeLimitedInputStream(InputStream ins, int size)
+	public SizeLimitedInputStream(InputStream ins, long size)
    {
 		this.in = ins;
 		this.togo = size;
@@ -68,9 +68,10 @@
    {
 		int rc = -1;
 
-		if (togo > 0)
+      if (togo > 0)
       {
-			rc = togo < len ? togo : len;
+         int ltogo = (int)togo;
+         rc = ltogo < len ? ltogo : len;
 			rc = in.read(buf, offs, rc);
 			if (rc != -1)
             togo -= rc;

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-06-13 16:14:10 UTC (rev 74539)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-06-13 16:41:36 UTC (rev 74540)
@@ -23,7 +23,9 @@
 
 import java.io.BufferedOutputStream;
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -45,6 +47,7 @@
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
 import org.jboss.logging.Logger;
 import org.jboss.virtual.VFSUtils;
@@ -234,15 +237,21 @@
       initEntries();
    }
 
+   /**
+    * Create zip source.
+    *
+    * @param rootPath the root path
+    * @return zip entry wrapper
+    * @throws IOException for any error
+    */
    protected ZipWrapper createZipSource(String rootPath) throws IOException
    {
       File file = null;
-      String relative;
+      String relative = null;
       File fp = new File(rootPath);
       if (fp.exists())
       {
          file = fp;
-         relative = "";
       }
       else
       {
@@ -265,10 +274,63 @@
       if (file == null)
          throw new IOException("VFS file does not exist: " + rootPath);
 
-      // TODO - use relative to create the right context
+      if (relative != null)
+      {
+         return findEntry(new FileInputStream(file), relative);
+      }
+      else
+      {
+         boolean noReaper = Boolean.valueOf(getOptions().get(VFSUtils.NO_REAPER_QUERY));
+         return new ZipFileWrapper(file, autoClean, noReaper);
+      }
+   }
 
-      String noReaper = getOptions().get(VFSUtils.NO_REAPER_QUERY);
-      return new ZipFileWrapper(file, autoClean, Boolean.valueOf(noReaper));
+   protected ZipWrapper findEntry(InputStream is, String relative) throws IOException
+   {
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      ZipEntryContext.copyStreamAndClose(is, baos);
+      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+      ZipInputStream zis = new ZipInputStream(bais);
+      ZipEntry entry;
+      String longestNameMatch = null;
+      while((entry = zis.getNextEntry()) != null)
+      {
+         String entryName = entry.getName();
+         if (relative.startsWith(entryName))
+         {
+            if (entryName.equals(relative))
+            {
+               InputStream stream = new SizeLimitedInputStream(zis, entry.getSize());
+               if (JarUtils.isArchive(entryName))
+               {
+                  return new ZipStreamWrapper(stream, entryName, System.currentTimeMillis());
+               }
+               else
+                  return new ZipEntryWrapper(stream, entryName, System.currentTimeMillis());
+            }
+
+            if (longestNameMatch == null || longestNameMatch.length() < entryName.length())
+            {
+               longestNameMatch = entryName;
+            }
+         }
+      }
+      if (longestNameMatch == null)
+         throw new IllegalArgumentException("Cannot find entry: " + is + ", " + relative);
+
+      bais.reset();
+      zis = new ZipInputStream(bais);
+      while((entry = zis.getNextEntry()) != null)
+      {
+         String entryName = entry.getName();
+         if (entryName.equals(longestNameMatch))
+         {
+            relative = relative.substring(longestNameMatch.length() + 1);
+            return findEntry(new SizeLimitedInputStream(zis, entry.getSize()), relative);
+         }
+      }
+      throw new IllegalArgumentException("No such entry: " + is + ", " + relative);
    }
 
    /**

Copied: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryWrapper.java (from rev 74373, 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/ZipEntryWrapper.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryWrapper.java	2008-06-13 16:41:36 UTC (rev 74540)
@@ -0,0 +1,131 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+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.util.Enumeration;
+import java.util.zip.ZipEntry;
+
+/**
+ * ZipEntryWrapper - for abstracted access to in-memory entry
+ *
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
+ */
+class ZipEntryWrapper extends ZipWrapper
+{
+   private static final EmptyEnumeration emptyEnumeration = new EmptyEnumeration();
+
+   /** Raw zip archive loaded in memory */
+   private byte [] zipBytes;
+
+   /** Name */
+   private String name;
+
+   /**
+    * ZipStreamWrapper is not aware of actual zip source so it can not detect
+    * if it's been modified, like ZipFileWrapper does.
+    *
+    * @param zipStream
+    * @param lastModified passed by zip stream provider - constant value
+    * @throws java.io.IOException
+    */
+   ZipEntryWrapper(InputStream zipStream, String name, long lastModified) throws IOException
+   {
+      // read the contents into memory buffer
+      ByteArrayOutputStream bout = new ByteArrayOutputStream();
+      ZipEntryContext.copyStreamAndClose(zipStream, bout);
+      zipBytes = bout.toByteArray();
+
+      // TODO - delegate file meta info operations to parent?
+      this.name = name;
+      this.lastModified = lastModified;
+   }
+
+   boolean exists()
+   {
+      return true;
+   }
+
+   long getLastModified()
+   {
+      return lastModified;
+   }
+
+   String getName()
+   {
+      return name;
+   }
+
+   long getSize()
+   {
+      return zipBytes.length;
+   }
+
+   InputStream openStream(ZipEntry ent) throws IOException
+   {
+      return getRootAsStream();
+   }
+
+   InputStream getRootAsStream() throws FileNotFoundException
+   {
+      return new ByteArrayInputStream(zipBytes);
+   }
+
+   void acquire() throws IOException
+   {
+   }
+
+   Enumeration<? extends ZipEntry> entries() throws IOException
+   {
+      return emptyEnumeration;
+   }
+
+   void close()
+   {
+      zipBytes = null;
+   }
+
+   public String toString()
+   {
+      return super.toString() + " - " + name;
+   }
+
+   /**
+    * Zip stream enumeration.
+    */
+   private static class EmptyEnumeration implements Enumeration<ZipEntry>
+   {
+      public boolean hasMoreElements()
+      {
+         return false;
+      }
+
+      public ZipEntry nextElement()
+      {
+         return null;
+      }
+   }
+}
\ No newline at end of file

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java	2008-06-13 16:14:10 UTC (rev 74539)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARSerializationUnitTestCase.java	2008-06-13 16:41:36 UTC (rev 74540)
@@ -58,7 +58,7 @@
 
    /**
     * Test reading the contents of nested jar entries.
-    * @throws Exception
+    * @throws Exception for any error
     */
    public void testInnerJarFile() throws Exception
    {
@@ -124,7 +124,7 @@
 
    /**
     * JBVFS-17 test
-    * @throws Exception
+    * @throws Exception for any error
     */
    public void testInnerJarFilesOnlyFileSerialization() throws Exception
    {

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-06-13 16:14:10 UTC (rev 74539)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/JARVFSContextUnitTestCase.java	2008-06-13 16:41:36 UTC (rev 74540)
@@ -22,6 +22,7 @@
 package org.jboss.test.virtual.test;
 
 import java.io.InputStream;
+import java.io.IOException;
 import java.net.URL;
 
 import junit.framework.Test;
@@ -190,11 +191,22 @@
    {
       URL url = getResource("/vfs/test/nested/" + getNestedName() + ".jar");
       String urlString = url.toExternalForm();
-      URL vfsURL = new URL(getProtocol() + urlString.substring(4) + "/complex.jar");
+      testInnerEntryOverURL(urlString, "/complex.jar", false);
+      // test children
+      testInnerEntryOverURL(urlString, "/complex.jar/child", false);
+      testInnerEntryOverURL(urlString, "/complex.jar/subfolder/subchild", false);
+      // test folder
+      testInnerEntryOverURL(urlString, "/complex.jar/subfolder", true);
+      testInnerEntryOverURL(urlString, "/complex.jar/subfolder/subsubfolder", true);
+   }
+
+   protected void testInnerEntryOverURL(String urlString, String entry, boolean result) throws IOException
+   {
+      URL vfsURL = new URL(getProtocol() + urlString.substring(4) + entry);
       InputStream is = vfsURL.openStream();
       try
       {
-         assertFalse("cannot read input stream", is.read() == -1);
+         assertEquals("cannot read input stream", result, is.read() == -1);
       }
       finally
       {




More information about the jboss-cvs-commits mailing list