[jboss-cvs] JBossAS SVN: r68574 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/plugins/context/jar and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Dec 27 07:38:15 EST 2007
Author: alesj
Date: 2007-12-27 07:38:15 -0500 (Thu, 27 Dec 2007)
New Revision: 68574
Added:
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/HierarchyVirtualFileHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/ZipEntryWrapper.java
Modified:
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/AbstractStructuredJarHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java
projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java
Log:
Making JarEntryContents hierarchy aware.
Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/HierarchyVirtualFileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/HierarchyVirtualFileHandler.java (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/HierarchyVirtualFileHandler.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -0,0 +1,40 @@
+/*
+* 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;
+
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+/**
+ * A handler that has children.
+ *
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ */
+public interface HierarchyVirtualFileHandler
+{
+ /**
+ * Add a child.
+ *
+ * @param child the child
+ * @throws IllegalArgumentException for a null child
+ */
+ void addChild(VirtualFileHandler child);
+}
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/AbstractStructuredJarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/AbstractStructuredJarHandler.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/AbstractStructuredJarHandler.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -31,10 +31,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.jar.JarFile;
import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
+import org.jboss.virtual.plugins.context.HierarchyVirtualFileHandler;
import org.jboss.virtual.plugins.vfs.helpers.PathTokenizer;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
@@ -42,9 +44,10 @@
/**
* AbstractStructuredJarHandler.
*
+ * @param <T> exact extra wrapper type
* @author <a href="ales.justin at jboss.com">Ales Justin</a>
*/
-public class AbstractStructuredJarHandler extends AbstractJarHandler implements StructuredVirtualFileHandler
+public abstract class AbstractStructuredJarHandler<T> extends AbstractJarHandler implements StructuredVirtualFileHandler
{
/**
* serialVersionUID
@@ -69,7 +72,7 @@
* @throws java.io.IOException for an error accessing the file system
* @throws IllegalArgumentException for a null context, url or vfsPath
*/
- protected AbstractStructuredJarHandler(VFSContext context, VirtualFileHandler parent, URL url, JarFile jar, JarEntry entry, String name) throws IOException
+ protected AbstractStructuredJarHandler(VFSContext context, VirtualFileHandler parent, URL url, JarFile jar, ZipEntry entry, String name) throws IOException
{
super(context, parent, url, jar, entry, name);
}
@@ -86,8 +89,18 @@
if (this.jar != null)
throw new IllegalStateException("jarFile has already been set");
*/
+ Enumeration<ZipEntryWrapper<T>> enumeration = new JarEntryEnumeration(getJar().entries());
+ initJarFile(enumeration);
+ }
- Enumeration<JarEntry> enumeration = getJar().entries();
+ /**
+ * Initialise the jar file.
+ *
+ * @param enumeration jar entry enumeration
+ * @throws IOException for any error
+ */
+ protected void initJarFile(Enumeration<ZipEntryWrapper<T>> enumeration) throws IOException
+ {
if (enumeration.hasMoreElements() == false)
{
entries = Collections.emptyList();
@@ -97,54 +110,55 @@
// Go through and create a structured representation of the jar
Map<String, VirtualFileHandler> parentMap = new HashMap<String, VirtualFileHandler>();
- ArrayList<ArrayList<JarEntry>> levelMapList = new ArrayList<ArrayList<JarEntry>>();
+ List<ArrayList<ZipEntryWrapper<T>>> levelMapList = new ArrayList<ArrayList<ZipEntryWrapper<T>>>();
entries = new ArrayList<VirtualFileHandler>();
entryMap = new HashMap<String, VirtualFileHandler>();
boolean trace = log.isTraceEnabled();
while (enumeration.hasMoreElements())
{
- JarEntry entry = enumeration.nextElement();
- String[] paths = entry.getName().split("/");
+ ZipEntryWrapper<T> wrapper = enumeration.nextElement();
+ extraWrapperInfo(wrapper);
+ String[] paths = wrapper.getName().split("/");
int depth = paths.length;
if (depth >= levelMapList.size())
{
for (int n = levelMapList.size(); n <= depth; n++)
- levelMapList.add(new ArrayList<JarEntry>());
+ levelMapList.add(new ArrayList<ZipEntryWrapper<T>>());
}
- ArrayList<JarEntry> levelMap = levelMapList.get(depth);
- levelMap.add(entry);
+ ArrayList<ZipEntryWrapper<T>> levelMap = levelMapList.get(depth);
+ levelMap.add(wrapper);
if (trace)
- log.trace("added " + entry.getName() + " at depth " + depth);
+ log.trace("added " + wrapper.getName() + " at depth " + depth);
}
// Process each level to build the handlers in parent first order
int level = 0;
- for (ArrayList<JarEntry> levels : levelMapList)
+ for (ArrayList<ZipEntryWrapper<T>> levels : levelMapList)
{
if (trace)
log.trace("Level(" + level++ + "): " + levels);
- for (JarEntry entry : levels)
+ for (ZipEntryWrapper<T> wrapper : levels)
{
- String name = entry.getName();
- int slash = entry.isDirectory() ? name.lastIndexOf('/', name.length() - 2) :
+ String name = wrapper.getName();
+ int slash = wrapper.isDirectory() ? name.lastIndexOf('/', name.length() - 2) :
name.lastIndexOf('/', name.length() - 1);
VirtualFileHandler parent = this;
if (slash >= 0)
{
- // Need to include the slash in the name to match the JarEntry.name
+ // Need to include the slash in the name to match the ZipEntry.name
String parentName = name.substring(0, slash + 1);
parent = parentMap.get(parentName);
if (parent == null)
{
// Build up the parent(s)
- parent = buildParents(parentName, parentMap, entry);
+ parent = buildParents(parentName, parentMap, wrapper);
}
}
// Get the entry name without any directory '/' ending
int start = slash + 1;
- int end = entry.isDirectory() ? name.length() - 1 : name.length();
+ int end = wrapper.isDirectory() ? name.length() - 1 : name.length();
String entryName = name.substring(start, end);
- VirtualFileHandler handler = createVirtualFileHandler(parent, entry, entryName);
- if (entry.isDirectory())
+ VirtualFileHandler handler = createVirtualFileHandler(parent, wrapper, entryName);
+ if (wrapper.isDirectory())
{
parentMap.put(name, handler);
if (trace)
@@ -156,32 +170,35 @@
entries.add(handler);
entryMap.put(entryName, handler);
}
- else if (parent instanceof JarEntryHandler)
+ else if (parent instanceof HierarchyVirtualFileHandler)
{
- // This is a child of the jar entry handler
- JarEntryHandler ehandler = (JarEntryHandler) parent;
+ HierarchyVirtualFileHandler ehandler = (HierarchyVirtualFileHandler) parent;
ehandler.addChild(handler);
}
- else if (parent instanceof SynthenticDirEntryHandler)
- {
- // This is a child of the jar entry handler
- SynthenticDirEntryHandler ehandler = (SynthenticDirEntryHandler) parent;
- ehandler.addChild(handler);
- }
}
}
}
/**
+ * Handle additional information about wrapper.
+ *
+ * @param wrapper the zip entry wrapper
+ * @throws IOException for any error
+ */
+ protected void extraWrapperInfo(ZipEntryWrapper<T> wrapper) throws IOException
+ {
+ }
+
+ /**
* Create any missing parents.
*
* @param parentName full vfs path name of parent
* @param parentMap initJarFile parentMap
- * @param entry JarEntry missing a parent
+ * @param wrapper ZipEntryWrapper missing a parent
* @return the VirtualFileHandler for the parent
* @throws java.io.IOException for any IO error
*/
- protected VirtualFileHandler buildParents(String parentName, Map<String, VirtualFileHandler> parentMap, JarEntry entry)
+ protected VirtualFileHandler buildParents(String parentName, Map<String, VirtualFileHandler> parentMap, ZipEntryWrapper<T> wrapper)
throws IOException
{
VirtualFileHandler parent = this;
@@ -200,25 +217,18 @@
{
// Create a synthetic parent
URL url = getURL(parent, path, true);
- next = new SynthenticDirEntryHandler(getVFSContext(), parent, path, entry.getTime(), url);
+ next = new SynthenticDirEntryHandler(getVFSContext(), parent, path, wrapper.getTime(), url);
if (parent == this)
{
// This is an immeadiate child of the jar handler
entries.add(next);
entryMap.put(path, next);
}
- else if (parent instanceof JarEntryHandler)
+ else if (parent instanceof HierarchyVirtualFileHandler)
{
- // This is a child of the jar entry handler
- JarEntryHandler ehandler = (JarEntryHandler) parent;
+ HierarchyVirtualFileHandler ehandler = (HierarchyVirtualFileHandler) parent;
ehandler.addChild(next);
}
- else if (parent instanceof SynthenticDirEntryHandler)
- {
- // This is a child of the jar entry handler
- SynthenticDirEntryHandler ehandler = (SynthenticDirEntryHandler) parent;
- ehandler.addChild(next);
- }
}
parentMap.put(pathName.toString(), next);
parent = next;
@@ -252,20 +262,21 @@
* Create a new virtual file handler
*
* @param parent the parent
- * @param entry the entry
+ * @param wrapper the entry wrapper
* @param entryName - the entry name without any trailing '/'
* @return the handler
* @throws java.io.IOException for any error accessing the file system
* @throws IllegalArgumentException for a null parent or entry
*/
- protected VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, JarEntry entry, String entryName)
+ protected VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, ZipEntryWrapper<T> wrapper, String entryName)
throws IOException
{
if (parent == null)
throw new IllegalArgumentException("Null parent");
- if (entry == null)
- throw new IllegalArgumentException("Null entry");
+ if (wrapper == null)
+ throw new IllegalArgumentException("Null entry wrapper");
+ ZipEntry entry = wrapper.getEntry();
URL url = getURL(parent, entryName, entry.isDirectory());
VFSContext context = parent.getVFSContext();
@@ -300,4 +311,27 @@
// Initial the parent jar entries
initJarFile();
}
+
+ private class JarEntryEnumeration implements Enumeration<ZipEntryWrapper<T>>
+ {
+ private Enumeration<JarEntry> enumeration;
+
+ public JarEntryEnumeration(Enumeration<JarEntry> enumeration)
+ {
+ if (enumeration == null)
+ throw new IllegalArgumentException("Null enumeration");
+ this.enumeration = enumeration;
+ }
+
+ public boolean hasMoreElements()
+ {
+ return enumeration.hasMoreElements();
+ }
+
+ public ZipEntryWrapper<T> nextElement()
+ {
+ JarEntry entry = enumeration.nextElement();
+ return new ZipEntryWrapper<T>(entry);
+ }
+ }
}
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryContents.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -22,18 +22,22 @@
package org.jboss.virtual.plugins.context.jar;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
+import org.jboss.virtual.plugins.context.HierarchyVirtualFileHandler;
+import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
@@ -43,7 +47,7 @@
* @author Ales.Justin at jboss.org
* @author Scott.Stark at jboss.org
*/
-public class JarEntryContents extends AbstractJarHandler
+public class JarEntryContents extends AbstractJarHandler implements StructuredVirtualFileHandler, HierarchyVirtualFileHandler
{
/**
* serialVersionUID
@@ -56,14 +60,17 @@
private NestedJarFromStream njar;
private InputStream openStream;
- JarEntryContents(VFSContext context, VirtualFileHandler parent, ZipEntry entry, URL jarURL, URL entryURL, InputStream zis)
+ private List<VirtualFileHandler> entryChildren;
+ private transient Map<String, VirtualFileHandler> entryMap;
+
+ JarEntryContents(VFSContext context, VirtualFileHandler parent, ZipEntry entry, String entryName, URL jarURL, URL entryURL, byte[] contents)
throws IOException
{
- super(context, parent, jarURL, null, entry, entry.getName());
+ super(context, parent, jarURL, null, entry, entryName);
try
{
- setPathName(getChildPathName(getName(), false));
- setVfsUrl(getChildVfsUrl(getName(), false));
+ setPathName(getChildPathName(entryName, false));
+ setVfsUrl(getChildVfsUrl(entryName, false));
}
catch (Exception e)
{
@@ -71,24 +78,28 @@
}
this.entryURL = entryURL;
this.isJar = JarUtils.isArchive(getName());
- int size = (int) entry.getSize();
- if (size > 0)
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
- byte[] tmp = new byte[1024];
- while (zis.available() > 0)
- {
- int length = zis.read(tmp);
- if (length > 0)
- baos.write(tmp, 0, length);
- }
- contents = baos.toByteArray();
- }
- else
- contents = new byte[0];
+ this.contents = contents;
}
+ protected void initCacheLastModified()
+ {
+ }
+
/**
+ * Add a child to an entry
+ *
+ * @param child the child
+ */
+ public synchronized void addChild(VirtualFileHandler child)
+ {
+ if (entryChildren == null)
+ entryChildren = new ArrayList<VirtualFileHandler>();
+ entryChildren.add(child);
+ if (entryMap != null)
+ entryMap.put(child.getName(), child);
+ }
+
+ /**
* TODO: removing the entry/jar that resulted in this needs
* to be detected.
*/
@@ -109,12 +120,18 @@
public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
{
- List<VirtualFileHandler> children = null;
+ List<VirtualFileHandler> children;
+
if (isJar)
{
initNestedJar();
children = njar.getChildren(ignoreErrors);
}
+ else
+ {
+ children = entryChildren;
+ }
+
if (children == null)
return Collections.emptyList();
else
@@ -136,12 +153,27 @@
}
else if (getEntry().isDirectory())
{
- checkParentExists();
- return getParent().findChild(getEntry().getName() + path);
+ return structuredFindChild(path);
}
throw new FileNotFoundException("JarEntryContents(" + getName() + ") has no children");
}
+ public VirtualFileHandler createChildHandler(String name) throws IOException
+ {
+ if (entryChildren == null)
+ throw new FileNotFoundException(this + " has no children");
+ if (entryMap == null)
+ {
+ entryMap = new HashMap<String, VirtualFileHandler>();
+ for (VirtualFileHandler child : entryChildren)
+ entryMap.put(child.getName(), child);
+ }
+ VirtualFileHandler child = entryMap.get(name);
+ if (child == null)
+ throw new FileNotFoundException(this + " has no child: " + name);
+ return child;
+ }
+
// Convience attribute accessors
public long getLastModified()
{
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarEntryHandler.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -31,10 +31,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.jar.JarEntry;
import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
+import org.jboss.virtual.plugins.context.HierarchyVirtualFileHandler;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
@@ -46,7 +47,7 @@
* @author Scott.Stark at jboss.org
* @version $Revision: 1.1 $
*/
-public class JarEntryHandler extends AbstractJarHandler implements StructuredVirtualFileHandler
+public class JarEntryHandler extends AbstractJarHandler implements StructuredVirtualFileHandler, HierarchyVirtualFileHandler
{
/**
* serialVersionUID
@@ -68,7 +69,7 @@
* @throws IOException for an error accessing the file system
* @throws IllegalArgumentException for a null context, url, jar or entry
*/
- public JarEntryHandler(VFSContext context, VirtualFileHandler parent, JarFile jar, JarEntry entry, String entryName, URL url)
+ public JarEntryHandler(VFSContext context, VirtualFileHandler parent, JarFile jar, ZipEntry entry, String entryName, URL url)
throws IOException
{
super(context, parent, url, jar, entry, entryName);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/JarHandler.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -23,7 +23,6 @@
import java.io.File;
import java.io.IOException;
-import java.io.ObjectInputStream;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.jar.JarFile;
@@ -37,7 +36,7 @@
* @author <a href="adrian at jboss.com">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
-public class JarHandler extends AbstractStructuredJarHandler
+public class JarHandler extends AbstractStructuredJarHandler<Object>
{
/** serialVersionUID */
private static final long serialVersionUID = 1L;
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarFromStream.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -5,16 +5,13 @@
import java.io.IOException;
import java.io.InputStream;
-import java.io.ObjectOutputStream;
+import java.io.ByteArrayOutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
+import java.util.Enumeration;
import java.util.List;
-import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
@@ -30,15 +27,14 @@
* @author Scott.Stark at jboss.org
* @version $Revision: 44334 $
*/
-public class NestedJarFromStream extends AbstractJarHandler
+public class NestedJarFromStream extends AbstractStructuredJarHandler<byte[]>
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
- private ZipInputStream zis;
- private Map<String, JarEntryContents> entries = new HashMap<String, JarEntryContents>();
+ private transient ZipInputStream zis;
private URL jarURL;
private URL entryURL;
private long lastModified;
@@ -87,65 +83,70 @@
*/
protected void init() throws IOException
{
- if (inited.get())
- return;
-
- inited.set(true);
- try
+ if (inited.get() == false)
{
- ZipEntry entry = zis.getNextEntry();
- while (entry != null)
- {
- try
- {
- String entryName = entry.getName();
- String url = toURI().toASCIIString() + "!/" + entryName;
- URL jecURL = new URL(url);
- JarEntryContents jec = new JarEntryContents(getVFSContext(), this, entry, toURL(), jecURL, zis);
- int end = entry.isDirectory() ? entryName.length() - 1 : entryName.length();
- entries.put(entryName.substring(0, end), jec);
- entry = zis.getNextEntry();
- }
- catch (Throwable t)
- {
- IOException ioe = new IOException("Exception while reading nested jar entry: " + this);
- ioe.initCause(t);
- ioe.setStackTrace(t.getStackTrace());
- throw ioe;
- }
- }
- }
- finally
- {
+ inited.set(true);
try
{
- zis.close();
+ initJarFile(new ZisEnumeration());
}
- catch (IOException ignored)
+ finally
{
+ close();
}
- zis = null;
}
}
+ public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
+ {
+ init();
+ return super.getChildren(ignoreErrors);
+ }
+
public VirtualFileHandler findChild(String path) throws IOException
{
- if (path == null)
- throw new IllegalArgumentException("Null path");
+ init();
+ return super.findChild(path);
+ }
- if ("".equals(path))
- return this;
-
- JarEntryContents handler = getEntry(path);
- if (handler == null)
- throw new IOException("No such child: " + path);
- return handler;
+ protected void extraWrapperInfo(ZipEntryWrapper<byte[]> wrapper) throws IOException
+ {
+ byte[] contents;
+ int size = (int)wrapper.getSize();
+ if (size > 0)
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
+ byte[] tmp = new byte[1024];
+ while (zis.available() > 0)
+ {
+ int length = zis.read(tmp);
+ if (length > 0)
+ baos.write(tmp, 0, length);
+ }
+ contents = baos.toByteArray();
+ }
+ else
+ contents = new byte[0];
+ wrapper.setExtra(contents);
}
- public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
+ protected VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, ZipEntryWrapper<byte[]> wrapper, String entryName) throws IOException
{
- init();
- return new ArrayList<VirtualFileHandler>(entries.values());
+ try
+ {
+ String url = toURI().toASCIIString() + "!/" + wrapper.getName();
+ URL jecURL = new URL(url);
+ VFSContext context = parent.getVFSContext();
+ byte[] contents = wrapper.getExtra();
+ return new JarEntryContents(context, parent, wrapper.getEntry(), entryName, toURL(), jecURL, contents);
+ }
+ catch (Throwable t)
+ {
+ IOException ioe = new IOException("Exception while reading nested jar entry: " + this);
+ ioe.initCause(t);
+ ioe.setStackTrace(t.getStackTrace());
+ throw ioe;
+ }
}
/**
@@ -172,30 +173,6 @@
return lastModified;
}
- public Iterator<JarEntryContents> getEntries() throws IOException
- {
- init();
- return entries.values().iterator();
- }
-
- public JarEntryContents getEntry(String name) throws IOException
- {
- init();
- return entries.get(name);
- }
-
- public ZipEntry getZipEntry(String name) throws IOException
- {
- JarEntryContents jec = getEntry(name);
- return (jec != null ? jec.getEntry() : null);
- }
-
- public byte[] getContents(String name) throws IOException
- {
- JarEntryContents jec = getEntry(name);
- return (jec != null ? jec.getContents() : null);
- }
-
// Stream accessor
public InputStream openStream() throws IOException
{
@@ -204,7 +181,6 @@
public void close()
{
- entries.clear();
if (zis != null)
{
try
@@ -255,16 +231,40 @@
return tmp.toString();
}
- /**
- * First, if not already, initialize entries.
- * Then do simple write.
- *
- * @param out object output stream
- * @throws IOException for any error
- */
- private void writeObject(ObjectOutputStream out) throws IOException
+ protected void initJarFile() throws IOException
{
- init();
- out.defaultWriteObject();
+ // todo - deserialize
}
+
+ private class ZisEnumeration implements Enumeration<ZipEntryWrapper<byte[]>>
+ {
+ private boolean moved = true;
+ private ZipEntry next = null;
+
+ public boolean hasMoreElements()
+ {
+ if (zis == null)
+ return false;
+
+ try
+ {
+ if (moved)
+ {
+ next = zis.getNextEntry();
+ moved = false;
+ }
+ return next != null;
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public ZipEntryWrapper<byte[]> nextElement()
+ {
+ moved = true;
+ return new ZipEntryWrapper<byte[]>(next);
+ }
+ }
}
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -29,8 +29,8 @@
import java.io.ObjectInputStream;
import java.net.URISyntaxException;
import java.net.URL;
-import java.util.jar.JarEntry;
import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
@@ -43,7 +43,7 @@
* @author Scott.Stark at jboss.org
* @version $Revision: 1.1 $
*/
-public class NestedJarHandler extends AbstractStructuredJarHandler
+public class NestedJarHandler extends AbstractStructuredJarHandler<Object>
{
/** serialVersionUID */
private static final long serialVersionUID = 1L;
@@ -63,7 +63,7 @@
* @return the jar file
* @throws IOException for any error
*/
- private static JarFile createTempJar(File temp, JarFile parentJar, JarEntry entry) throws IOException
+ private static JarFile createTempJar(File temp, JarFile parentJar, ZipEntry entry) throws IOException
{
InputStream inputStream = parentJar.getInputStream(entry);
try
@@ -99,7 +99,7 @@
}
public static NestedJarHandler create(VFSContext context, VirtualFileHandler parent,
- JarFile parentJar, JarEntry entry, URL url, String entryName) throws IOException
+ JarFile parentJar, ZipEntry entry, URL url, String entryName) throws IOException
{
File temp;
try
@@ -130,7 +130,7 @@
* @throws IOException for an error accessing the file system
* @throws IllegalArgumentException for a null context, url or vfsPath
*/
- protected NestedJarHandler(VFSContext context, VirtualFileHandler parent, JarFile parentJar, JarEntry entry, URL original, File temp, String entryName)
+ protected NestedJarHandler(VFSContext context, VirtualFileHandler parent, JarFile parentJar, ZipEntry entry, URL original, File temp, String entryName)
throws IOException
{
super(context, parent, temp.toURL(), createTempJar(temp, parentJar, entry), entry, entryName);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/NoCopyNestedJarHandler.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -26,8 +26,8 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
-import java.util.jar.JarEntry;
import java.util.jar.JarFile;
+import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.jboss.virtual.spi.VFSContext;
@@ -59,7 +59,7 @@
* @throws IOException for an error accessing the file system
* @throws IllegalArgumentException for a null context, url or vfsPath
*/
- public NoCopyNestedJarHandler(VFSContext context, VirtualFileHandler parent, JarFile parentJar, JarEntry entry, URL url, String entryName) throws IOException
+ public NoCopyNestedJarHandler(VFSContext context, VirtualFileHandler parent, JarFile parentJar, ZipEntry entry, URL url, String entryName) throws IOException
{
super(context, parent, url, parentJar, entry, entryName);
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/SynthenticDirEntryHandler.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -34,6 +34,7 @@
import org.jboss.virtual.plugins.context.AbstractURLHandler;
import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
+import org.jboss.virtual.plugins.context.HierarchyVirtualFileHandler;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VirtualFileHandler;
@@ -44,7 +45,7 @@
* @version $Revision: 1.1 $
*/
public class SynthenticDirEntryHandler extends AbstractURLHandler
- implements StructuredVirtualFileHandler
+ implements StructuredVirtualFileHandler, HierarchyVirtualFileHandler
{
/**
* serialVersionUID
@@ -55,7 +56,7 @@
* The jar file
*/
private long lastModified;
- private transient List<VirtualFileHandler> entryChildren;
+ private List<VirtualFileHandler> entryChildren;
private transient Map<String, VirtualFileHandler> entryMap;
/**
Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/ZipEntryWrapper.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/ZipEntryWrapper.java (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/jar/ZipEntryWrapper.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -0,0 +1,78 @@
+/*
+* 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.jar;
+
+import java.util.zip.ZipEntry;
+
+/**
+ * Zip entry wrapper.
+ *
+ * @param <T> exact extra type
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+class ZipEntryWrapper<T>
+{
+ private ZipEntry entry;
+ private T extra;
+
+ ZipEntryWrapper(ZipEntry entry)
+ {
+ if (entry == null)
+ throw new IllegalArgumentException("Null zip entry");
+ this.entry = entry;
+ }
+
+ public ZipEntry getEntry()
+ {
+ return entry;
+ }
+
+ public String getName()
+ {
+ return entry.getName();
+ }
+
+ public boolean isDirectory()
+ {
+ return entry.isDirectory();
+ }
+
+ public long getTime()
+ {
+ return entry.getTime();
+ }
+
+ public long getSize()
+ {
+ return entry.getSize();
+ }
+
+ public T getExtra()
+ {
+ return extra;
+ }
+
+ public void setExtra(T extra)
+ {
+ this.extra = extra;
+ }
+}
Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java 2007-12-26 21:05:31 UTC (rev 68573)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/FileVFSUnitTestCase.java 2007-12-27 12:38:15 UTC (rev 68574)
@@ -51,13 +51,13 @@
import org.jboss.virtual.VFSUtils;
import org.jboss.virtual.VirtualFile;
import org.jboss.virtual.VisitorAttributes;
-import org.jboss.virtual.plugins.context.jar.JarEntryContents;
import org.jboss.virtual.plugins.context.jar.NestedJarFromStream;
import org.jboss.virtual.plugins.vfs.helpers.SuffixMatchFilter;
import org.jboss.virtual.spi.LinkInfo;
import org.jboss.virtual.spi.VFSContext;
import org.jboss.virtual.spi.VFSContextFactory;
import org.jboss.virtual.spi.VFSContextFactoryLocator;
+import org.jboss.virtual.spi.VirtualFileHandler;
/**
* Tests of the VFS implementation
@@ -130,10 +130,10 @@
ZipInputStream jis1 = new ZipInputStream(jf.getInputStream(jar1));
NestedJarFromStream njfs = new NestedJarFromStream(context, parent1.getRoot(), jis1, jar1URL, jf, jar1, "jar1.jar");
- JarEntryContents e1 = njfs.getEntry("org/jboss/test/vfs/support/jar1/ClassInJar1.class");
+ VirtualFileHandler e1 = njfs.findChild("org/jboss/test/vfs/support/jar1/ClassInJar1.class");
assertNotNull(e1);
log.info("org/jboss/test/vfs/support/CommonClass.class: "+e1);
- JarEntryContents mfe1 = njfs.getEntry("META-INF/MANIFEST.MF");
+ VirtualFileHandler mfe1 = njfs.findChild("META-INF/MANIFEST.MF");
assertNotNull("jar1!/META-INF/MANIFEST.MF", mfe1);
InputStream mfIS = mfe1.openStream();
Manifest mf = new Manifest(mfIS);
@@ -150,10 +150,10 @@
ZipInputStream jis2 = new ZipInputStream(jf.getInputStream(jar2));
NestedJarFromStream njfs2 = new NestedJarFromStream(context, parent2.getRoot(), jis2, jar2URL, jf, jar2, "jar2.jar");
- JarEntryContents e2 = njfs2.getEntry("org/jboss/test/vfs/support/jar2/ClassInJar2.class");
+ VirtualFileHandler e2 = njfs2.findChild("org/jboss/test/vfs/support/jar2/ClassInJar2.class");
assertNotNull(e2);
log.info("org/jboss/test/vfs/support/CommonClass.class: "+e2);
- JarEntryContents mfe2 = njfs2.getEntry("META-INF/MANIFEST.MF");
+ VirtualFileHandler mfe2 = njfs2.findChild("META-INF/MANIFEST.MF");
assertNotNull("jar2!/META-INF/MANIFEST.MF", mfe2);
InputStream mf2IS = mfe2.openStream();
Manifest mf2 = new Manifest(mf2IS);
More information about the jboss-cvs-commits
mailing list