[jboss-svn-commits] JBoss Common SVN: r4192 - in shrinkwrap/trunk: extension-vfs3 and 15 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sat Mar 27 12:56:17 EDT 2010


Author: ALRubinger
Date: 2010-03-27 12:56:17 -0400 (Sat, 27 Mar 2010)
New Revision: 4192

Added:
   shrinkwrap/trunk/extension-vfs3/
   shrinkwrap/trunk/extension-vfs3/pom.xml
   shrinkwrap/trunk/extension-vfs3/src/
   shrinkwrap/trunk/extension-vfs3/src/main/
   shrinkwrap/trunk/extension-vfs3/src/main/java/
   shrinkwrap/trunk/extension-vfs3/src/main/java/org/
   shrinkwrap/trunk/extension-vfs3/src/main/java/org/jboss/
   shrinkwrap/trunk/extension-vfs3/src/main/java/org/jboss/shrinkwrap/
   shrinkwrap/trunk/extension-vfs3/src/main/java/org/jboss/shrinkwrap/vfs3/
   shrinkwrap/trunk/extension-vfs3/src/main/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystem.java
   shrinkwrap/trunk/extension-vfs3/src/main/resources/
   shrinkwrap/trunk/extension-vfs3/src/test/
   shrinkwrap/trunk/extension-vfs3/src/test/java/
   shrinkwrap/trunk/extension-vfs3/src/test/java/org/
   shrinkwrap/trunk/extension-vfs3/src/test/java/org/jboss/
   shrinkwrap/trunk/extension-vfs3/src/test/java/org/jboss/shrinkwrap/
   shrinkwrap/trunk/extension-vfs3/src/test/java/org/jboss/shrinkwrap/vfs3/
   shrinkwrap/trunk/extension-vfs3/src/test/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystemUnitTestCase.java
   shrinkwrap/trunk/extension-vfs3/src/test/resources/
   shrinkwrap/trunk/extension-vfs3/src/test/resources/META-INF/
   shrinkwrap/trunk/extension-vfs3/src/test/resources/META-INF/test
   shrinkwrap/trunk/extension-vfs3/src/test/resources/logger.properties
   shrinkwrap/trunk/extension-vfs3/src/test/resources/test
Modified:
   shrinkwrap/trunk/pom.xml
Log:
[SHRINKWRAP-146] Add a VFS3 FileSystem to read ShrinkWrap archives


Property changes on: shrinkwrap/trunk/extension-vfs3
___________________________________________________________________
Name: svn:ignore
   + target
.classpath
.settings
.project
bin


Added: shrinkwrap/trunk/extension-vfs3/pom.xml
===================================================================
--- shrinkwrap/trunk/extension-vfs3/pom.xml	                        (rev 0)
+++ shrinkwrap/trunk/extension-vfs3/pom.xml	2010-03-27 16:56:17 UTC (rev 4192)
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+  <!--
+  vi:ts=2:sw=2:expandtab:
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <!-- Parent -->
+  <parent>
+    <groupId>org.jboss.shrinkwrap</groupId>
+    <artifactId>shrinkwrap-build</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+    <relativePath>../build/pom.xml</relativePath>
+  </parent>
+
+  <!-- Model Version -->
+  <modelVersion>4.0.0</modelVersion>
+
+  <!-- Artifact Configuration -->
+  <artifactId>shrinkwrap-extension-vfs3</artifactId>
+  <name>ShrinkWrap Extension JBoss VFS3</name>
+  <description>ShrinkWrap Extension for the JBoss Virtual Filesystem 3</description>
+
+
+  <!-- Properties -->
+  <properties>
+  
+    <!-- Versioning -->
+    <version.org.jboss_jboss.vfs>3.0.0.CR5</version.org.jboss_jboss.vfs>
+    <version.jboss.logging>2.2.0.CR1</version.jboss.logging>
+    <version.org.jboss.logmanager>1.2.0.CR1</version.org.jboss.logmanager>
+
+  </properties>
+
+  <!-- Dependencies -->
+  <dependencies>
+
+    <!-- 
+    org.jboss.shrinkwrap    
+     -->
+    <dependency>
+      <groupId>org.jboss.shrinkwrap</groupId>
+      <artifactId>shrinkwrap-impl-base</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+
+    <!-- 
+    External Projects
+     -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    
+    <!-- org.jboss:jboss-vfs --> 
+    <dependency>
+      <groupId>org.jboss</groupId>
+      <artifactId>jboss-vfs</artifactId>
+      <version>${version.org.jboss_jboss.vfs}</version>
+    </dependency>
+    
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-spi</artifactId>
+      <version>${version.jboss.logging}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-jdk</artifactId>
+      <version>${version.jboss.logging}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.logmanager</groupId>
+      <artifactId>jboss-logmanager</artifactId>
+      <version>${version.org.jboss.logmanager}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-logmanager</artifactId>
+      <version>${version.jboss.logging}</version>
+      <scope>test</scope>
+    </dependency>
+    
+
+  </dependencies>
+
+  <!-- Build Configuration -->
+  <build>
+  
+    <plugins>
+
+    </plugins>
+    
+  </build>
+  
+</project>
+

