[jboss-cvs] JBossAS SVN: r94632 - in projects/vfs/branches/dml-zip-rework/src: main/java/org/jboss/vfs/spi and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 9 18:52:45 EDT 2009


Author: johnbailey
Date: 2009-10-09 18:52:44 -0400 (Fri, 09 Oct 2009)
New Revision: 94632

Added:
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualJarInputStream.java
   projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/VirtualJarInputStreamTest.java
Modified:
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFSUtils.java
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFile.java
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFileAssembly.java
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
   projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java
Log:
Added support for getting a JarInputStream from a VFS directory/archive

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFSUtils.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFSUtils.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFSUtils.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -35,6 +35,7 @@
 import java.net.URL;
 import java.net.URLDecoder;
 import java.util.Collection;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
@@ -684,4 +685,36 @@
     public static InputStream emptyStream() {
         return EMPTY_STREAM;
     }
+    
+    
+    /**
+     * Determine the relative path within the assembly.
+     * 
+     * @param mountPoint
+     * @param target
+     * @return
+     */
+    public static List<String> getRelativePath(VirtualFile mountPoint, VirtualFile target) {
+       List<String> pathParts = new LinkedList<String>();
+       collectPathParts(mountPoint, target, pathParts);
+       return pathParts;
+    }
+
+    /**
+     * Recursively work from the target to the mount-point and collect the path elements.
+     * 
+     * @param mountPoint
+     * @param current
+     * @param pathParts
+     */
+    private static void collectPathParts(VirtualFile mountPoint, VirtualFile current, List<String> pathParts) {
+       if (current == null) {
+          throw new IllegalArgumentException("VirtualFile not a child of provided mount point");
+       }
+       if (current.equals(mountPoint)) {
+          return;
+       }
+       collectPathParts(mountPoint, current.getParent(), pathParts);
+       pathParts.add(current.getName());
+    }
 }

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFile.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFile.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFile.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -28,6 +28,7 @@
 import java.net.URI;
 import java.net.URL;
 import java.net.URISyntaxException;
+import java.security.CodeSigner;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -186,6 +187,9 @@
      * @throws IOException for any error accessing the file system
      */
     public InputStream openStream() throws IOException {
+        if(isDirectory()) {
+           return new VirtualJarInputStream(this);
+        }
         final VFS.Mount mount = VFS.instance.getMount(this);
         return mount.getFileSystem().openInputStream(mount.getMountPoint(), this);
     }
@@ -420,6 +424,17 @@
     public URI toURI() throws URISyntaxException {
         return VFSUtils.getVirtualURI(this);
     }
+    
+    /**
+     * Get the {@link CodeSigner}s for a the virtual file.
+     *
+     * @return {@link CodeSigner} for the virtual file or null if not signed. 
+    * @throws IOException 
+     */
+    public CodeSigner[] getCodeSigners() {
+        final VFS.Mount mount = VFS.instance.getMount(this);
+        return mount.getFileSystem().getCodeSigners(mount.getMountPoint(), this);
+    }
 
     /**
      * Get a human-readable (but non-canonical) representation of this virtual file.

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFileAssembly.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFileAssembly.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFileAssembly.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -101,7 +101,7 @@
     * @throws IOException 
     */
    public VirtualFile getFile(VirtualFile mountPoint, VirtualFile target) {
-      Path path = getRelativePath(mountPoint, target);
+      Path path = new Path(VFSUtils.getRelativePath(mountPoint, target));
       return rootNode.getFile(path, mountPoint);
    }
 
