[jboss-cvs] JBossAS SVN: r99617 - in projects/vfs/trunk/src: test/java/org/jboss/test/vfs/util/automount and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jan 19 16:24:30 EST 2010


Author: johnbailey
Date: 2010-01-19 16:24:30 -0500 (Tue, 19 Jan 2010)
New Revision: 99617

Added:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountConfig.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountOption.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/Automounter.java
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/util/automount/AutomounterTestCase.java
Log:
Automounter cleanup

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/Automounter.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/Automounter.java	2010-01-19 21:22:52 UTC (rev 99616)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/Automounter.java	2010-01-19 21:24:30 UTC (rev 99617)
@@ -33,7 +33,7 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import org.jboss.util.id.GUID;
+import org.jboss.logging.Logger;
 import org.jboss.vfs.TempFileProvider;
 import org.jboss.vfs.VFS;
 import org.jboss.vfs.VFSUtils;
@@ -55,11 +55,10 @@
    /* Map of owners and their references */
    private static final ConcurrentMap<MountOwner, Set<RegistryEntry>> ownerReferences = new ConcurrentHashMap<MountOwner, Set<RegistryEntry>>();
 
-   /* VirutalFile used as a base mount for 'hidden' original copies of mounted files */
-   private static final VirtualFile originalsRoot = VFS.getChild("/.vfs/backups");
+   /* Provider of temp files/directories*/
+   private static TempFileProvider tempFileProvider;
    
-   /* Mount types */
-   private static enum MountType {ZIP, EXPANDED};
+   private static final Logger log = Logger.getLogger("org.jboss.vfs.util.automount");
    
    /**
     * Private constructor
@@ -72,11 +71,12 @@
     * Mount provided {@link VirtualFile} (if not mounted) and set the owner to be the provided target.  (Self owned mount)
     * 
     * @param target VirtualFile to mount
+    * @param mountOptions optional configuration to use for mounting
     * @throws IOException when the target can not be mounted.
     */
-   public static void mount(VirtualFile target) throws IOException
+   public static void mount(VirtualFile target, MountOption... mountOptions) throws IOException
    {
-      mount(new VirtualFileOwner(target), target);
+      mount(new VirtualFileOwner(target), target, mountOptions);
    }
 
    /**
@@ -84,11 +84,12 @@
     * 
     * @param owner Object that owns the reference to the mount
     * @param target VirtualFile to mount
+    * @param mountOptions optional configuration to use for mounting
     * @throws IOException when the target can not be mounted.
     */
-   public static void mount(Object owner, VirtualFile target) throws IOException
+   public static void mount(Object owner, VirtualFile target, MountOption... mountOptions) throws IOException
    {
-      mount(new SimpleMountOwner(owner), target);
+      mount(new SimpleMountOwner(owner), target, mountOptions);
    }
 
    /**
@@ -96,86 +97,44 @@
     * 
     * @param owner VirtualFile that owns the reference to the mount
     * @param target VirtualFile to mount
+    * @param mountOptions optional configuration to use for mounting
     * @throws IOException when the target can not be mounted.
     */
-   public static void mount(VirtualFile owner, VirtualFile target) throws IOException
+   public static void mount(VirtualFile owner, VirtualFile target, MountOption... mountOptions) throws IOException
    {
-      mount(new VirtualFileOwner(owner), target);
+      mount(new VirtualFileOwner(owner), target, mountOptions);
    }
 
    /**
-    * Mount provided {@link VirtualFile} as an expanded file system (if not mounted) and add an owner entry.  Also creates a back-reference to from the owner to the target.
+    * Mount provided {@link VirtualFile} (if not mounted) and add an owner entry.  Also creates a back-reference to from the owner to the target.
     * 
     * @param owner MountOwner that owns the reference to the mount
     * @param target VirtualFile to mount
+    * @param mountOptions optional configuration to use for mounting
     * @throws IOException when the target can not be mounted
     */
