[jboss-cvs] JBossAS SVN: r97989 - in projects/jboss-deployers/branches/vfs3/deployers-vfs/src: test/java/org/jboss/test/deployers/vfs/structure and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Dec 18 10:30:17 EST 2009
Author: johnbailey
Date: 2009-12-18 10:30:16 -0500 (Fri, 18 Dec 2009)
New Revision: 97989
Added:
projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/Automounter.java
projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/AutomounterTest.java
Removed:
projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/VFSHandleRegistry.java
projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/VFSHandleRegistryTest.java
Log:
Initial non-thread safe implementation of Automounter
Copied: projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/Automounter.java (from rev 97696, projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/VFSHandleRegistry.java)
===================================================================
--- projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/Automounter.java (rev 0)
+++ projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/Automounter.java 2009-12-18 15:30:16 UTC (rev 97989)
@@ -0,0 +1,257 @@
+/*
+ * 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.deployers.vfs.plugins.structure;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.Executors;
+
+import org.jboss.vfs.TempFileProvider;
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.util.PathTokenizer;
+
+/**
+ * Utility used to manage mounting Virtual FileSystems.
+ *
+ * TODO - Make this thread safe.............
+ *
+ * @author <a href="jbailey at redhat.com">John Bailey</a>
+ */
+public class Automounter
+{
+ /* Root entry in the tree. */
+ private static final RegistryEntry rootEntry = new RegistryEntry();
+
+ /* Possible mount types */
+ private static enum MountType {
+ ZIP, EXPANDED
+ };
+
+ /**
+ * Private constructor
+ */
+ private Automounter()
+ {
+ }
+
+ /**
+ * 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 mount(VirtualFile target) throws IOException
+ {
+ mount(target, 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 Virtual file that owns the reference to the mount
+ * @param target VirtualFile to mount
+ * @throws IOException when the target can not be mounted
+ */
+ public static void mount(VirtualFile owner, VirtualFile target) throws IOException
+ {
+ RegistryEntry targetEntry = getEntry(target);
+ RegistryEntry ownerEntry = getEntry(owner);
+ targetEntry.mount(ownerEntry, target, MountType.ZIP);
+ }
+
+ /**
+ * Mount provided {@link VirtualFile} (if not mounted) as an expanded Zip mount and add an owner entry.
+ * Also creates a back-reference to from the owner to the target. (Self owned mount)
+ *
+ * @param owner Virtual file 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(VirtualFile target) throws IOException
+ {
+ mountExpanded(target, target);
+ }
+
+ /**
+ * Mount provided {@link VirtualFile} (if not mounted) as an expanded Zip mount and add an owner entry.
+ * Also creates a back-reference to from the owner to the target.
+ *
+ * @param owner Virtual file 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(VirtualFile owner, VirtualFile target) throws IOException
+ {
+ RegistryEntry targetEntry = getEntry(target);
+ RegistryEntry ownerEntry = getEntry(owner);
+ targetEntry.mount(ownerEntry, target, MountType.EXPANDED);
+ }
+
+ /**
+ * Recursively cleanup all mounted handles starting at the provided {@link VirtualFile} location
+ * and remove all references to other mounts.
+ *
+ * @param owner VirtualFile to cleanup
+ */
+ public static void cleanup(VirtualFile owner)
+ {
+ getEntry(owner).cleanup();
+ }
+
+ /**
+ * Determines whether a target {@link VirtualFile} is mounted.
+ *
+ * @param target
+ * @return
+ */
+ public static boolean isMounted(VirtualFile target)
+ {
+ return getEntry(target).isMounted();
+ }
+
+ /**
+ * Get the entry from the tree creating the entry if not present.
+ *
+ * @param virtualFile
+ * @return
+ */
+ private static RegistryEntry getEntry(VirtualFile virtualFile)
+ {
+ if (virtualFile == null)
+ {
+ throw new IllegalArgumentException("A valid VirtualFile is required.");
+ }
+ return rootEntry.find(virtualFile);
+ }
+
+ private static TempFileProvider getTempFileProvider(String name) throws IOException
+ {
+ return TempFileProvider.create(name, Executors.newSingleThreadScheduledExecutor());
+ }
+
+ static class RegistryEntry
+ {
+ private final ConcurrentMap<String, RegistryEntry> children = new ConcurrentHashMap<String, RegistryEntry>();
+
+ private final Set<RegistryEntry> inboundReferences = new HashSet<RegistryEntry>();
+
+ private final Set<RegistryEntry> outboundReferences = new HashSet<RegistryEntry>();
+
+ private Closeable handle;
+
+ Collection<RegistryEntry> getChildren()
+ {
+ return Collections.unmodifiableCollection(children.values());
+ }
+
+ void mount(RegistryEntry owner, VirtualFile target, MountType mountType) throws IOException
+ {
+ if (!isMounted() && target.isFile())
+ {
+ if (MountType.ZIP.equals(mountType))
+ handle = VFS.mountZip(target, target, getTempFileProvider(target.getName()));
+ else
+ handle = VFS.mountZipExpanded(target, target, getTempFileProvider(target.getName()));
+ }
+ if (owner.equals(this) == false)
+ {
+ inboundReferences.add(owner);
+ owner.outboundReferences.add(this);
+ }
+ }
+
+ void removeInboundReference(RegistryEntry owner)
+ {
+ inboundReferences.remove(owner);
+ if (inboundReferences.isEmpty())
+ {
+ cleanup();
+ }
+ }
+
+ void cleanup()
+ {
+ VFSUtils.safeClose(handle);
+ handle = null;
+
+ Collection<RegistryEntry> entries = getEntriesRecursive();
+ for (RegistryEntry entry : entries)
+ {
+ entry.cleanup();
+ }
+ for (RegistryEntry entry : outboundReferences)
+ {
+ entry.removeInboundReference(this);
+ }
+ }
+
+ boolean isMounted()
+ {
+ return handle != null;
+ }
+
+ RegistryEntry find(VirtualFile file)
+ {
+ return find(PathTokenizer.getTokens(file.getPathName()));
+ }
+
+ RegistryEntry find(List<String> path)
+ {
+ if (path.isEmpty())
+ {
+ return this;
+ }
+ String current = path.remove(0);
+ children.putIfAbsent(current, new RegistryEntry());
+ RegistryEntry childEntry = children.get(current);
+ return childEntry.find(path);
+ }
+
+ Collection<RegistryEntry> getEntriesRecursive()
+ {
+ List<RegistryEntry> allHandles = new LinkedList<RegistryEntry>();
+ collectEntries(this, allHandles);
+ return allHandles;
+ }
+
+ void collectEntries(RegistryEntry registryEntry, List<RegistryEntry> entries)
+ {
+ for (RegistryEntry childEntry : registryEntry.getChildren())
+ {
+ collectEntries(childEntry, entries);
+ entries.add(childEntry);
+ }
+
+ }
+
+ }
+}
Deleted: projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/VFSHandleRegistry.java
===================================================================
--- projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/VFSHandleRegistry.java 2009-12-18 15:03:38 UTC (rev 97988)
+++ projects/jboss-deployers/branches/vfs3/deployers-vfs/src/main/java/org/jboss/deployers/vfs/plugins/structure/VFSHandleRegistry.java 2009-12-18 15:30:16 UTC (rev 97989)
@@ -1,233 +0,0 @@
-/*
- * 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.deployers.vfs.plugins.structure;
-
-import java.io.Closeable;
-import java.io.IOException;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.Executors;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import org.jboss.vfs.TempFileProvider;
-import org.jboss.vfs.VFS;
-import org.jboss.vfs.VFSUtils;
-import org.jboss.vfs.VirtualFile;
-import org.jboss.vfs.util.PathTokenizer;
-
-/**
- * Registry used to manage handles to mounted Virtual FileSystems.
- *
- * @author <a href="jbailey at redhat.com">John Bailey</a>
- */
-public class VFSHandleRegistry
-{
- /*
- * Simple handle with no side effects.
- */
- private static final Closeable NO_OP_HANDLE = new Closeable()
- {
- public void close() throws IOException
- {
- }
- };
-
- /* Root entry in the tree. */
- private static final RegistryEntry rootEntry = new RegistryEntry();
-
- /**
- * Private constructor
- */
- private VFSHandleRegistry()
- {
- }
-
- /**
- * Determine if the provided {@link VirtualFile} has been mounted.
- */
- public static boolean isMounted(VirtualFile virtualFile)
- {
- return getEntry(virtualFile).isMounted();
- }
-
- /**
- * Add a registry entry for the provided {@link VirtualFile} and attache the handle
- *
- * @param virtualFile
- * @param handle
- */
- public static void addHandle(VirtualFile virtualFile, Closeable handle)
- {
- getEntry(virtualFile).setHandle(handle);
- }
-
- /**
- * Get a handle for the provided {@link VirtualFile}. If the entry is already mounted
- * a the reference count will be incremented and a a handle will be returned. If the entry
- * is not mounted, the entry will be mounted based on they type of {@link VirtualFile} provided.
- * Files will be mounted as a Zip archive, and directories will register a no-op handle.
- *
- * @param virtualFile
- * @return a handle to the {@link VirtualFile}
- * @throws IOException
- */
- public static Closeable getHandleFor(VirtualFile virtualFile) throws IOException
- {
- RegistryEntry entry = getEntry(virtualFile);
- if (!entry.isMounted())
- {
- Closeable handle = null;
- if (virtualFile.isFile())
- {
- handle = VFS.mountZip(virtualFile, virtualFile, getTempFileProvider(virtualFile.getName()));
- }
- else
- {
- handle = NO_OP_HANDLE;
- }
- addHandle(virtualFile, handle);
- }
- return entry.aquireReference();
- }
-
- /**
- * Recursively cleanup all mounted handles starting at the provided {@link VirtualFile} location.
- *
- * @param virtualFile
- */
- public static void cleanup(VirtualFile virtualFile)
- {
- RegistryEntry startEntry = getEntry(virtualFile);
- Collection<RegistryEntry> entries = startEntry.getEntriesRecursive();
- for (RegistryEntry entry : entries)
- {
- entry.cleanup();
- }
- }
-
- /**
- * Get the entry from the tree creating the entry if not present.
- *
- * @param virtualFile
- * @return
- */
- private static RegistryEntry getEntry(VirtualFile virtualFile)
- {
- if (virtualFile == null)
- {
- throw new IllegalArgumentException("A valid VirtualFile is required.");
- }
- return rootEntry.find(virtualFile);
- }
-
- private static TempFileProvider getTempFileProvider(String name) throws IOException
- {
- return TempFileProvider.create(name, Executors.newSingleThreadScheduledExecutor());
- }
-
- static class RegistryEntry
- {
- private final ConcurrentMap<String, RegistryEntry> children = new ConcurrentHashMap<String, RegistryEntry>();
-
- private Closeable handle;
-
- private final AtomicInteger refCount = new AtomicInteger();
-
- Collection<RegistryEntry> getChildren()
- {
- return Collections.unmodifiableCollection(children.values());
- }
-
- RegistryEntry find(VirtualFile file)
- {
- return find(PathTokenizer.getTokens(file.getPathName()));
- }
-
- RegistryEntry find(List<String> path)
- {
- if (path.isEmpty())
- {
- return this;
- }
- String current = path.remove(0);
- children.putIfAbsent(current, new RegistryEntry());
- RegistryEntry childEntry = children.get(current);
- return childEntry.find(path);
- }
-
- void setHandle(Closeable handle)
- {
- this.handle = handle;
- }
-
- void cleanup()
- {
- VFSUtils.safeClose(handle);
- handle = null;
- refCount.set(0);
- }
-
- boolean isMounted()
- {
- return handle != null;
- }
-
- Collection<RegistryEntry> getEntriesRecursive()
- {
- List<RegistryEntry> allHandles = new LinkedList<RegistryEntry>();
- collectEntries(this, allHandles);
- return allHandles;
- }
-
- void collectEntries(RegistryEntry registryEntry, List<RegistryEntry> entries)
- {
- for (RegistryEntry childEntry : registryEntry.getChildren())
- {
- collectEntries(childEntry, entries);
- }
- entries.add(registryEntry);
- }
-
- Closeable aquireReference()
- {
- refCount.incrementAndGet();
- return new Reference();
- }
-
- class Reference implements Closeable
- {
- public void close() throws IOException
- {
- if (refCount.decrementAndGet() == 0)
- {
- cleanup();
- }
-
- }
- }
-
- }
-}
Copied: projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/AutomounterTest.java (from rev 97696, projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/VFSHandleRegistryTest.java)
===================================================================
--- projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/AutomounterTest.java (rev 0)
+++ projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/AutomounterTest.java 2009-12-18 15:30:16 UTC (rev 97989)
@@ -0,0 +1,133 @@
+/*
+ * 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.deployers.vfs.structure;
+
+import java.net.URL;
+
+import org.jboss.deployers.vfs.plugins.structure.Automounter;
+import org.jboss.test.BaseTestCase;
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VirtualFile;
+
+/**
+ * Test for {@link Automounter}
+ *
+ * @author <a href="jbailey at redhat.com">John Bailey</a>
+ */
+public class AutomounterTest extends BaseTestCase
+{
+
+ public AutomounterTest(String name)
+ {
+ super(name);
+ }
+
+ public void testMountAndCleanup() throws Exception
+ {
+ VirtualFile virtualFile = getVirtualFile("/structure/ear/archive.ear");
+ Automounter.mount(virtualFile, virtualFile);
+ assertTrue(Automounter.isMounted(virtualFile));
+ Automounter.cleanup(virtualFile);
+ assertFalse(Automounter.isMounted(virtualFile));
+ }
+
+ public void testCleanupWithOwner() throws Exception
+ {
+ VirtualFile earVirtualFile = getVirtualFile("/structure/ear/simple/simple.ear");
+ Automounter.mount(earVirtualFile);
+
+ VirtualFile jarVirtualFile = earVirtualFile.getChild("archive.jar");
+ Automounter.mount(earVirtualFile, jarVirtualFile);
+
+ VirtualFile warVirtualFile = earVirtualFile.getChild("simple.war");
+ Automounter.mount(earVirtualFile, warVirtualFile);
+
+ assertTrue(Automounter.isMounted(earVirtualFile));
+ assertTrue(Automounter.isMounted(warVirtualFile));
+ assertTrue(Automounter.isMounted(jarVirtualFile));
+
+ Automounter.cleanup(earVirtualFile);
+
+ assertFalse(Automounter.isMounted(earVirtualFile));
+ assertFalse(Automounter.isMounted(warVirtualFile));
+ assertFalse(Automounter.isMounted(jarVirtualFile));
+ }
+
+ public void testCleanupRecursive() throws Exception
+ {
+ VirtualFile earVirtualFile = getVirtualFile("/structure/ear/simple/simple.ear");
+ Automounter.mount(earVirtualFile);
+
+ VirtualFile jarVirtualFile = earVirtualFile.getChild("archive.jar");
+ Automounter.mount(jarVirtualFile);
+
+ VirtualFile warVirtualFile = earVirtualFile.getChild("simple.war");
+ Automounter.mount(warVirtualFile);
+
+ assertTrue(Automounter.isMounted(earVirtualFile));
+ assertTrue(Automounter.isMounted(warVirtualFile));
+ assertTrue(Automounter.isMounted(jarVirtualFile));
+
+ Automounter.cleanup(earVirtualFile);
+
+ assertFalse(Automounter.isMounted(earVirtualFile));
+ assertFalse(Automounter.isMounted(warVirtualFile));
+ assertFalse(Automounter.isMounted(jarVirtualFile));
+ }
+
+ public void testCleanupRefereces() throws Exception
+ {
+ VirtualFile earVirtualFile = getVirtualFile("/structure/ear/simple/simple.ear");
+ Automounter.mount(earVirtualFile);
+
+ VirtualFile jarVirtualFile = getVirtualFile("/structure/jar/indirectory/archive.jar");
+ Automounter.mount(earVirtualFile, jarVirtualFile);
+
+ VirtualFile warVirtualFile = getVirtualFile("/structure/war/simple/simple.war");
+ Automounter.mount(earVirtualFile, warVirtualFile);
+
+ assertTrue(Automounter.isMounted(earVirtualFile));
+ assertTrue(Automounter.isMounted(warVirtualFile));
+ assertTrue(Automounter.isMounted(jarVirtualFile));
+
+ VirtualFile otherEarVirtualFile = getVirtualFile("/structure/ear/scanning.ear");
+ Automounter.mount(otherEarVirtualFile, jarVirtualFile);
+
+ Automounter.cleanup(earVirtualFile);
+
+ assertFalse(Automounter.isMounted(earVirtualFile));
+ assertFalse(Automounter.isMounted(warVirtualFile));
+ assertTrue("Should not have unmounted the reference from two locations", Automounter.isMounted(jarVirtualFile));
+
+ Automounter.cleanup(otherEarVirtualFile);
+ assertFalse(Automounter.isMounted(jarVirtualFile));
+
+ }
+
+ protected VirtualFile getVirtualFile(String path) throws Exception
+ {
+ URL url = getResource(path);
+ VirtualFile rootFile = VFS.getChild(url);
+ return rootFile;
+ }
+
+}
Deleted: projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/VFSHandleRegistryTest.java
===================================================================
--- projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/VFSHandleRegistryTest.java 2009-12-18 15:03:38 UTC (rev 97988)
+++ projects/jboss-deployers/branches/vfs3/deployers-vfs/src/test/java/org/jboss/test/deployers/vfs/structure/VFSHandleRegistryTest.java 2009-12-18 15:30:16 UTC (rev 97989)
@@ -1,187 +0,0 @@
-/*
- * 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.deployers.vfs.structure;
-
-import java.io.Closeable;
-import java.io.IOException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.jboss.deployers.vfs.plugins.structure.VFSHandleRegistry;
-import org.jboss.test.BaseTestCase;
-import org.jboss.vfs.VFS;
-import org.jboss.vfs.VirtualFile;
-
-/**
- * Test for {@link VFSHandleRegistry}
- *
- * @author <a href="jbailey at redhat.com">John Bailey</a>
- */
-public class VFSHandleRegistryTest extends BaseTestCase
-{
- private static final Closeable DEFAULT_NO_OP_HANDLE = createNoOpTestHandle();
-
- public VFSHandleRegistryTest(String name)
- {
- super(name);
- }
-
- public void testRegister() throws Exception
- {
- VirtualFile virtualFile = VFS.getChild("test/path/test.ear");
-
- VFSHandleRegistry.addHandle(virtualFile, DEFAULT_NO_OP_HANDLE);
- assertTrue(VFSHandleRegistry.isMounted(virtualFile));
- }
-
- public void testCleanup() throws Exception
- {
- VirtualFile virtualFile = VFS.getChild("test/path/test.ear");
-
- TestHandle handle = createNoOpTestHandle();
-
- VFSHandleRegistry.addHandle(virtualFile, handle);
- assertTrue(VFSHandleRegistry.isMounted(virtualFile));
-
- VFSHandleRegistry.cleanup(virtualFile);
-
- assertTrue(handle.isClosed());
- assertFalse(VFSHandleRegistry.isMounted(virtualFile));
- }
-
- public void testCleanupWithChildren() throws Exception
- {
- final List<Closeable> handles = new ArrayList<Closeable>();
-
- Callback callback = new Callback()
- {
- public void call(TestHandle handle)
- {
- handles.add(handle);
- }
- };
-
- TestHandle earHandle = createTestHandle(callback);
-
- VirtualFile earVirtualFile = VFS.getChild("test/path/test.ear");
- VFSHandleRegistry.addHandle(earVirtualFile, earHandle);
-
- TestHandle warHandle = createTestHandle(callback);
-
- VirtualFile warVirtualFile = VFS.getChild("test/path/test.ear/test.war");
- VFSHandleRegistry.addHandle(warVirtualFile, warHandle);
-
- TestHandle jarHandle = createTestHandle(callback);
-
- VirtualFile jarVirtualFile = VFS.getChild("test/path/test.ear/test.war/WEB-INF/lib/test.jar");
- VFSHandleRegistry.addHandle(jarVirtualFile, jarHandle);
-
- assertFalse(jarHandle.isClosed());
- assertFalse(warHandle.isClosed());
- assertFalse(earHandle.isClosed());
-
- VFSHandleRegistry.cleanup(earVirtualFile);
-
- assertTrue(jarHandle.isClosed());
- assertTrue(warHandle.isClosed());
- assertTrue(earHandle.isClosed());
-
- assertEquals(jarHandle, handles.get(0));
- assertEquals(warHandle, handles.get(1));
- assertEquals(earHandle, handles.get(2));
- }
-
- public void testReferenceCounting() throws Exception
- {
- VirtualFile virtualFile = VFS.getChild("test/path/test.jar");
- Closeable handleOne = VFSHandleRegistry.getHandleFor(virtualFile);
- assertTrue(VFSHandleRegistry.isMounted(virtualFile));
- Closeable handleTwo = VFSHandleRegistry.getHandleFor(virtualFile);
- handleOne.close();
- assertTrue(VFSHandleRegistry.isMounted(virtualFile));
- handleTwo.close();
- assertFalse(VFSHandleRegistry.isMounted(virtualFile));
- }
-
- public void testAutoMount() throws Exception {
- VirtualFile file = getVirtualFile("/structure/jar/indirectory/archive.jar");
- assertTrue(file.isFile());
- Closeable handleOne = VFSHandleRegistry.getHandleFor(file);
- assertTrue(VFSHandleRegistry.isMounted(file));
- assertTrue(file.isDirectory());
- handleOne.close();
- assertTrue(file.isFile());
- }
-
- private static TestHandle createTestHandle(Callback callback)
- {
- return new TestHandle(callback);
- }
-
- private static TestHandle createNoOpTestHandle()
- {
- return new TestHandle(new Callback()
- {
- public void call(TestHandle handle)
- {
-
- }
- });
- }
-
- protected VirtualFile getVirtualFile(String path) throws Exception
- {
- URL url = getResource(path);
- VirtualFile rootFile = VFS.getChild(url);
- return rootFile;
- }
-
- private static class TestHandle implements Closeable
- {
- private boolean closed;
-
- private final Callback callback;
-
- public TestHandle(Callback callback)
- {
- super();
- this.callback = callback;
- }
-
- public void close() throws IOException
- {
- closed = true;
- callback.call(this);
- }
-
- public boolean isClosed()
- {
- return closed;
- }
- }
-
- private static interface Callback
- {
- void call(TestHandle handle);
- }
-}
More information about the jboss-cvs-commits
mailing list