[jboss-cvs] JBossAS SVN: r72344 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/plugins/context/memory and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 17 08:47:59 EDT 2008


Author: alesj
Date: 2008-04-17 08:47:59 -0400 (Thu, 17 Apr 2008)
New Revision: 72344

Added:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/MemoryFileFactory.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContext.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextFactory.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsmemory/Handler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MemoryTestCase.java
Log:
Introduce MemoryFileFactory API.

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/MemoryFileFactory.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/MemoryFileFactory.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/MemoryFileFactory.java	2008-04-17 12:47:59 UTC (rev 72344)
@@ -0,0 +1,119 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, 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.virtual;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URL;
+
+import org.jboss.virtual.plugins.context.memory.MemoryContextFactory;
+import org.jboss.virtual.spi.VFSContext;
+
+/**
+ * Memory VFS API.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class MemoryFileFactory
+{
+   private static final MemoryContextFactory factory = MemoryContextFactory.getInstance();
+
+   /**
+    * Find host's VFS.
+    *
+    * @param host the host
+    * @return host's vfs
+    */
+   public static VFS find(String host)
+   {
+      VFSContext context = factory.find(host);
+      return context != null ? context.getVFS() : null;
+   }
+
+   /**
+    * Create the memory root.
+    *
+    * @param uri the uri
+    * @return root's vfs
+    * @throws IOException for any error
+    */
+   public static VFS createRoot(URI uri) throws IOException
+   {
+      return createRoot(uri.toURL());
+   }
+
+   /**
+    * Create root vfs.
+    *
+    * @param url the url
+    * @return root's vfs
+    */
+   public static VFS createRoot(URL url)
+   {
+      return factory.createRoot(url).getVFS();
+   }
+
+   /**
+    * Create memory directory.
+    *
+    * @param url the url
+    * @return vfs directory
+    */
+   public static VirtualFile createDirectory(URL url)
+   {
+      return factory.createDirectory(url);
+   }
+
+   /**
+    * Put file.
+    *
+    * @param url the url
+    * @param contents the contents
+    * @return vfs file
+    */
+   public static VirtualFile putFile(URL url, byte[] contents)
+   {
+      return factory.putFile(url, contents);
+   }
+
+   /**
+    * Delete root.
+    *
+    * @param url the url
+    * @return true if deleted
+    */
+   public static boolean deleteRoot(URL url)
+   {
+      return factory.deleteRoot(url);
+   }
+
+   /**
+    * Delete.
+    *
+    * @param url the url
+    * @return true if deleted
+    */
+   public static boolean delete(URL url)
+   {
+      return factory.delete(url);
+   }
+}

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContext.java	2008-04-17 12:36:02 UTC (rev 72343)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContext.java	2008-04-17 12:47:59 UTC (rev 72344)
@@ -37,6 +37,7 @@
  * Virtual memory context.
  *
  * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public class MemoryContext extends AbstractVFSContext implements Serializable
@@ -66,12 +67,12 @@
       return root;
    }
    
-   VirtualFileHandler createDirectory(URL url)
+   VirtualFile createDirectory(URL url)
    {
       return putFile(url, null);
    }
    
-   VirtualFileHandler putFile(URL url, byte[] contents)
+   VirtualFile putFile(URL url, byte[] contents)
    {
       try
       {
@@ -116,7 +117,7 @@
          }
          
          current.setContents(contents);
-         return current;
+         return current.getVirtualFile();
       }
       catch(MalformedURLException e)
       {

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextFactory.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextFactory.java	2008-04-17 12:36:02 UTC (rev 72343)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextFactory.java	2008-04-17 12:47:59 UTC (rev 72344)
@@ -26,12 +26,12 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
+import org.jboss.virtual.VirtualFile;
 import org.jboss.virtual.spi.VFSContext;
 import org.jboss.virtual.spi.VFSContextFactory;
-import org.jboss.virtual.spi.VirtualFileHandler;
 
 /**
  * Singelton implementation of a MemoryContextFactory.
@@ -77,6 +77,7 @@
 
    /**
     * Gets hold of a root MemoryContext
+    *
     * @param host The name of the root
     * @return the found root MemoryContext, or null if none exists for the name 
     */
@@ -102,14 +103,13 @@
          }
          
          String rootName = url.getHost();
-         MemoryContext ctx = registry.get(rootName);
+         MemoryContext ctx = find(rootName);
          if (ctx == null)
          {
             URL ctxURL = new URL("vfsmemory://" + rootName);
             ctx = new MemoryContext(ctxURL);
             registry.put(rootName, ctx);
          }