-   public static void mount(MountOwner owner, VirtualFile target) throws IOException {
-      mount(owner, target, MountType.ZIP);
-   }
-
-   /**
-    * Mount provided {@link VirtualFile} (if not mounted) and set the owner to be the provided target.  (Self owned mount)
-    * 
-    * @param target VirtualFile to mount
-    * @throws IOException when the target can not be mounted.
-    */
-   public static void mountExpanded(VirtualFile target) throws IOException
+   public static void mount(MountOwner owner, VirtualFile target, MountOption... mountOptions) throws IOException
    {
-      mountExpanded(new VirtualFileOwner(target), target);
+      final RegistryEntry targetEntry = getEntry(target);
+      targetEntry.mount(target, getMountConfig(mountOptions));
+      targetEntry.inboundReferences.add(owner);
+      ownerReferences.putIfAbsent(owner, new HashSet<RegistryEntry>());
+      ownerReferences.get(owner).add(targetEntry);
    }
-
-   /**
-    * Mount provided {@link VirtualFile} as an expanded file system (if not mounted) and add an owner entry.  Also creates a back-reference to from the owner to the target.
-    * 
-    * @param owner Object that owns the reference to the mount
-    * @param target VirtualFile to mount
-    * @throws IOException when the target can not be mounted.
-    */
-   public static void mountExpanded(Object owner, VirtualFile target) throws IOException
-   {
-      mountExpanded(new SimpleMountOwner(owner), target);
-   }
    
    /**
-    * Mount provided {@link VirtualFile} as an expanded file system (if not mounted) and add an owner entry.  Also creates a back-reference to from the owner to the target.
+    * Creates a MountConfig and applies the provided mount options
     * 
-    * @param owner VirtualFile that owns the reference to the mount
-    * @param target VirtualFile to mount
-    * @throws IOException when the target can not be mounted.
+    * @param mountOptions options to use for mounting
+    * @return a MountConfig
     */