Added: shrinkwrap/trunk/extension-vfs3/src/main/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystem.java
===================================================================
--- shrinkwrap/trunk/extension-vfs3/src/main/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystem.java	                        (rev 0)
+++ shrinkwrap/trunk/extension-vfs3/src/main/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystem.java	2010-03-27 16:56:17 UTC (rev 4192)
@@ -0,0 +1,316 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.vfs3;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.CodeSigner;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.Asset;
+import org.jboss.shrinkwrap.api.Node;
+import org.jboss.vfs.TempDir;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.spi.FileSystem;
+import org.jboss.vfs.util.PathTokenizer;
+
+/**
+ * VFS {@link FileSystem} which uses an {@link Archive} as a backing.
+ * 
+ * @author <a href="jbailey at redhat.com">John Bailey</a>
+ */
+public class ArchiveFileSystem implements FileSystem
+{
+   /**
+    * The Archive used for backing
+    */
+   private final Archive<?> archive;
+
+   /**
+    * A temp directory handle
+    */
+   private final TempDir tempDirectory;
+
+   /**
+    * The filesystem root file for cached assets
+    */
+   private final File fsRoot;
+
+   /**
+    * Creation time to be used for last modified checks for non-cached files  
+    */
+   private final long creationTime = System.currentTimeMillis();
+
+   /**
+    * Constructs an Archive filesystem
+    * 
+    * @param archive the archive used for backing
+    * @param tempDirectory the temp directory used for caching files
+    * @throws IOException if any problem occurs
+    */
+   public ArchiveFileSystem(Archive<?> archive, TempDir tempDirectory) throws IOException
+   {
+      this.archive = archive;
+      this.tempDirectory = tempDirectory;
+      this.fsRoot = tempDirectory.getRoot();
+   }
+
+   /** {@inheritDoc} */
+   public void close() throws IOException
+   {
+      VFSUtils.safeClose(tempDirectory);
+   }
+
+   /** {@inheritDoc} */
+   public boolean delete(VirtualFile mountPoint, VirtualFile target)
+   {
+      boolean deleted = true;
+      final Node node = getNode(mountPoint, target);
+      if (node != null)
+      {
+         final File cachedFile = getCachedFile(node);
+         if (cachedFile.exists())
+         {
+            deleted = cachedFile.delete();
+         }
+         deleted = deleted && archive.delete(node.getPath());
+      }
+      return deleted;
+   }
+
+   /** {@inheritDoc} */
+   public boolean exists(VirtualFile mountPoint, VirtualFile target)
+   {
+      return getNode(mountPoint, target) != null;
+   }
+
+   /** {@inheritDoc} */
+   public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target)
+   {
+      return null;
+   }
+
+   /** {@inheritDoc} */
+   public List<String> getDirectoryEntries(VirtualFile mountPoint, VirtualFile target)
+   {
+      List<String> entries = new ArrayList<String>();
+      final Node node = getNode(mountPoint, target);
+      if (node != null)
+      {
+         final Set<Node> children = node.getChildren();
+         for (Node childNode : children)
+         {
+            final String path = childNode.getPath().get();
+            final String entryName = path.substring(path.lastIndexOf("/") + 1);
+            entries.add(entryName);
+         }
+      }
+      return entries;
+   }
+
+   /** 
+    * {@inheritDoc} 
+    * <br />
+    * Note: This is called by VirtualFile:getPhysicalFile.  This should be avoided if possible to maintain the in-memory nature of the archive.    
+    * */
+   public File getFile(VirtualFile mountPoint, VirtualFile target) throws IOException
+   {
+      final Node node = getNode(mountPoint, target);
+      if (node == null)
+      {
+         return null;
+      }
+
+      File cachedFile = getCachedFile(node);
+
+      if (fsRoot.equals(cachedFile))
+      {
+         return fsRoot;
+      }
+
+      if (cachedFile.exists())
+      {
+         return cachedFile;
+      }
+      synchronized (node)
+      {
+         final Asset asset = node.getAsset();
+         cachedFile = buildFile(node.getPath().get());
+         if (asset == null)
+         {
+            cachedFile.mkdir();
+         }
+         else
+         {
+            VFSUtils.copyStreamAndClose(asset.openStream(), new BufferedOutputStream(new FileOutputStream(cachedFile)));
+         }
+         return cachedFile;
+      }
+   }
+
+   /** {@inheritDoc} */
+   public long getLastModified(VirtualFile mountPoint, VirtualFile target)
+   {
+      final Node node = getNode(mountPoint, target);
+      final File cachedFile = getCachedFile(node);
+      if (cachedFile.exists())
+      {
+         return cachedFile.lastModified();
+      }
+      return creationTime;
+   }
+
+   /** {@inheritDoc} */
+   public long getSize(VirtualFile mountPoint, VirtualFile target)
+   {
+      final Node node = getNode(mountPoint, target);
+      final File cachedFile = getCachedFile(node);
+      if (cachedFile.exists())
+      {
+         return cachedFile.length();
+      }
+      else if (node.getAsset() != null)
+      {
+         // This sucks, but is the only way to get at it.
+         try
+         {
+            return getFile(mountPoint, target).length();
+         }
+         catch (IOException e)
+         {
+            throw new RuntimeException("Failed to get File", e);
+         }
+      }
+      return 0L;
+   }
+
+   /** {@inheritDoc} */
+   public boolean isDirectory(VirtualFile mountPoint, VirtualFile target)
+   {
+      final Node node = getNode(mountPoint, target);
+      return node == null || node.getAsset() == null;
+   }
+
+   /** {@inheritDoc} */
+   public boolean isFile(VirtualFile mountPoint, VirtualFile target)
+   {
+      final Node node = getNode(mountPoint, target);
+      return node != null && node.getAsset() != null;
+   }
+
+   /** {@inheritDoc} */
+   public boolean isReadOnly()
+   {
+      return false;
+   }
+
+   /** {@inheritDoc} */
+   public InputStream openInputStream(VirtualFile mountPoint, VirtualFile target) throws IOException
+   {
+      InputStream stream = null;
+      final Node node = getNode(mountPoint, target);
+      if (node != null)
+      {
+         final File cachedFile = getCachedFile(node);
+         if (cachedFile.exists())
+         {
+            stream = new FileInputStream(cachedFile);
+         }
+         else if (node.getAsset() != null)
+         {
+            stream = node.getAsset().openStream();
+         }
+      }
+      return stream;
+   }
+
+   /**
+    * Get the cached file for a Node
+    *
+    * @param node The node to get the cached file for
+    * @return Returns the cached file location
+    */
+   private File getCachedFile(Node node)
+   {
+      return new File(fsRoot, node.getPath().get());
+   }
+
+   /**
+    * Get the node for the provided target
+    * 
+    * @param mountPoint the filesystem mount point
+    * @param target the target file 
+    * @return the node or null if the node is not found
+    */
+   private Node getNode(VirtualFile mountPoint, VirtualFile target)
+   {
+      String archivePath = getRelativePath(mountPoint, target);
+      if ("".equals(archivePath))
+         archivePath = "/";
+      return archive.get(archivePath);
+   }
+
+   /**
+    * Get the relative path between the mountpoint and the target
+    * 
+    * @param mountPoint the filesystem mount point
+    * @param target the target file 
+    * @return the relative path
+    */
+   private String getRelativePath(VirtualFile mountPoint, VirtualFile target)
+   {
+      if (mountPoint.equals(target))
+         return "";
+      return target.getPathNameRelativeTo(mountPoint);
+   }
+
+   /**
+    * Build the cache file and create any parent directories that do not exist.
+    *
+    * @param path The path to build the file for
+    * @return The built file
+    */
+   private File buildFile(String path)
+   {
+      final List<String> tokens = PathTokenizer.getTokens(path);
+      File currentFile = fsRoot;
+      for (String token : tokens)
+      {
+         currentFile = new File(currentFile, token);
+      }
+      currentFile.getParentFile().mkdirs();
+      return currentFile;
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see org.jboss.vfs.spi.FileSystem#getMountSource()
+    */
+   public File getMountSource()
+   {
+      return fsRoot;
+   }
+
+}
\ No newline at end of file