@@ -113,39 +113,8 @@
    }
 
    /**
-    * Determine the relative path within the assembly.
     * 
-    * @param mountPoint
-    * @param target
     * @return
-    */
-   private Path getRelativePath(VirtualFile mountPoint, VirtualFile target) {
-      List<String> pathParts = new LinkedList<String>();
-      collectPathParts(mountPoint, target, pathParts);
-      return new Path(pathParts);
-   }
-
-   /**
-    * Recursively work from the target to the mount-point and collect the path elements.
-    * 
-    * @param mountPoint
-    * @param current
-    * @param pathParts
-    */
-   private void collectPathParts(VirtualFile mountPoint, VirtualFile current, List<String> pathParts) {
-      if (current == null) {
-         throw new IllegalArgumentException("VirtualFile not a child of provided mount point");
-      }
-      if (current.equals(mountPoint)) {
-         return;
-      }
-      collectPathParts(mountPoint, current.getParent(), pathParts);
-      pathParts.add(current.getName());
-   }
-
-   /**
-    * 
-    * @return
     * @throws IOException
     */
    private TempFileProvider getTempFileProvider() throws IOException {

Added: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualJarInputStream.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualJarInputStream.java	                        (rev 0)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualJarInputStream.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -0,0 +1,294 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.vfs;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.CodeSigner;
+import java.security.cert.Certificate;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.Iterator;
+import java.util.List;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+import java.util.jar.Manifest;
+import java.util.zip.ZipEntry;
+
+import org.jboss.vfs.util.PathTokenizer;
+
+/**
+ * Virtual JarInputStream used for representing any VFS directory as a JarInputStream.
+ *
+ * @author <a href="baileyje at gmail.com">John Bailey</a>
+ */
+public class VirtualJarInputStream extends JarInputStream {
+   private final Deque<Iterator<VirtualFile>> entryItr = new ArrayDeque<Iterator<VirtualFile>>();
+
+   private final VirtualFile root;
+
+   private InputStream currentEntryStream;
+
+   private boolean closed;
+
+   /**
+    * Construct a {@link VirtualJarInputStream} from a {@link VirtualFile} root
+    * 
+    * @param root VirtualFile directory to use as the base of the virtual Jar.
+    * @throws IOException
+    */
+   public VirtualJarInputStream(VirtualFile root) throws IOException {
+      super(VFSUtils.emptyStream());
+      this.root = root;
+      entryItr.add(root.getChildren().iterator());
+   }
+
+   /*
+    * {@inheritDoc} 
+    * @see java.util.jar.JarInputStream#getNextEntry()
+    */
+   @Override
+   public ZipEntry getNextEntry() throws IOException {
+      return getNextJarEntry();
+   }
+
+   /*
+    *  {@inheritDoc} 
+    * @see java.util.jar.JarInputStream#getNextJarEntry()
+    */
+   @Override
+   public JarEntry getNextJarEntry() throws IOException {
+      if (currentEntryStream != null) {
+         currentEntryStream.close();
+      }
+
+      Iterator<VirtualFile> topItr = entryItr.peekFirst();
+      if (topItr == null) {
+         return null;
+      }
+      if (!topItr.hasNext()) {
+         entryItr.pop();
+         return getNextJarEntry();
+      }
+      VirtualFile current = topItr.next();
+      if (current.isDirectory()) {
+         entryItr.add(current.getChildren().iterator());
+         return getNextJarEntry();
+      }
+
+      openCurrent(current);
+      String entryName = getEntryName(current);
+      Attributes attributes = null;
+      Manifest manifest = getManifest();
+      if (manifest != null) {
+         attributes = manifest.getAttributes(entryName);
+      }
+      return new VirtualJarEntry(entryName, current, attributes);
+   }
+
+   /*
+    * {@inheritDoc} 
+    * @see java.util.jar.JarInputStream#getManifest()
+    */
+   @Override
+   public Manifest getManifest() {
+      try {
+         return VFSUtils.getManifest(root);
+      }
+      catch (IOException e) {
+         throw new RuntimeException(e);
+      }
+   }
+
+   /*
+    * {@inheritDoc} 
+    * @see java.util.zip.InflaterInputStream#read()
+    */
+   @Override
+   public int read() throws IOException {
+      ensureOpen();
+      return checkForEoSAndReturn(currentEntryStream.read());
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see java.io.FilterInputStream#read(byte[])
+    */
+   @Override
+   public int read(byte[] b) throws IOException {
+      return read(b, 0, b.length);
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see java.util.jar.JarInputStream#read(byte[], int, int)
+    */
+   @Override
+   public int read(byte[] b, int off, int len) throws IOException {
+      ensureOpen();
+      return checkForEoSAndReturn(currentEntryStream.read(b, off, len));
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see java.util.zip.ZipInputStream#available()
+    */
+   @Override
+   public int available() throws IOException {
+      ensureOpen();
+      return currentEntryStream.available() > 0 ? 1 : 0;
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see java.util.zip.ZipInputStream#close()
+    */
+   @Override
+   public void close() throws IOException {
+      closed = true;
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see java.util.zip.ZipInputStream#closeEntry()
+    */
+   @Override
+   public void closeEntry() throws IOException {
+      if (currentEntryStream != null) {
+         currentEntryStream.close();
+      }
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see java.util.zip.ZipInputStream#skip(long)
+    */
+   @Override
+   public long skip(long n) throws IOException {
+      ensureOpen();
+      return currentEntryStream.skip(n);
+   }
+
+   /**
+    * Ensure there is currently a JarEntry stream open.
+    * @throws IOException
+    */
+   private void ensureOpen() throws IOException {
+      if (closed) {
+         throw new IOException("Stream is closed");
+      }
+   }
+
+   /**
+    * Check to see if the result is the EOF and if so exchange the current entry stream with the empty stream.
+    * 
+    * @param result
+    * @return int result
+    * @throws IOException
+    */
+   private int checkForEoSAndReturn(int result) throws IOException {
+      if (result == -1) {
+         closeEntry();
+         currentEntryStream = VFSUtils.emptyStream();
+      }
+      return result;
+   }
+
+   /**
+    * Open the current virtual file as the current JarEntry stream.
+    * @param current
+    * @throws IOException
+    */
+   private void openCurrent(VirtualFile current) throws IOException {
+      currentEntryStream = current.openStream();
+   }
+
+   /**
+    * Get the entry name from a VirtualFile.
+    * @param entry
+    * @return
+    */
+   private String getEntryName(VirtualFile entry) {
+      List<String> pathParts = VFSUtils.getRelativePath(root, entry);
+      return PathTokenizer.getRemainingPath(pathParts, 0);
+   }
+
+   /**
+    * Virtual JarEntry used for representing a child VirtualFile as a JarEntry.
+    *
+    * @author <a href="baileyje at gmail.com">John Bailey</a>
+    * @version $Revision: 1.1 $
+    */
+   public static class VirtualJarEntry extends JarEntry {
+      private final VirtualFile virtualFile;
+      private final Attributes attributes;
+      
+      /**
+       * Construct a new 
+       * @param name
+       * @param virtualFile
+       * @param attributes
+       */
+      public VirtualJarEntry(String name, VirtualFile virtualFile, Attributes attributes) {
+         super(name);
+         this.virtualFile = virtualFile;
+         this.attributes = attributes;
+      }
+
+      /*
+       * {@inheritDoc}
+       * @see java.util.jar.JarEntry#getAttributes()
+       */
+      @Override
+      public Attributes getAttributes() throws IOException {
+         return attributes;
+      }
+
+      /*
+       * @inheritDoc}
+       * @see java.util.jar.JarEntry#getCertificates()
+       */
+      @Override
+      public Certificate[] getCertificates() {
+         CodeSigner[] signers = getCodeSigners();
+         if (signers == null) {
+            return null;
+         }
+         List<Certificate> certs = new ArrayList<Certificate>();
+         for (CodeSigner signer : signers) {
+            certs.addAll(signer.getSignerCertPath().getCertificates());
+         }
+         return certs.toArray(new Certificate[certs.size()]);
+      }
+      
+      /*
+       * @inheritDoc}
+       * @see java.util.jar.JarEntry#getCodeSigners()
+       */
+      @Override
+      public CodeSigner[] getCodeSigners() {
+         return virtualFile.getCodeSigners();
+      }
+   }
+}

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -25,6 +25,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.FileNotFoundException;
+import java.security.CodeSigner;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
@@ -107,6 +108,15 @@
     public void close() throws IOException {
         assembly.close();
     }
+    
+    /** {@inheritDoc} */
+    public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
+       VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
+       if (assemblyFile == null) {
+          return null;
+       }
+       return assemblyFile.getCodeSigners();
+    }
 
     private VirtualFile getExistingFile(final VirtualFile mountPoint, final VirtualFile target) throws FileNotFoundException {
         VirtualFile assemblyFile = assembly.getFile(mountPoint, target);

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/FileSystem.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.io.Closeable;
 import java.io.InputStream;
+import java.security.CodeSigner;
 import java.util.List;
 
 /**
@@ -136,6 +137,16 @@
      * @return the collection of children names
      */
     List<String> getDirectoryEntries(VirtualFile mountPoint, VirtualFile target);
+    
+    /**
+     * Get the {@link CodeSigner}s for a the virtual file.
+     * 
+     * @param mountPoint the mount point of the filesystem instance (guaranteed to be a parent of {@code target})
+     * @param target the virtual file to act upon
+     *
+     * @return {@link CodeSigner} for the virtual file or null if not signed. 
+     */
+    CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target);
 
     /**
      * Destroy this filesystem instance.  After this method is called, the filesystem may not be used in any way.  This

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -30,6 +30,7 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.security.CodeSigner;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -38,8 +39,8 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
 import org.jboss.vfs.TempDir;
 import org.jboss.vfs.VFSUtils;
@@ -54,7 +55,7 @@
  */
 public final class JavaZipFileSystem implements FileSystem {
 
-    private final ZipFile zipFile;
+    private final JarFile zipFile;
     private final File archiveFile;
     private final long zipTime;
     private final ZipNode rootNode;
@@ -84,14 +85,14 @@
      */
     public JavaZipFileSystem(File archiveFile, TempDir tempDir) throws IOException {
         zipTime = archiveFile.lastModified();
-        final ZipFile zipFile;
-        this.zipFile = zipFile = new ZipFile(archiveFile);
+        final JarFile zipFile;
+        this.zipFile = zipFile = new JarFile(archiveFile);
         this.archiveFile = archiveFile;
         this.tempDir = tempDir;
-        final Enumeration<? extends ZipEntry> entries = zipFile.entries();
+        final Enumeration<? extends JarEntry> entries = zipFile.entries();
         final ZipNode rootNode = new ZipNode(new HashMap<String, ZipNode>(), "", null);
         FILES:
-        for (ZipEntry entry : iter(entries)) {
+        for (JarEntry entry : iter(entries)) {
             final String name = entry.getName();
             final boolean isDirectory = entry.isDirectory();
             final List<String> tokens = PathTokenizer.getTokens(name);
@@ -140,7 +141,7 @@
                 return cachedFile;
             }
             // nope, create a cached temp
-            final ZipEntry zipEntry = getNodeEntry(zipNode);
+            final JarEntry zipEntry = getNodeEntry(zipNode);
             final String name = zipEntry.getName();
             cachedFile = buildFile(contentsDir, name);
             cachedFile.getParentFile().mkdirs(); 
@@ -159,7 +160,7 @@
         if (rootNode == zipNode) {
             return new FileInputStream(archiveFile);
         }
-        final ZipEntry entry = zipNode.entry;
+        final JarEntry entry = zipNode.entry;
         if (entry == null) {
             throw new IOException("Not a file: \"" + target.getPathName() + "\"");
         }
@@ -181,7 +182,7 @@
             return 0L;
         }
         final File cachedFile = zipNode.cachedFile;
-        final ZipEntry entry = zipNode.entry;
+        final JarEntry entry = zipNode.entry;
         if (zipNode == rootNode) {
             return archiveFile.length();
         }
@@ -194,7 +195,7 @@
             return 0L;
         }
         final File cachedFile = zipNode.cachedFile;
-        final ZipEntry entry = zipNode.entry;
+        final JarEntry entry = zipNode.entry;
         return cachedFile != null ? cachedFile.lastModified() : entry == null ? zipTime : entry.getTime();
     }
 
@@ -229,10 +230,22 @@
         }
         return names;
     }
+    
+    /**
+     * {@inheritDoc}
+     */
+    public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
+       final ZipNode zipNode = getZipNode(mountPoint, target);
+       if (zipNode == null) {
+           return null;
+       }
+       JarEntry jarEntry = zipNode.entry;
+       return jarEntry.getCodeSigners();
+    }
 
-    private ZipEntry getNodeEntry(ZipNode zipNode)
+    private JarEntry getNodeEntry(ZipNode zipNode)
             throws IOException {
-        final ZipEntry entry = zipNode.entry;
+        final JarEntry entry = zipNode.entry;
         if (entry == null) {
             throw new IOException("Cannot call this operation on a directory");
         }
@@ -259,7 +272,7 @@
     public boolean isReadOnly() {
         return true;
     }
-
+    
     public void close() throws IOException {
         VFSUtils.safeClose(new Closeable() {
             public void close() throws IOException {
@@ -284,10 +297,10 @@
         // immutable child map
         private final Map<String, ZipNode> children;
         private final String name;
-        private final ZipEntry entry;
+        private final JarEntry entry;
         private volatile File cachedFile;
 
-        private ZipNode(Map<String, ZipNode> children, String name, ZipEntry entry) {
+        private ZipNode(Map<String, ZipNode> children, String name, JarEntry entry) {
             this.children = children;
             this.name = name;
             this.entry = entry;

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/RealFileSystem.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.FileInputStream;
+import java.security.CodeSigner;
 import java.util.List;
 import java.util.Arrays;
 import java.util.Collections;
@@ -120,8 +121,15 @@
         final String[] names = getFile(mountPoint, target).list();
         return names == null ? Collections.<String>emptyList() : Arrays.asList(names);
     }
+    
+   /**
+    * {@inheritDoc}
+    */
+   public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
+      return null;
+   }
 
-    /**
+   /**
      * {@inheritDoc}
      */
     public void close() throws IOException {

Modified: projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java	2009-10-09 22:48:09 UTC (rev 94631)
+++ projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -39,6 +39,7 @@
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 
@@ -1088,19 +1089,8 @@
          try
          {
             InputStream is = jar.openStream();
-            FileOutputStream fos = new FileOutputStream(tmpJar);
-            byte[] buffer = new byte[1024];
-            int read;
-            while ((read = is.read(buffer)) > 0)
-            {
-               fos.write(buffer, 0, read);
-            }
-            fos.close();
-            log.debug("outer.jar/jar1.jar size is: " + jar.getSize());
-            log.debug(tmpJar.getAbsolutePath() + " size is: " + tmpJar.length());
-            assertTrue("outer.jar > 0", jar.getSize() > 0);
-            assertEquals("copy jar size", jar.getSize(), tmpJar.length());
-            is.close();
+            
+            assertTrue(is instanceof JarInputStream);
          }
          finally
          {

Added: projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/VirtualJarInputStreamTest.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/VirtualJarInputStreamTest.java	                        (rev 0)
+++ projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/VirtualJarInputStreamTest.java	2009-10-09 22:52:44 UTC (rev 94632)
@@ -0,0 +1,284 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.test.vfs;
+
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.jar.Attributes;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+import java.util.jar.Manifest;
+
+import org.jboss.vfs.TempFileProvider;
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.VirtualFile;
+import org.junit.Before;
+
+import org.junit.Test;
+
+/**
+ * Test to ensure the functionality of {@link VirtualJarInputStream} 
+ *
+ * @author <a href="baileyje at gmail.com">John Bailey</a>
+ */
+public class VirtualJarInputStreamTest extends AbstractVFSTest {
+
+   private TempFileProvider provider;
+
+   public VirtualJarInputStreamTest(String name) {
+      super(name);
+   }
+
+   @Before
+   public void setUp() throws Exception {
+      super.setUp();
+      provider = TempFileProvider.create("test", new ScheduledThreadPoolExecutor(2));
+   }
+
+   @Test
+   public void testIteration() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1.jar");
+      Closeable mount = VFS.mountZip(jar, jar, provider);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         JarEntry next = null;
+
+         List<String> entryNames = new LinkedList<String>();
+         while ((next = jarInput.getNextJarEntry()) != null) {
+            entryNames.add(next.getName());
+         }
+         assertTrue(entryNames.contains("META-INF/MANIFEST.MF"));
+         assertTrue(entryNames.contains("org/jboss/test/vfs/support/jar1/ClassInJar1$InnerClass.class"));
+         assertTrue(entryNames.contains("org/jboss/test/vfs/support/jar1/ClassInJar1.class"));
+      }
+      finally {
+         mount.close();
+      }
+   }
+
+   @Test
+   public void testIterationNonJar() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1");
+      Closeable mount = VFS.mountReal(jar.getPhysicalFile(), jar);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         JarEntry next = null;
+
+         List<String> entryNames = new LinkedList<String>();
+         while ((next = jarInput.getNextJarEntry()) != null) {
+            entryNames.add(next.getName());
+         }
+         assertTrue(entryNames.contains("META-INF/MANIFEST.MF"));
+         assertTrue(entryNames.contains("org/jboss/test/vfs/support/jar1/ClassInJar1$InnerClass.class"));
+         assertTrue(entryNames.contains("org/jboss/test/vfs/support/jar1/ClassInJar1.class"));
+      }
+      finally {
+         mount.close();
+      }
+   }
+
+   @Test
+   public void testRead() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1.jar");
+      Closeable mount = VFS.mountZip(jar, jar, provider);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         JarEntry next = jarInput.getNextJarEntry();
+         assertEquals("META-INF/MANIFEST.MF", next.getName());
+
+         ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         VFSUtils.copyStreamAndClose(jarInput, baos);
+         byte[] actualBytes = baos.toByteArray();
+         
+         baos.reset();
+
+         VFSUtils.copyStreamAndClose(jar.getChild("META-INF/MANIFEST.MF").openStream(), baos);
+         byte[] expectedBytes = baos.toByteArray();
+
+         assertTrue(Arrays.equals(expectedBytes, actualBytes));
+      }
+      finally {
+         mount.close();
+      }
+   }
+   
+   @Test
+   public void testReadNonJar() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1");
+      Closeable mount = VFS.mountReal(jar.getPhysicalFile(), jar);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         JarEntry next = jarInput.getNextJarEntry();
+         assertEquals("META-INF/MANIFEST.MF", next.getName());
+
+         ByteArrayOutputStream baos = new ByteArrayOutputStream();
+         VFSUtils.copyStreamAndClose(jarInput, baos);
+         byte[] actualBytes = baos.toByteArray();
+         
+         baos.reset();
+
+         VFSUtils.copyStreamAndClose(jar.getChild("META-INF/MANIFEST.MF").openStream(), baos);
+         byte[] expectedBytes = baos.toByteArray();
+
+         assertTrue(Arrays.equals(expectedBytes, actualBytes));
+      }
+      finally {
+         mount.close();
+      }
+   }
+
+   @Test
+   public void testReadClosed() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1.jar");
+      Closeable mount = VFS.mountZip(jar, jar, provider);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         JarEntry next = jarInput.getNextJarEntry();
+         assertEquals("META-INF/MANIFEST.MF", next.getName());
+
+         jarInput.close();
+         try {
+            jarInput.read();
+            fail("Should have thrown IOException");
+         }
+         catch (IOException expected) {
+         }
+      }
+      finally {
+         mount.close();
+      }
+   }
+
+   @Test
+   public void testGetManifest() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1.jar");
+      Closeable mount = VFS.mountZip(jar, jar, provider);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         Manifest actual = jarInput.getManifest();
+         Manifest expected = VFSUtils.getManifest(jar);
+         assertEquals(expected, actual);
+      }
+      finally {
+         mount.close();
+      }
+   }
+   
+   @Test
+   public void testGetManifestNonJar() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1");
+      Closeable mount = VFS.mountReal(jar.getPhysicalFile(), jar);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         Manifest actual = jarInput.getManifest();
+         Manifest expected = VFSUtils.getManifest(jar);
+         assertEquals(expected, actual);
+      }
+      finally {
+         mount.close();
+      }
+   }
+   
+   @Test
+   public void testGetAttributes() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+
+      VirtualFile jar = testdir.getChild("jar1.jar");
+      Closeable mount = VFS.mountZip(jar, jar, provider);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         Manifest manifest = jarInput.getManifest();
+         
+         JarEntry next = null;
+         while ((next = jarInput.getNextJarEntry()) != null) {
+            Attributes expected = manifest.getAttributes(next.getName());
+            Attributes actual = next.getAttributes();
+            assertEquals(expected, actual);
+         }
+      }
+      finally {
+         mount.close();
+      }
+   }
+   
+   @Test
+   public void testGetAttributesNonJar() throws Exception {
+      URL rootURL = getResource("/vfs/test");
+      VFS vfs = VFS.getInstance();
+      VirtualFile testdir = vfs.getChild(rootURL.getPath());
+      
+      VirtualFile jar = testdir.getChild("jar1");
+      Closeable mount = VFS.mountReal(jar.getPhysicalFile(), jar);
+      try {
+         JarInputStream jarInput = (JarInputStream) jar.openStream();
+         Manifest manifest = jarInput.getManifest();
+         
+         JarEntry next = null;
+         while ((next = jarInput.getNextJarEntry()) != null) {
+            Attributes expected = manifest.getAttributes(next.getName());
+            Attributes actual = next.getAttributes();
+            assertEquals(expected, actual);
+         }
+      }
+      finally {
+         mount.close();
+      }
+   }
+
+}




More information about the jboss-cvs-commits mailing list