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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Oct 1 19:41:02 EDT 2009


Author: johnbailey
Date: 2009-10-01 19:41:02 -0400 (Thu, 01 Oct 2009)
New Revision: 94230

Added:
   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/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java
   projects/vfs/branches/dml-zip-rework/src/test/resources/vfs/test/test-web.xml
Modified:
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFS.java
   projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java
Log:
Added inital VirtualFile Assembly impl.

Modified: projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFS.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFS.java	2009-10-01 23:35:43 UTC (rev 94229)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VFS.java	2009-10-01 23:41:02 UTC (rev 94230)
@@ -47,6 +47,8 @@
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.spi.AssemblyFileSystem;
 import org.jboss.vfs.spi.FileSystem;
 import org.jboss.vfs.spi.RealFileSystem;
 import org.jboss.vfs.spi.JavaZipFileSystem;
@@ -605,6 +607,20 @@
     public static Closeable mountZipExpanded(VirtualFile zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         return mountZipExpanded(zipFile.openStream(), zipFile.getName(), mountPoint, tempFileProvider);
     }
+    
+    /**
+     * Create and mount an assembly file system, returning a single handle which will unmount and
+     * close the filesystem when closed. 
+     * 
+     * @param assembly an {@link VirtualFileAssembly} to mount in the VFS
+     * @param mountPoint the point at which the filesystem should be mounted
+     * @return a handle
+     * 
+     * @throws IOException if an error occurs
+     */
+    public static Closeable mountAssembly(VirtualFileAssembly assembly, VirtualFile mountPoint) throws IOException {
+       return doMount(new AssemblyFileSystem(assembly), mountPoint);
+    }
 
     /**
      * Expand a zip file to a destination directory.  The directory must exist.  If an error occurs, the destination

Added: 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	                        (rev 0)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/VirtualFileAssembly.java	2009-10-01 23:41:02 UTC (rev 94230)
@@ -0,0 +1,288 @@
+/*
+ * 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.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.security.SecureRandom;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Random;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Executors;
+
+import org.jboss.vfs.util.PathTokenizer;
+
+/**
+ * Assembly of VirtualFiles that can be mounted into the VFS in a structure
+ * that is not required to match a real filesystem structure. 
+ *  
+ * @author <a href="baileyje at gmail.com">John Bailey</a>
+ */
+public class VirtualFileAssembly implements Closeable {
+
+   private static final Random RANDOM_NUM_GEN = new SecureRandom();
+
+   private final AssemblyNode rootNode = new AssemblyNode();
+
+   private final List<Closeable> mountHandles = new CopyOnWriteArrayList<Closeable>();
+
+   private final VirtualFile mountRoot = VFS.getInstance().getChild("assembly-mounts").getChild(getAssemblyId());
+
+   private TempFileProvider tempFileProvider;
+
+   /** 
+    * Add a {@link VirtualFile} to the assembly.
+    *  
+    * @param virtualFile
+    */
+   public void add(VirtualFile virtualFile) {
+      String path = virtualFile.getName();
+      AssemblyNode assemblyNode = rootNode.findOrBuild(new Path(path));
+      assemblyNode.setTarget(virtualFile);
+   }
+
+   /** 
+    * Add a {@link VirtualFile} to the assembly in a given path.
+    *  
+    * @param path
+    * @param virtualFile
+    */
+   public void add(String path, VirtualFile virtualFile) {
+      AssemblyNode assemblyNode = rootNode.findOrBuild(new Path(path));
+      assemblyNode.setTarget(virtualFile);
+   }
+
+   public void add(final String path, final File root) throws IOException {
+      VirtualFile mountPoint = mountRoot.getChild(path);
+      Closeable handle = VFS.mountReal(root, mountPoint);
+      mountHandles.add(handle);
+      add(path, mountPoint);
+   }
+
+   public void addZip(final String path, final File zipFile) throws IOException {
+      VirtualFile mountPoint = mountRoot.getChild(path);
+      Closeable handle = VFS.mountZip(zipFile, mountPoint, getTempFileProvider());
+      mountHandles.add(handle);
+      add(path, mountPoint);
+   }
+
+   /**
+    * Get the VirtualFile from the assembly.  This will traverse VirtualFiles in assembly 
+    * to find children if needed. 
+    * 
+    * @param mountPoint
+    * @param target
+    * @return
+    * @throws IOException 
+    */
+   public VirtualFile getFile(VirtualFile mountPoint, VirtualFile target) {
+      Path path = getRelativePath(mountPoint, target);
+      return rootNode.getFile(path, mountPoint);
+   }
+
+   /**
+    * Close the assembly and nested resources.
+    */
+   public void close() {
+      VFSUtils.safeClose(mountHandles);
+   }
+
+   /**
+    * 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 {
+      if (tempFileProvider == null) {
+         tempFileProvider = TempFileProvider.create("temp", Executors.newSingleThreadScheduledExecutor());
+      }
+      return tempFileProvider;
+   }
+
+   private String getAssemblyId() {
+      return Long.toHexString(RANDOM_NUM_GEN.nextLong());
+   }
+
+   /**
+    * Path representation to hold onto the elements of the path.
+    */
+   private static class Path {
+      private final Queue<String> parts;
+
+      private Path(String path) {
+         parts = new LinkedList<String>();
+         List<String> tokens = PathTokenizer.getTokens(path);
+         parts.addAll(tokens);
+      }
+
+      private Path(List<String> parts) {
+         this.parts = new LinkedList<String>(parts);
+      }
+
+      private boolean isEndOfPath() {
+         return parts.isEmpty();
+      }
+
+      private String getCurrent() {
+         return parts.poll();
+      }
+   }
+
+   /**
+    * Node located within the assembly. 
+    */
+   private static class AssemblyNode {
+      private final Map<String, AssemblyNode> children = new ConcurrentHashMap<String, AssemblyNode>();
+
+      private VirtualFile target;
+
+      public AssemblyNode() {
+      }
+
+      /**
+       * Find an AssemblyNode staring with this node and return null if not found.
+       * 
+       * @param path
+       * @return
+       */
+      public AssemblyNode find(Path path) {
+         return find(path, false);
+      }
+
+      /**
+       * Find an AssemblyNode starting with this node and build the required nodes if not found.
+       * 
+       * @param path
+       * @return
+       */
+      public AssemblyNode findOrBuild(Path path) {
+         return find(path, true);
+      }
+
+      /**
+       * Find an AssemblyNode starting with this node.
+       * 
+       * @param path
+       * @param createIfMissing
+       * @return
+       */
+      public AssemblyNode find(Path path, boolean createIfMissing) {
+         if (path.isEndOfPath()) {
+            return this;
+         }
+         String current = path.getCurrent();
+         AssemblyNode childNode = getChild(current);
+         if (childNode == null) {
+            if (!createIfMissing) {
+               return null;
+            }
+            childNode = new AssemblyNode();
+            addChild(current, childNode);
+         }
+         return childNode.find(path, createIfMissing);
+      }
+
+      /**
+       * Get the VirtualFile for a given path.  Will traverse VirtualFile links if not 
+       * found in the assembly. 
+       * 
+       * @param path
+       * @return
+       * @throws IOException 
+       */
+      public VirtualFile getFile(Path path, VirtualFile assemblyMountPoint) {
+
+         if (path.isEndOfPath()) {
+            return target;
+         }
+         String current = path.getCurrent();
+         AssemblyNode childNode = getChild(current);
+         if (childNode != null) {
+            return childNode.getFile(path, assemblyMountPoint);
+         }
+         if (target != null) {
+            VirtualFile currentFile = target != null ? target.getChild(current) : null;
+            if (currentFile != null) {
+               while (!path.isEndOfPath()) {
+                  current = path.getCurrent();
+                  currentFile = currentFile.getChild(current);
+                  if (currentFile == null) {
+                     return null;
+                  }
+               }
+               return currentFile;
+            }
+         }
+         return null;
+
+      }
+
+      private void addChild(String name, AssemblyNode child) {
+         children.put(name.toLowerCase(), child);
+      }
+
+      private AssemblyNode getChild(String name) {
+         return children.get(name.toLowerCase());
+      }
+
+      private void setTarget(VirtualFile target) {
+         this.target = target;
+      }
+   }
+}