Added: shrinkwrap/trunk/extension-vfs3/src/test/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystemUnitTestCase.java
===================================================================
--- shrinkwrap/trunk/extension-vfs3/src/test/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystemUnitTestCase.java	                        (rev 0)
+++ shrinkwrap/trunk/extension-vfs3/src/test/java/org/jboss/shrinkwrap/vfs3/ArchiveFileSystemUnitTestCase.java	2010-03-27 16:56:17 UTC (rev 4192)
@@ -0,0 +1,137 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.shrinkwrap.vfs3;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.Executors;
+import java.util.logging.Logger;
+
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.Archives;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.jboss.vfs.TempDir;
+import org.jboss.vfs.TempFileProvider;
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VirtualFile;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Tests to ensure the {@link ArchiveFileSystem}
+ * is working as contracted
+ * 
+ * @author <a href="jbailey at redhat.com">John Bailey</a>
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class ArchiveFileSystemUnitTestCase
+{
+
+   //-------------------------------------------------------------------------------------||
+   // Class Members ----------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Logger
+    */
+   private static final Logger log = Logger.getLogger(ArchiveFileSystemUnitTestCase.class.getName());
+
+   /**
+    * Name of the test archive
+    */
+   private static final String NAME_ARCHIVE = "test.jar";
+
+   private static TempFileProvider tempFileProvider = null;
+
+   //-------------------------------------------------------------------------------------||
+   // Instance Members -------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Handles to close when test is done
+    */
+   private final List<Closeable> vfsHandles = new ArrayList<Closeable>();
+
+   //-------------------------------------------------------------------------------------||
+   // Lifecycle --------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Sets up the temporary file provider
+    */
+   @BeforeClass
+   public static void createTempFileProvider() throws IOException
+   {
+      tempFileProvider = TempFileProvider.create("shrinkwrap-", Executors.newSingleThreadScheduledExecutor());
+   }
+
+   /**
+    * Closes handles marked open
+    */
+   @After
+   public void closeHandles()
+   {
+      for (final Closeable handle : vfsHandles)
+      {
+         try
+         {
+            handle.close();
+         }
+         catch (final IOException ioe)
+         {
+            log.warning("Could not close " + handle + ": " + ioe);
+         }
+      }
+   }
+
+   //-------------------------------------------------------------------------------------||
+   // Tests ------------------------------------------------------------------------------||
+   //-------------------------------------------------------------------------------------||
+
+   /**
+    * Adds some files/directories to an {@link Archive}, then reads via the 
+    * {@link ArchiveFileSystem} 
+    */
+   @Test
+   public void testArchiveFileSystem() throws Exception
+   {
+      final JavaArchive archive = Archives.create(NAME_ARCHIVE, JavaArchive.class);
+
+      // Back VFS by a temp directory
+      final TempDir tempDir = tempFileProvider.createTempDir(archive.getName());
+      VirtualFile virtualFile = VFS.getChild(UUID.randomUUID().toString()).getChild(archive.getName());
+      vfsHandles.add(VFS.mount(virtualFile, new ArchiveFileSystem(archive, tempDir)));
+
+      // Add to the archive
+      archive.addResource("META-INF/test", "META-INF/test");
+      archive.addResource("test", "test");
+      log.info("Using archive: " + archive.toString(true));
+
+      // Perform assertions
+      Assert.assertTrue("Could not read file", virtualFile.getChild("test").isFile());
+      Assert.assertTrue("Could not read directory", virtualFile.getChild("META-INF").isDirectory());
+      Assert.assertTrue("Could not read file in a directory", virtualFile.getChild("META-INF").getChild("test")
+            .isFile());
+
+   }
+}

Added: shrinkwrap/trunk/extension-vfs3/src/test/resources/META-INF/test
===================================================================

Added: shrinkwrap/trunk/extension-vfs3/src/test/resources/logger.properties
===================================================================

Added: shrinkwrap/trunk/extension-vfs3/src/test/resources/test
===================================================================

Modified: shrinkwrap/trunk/pom.xml
===================================================================
--- shrinkwrap/trunk/pom.xml	2010-03-26 19:47:45 UTC (rev 4191)
+++ shrinkwrap/trunk/pom.xml	2010-03-27 16:56:17 UTC (rev 4192)
@@ -72,6 +72,7 @@
     <module>extension-glassfish</module>
     <module>extension-jetty</module>
     <module>extension-openejb</module>
+    <module>extension-vfs3</module>
   </modules>
 
 </project>



More information about the jboss-svn-commits mailing list