[jboss-cvs] JBossAS SVN: r102178 - in projects/vfs/trunk/src: main/java/org/jboss/vfs/spi and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Mar 9 19:16:07 EST 2010


Author: johnbailey
Date: 2010-03-09 19:16:07 -0500 (Tue, 09 Mar 2010)
New Revision: 102178

Added:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/BasicMountHandle.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/MountHandle.java
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/MountHandleTestCase.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/FileSystem.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AbstractVFSTest.java
Log:
[JBVFS-150] - Added MountHandle interface to allow exclusive access for a mount caller to get to the mount source

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/BasicMountHandle.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/BasicMountHandle.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/BasicMountHandle.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -0,0 +1,69 @@
+/*
+ * 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 org.jboss.vfs.spi.FileSystem;
+import org.jboss.vfs.spi.MountHandle;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * MountHandle implementation.  Provides the default behavior
+ * of delegating to the FileSystem to get the mount source as
+ * well as cleaning up resources.
+ *
+ * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
+ */
+class BasicMountHandle implements MountHandle {
+   private final FileSystem fileSystem;
+   private final Closeable mountHandle;
+   private final Closeable[] closeables;
+
+   /**
+    * Create new DefaultMountHandle with a FileSystem and an array of closeable.
+    *
+    * @param fileSystem to use to retrieve the mount source
+    * @param mountHandle the handle to close the actual mount
+    * @param additionalCloseables addition Closeable to execute on close 
+    */
+   public BasicMountHandle(final FileSystem fileSystem, Closeable mountHandle, Closeable... additionalCloseables) {
+      this.fileSystem = fileSystem;
+      this.mountHandle = mountHandle;
+      this.closeables = additionalCloseables;
+   }
+
+   /* {@inheritDoc} */
+   public File getMountSource() {
+      return fileSystem.getMountSource();
+   }
+
+   /* {@inheritDoc} */
+   public void close() throws IOException {
+      VFSUtils.safeClose(fileSystem);
+      VFSUtils.safeClose(mountHandle);
+      for(Closeable closeable : closeables) {
+         VFSUtils.safeClose(closeable);
+      }
+   }
+}

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java	2010-03-09 23:53:43 UTC (rev 102177)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -46,6 +46,7 @@
 
 import org.jboss.vfs.spi.AssemblyFileSystem;
 import org.jboss.vfs.spi.FileSystem;
+import org.jboss.vfs.spi.MountHandle;
 import org.jboss.vfs.spi.RealFileSystem;
 import org.jboss.vfs.spi.JavaZipFileSystem;
 import org.jboss.vfs.spi.RootFileSystem;
@@ -372,18 +373,12 @@
         return new HashSet<String>(mountMap.keySet());
     }
 