-//         ctx.createDirectory(url);
          return ctx;
       }
       catch(MalformedURLException e)
@@ -128,7 +128,7 @@
     * @return The created directory
     * @throws IllegalArgumentException if there is no root matching the host part of the url 
     */
-   public VirtualFileHandler createDirectory(URL url)
+   public VirtualFile createDirectory(URL url)
    {
       String rootName = url.getHost();
       MemoryContext ctx = registry.get(rootName);
@@ -147,7 +147,7 @@
     * @return The created file
     * @throws IllegalArgumentException if there is no root matching the host part of the url 
     */
-   public VirtualFileHandler putFile(URL url, byte[] contents)
+   public VirtualFile putFile(URL url, byte[] contents)
    {
       String rootName = url.getHost();
       MemoryContext ctx = registry.get(rootName);
@@ -205,5 +205,4 @@
          throw new RuntimeException(e);
       }
    }
-   
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsmemory/Handler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsmemory/Handler.java	2008-04-17 12:36:02 UTC (rev 72343)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsmemory/Handler.java	2008-04-17 12:47:59 UTC (rev 72344)
@@ -26,15 +26,16 @@
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
 
+import org.jboss.virtual.MemoryFileFactory;
+import org.jboss.virtual.VFS;
 import org.jboss.virtual.VirtualFile;
