[jboss-cvs] JBossAS SVN: r94656 - in projects/vfs/branches/dml-zip-rework/src: test/java/org/jboss/test/vfs and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Sun Oct 11 23:46:31 EDT 2009
Author: johnbailey
Date: 2009-10-11 23:46:31 -0400 (Sun, 11 Oct 2009)
New Revision: 94656
Modified:
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
Log:
Updated VirtualJarInputStream to properly handle the META-INF dir and the MANIFEST.MF
Modified: 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 2009-10-11 23:27:47 UTC (rev 94655)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualJarInputStream.java 2009-10-12 03:46:31 UTC (rev 94656)
@@ -27,11 +27,13 @@
import java.security.cert.Certificate;
import java.util.ArrayDeque;
import java.util.ArrayList;
+import java.util.Collections;
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.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
@@ -44,6 +46,10 @@
* @author <a href="baileyje at gmail.com">John Bailey</a>
*/
public class VirtualJarInputStream extends JarInputStream {
+ private static final String MANIFEST_NAME = "MANIFEST.MF";
+
+ private static final String META_INF_DIR = "META-INF";
+
private final Deque<Iterator<VirtualFile>> entryItr = new ArrayDeque<Iterator<VirtualFile>>();
private final VirtualFile root;
@@ -61,27 +67,23 @@
public VirtualJarInputStream(VirtualFile root) throws IOException {
super(VFSUtils.emptyStream());
this.root = root;
+ VirtualFile manifest = root.getChild(JarFile.MANIFEST_NAME);
+ if(manifest.exists()) {
+ entryItr.add(Collections.singleton(manifest).iterator());
+ }
entryItr.add(root.getChildren().iterator());
}
- /*
- * {@inheritDoc}
- * @see java.util.jar.JarInputStream#getNextEntry()
- */
+ /** {@inheritDoc} **/
@Override
public ZipEntry getNextEntry() throws IOException {
return getNextJarEntry();
}
- /*
- * {@inheritDoc}
- * @see java.util.jar.JarInputStream#getNextJarEntry()
- */
+ /** {@inheritDoc} **/
@Override
public JarEntry getNextJarEntry() throws IOException {
- if (currentEntryStream != null) {
- currentEntryStream.close();
- }
+ closeEntry();
Iterator<VirtualFile> topItr = entryItr.peekFirst();
if (topItr == null) {
@@ -91,26 +93,33 @@
entryItr.pop();
return getNextJarEntry();
}
- VirtualFile current = topItr.next();
- if (current.isDirectory()) {
- entryItr.add(current.getChildren().iterator());
- return getNextJarEntry();
+
+ VirtualFile nextEntry = topItr.next();
+ String entryName = getEntryName(nextEntry);
+ if (nextEntry.isDirectory()) {
+ List<VirtualFile> children = nextEntry.getChildren();
+ if(entryName.equalsIgnoreCase(META_INF_DIR)) {
+ children = nextEntry.getChildren(new VirtualFileFilter() {
+ public boolean accepts(VirtualFile file) {
+ return !MANIFEST_NAME.equalsIgnoreCase(file.getName());
+ }
+ });
+ }
+ entryItr.add(children.iterator());
+ entryName = fixDirectoryName(entryName);
}
-
- openCurrent(current);
- String entryName = getEntryName(current);
+ openCurrent(nextEntry);
+
Attributes attributes = null;
Manifest manifest = getManifest();
if (manifest != null) {
attributes = manifest.getAttributes(entryName);
}
- return new VirtualJarEntry(entryName, current, attributes);
+ return new VirtualJarEntry(entryName, nextEntry, attributes);
}
+
- /*
- * {@inheritDoc}
- * @see java.util.jar.JarInputStream#getManifest()
- */
+ /** {@inheritDoc} **/
@Override
public Manifest getManifest() {
try {
@@ -121,58 +130,40 @@
}
}
- /*
- * {@inheritDoc}
- * @see java.util.zip.InflaterInputStream#read()
- */
+ /** {@inheritDoc} **/
@Override
public int read() throws IOException {
ensureOpen();
return checkForEoSAndReturn(currentEntryStream.read());
}
- /*
- * {@inheritDoc}
- * @see java.io.FilterInputStream#read(byte[])
- */
+ /** {@inheritDoc} **/
@Override
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}
- /*
- * {@inheritDoc}
- * @see java.util.jar.JarInputStream#read(byte[], int, int)
- */
+ /** {@inheritDoc} **/
@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()
- */
+
+ /** {@inheritDoc} **/
@Override
public int available() throws IOException {
ensureOpen();
return currentEntryStream.available() > 0 ? 1 : 0;
}
- /*
- * {@inheritDoc}
- * @see java.util.zip.ZipInputStream#close()
- */
+ /** {@inheritDoc} **/
@Override
public void close() throws IOException {
closed = true;
}
- /*
- * {@inheritDoc}
- * @see java.util.zip.ZipInputStream#closeEntry()
- */
+ /** {@inheritDoc} **/
@Override
public void closeEntry() throws IOException {
if (currentEntryStream != null) {
@@ -180,20 +171,14 @@
}
}
- /*
- * {@inheritDoc}
- * @see java.util.zip.ZipInputStream#skip(long)
- */
+ /** {@inheritDoc} **/
@Override
public long skip(long n) throws IOException {
ensureOpen();
return currentEntryStream.skip(n);
}
- /**
- * Ensure there is currently a JarEntry stream open.
- * @throws IOException
- */
+ /** {@inheritDoc} **/
private void ensureOpen() throws IOException {
if (closed) {
throw new IOException("Stream is closed");
@@ -221,7 +206,11 @@
* @throws IOException
*/
private void openCurrent(VirtualFile current) throws IOException {
- currentEntryStream = current.openStream();
+ if(current.isDirectory()) {
+ currentEntryStream = VFSUtils.emptyStream();
+ } else {
+ currentEntryStream = current.openStream();
+ }
}
/**
@@ -233,6 +222,18 @@
List<String> pathParts = VFSUtils.getRelativePath(root, entry);
return PathTokenizer.getRemainingPath(pathParts, 0);
}
+
+ /**
+ * Make sure directory names end with a trailing slash
+ * @param name
+ * @return
+ */
+ private String fixDirectoryName(String name) {
+ if(!name.endsWith("/")) {
+ return name + "/";
+ }
+ return name;
+ }
/**
* Virtual JarEntry used for representing a child VirtualFile as a JarEntry.
@@ -256,20 +257,26 @@
this.attributes = attributes;
}
- /*
- * {@inheritDoc}
- * @see java.util.jar.JarEntry#getAttributes()
- */
+ /** {@inheritDoc} **/
@Override
public Attributes getAttributes() throws IOException {
return attributes;
}
- /*
- * @inheritDoc}
- * @see java.util.jar.JarEntry#getCertificates()
- */
+ /** {@inheritDoc} **/
@Override
+ public long getSize() {
+ return virtualFile.getSize();
+ }
+
+ /** {@inheritDoc} **/
+ @Override
+ public boolean isDirectory() {
+ return virtualFile.isDirectory();
+ }
+
+ /** {@inheritDoc} **/
+ @Override
public Certificate[] getCertificates() {
CodeSigner[] signers = getCodeSigners();
if (signers == null) {
@@ -282,10 +289,7 @@
return certs.toArray(new Certificate[certs.size()]);
}
- /*
- * @inheritDoc}
- * @see java.util.jar.JarEntry#getCodeSigners()
- */
+ /** {@inheritDoc} **/
@Override
public CodeSigner[] getCodeSigners() {
return virtualFile.getCodeSigners();
Modified: 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 2009-10-11 23:27:47 UTC (rev 94655)
+++ projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/VirtualJarInputStreamTest.java 2009-10-12 03:46:31 UTC (rev 94656)
@@ -23,14 +23,17 @@
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
+import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
+import java.util.Enumeration;
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.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
@@ -38,8 +41,8 @@
import org.jboss.vfs.VFS;
import org.jboss.vfs.VFSUtils;
import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.VirtualJarInputStream;
import org.junit.Before;
-
import org.junit.Test;
/**
@@ -63,7 +66,7 @@
@Test
public void testIteration() throws Exception {
- URL rootURL = getResource("/vfs/test");
+ URL rootURL = getResource("/vfs/test/");
VFS vfs = VFS.getInstance();
VirtualFile testdir = vfs.getChild(rootURL.getPath());
@@ -77,9 +80,12 @@
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"));
+ JarFile jarFile = new JarFile(new File(new URL(rootURL, "jar1.jar").toURI()));
+ Enumeration<JarEntry> entries = jarFile.entries();
+ while(entries.hasMoreElements()) {
+ String entryName = entries.nextElement().getName();
+ assertTrue("JarEntry for " + entryName + " should be found in VirtualJarInputStream", entryNames.contains(entryName));
+ }
}
finally {
mount.close();
More information about the jboss-cvs-commits
mailing list