Added: 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	                        (rev 0)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java	2009-10-01 23:41:02 UTC (rev 94230)
@@ -0,0 +1,217 @@
+/*
+ * 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.spi;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.VirtualFileAssembly;
+
+/**
+ * FileSystem used to mount an Assembly into the VFS.
+ *  
+ * @author <a href="baileyje at gmail.com">John Bailey</a>
+ */
+public class AssemblyFileSystem implements FileSystem {
+
+   private final VirtualFileAssembly assembly;
+
+   public AssemblyFileSystem(VirtualFileAssembly assembly) {
+      this.assembly = assembly;
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#getFile(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public File getFile(VirtualFile mountPoint, VirtualFile target) throws IOException {
+      return getVirtualFileAndRun(mountPoint, target, new VirtualFileTask<File>() {
+         public File with(VirtualFile file) throws IOException {
+            return file.getPhysicalFile();
+         }
+
+         public File without() {
+            return null;
+         }
+      });
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#delete(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public boolean delete(VirtualFile mountPoint, VirtualFile target) {
+      VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
+      if (assemblyFile == null) {
+         return false;
+      }
+      return assemblyFile.delete();
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#exists(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public boolean exists(VirtualFile mountPoint, VirtualFile target) {
+      VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
+      if (assemblyFile == null) {
+         return false;
+      }
+      return assemblyFile.exists();
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#getDirectoryEntries(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public List<String> getDirectoryEntries(VirtualFile mountPoint, VirtualFile target) {
+      VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
+      if (assemblyFile == null) {
+         return Collections.<String> emptyList();
+      }
+      List<String> directoryEntries = new LinkedList<String>();
+      for (VirtualFile child : assemblyFile.getChildren()) {
+         directoryEntries.add(child.getName());
+      }
+      return directoryEntries;
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#getLastModified(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public long getLastModified(VirtualFile mountPoint, VirtualFile target) throws IOException {
+      return getVirtualFileAndRun(mountPoint, target, new VirtualFileTask<Long>() {
+         public Long with(VirtualFile file) throws IOException {
+            return file.getLastModified();
+         }
+
+         public Long without() {
+            return -1L;
+         }
+      });
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#getSize(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public long getSize(VirtualFile mountPoint, VirtualFile target) throws IOException {
+      return getVirtualFileAndRun(mountPoint, target, new VirtualFileTask<Long>() {
+         public Long with(VirtualFile file) throws IOException {
+            return file.getSize();
+         }
+
+         public Long without() {
+            return 0L;
+         }
+      });
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#isDirectory(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public boolean isDirectory(VirtualFile mountPoint, VirtualFile target) {
+      VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
+      if (assemblyFile == null) {
+         return false;
+      }
+      return assemblyFile.isDirectory();
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#isReadOnly()
+    */
+   public boolean isReadOnly() {
+      return false;
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#openInputStream(org.jboss.vfs.VirtualFile, org.jboss.vfs.VirtualFile)
+    */
+   public InputStream openInputStream(VirtualFile mountPoint, VirtualFile target) throws IOException {
+      return getVirtualFileAndRun(mountPoint, target, new VirtualFileTask<InputStream>() {
+         public InputStream with(VirtualFile file) throws IOException {
+            return file.openStream();
+         }
+
+         public InputStream without() {
+            return null;
+         }
+      });
+   }
+
+   /*
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#close()
+    */
+   public void close() throws IOException {
+      assembly.close();
+   }
+
+   /**
+    * Get the file for the mount/target combination and run the FileTask if the File is found otherwise return the 
+    * result of FileStask.getNullReturn.
+    * 
+    * @param <T>
+    * @param mountPoint
+    * @param target
+    * @param task
+    * @return
+    */
+   private <T> T getVirtualFileAndRun(VirtualFile mountPoint, VirtualFile target, VirtualFileTask<T> task)
+         throws IOException {
+      VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
+      if (assemblyFile != null) {
+         return task.with(assemblyFile);
+      }
+      return task.without();
+   }
+
+   /**
+    * Task that can be run with a File.
+    */
+   private static interface VirtualFileTask<T> {
+      /** 
+       * Method executed it the File is found.
+       * 
+       * @param file
+       * @return
+       */
+      T with(VirtualFile file) throws IOException;
+
+      /**
+       * Method executed if the File is not found.
+       * @return
+       */
+      T without();
+   }
+
+}

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-01 23:35:43 UTC (rev 94229)
+++ projects/vfs/branches/dml-zip-rework/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java	2009-10-01 23:41:02 UTC (rev 94230)
@@ -22,30 +22,30 @@
 
 package org.jboss.vfs.spi;
 
-import org.jboss.vfs.util.PathTokenizer;
-import org.jboss.vfs.VFSUtils;
-import org.jboss.vfs.VirtualFile;
-import org.jboss.vfs.TempDir;
-
+import java.io.BufferedOutputStream;
+import java.io.Closeable;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileInputStream;
-import java.io.Closeable;
-import java.io.BufferedOutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
-import java.util.Iterator;
 import java.util.Map;
-import java.util.HashMap;
-import java.util.Enumeration;
-import java.util.Collection;
-import java.util.ArrayList;
-import java.util.Collections;
+import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
-import java.util.zip.ZipEntry;
 
+import org.jboss.vfs.TempDir;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.util.PathTokenizer;
+
 /**
  * {@inheritDoc}
  * <p/>
@@ -142,14 +142,15 @@
             // nope, create a cached temp
             final ZipEntry zipEntry = getNodeEntry(zipNode);
             final String name = zipEntry.getName();
-            cachedFile = new File(contentsDir, name);
+            cachedFile = buildFile(contentsDir, name);
+            cachedFile.getParentFile().mkdirs(); 
             VFSUtils.copyStreamAndClose(zipFile.getInputStream(zipEntry), new BufferedOutputStream(new FileOutputStream(cachedFile)));
             zipNode.cachedFile = cachedFile;
             return cachedFile;
         }
     }
 
-    public InputStream openInputStream(VirtualFile mountPoint, VirtualFile target) throws IOException {
+   public InputStream openInputStream(VirtualFile mountPoint, VirtualFile target) throws IOException {
         final ZipNode zipNode = getExistingZipNode(mountPoint, target);
         final File cachedFile = zipNode.cachedFile;
         if (cachedFile != null) {
@@ -261,6 +262,16 @@
         });
         tempDir.close();
     }
+    
+    private File buildFile(File contentsDir, String name) {
+       List<String> tokens = PathTokenizer.getTokens(name);
+       File currentFile = contentsDir;
+       for(String token : tokens) {
+          currentFile = new File(currentFile, token);
+       }
+       currentFile.getParentFile().mkdirs();
+       return currentFile;
+    }
 
     private static final class ZipNode {
 
@@ -275,7 +286,7 @@
             this.name = name;
             this.entry = entry;
         }
-
+        
         private ZipNode find(VirtualFile mountPoint, VirtualFile target) {
             if (mountPoint.equals(target)) {
                 return this;

Added: projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java
===================================================================
--- projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java	                        (rev 0)
+++ projects/vfs/branches/dml-zip-rework/src/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java	2009-10-01 23:41:02 UTC (rev 94230)
@@ -0,0 +1,122 @@
+package org.jboss.test.vfs;
+
+import java.io.Closeable;
+import java.io.File;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.VirtualFileAssembly;
+import org.junit.Test;
+
+public class AssemblyFileSystemTest extends AbstractVFSTest {
+
+   public AssemblyFileSystemTest(String name) {
+      super(name);
+   }
+
+   @Test
+   public void testBuildAssembly() throws Exception {
+
+      VirtualFileAssembly earAssembly = new VirtualFileAssembly();
+      VirtualFile earAssemblyLocation = VFS.getInstance().getChild("assembly.ear");
+      Closeable earAssemblyHandle = VFS.mountAssembly(earAssembly, earAssemblyLocation);
+
+      VirtualFileAssembly warAssembly = new VirtualFileAssembly();
+      VirtualFile warAssemblyLocation = earAssemblyLocation.getChild("assembly.war");
+      Closeable warAssemblyHandle = VFS.mountAssembly(warAssembly, warAssemblyLocation);
+
+      try {
+         URL rootURL = getResource("/vfs/test");
+         VirtualFile testDir = VFS.getInstance().getChild(rootURL.getPath());
+
+         earAssembly.add("assembly.war", warAssemblyLocation);
+
+         URL nestedURL = getResource("/vfs/test/nested");
+         warAssembly.add("WEB-INF/lib", new File(nestedURL.toURI()));
+
+         URL jar1URL = getResource("/vfs/test/jar1.jar");
+         warAssembly.addZip("WEB-INF/lib/jar1.jar", new File(jar1URL.toURI()));
+
+         warAssembly.add("WEB-INF/lib/jar1.jar/META-INF/Manifest.mf", testDir.getChild("jar1-filesonly.mf"));
+         warAssembly.add("WEB-INF/web.xml", testDir.getChild("web.xml"));
+
+         assertMapped(testDir.getChild("nested/nested.jar"), VFS.getInstance().getChild(
+               "assembly.ear/assembly.war/WEB-INF/lib/nested.jar"));
+         assertMapped(testDir.getChild("nested/nested_copy.jar"), VFS.getInstance().getChild(
+               "assembly.ear/assembly.war/WEB-INF/lib/nested_copy.jar"));
+         assertMapped(testDir.getChild("jar1-filesonly.mf"), VFS.getInstance().getChild("assembly.ear").getChild(
+               "assembly.war").getChild("WEB-INF").getChild("lib").getChild("jar1.jar").getChild("META-INF").getChild(
+               "Manifest.mf"));
+         assertTrue(VFS.getInstance().getChild(
+               "assembly.ear/assembly.war/WEB-INF/lib/jar1.jar/org/jboss/test/vfs/support/jar1/ClassInJar1.class")
+               .exists());
+      }
+      finally {
+         VFSUtils.safeClose(Arrays.asList(earAssemblyHandle, warAssemblyHandle));
+      }
+   }
+
+   @Test
+   public void testGetNonExistentFile() throws Exception {
+
+      VirtualFile assemblyLocation = VFS.getInstance().getChild("/assembly");
+      VirtualFileAssembly assembly = new VirtualFileAssembly();
+      Closeable assemblyHandle = VFS.mountAssembly(assembly, assemblyLocation);
+      try {
+         VirtualFile virtualFile = assemblyLocation.getChild("missingFile.txt");
+         assertFalse(virtualFile.exists());
+      }
+      finally {
+         VFSUtils.safeClose(assemblyHandle);
+      }
+   }
+
+   @Test
+   public void testDelete() throws Exception {
+
+      VirtualFile assemblyLocation = VFS.getInstance().getChild("/assembly");
+      VirtualFileAssembly assembly = new VirtualFileAssembly();
+      Closeable assemblyHandle = VFS.mountAssembly(assembly, assemblyLocation);
+      try {
+         VirtualFile virtualFile = assemblyLocation.getChild("missingFile.txt");
+         assertFalse(virtualFile.exists());
+      }
+      finally {
+         VFSUtils.safeClose(assemblyHandle);
+      }
+   }
+
+   @Test
+   public void testGetChildren() throws Exception {
+      VirtualFileAssembly assembly = new VirtualFileAssembly();
+      VirtualFile assemblyLocation = VFS.getInstance().getChild("/assembly");
+      Closeable assemblyHandle = VFS.mountAssembly(assembly, assemblyLocation);
+      try {
+         URL jar1URL = getResource("/vfs/test/jar1.jar");
+         assembly.addZip("jar1.jar", new File(jar1URL.toURI()));
+
+         VirtualFile virtualFile = assemblyLocation.getChild("jar1.jar");
+         assertTrue(virtualFile.exists());
+         List<VirtualFile> directoryEntries = virtualFile.getChildren();
+
+         assertFalse(directoryEntries.isEmpty());
+         for (VirtualFile child : directoryEntries) {
+            assertTrue(child.getPathName().startsWith("/assembly/jar1.jar"));
+         }
+
+      }
+      finally {
+         VFSUtils.safeClose(assemblyHandle);
+      }
+   }
+
+   @Test
+   public void assertMapped(VirtualFile expected, VirtualFile actual) throws Exception {
+      assertNotNull(actual);
+      assertEquals(expected.getPhysicalFile(), actual.getPhysicalFile());
+   }
+}

Added: projects/vfs/branches/dml-zip-rework/src/test/resources/vfs/test/test-web.xml
===================================================================




More information about the jboss-cvs-commits mailing list