-import org.jboss.virtual.plugins.context.memory.MemoryContext;
-import org.jboss.virtual.plugins.context.memory.MemoryContextFactory;
 import org.jboss.virtual.plugins.vfs.VirtualFileURLConnection;
 
 /**
  * URLStreamHandler for VFS
  *
  * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public class Handler extends URLStreamHandler
@@ -42,11 +43,11 @@
    protected URLConnection openConnection(URL u) throws IOException
    {
       String host = u.getHost();
-      MemoryContext ctx = MemoryContextFactory.getInstance().find(host);
-      if (ctx == null)
+      VFS vfs = MemoryFileFactory.find(host);
+      if (vfs == null)
          throw new IOException("vfs does not exist: " + u.toString());
 
-      VirtualFile vf = ctx.getChild(ctx.getRoot(), u.getPath()).getVirtualFile();
+      VirtualFile vf = vfs.getChild(u.getPath());
       if (vf == null)
          throw new IOException("vfs does not exist: " + u.toString());
 

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MemoryTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MemoryTestCase.java	2008-04-17 12:36:02 UTC (rev 72343)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MemoryTestCase.java	2008-04-17 12:47:59 UTC (rev 72344)
@@ -32,6 +32,7 @@
 import org.jboss.util.id.GUID;
 import org.jboss.virtual.VFS;
 import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.MemoryFileFactory;
 import org.jboss.virtual.plugins.context.memory.MemoryContextFactory;
 import org.jboss.virtual.plugins.context.memory.MemoryContextHandler;
 import org.jboss.virtual.spi.VFSContext;
@@ -42,6 +43,7 @@
 /**
  * 
  * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
 public class MemoryTestCase extends AbstractVFSTest
@@ -108,14 +110,12 @@
    
    public void testWriteAndReadData() throws Exception
    {
-      MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
       URL root = new URL("vfsmemory://aopdomain");
       try
       {
          long now = System.currentTimeMillis();
-         VFSContext ctx = mfactory.createRoot(root);
          URL url = new URL("vfsmemory://aopdomain/org/acme/test/Test.class");
-         mfactory.putFile(url,  new byte[] {'a', 'b', 'c'});
+         MemoryFileFactory.putFile(url,  new byte[] {'a', 'b', 'c'});
          
          String read = readURL(url);
          assertEquals("abc", read);
@@ -129,7 +129,7 @@
          assertTrue(classFile.isLeaf());
          assertTrue(classFile.getLastModified() >= now);
 
-         assertTrue(mfactory.delete(url));
+         assertTrue(MemoryFileFactory.delete(url));
          try
          {
             InputStream is = url.openStream();
@@ -139,32 +139,31 @@
          {
          }
          
-         ctx = mfactory.find("aopdomain");
+         VFS ctx = MemoryFileFactory.find("aopdomain");
          assertNotNull(ctx);
          
-         assertTrue(mfactory.deleteRoot(root));
-         ctx = mfactory.find("aopdomain");
+         assertTrue(MemoryFileFactory.deleteRoot(root));
+         ctx = MemoryFileFactory.find("aopdomain");
          assertNull(ctx);
       }
       finally
       {
-         mfactory.deleteRoot(root);
+         MemoryFileFactory.deleteRoot(root);
       }
    }
    
    public void testMultipleFiles() throws Exception
    {
-      MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
       URL root = new URL("vfsmemory://aopdomain");
       try
       {
-         VFSContext ctx = mfactory.createRoot(root);
-         
+         VFS ctx = MemoryFileFactory.createRoot(root);
+
          URL urlA = new URL("vfsmemory://aopdomain/org/acme/test/Test.class");
-         mfactory.putFile(urlA,  new byte[] {'a', 'b', 'c'});
+         MemoryFileFactory.putFile(urlA,  new byte[] {'a', 'b', 'c'});
          
          URL urlB = new URL("vfsmemory://aopdomain/org/foo/test/Test.class");
-         mfactory.putFile(urlB,  new byte[] {'d', 'e', 'f'});
+         MemoryFileFactory.putFile(urlB,  new byte[] {'d', 'e', 'f'});
          
          String readA = readURL(urlA);
          assertEquals("abc", readA);
@@ -174,25 +173,25 @@
       }
       finally
       {
-         mfactory.deleteRoot(root);
+         MemoryFileFactory.deleteRoot(root);
       }
    }
 
    public void testNavigate() throws Exception
    {
-      MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
       URL root = new URL("vfsmemory://aopdomain");
       try
       {
-         VFSContext ctx = mfactory.createRoot(root);
+         MemoryFileFactory.createRoot(root);
+
          URL url = new URL("vfsmemory://aopdomain/org/acme/test/Test.class");
-         mfactory.putFile(url,  new byte[] {'a', 'b', 'c'});
+         MemoryFileFactory.putFile(url,  new byte[] {'a', 'b', 'c'});
          URL url2 = new URL("vfsmemory://aopdomain/org/acme/test/Test2.class");
-         mfactory.putFile(url2,  new byte[] {'a', 'b', 'c'});
+         MemoryFileFactory.putFile(url2,  new byte[] {'a', 'b', 'c'});
          URL url3 = new URL("vfsmemory://aopdomain/org/acme/test/Test3.class");
-         mfactory.putFile(url3,  new byte[] {'a', 'b', 'c'});
+         MemoryFileFactory.putFile(url3,  new byte[] {'a', 'b', 'c'});
          
-         VFS vfs = ctx.getVFS();
+         VFS vfs = MemoryFileFactory.createRoot(root);
          VirtualFile file = vfs.getVirtualFile(root, "/org/acme/test/Test.class");
          assertNotNull(file);
          
@@ -224,24 +223,23 @@
       }
       finally
       {
-         mfactory.deleteRoot(root);
+         MemoryFileFactory.deleteRoot(root);
       }
    }
 
    public void testLeaf() throws Exception
    {
-      MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
       URL root = new URL("vfsmemory://aopdomain");
       try
       {
-         VFSContext ctx = mfactory.createRoot(root);
+         VFS ctx = MemoryFileFactory.createRoot(root);
          URL url = new URL("vfsmemory://aopdomain/org/acme/leaf");
-         mfactory.putFile(url,  new byte[] {'a', 'b', 'c'});
+         MemoryFileFactory.putFile(url,  new byte[] {'a', 'b', 'c'});
 
          URL url2 = new URL("vfsmemory://aopdomain/org/acme/leaf/shouldnotwork");
          try
          {
-            mfactory.putFile(url2,  new byte[] {'d', 'e', 'f'});
+            MemoryFileFactory.putFile(url2,  new byte[] {'d', 'e', 'f'});
             fail("It should not have been possible to add a child to a leaf node");
          }
          catch(Exception e)
@@ -263,7 +261,7 @@
          try
          {
             URL url3 = new URL("vfsmemory://aopdomain/org/acme");
-            mfactory.putFile(url3, new byte[] {'1', '2', '3'});
+            MemoryFileFactory.putFile(url3, new byte[] {'1', '2', '3'});
             fail("Should not have been possible to set contents for a non-leaf node");
          }
          catch (Exception expected)
@@ -273,7 +271,7 @@
          try
          {
             URL url4 = new URL("vfsmemory://aopdomain/org");
-            mfactory.putFile(url4, new byte[] {'1', '2', '3'});
+            MemoryFileFactory.putFile(url4, new byte[] {'1', '2', '3'});
             fail("Should not have been possible to set contents for a non-leaf node");
          }
          catch (Exception expected)
@@ -282,7 +280,7 @@
       }
       finally
       {
-         mfactory.deleteRoot(root);
+         MemoryFileFactory.deleteRoot(root);
       }
    }
    
@@ -337,5 +335,4 @@
          }
       }      
    }
-   
 }




More information about the jboss-cvs-commits mailing list