-    private static Closeable doMount(final FileSystem fileSystem, final VirtualFile mountPoint) throws IOException {
+    private static MountHandle doMount(final FileSystem fileSystem, final VirtualFile mountPoint, Closeable... additionalCloseables) throws IOException {
         boolean ok = false;
         try {
             final Closeable mountHandle = mount(mountPoint, fileSystem);
-            final Closeable closeable = new Closeable() {
-                public void close() throws IOException {
-                    VFSUtils.safeClose(mountHandle);
-                    VFSUtils.safeClose(fileSystem);
-                }
-            };
             ok = true;
-            return closeable;
+            return new BasicMountHandle(fileSystem, mountHandle, additionalCloseables);
         } finally {
             if (!ok) {
                 VFSUtils.safeClose(fileSystem);
@@ -403,13 +398,13 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountZip(File zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
+    public static MountHandle mountZip(File zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         boolean ok = false;
         final TempDir tempDir = tempFileProvider.createTempDir(zipFile.getName());
         try {
-            final Closeable closeable = doMount(new JavaZipFileSystem(zipFile, tempDir), mountPoint);
+            final MountHandle handle = doMount(new JavaZipFileSystem(zipFile, tempDir), mountPoint);
             ok = true;
-            return closeable;
+            return handle;
         } finally {
             if (!ok) {
                 VFSUtils.safeClose(tempDir);
@@ -430,14 +425,14 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountZip(InputStream zipData, String zipName, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
+    public static MountHandle mountZip(InputStream zipData, String zipName, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         boolean ok = false;
         try {
             final TempDir tempDir = tempFileProvider.createTempDir(zipName);
             try {
-                final Closeable closeable = doMount(new JavaZipFileSystem(zipName, zipData, tempDir), mountPoint);
+                final MountHandle handle = doMount(new JavaZipFileSystem(zipName, zipData, tempDir), mountPoint);
                 ok = true;
-                return closeable;
+                return handle;
             } finally {
                 if (!ok) {
                     VFSUtils.safeClose(tempDir);
@@ -460,7 +455,7 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountZip(VirtualFile zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
+    public static MountHandle mountZip(VirtualFile zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         return mountZip(zipFile.openStream(), zipFile.getName(), mountPoint, tempFileProvider);
     }
 
@@ -475,7 +470,7 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountReal(File realRoot, VirtualFile mountPoint) throws IOException {
+    public static MountHandle mountReal(File realRoot, VirtualFile mountPoint) throws IOException {
         return doMount(new RealFileSystem(realRoot), mountPoint);
     }
 
@@ -490,18 +485,13 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountTemp(VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
+    public static MountHandle mountTemp(VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         boolean ok = false;
         final TempDir tempDir = tempFileProvider.createTempDir("tmpfs");
         try {
-            final Closeable closeable = doMount(new RealFileSystem(tempDir.getRoot()), mountPoint);
+            final MountHandle handle = doMount(new RealFileSystem(tempDir.getRoot()), mountPoint, tempDir);
             ok = true;
-            return new Closeable() {
-                public void close() throws IOException {
-                    VFSUtils.safeClose(closeable);
-                    VFSUtils.safeClose(tempDir);
-                }
-            };
+            return handle;
         } finally {
             if (!ok) {
                 VFSUtils.safeClose(tempDir);
@@ -521,20 +511,15 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountZipExpanded(File zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
+    public static MountHandle mountZipExpanded(File zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         boolean ok = false;
         final TempDir tempDir = tempFileProvider.createTempDir(zipFile.getName());
         try {
             final File rootFile = tempDir.getRoot();
             VFSUtils.unzip(zipFile, rootFile);
-            final Closeable closeable = doMount(new RealFileSystem(rootFile), mountPoint);
+            final MountHandle handle = doMount(new RealFileSystem(rootFile), mountPoint, tempDir);
             ok = true;
-            return new Closeable() {
-                public void close() throws IOException {
-                    VFSUtils.safeClose(closeable);
-                    VFSUtils.safeClose(tempDir);
-                }
-            };
+            return handle;
         } finally {
             if (!ok) {
                 VFSUtils.safeClose(tempDir);
@@ -555,7 +540,7 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountZipExpanded(InputStream zipData, String zipName, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
+    public static MountHandle mountZipExpanded(InputStream zipData, String zipName, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         try {
             boolean ok = false;
             final TempDir tempDir = tempFileProvider.createTempDir(zipName);
@@ -574,14 +559,9 @@
                     }
                     final File rootFile = tempDir.getRoot();
                     VFSUtils.unzip(zipFile, rootFile);
-                    final Closeable closeable = doMount(new RealFileSystem(rootFile), mountPoint);
+                    final MountHandle handle = doMount(new RealFileSystem(rootFile), mountPoint, tempDir);
                     ok = true;
-                    return new Closeable() {
-                        public void close() throws IOException {
-                            VFSUtils.safeClose(closeable);
-                            VFSUtils.safeClose(tempDir);
-                        }
-                    };
+                    return handle;
                 } finally {
                     zipFile.delete();
                 }
@@ -607,7 +587,7 @@
      *
      * @throws IOException if an error occurs
      */
-    public static Closeable mountZipExpanded(VirtualFile zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
+    public static MountHandle mountZipExpanded(VirtualFile zipFile, VirtualFile mountPoint, TempFileProvider tempFileProvider) throws IOException {
         return mountZipExpanded(zipFile.openStream(), zipFile.getName(), mountPoint, tempFileProvider);
     }
     
@@ -621,7 +601,7 @@
      * 
      * @throws IOException if an error occurs
      */
-    public static Closeable mountAssembly(VirtualFileAssembly assembly, VirtualFile mountPoint) throws IOException {
+    public static MountHandle mountAssembly(VirtualFileAssembly assembly, VirtualFile mountPoint) throws IOException {
        return doMount(new AssemblyFileSystem(assembly), mountPoint);
     }
 

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java	2010-03-09 23:53:43 UTC (rev 102177)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/AssemblyFileSystem.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -140,6 +140,11 @@
        return assemblyFile.getCodeSigners();
     }
 
+    /** {@inheritDoc} */
+    public File getMountSource() {
+        return null;
+    }
+
     private VirtualFile getExistingFile(final VirtualFile mountPoint, final VirtualFile target) throws FileNotFoundException {
         final VirtualFile assemblyFile = assembly.getFile(mountPoint, target);
         if (assemblyFile == null) {

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/FileSystem.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/FileSystem.java	2010-03-09 23:53:43 UTC (rev 102177)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/FileSystem.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -166,4 +166,11 @@
      * @throws IOException if an I/O error occurs during close
      */
     void close() throws IOException;
+
+    /**
+     * Get the {@link java.io.File} source provided at mount time.
+     *
+     * @return the source used for mounting
+     */
+    File getMountSource();
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java	2010-03-09 23:53:43 UTC (rev 102177)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/JavaZipFileSystem.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -284,8 +284,13 @@
     public boolean isReadOnly() {
         return true;
     }
-    
+
     /** {@inheritDoc} */
+    public File getMountSource() {
+        return archiveFile;
+    }
+
+    /** {@inheritDoc} */
     public void close() throws IOException {
         log.tracef("Closing zip filesystem %s", this);
         VFSUtils.safeClose(new Closeable() {

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/MountHandle.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/MountHandle.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/MountHandle.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -0,0 +1,41 @@
+/*
+ * 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.Closeable;
+import java.io.File;
+
+/**
+ * This class represents a handle to a mounted FileSystem.  This can
+ * be used to close/cleanup the FileSystem as well as to access the
+ * mount source.
+ *
+ * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
+ */
+public interface MountHandle extends Closeable {
+   /**
+    * Get the source file used for the mount.
+    *
+    * @return the source file
+    */
+   File getMountSource();
+}

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java	2010-03-09 23:53:43 UTC (rev 102177)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -135,16 +135,23 @@
         return names == null ? Collections.<String>emptyList() : Arrays.asList(names);
     }
     
-   /**
-    * {@inheritDoc}
-    */
-   public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
-      return null;
-   }
+    /**
+     * {@inheritDoc}
+     */
+    public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
+        return null;
+    }
 
-   /**
+    /**
      * {@inheritDoc}
      */
+    public File getMountSource() {
+        return realRoot;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     public void close() throws IOException {
         // no operation - the real FS can't be closed
     }

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java	2010-03-09 23:53:43 UTC (rev 102177)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -117,16 +117,24 @@
         return names == null ? Collections.<String>emptyList() : Arrays.asList(names);
     }
     
-   /**
-    * {@inheritDoc}
-    */
-   public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
-      return null;
-   }
+    /**
+     * {@inheritDoc}
+     */
+    public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
+        return null;
+    }
 
-   /**
+
+    /**
      * {@inheritDoc}
      */
+    public File getMountSource() {
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     public void close() throws IOException {
         // no operation - the root FS can't be closed
     }

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AbstractVFSTest.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AbstractVFSTest.java	2010-03-09 23:53:43 UTC (rev 102177)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AbstractVFSTest.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -48,7 +48,7 @@
  */
 public abstract class AbstractVFSTest extends BaseTestCase
 {
-   private TempFileProvider provider;
+   protected TempFileProvider provider;
 
    public AbstractVFSTest(String name)
    {

Added: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/MountHandleTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/MountHandleTestCase.java	                        (rev 0)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/MountHandleTestCase.java	2010-03-10 00:16:07 UTC (rev 102178)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.vfs;
+
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.spi.MountHandle;
+
+import java.io.File;
+
+/**
+ * Tests functionality of the MountHandle retrieving mount source.
+ *
+ * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
+ */
+public class MountHandleTestCase extends AbstractVFSTest {
+
+   public MountHandleTestCase(final String name) {
+      super(name);
+   }
+
+   public void testZipGetMountSource() throws Exception {
+      VirtualFile jar  = getVirtualFile("/vfs/test/jar1.jar");
+      File origin = jar.getPhysicalFile();
+      MountHandle mountHandle = VFS.mountZip(jar, jar, provider);
+      try
+      {
+         File mounted = jar.getPhysicalFile();
+         File source = mountHandle.getMountSource();
+
+         assertNotNull(origin);
+         assertNotNull(mounted);
+         assertNotNull(source);
+         assertFalse(origin.equals(mounted));
+         assertFalse(origin.equals(source));
+         assertFalse(mounted.equals(source));
+
+         assertTrue(origin.isFile());
+         assertTrue(source.isFile());
+         assertTrue(mounted.isDirectory());
+
+         assertEquals(origin.length(), source.length());
+      } finally {
+         VFSUtils.safeClose(mountHandle);
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list