-   public static void mountExpanded(VirtualFile owner, VirtualFile target) throws IOException
-   {
-      mountExpanded(new VirtualFileOwner(owner), target);
+   private static MountConfig getMountConfig(MountOption[] mountOptions) {
+      final MountConfig config = new MountConfig();
+      for(MountOption option : mountOptions) {
+         option.apply(config);
+      }
+      return config;
    }
-   
-   /**
-    * Mount provided {@link VirtualFile} (if not mounted) and add an owner entry.  Also creates a back-reference to from the owner to the target.
-    * 
-    * @param owner MountOwner that owns the reference to the mount
-    * @param target VirtualFile to mount
-    * @throws IOException when the target can not be mounted
-    */
-   public static void mountExpanded(MountOwner owner, VirtualFile target) throws IOException {
-      mount(owner, target, MountType.EXPANDED);
-   }
-   
-   /**
-    * Mount provided {@link VirtualFile} (if not mounted) and add an owner entry.  Also creates a back-reference to from the owner to the target.
-    * 
-    * @param owner MountOwner that owns the reference to the mount
-    * @param target VirtualFile to mount
-    * @param mountType {@link MountType} type of mount to create
-    * @throws IOException when the target can not be mounted
-    */
-   private static void mount(MountOwner owner, VirtualFile target, MountType mountType) throws IOException
-   {
-      RegistryEntry targetEntry = getEntry(target);
-      targetEntry.mount(target, mountType);
-      targetEntry.inboundReferences.add(owner);
-      ownerReferences.putIfAbsent(owner, new HashSet<RegistryEntry>());
-      ownerReferences.get(owner).add(targetEntry);
-   }
 
    /**
     * Cleanup all references from the owner.  Cleanup any mounted entries that become un-referenced in the process.
@@ -227,47 +186,6 @@
    }
    
    /**
-    * Create a backup of the provided root.  Only one backup is allowed for a location.
-    * TODO: Find a way to remove the need for this.   
-    * 
-    * @param original the original VirtualFile to backup
-    * @return a reference to the backup location
-    * @throws IOException if any problems occur during the backup process
-    */
-   public static VirtualFile backup(VirtualFile original) throws IOException
-   {
-      RegistryEntry entry = getEntry(original); 
-      entry.backup(original);
-      return entry.backupFile;
-   }
-   
-   /**
-    * Get the backup for the provided target
-    * TODO: Find a way to remove the need for this.
-    * 
-    * @param target the location to get the backup for
-    * @return the backup
-    */
-   public static VirtualFile getBackup(VirtualFile target) 
-   {
-      RegistryEntry entry = getEntry(target);
-      return entry.backupFile != null ? entry.backupFile : null;
-   }
-   
-   /**
-    * Check to see if a backup exists for the provided location.
-    * TODO: Find a way to remove the need for this.
-    * 
-    * @param target the location to check for a backup.
-    * @return true if the backup exists.
-    */
-   public static boolean hasBackup(VirtualFile target) 
-   {
-      return getEntry(target).backupFile != null;
-   }
-   
-   
-   /**
     * Get the entry from the tree creating the entry if not present.
     * 
     * @param virtualFile
@@ -282,9 +200,11 @@
       return rootEntry.find(virtualFile);
    }
 
-   private static TempFileProvider getTempFileProvider(String name) throws IOException
+   private static TempFileProvider getTempFileProvider() throws IOException
    {
-      return TempFileProvider.create(name, Executors.newSingleThreadScheduledExecutor());
+      if(tempFileProvider == null)
+         tempFileProvider = TempFileProvider.create("automount", Executors.newScheduledThreadPool(2));
+      return tempFileProvider;
    }
    
    static class RegistryEntry
@@ -297,22 +217,30 @@
       
       private final AtomicBoolean mounted = new AtomicBoolean();
       
-      private VirtualFile backupFile;
-      
-      private void mount(VirtualFile target, MountType mountType) throws IOException
+      private void mount(VirtualFile target, MountConfig mountConfig) throws IOException
       {
          if (mounted.compareAndSet(false, true))
          {
             if(target.isFile())
             {
-               final TempFileProvider provider = getTempFileProvider(target.getName());
-               // Make sure we can get to the original
-               backup(target);
-               if(MountType.ZIP.equals(mountType))
-                  handles.add(VFS.mountZip(target, target, provider));
+               log.debugf("Automounting: %s with options %s", target, mountConfig);
+               
+               final TempFileProvider provider = getTempFileProvider();
+               if(mountConfig.mountExpanded()) 
+               {
+                  if(mountConfig.copyTarget())
+                     handles.add(VFS.mountZipExpanded(target, target, provider));
+                  else 
+                     handles.add(VFS.mountZipExpanded(target.getPhysicalFile(), target, provider));
+               }
                else
-                  handles.add(VFS.mountZipExpanded(target, target, provider));
-            }
+               {
+                  if(mountConfig.copyTarget())
+                     handles.add(VFS.mountZip(target, target, provider));
+                  else
+                     handles.add(VFS.mountZip(target.getPhysicalFile(), target, provider));
+               }
+             }
          }
       }
       
@@ -327,21 +255,22 @@
 
       void cleanup()
       {
-         VFSUtils.safeClose(handles);
-         handles.clear();
-
-         Collection<RegistryEntry> entries = getEntriesRecursive();
-         for (RegistryEntry entry : entries)
+         if(mounted.compareAndSet(true, false)) 
          {
-            entry.cleanup();
+            VFSUtils.safeClose(handles);
+            handles.clear();
+   
+            final Collection<RegistryEntry> entries = getEntriesRecursive();
+            for (RegistryEntry entry : entries)
+            {
+               entry.cleanup();
+            }
          }
-         
-         mounted.set(false);
       }
 
       private boolean isMounted()
       {
-         return !handles.isEmpty();
+         return mounted.get();
       }
 
       private RegistryEntry find(VirtualFile file)
@@ -355,15 +284,15 @@
          {
             return this;
          }
-         String current = path.remove(0);
+         final String current = path.remove(0);
          children.putIfAbsent(current, new RegistryEntry());
-         RegistryEntry childEntry = children.get(current);
+         final RegistryEntry childEntry = children.get(current);
          return childEntry.find(path);
       }
 
       private Collection<RegistryEntry> getEntriesRecursive()
       {
-         List<RegistryEntry> allHandles = new LinkedList<RegistryEntry>();
+         final List<RegistryEntry> allHandles = new LinkedList<RegistryEntry>();
          collectEntries(this, allHandles);
          return allHandles;
       }
@@ -376,11 +305,5 @@
             entries.add(childEntry);
          }
       }
-      
-      private void backup(VirtualFile target) throws IOException
-      {
-         backupFile = originalsRoot.getChild(GUID.asString() + target.getName());
-         handles.add(VFS.mountReal(target.getPhysicalFile(), backupFile));
-      }
    }
 }

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountConfig.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountConfig.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountConfig.java	2010-01-19 21:24:30 UTC (rev 99617)
@@ -0,0 +1,83 @@
+/*
+ * 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.util.automount;
+
+/**
+ * Configuration used to control the auto-mount behavior.
+ * 
+ * @author <a href="jbailey at redhat.com">John Bailey</a>
+ */
+class MountConfig
+{
+   private boolean mountExpanded;
+
+   private boolean copyTarget;
+
+   /**
+    * Should the archive be mounted as an expanded zip filesystem.  Defaults to false.
+    * 
+    * @return true if it should be expanded
+    */
+   boolean mountExpanded()
+   {
+      return mountExpanded;
+   }
+   
+   /**
+    * Set whether the mount should be an expanded zip filesystem.
+    * 
+    * @param mountExpanded the boolean value to set it to
+    */
+   void setMountExpanded(boolean mountExpanded)
+   {
+      this.mountExpanded = mountExpanded;
+   }
+
+   /**
+    * Should the archive be copied to a temporary location before being mounted.
+    * Defaults to false.
+    * 
+    * @return true if the archive should be copied before being mounted
+    */
+   boolean copyTarget()
+   {
+      return copyTarget;
+   }
+
+   /**
+    * Set whether the archive should be copied before being mounted.
+    * 
+    * @param copyTarget the boolean value to set it to
+    */
+   void setCopyTarget(boolean copyTarget)
+   {
+      this.copyTarget = copyTarget;
+   }
+
+   @Override
+   public String toString()
+   {
+      return new StringBuilder().append("MountConfig[Expanded: ").append(mountExpanded).append(", Copy: ").append(
+            copyTarget).append("]").toString();
+   }
+
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountOption.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountOption.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/util/automount/MountOption.java	2010-01-19 21:24:30 UTC (rev 99617)
@@ -0,0 +1,50 @@
+/*
+ * 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.util.automount;
+
+/**
+ * Custom configurations to use when mounting archives through the {@link Automounter}.
+ *   
+ * @author <a href="jbailey at redhat.com">John Bailey</a>
+ */
+public enum MountOption {
+   
+   EXPANDED {
+      void apply(MountConfig config)
+      {
+         config.setMountExpanded(true);
+      }
+   },
+   COPY {
+      void apply(MountConfig config)
+      {
+         config.setCopyTarget(true);
+      }
+   };
+
+   /**
+    * Each option must apply its custom settings to teh {@link MountConfig}.
+    * 
+    * @param config MountConfig to apply settings to
+    */
+   abstract void apply(MountConfig config);
+}

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/util/automount/AutomounterTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/util/automount/AutomounterTestCase.java	2010-01-19 21:22:52 UTC (rev 99616)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/util/automount/AutomounterTestCase.java	2010-01-19 21:24:30 UTC (rev 99617)
@@ -21,9 +21,12 @@
  */
 package org.jboss.test.vfs.util.automount;
 
+import java.io.File;
+
 import org.jboss.test.vfs.AbstractVFSTest;
 import org.jboss.vfs.VirtualFile;
 import org.jboss.vfs.util.automount.Automounter;
+import org.jboss.vfs.util.automount.MountOption;
 import org.jboss.vfs.util.automount.MountOwner;
 import org.jboss.vfs.util.automount.SimpleMountOwner;
 import org.jboss.vfs.util.automount.VirtualFileOwner;
@@ -176,5 +179,19 @@
 
       assertFalse("Should have been unmounted since the owner object is the same", Automounter.isMounted(jarVirtualFile));
    }
+   
+   public void testMountWithCopy() throws Exception
+   {
+      VirtualFile jarVirtualFile = getVirtualFile("/vfs/test/jar1.jar");
+      File originalFile = jarVirtualFile.getPhysicalFile();
+      Automounter.mount(jarVirtualFile, MountOption.COPY);
 
+      File copiedFile = jarVirtualFile.getPhysicalFile();   
+      
+      assertFalse(copiedFile.equals(originalFile));
+      
+      Automounter.cleanup(jarVirtualFile);
+      assertFalse("Should have been unmounted since the owner object is the same", Automounter.isMounted(jarVirtualFile));
+   }
+
 }




More information about the jboss-cvs-commits mailing list