[jboss-cvs] JBossAS SVN: r66195 - in projects/vfs/trunk/src: main/java/org/jboss/virtual/plugins/context/memory and 4 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Oct 16 17:39:04 EDT 2007
Author: kabir.khan at jboss.com
Date: 2007-10-16 17:39:04 -0400 (Tue, 16 Oct 2007)
New Revision: 66195
Added:
projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/
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/plugins/context/memory/MemoryContextHandler.java
projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsmemory/
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
Modified:
projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContextFactoryLocator.java
Log:
Add a memory vfs uri handler
Added: 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 (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContext.java 2007-10-16 21:39:04 UTC (rev 66195)
@@ -0,0 +1,130 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.plugins.context.memory;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.jboss.virtual.VFSUtils;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.plugins.context.AbstractVFSContext;
+import org.jboss.virtual.plugins.vfs.helpers.PathTokenizer;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class MemoryContext extends AbstractVFSContext
+{
+ private static final long serialVersionUID = 1L;
+ /** The root file */
+ private final MemoryContextHandler root;
+
+ /** A reference to the virtual file of the root to stop it getting closed */
+ private final VirtualFile rootFile;
+
+ protected MemoryContext(URL url) throws URISyntaxException
+ {
+ super(url);
+ root = new MemoryContextHandler(this, null, url, url.getFile());
+ rootFile = root.getVirtualFile();
+ }
+
+
+ public VirtualFileHandler getRoot() throws IOException
+ {
+ return root;
+ }
+
+ VirtualFileHandler createDirectory(URL url)
+ {
+ return putFile(url, null);
+ }
+
+ VirtualFileHandler putFile(URL url, byte[] contents)
+ {
+ try
+ {
+ URI uri = VFSUtils.toURI(url);
+
+
+ String[] tokens = PathTokenizer.getTokens(url.getPath());
+ if (tokens == null || tokens.length == 0)
+ {
+ return null;
+ }
+
+ boolean definitelyNew = false;
+ String protocolAndHost = url.getProtocol() + "://" + url.getHost();
+ StringBuffer path = new StringBuffer(protocolAndHost);
+ MemoryContextHandler current = root;
+ for (int i = 0 ; i < tokens.length ; i++)
+ {
+ path.append("/");
+ path.append(tokens[i]);
+
+ if (!definitelyNew)
+ {
+ try
+ {
+ MemoryContextHandler child = current.getDirectChild(tokens[i]);
+ if (child != null)
+ {
+ current = child;
+ continue;
+ }
+ }
+ catch(Exception ignore)
+ {
+ }
+ definitelyNew = true;
+ }
+
+ URL localUrl = new URL(path.toString());
+ if (current.contents != null)
+ {
+ throw new IllegalStateException("Cannot add a child to " + current + " it already has contents");
+ }
+ current = new MemoryContextHandler(this, current, localUrl, tokens[i]);
+ }
+
+ current.setContents(contents);
+ return current;
+ }
+ catch(MalformedURLException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (URISyntaxException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
Added: 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 (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextFactory.java 2007-10-16 21:39:04 UTC (rev 66195)
@@ -0,0 +1,209 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.plugins.context.memory;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.spi.VFSContextFactory;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+/**
+ * Singelton implementation of a MemoryContextFactory.
+ * The roots are indexed as the 'host' part of the URLs they are stored under
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class MemoryContextFactory implements VFSContextFactory
+{
+ private static final String[] PROTOCOLS = {"vfsmemory"};
+
+ private static MemoryContextFactory instance = new MemoryContextFactory();
+ private ConcurrentHashMap<String, MemoryContext> registry = new ConcurrentHashMap<String, MemoryContext>();
+
+ private MemoryContextFactory()
+ {
+
+ }
+
+ /**
+ * Gets the singleton instance
+ * @return The singleton instance
+ */
+ public static MemoryContextFactory getInstance()
+ {
+ return instance;
+ }
+
+ public String[] getProtocols()
+ {
+ return PROTOCOLS;
+ }
+
+ public VFSContext getVFS(URL rootURL) throws IOException
+ {
+ return createRoot(rootURL);
+ }
+
+ public VFSContext getVFS(URI rootURI) throws IOException
+ {
+ return createRoot(rootURI.toURL());
+ }
+
+ /**
+ * 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
+ */
+ public MemoryContext find(String host)
+ {
+ return registry.get(host);
+ }
+
+ /**
+ * Creates a new root MemoryContext, or returns an already exixting one of one already
+ * exists for the name
+ * @param url The url of the root, we use the 'host' part of the name for indexing the context
+ * @return The found or created context
+ * @throws IllegalArgumentException If the url parameter contains a path
+ */
+ public VFSContext createRoot(URL url)
+ {
+ try
+ {
+ if (url.getPath() != null && url.getPath().length() > 0)
+ {
+ throw new IllegalArgumentException("Root can not contain '/'");
+ }
+
+ String rootName = url.getHost();
+ MemoryContext ctx = registry.get(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)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (URISyntaxException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Creates a 'directory' within the context determined by the url host part
+ * @param URL url The url of the directory we want tot create
+ * @return The created directory
+ * @throws IllegalArgumentException if there is no root matching the host part of the url
+ */
+ public VirtualFileHandler createDirectory(URL url)
+ {
+ String rootName = url.getHost();
+ MemoryContext ctx = registry.get(rootName);
+ if (ctx == null)
+ {
+ throw new IllegalArgumentException("No MemoryContext exists for " + rootName);
+ }
+
+ return ctx.createDirectory(url);
+ }
+
+ /**
+ * Creates a 'file' within the context determined by the url host part
+ * @param URL url The url of the directory we want tot create
+ * @param byte[] contents The contents of the file
+ * @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)
+ {
+ String rootName = url.getHost();
+ MemoryContext ctx = registry.get(rootName);
+ if (ctx == null)
+ {
+ throw new RuntimeException("No MemoryContext exists for " + rootName);
+ }
+
+ return ctx.putFile(url, contents);
+ }
+
+ /**
+ * Deletes a root MemoryContext
+ * @param The url of the root context we want to delete
+ * @return true if we deleted a root MemoryContext, false otherwise
+ * @throws IllegalArgumentException If the url parameter contains a path
+ */
+ public boolean deleteRoot(URL url)
+ {
+ if (url.getPath() != null && url.getPath().length() > 0)
+ {
+ throw new IllegalArgumentException("Root can not contain '/'");
+ }
+
+ String rootName = url.getHost();
+ return (registry.remove(rootName) != null);
+ }
+
+ /**
+ * Deletes a 'file' or a 'directory'
+ * @param The url of the 'file' or 'directory' we want to delete
+ * @return true if we deleted a 'file' or 'directory', false otherwise
+ */
+ public boolean delete(URL url)
+ {
+ try
+ {
+ if (url.getPath() == null || url.getPath().length() == 0)
+ {
+ return deleteRoot(url);
+ }
+
+ String rootName = url.getHost();
+ MemoryContext ctx = registry.get(rootName);
+ if (ctx != null)
+ {
+ MemoryContextHandler child = (MemoryContextHandler)ctx.findChild(ctx.getRoot(), url.getPath());
+ MemoryContextHandler parent = (MemoryContextHandler)child.getParent();
+ return parent.deleteChild(child);
+ }
+ return false;
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java 2007-10-16 21:39:04 UTC (rev 66195)
@@ -0,0 +1,146 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.plugins.context.memory;
+
+import java.io.ByteArrayInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jboss.virtual.plugins.context.AbstractURLHandler;
+import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
+import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class MemoryContextHandler extends AbstractURLHandler implements StructuredVirtualFileHandler
+{
+ /** serialVersionUID */
+ private static final long serialVersionUID = 1L;
+
+ private transient List<VirtualFileHandler> entryChildren = Collections.EMPTY_LIST;
+ private transient Map<String, MemoryContextHandler> entryMap = Collections.EMPTY_MAP;
+ byte[] contents;
+
+
+ public MemoryContextHandler(VFSContext context, VirtualFileHandler parent, URL url, String name)
+ {
+ super(context, parent, url, name);
+ if (parent != null)
+ {
+ ((MemoryContextHandler)parent).addChild(name, this);
+ }
+ }
+
+ private void addChild(String name, MemoryContextHandler child)
+ {
+ if (entryChildren == Collections.EMPTY_LIST)
+ {
+ entryChildren = new ArrayList<VirtualFileHandler>();
+ }
+ if (entryMap == Collections.EMPTY_MAP)
+ {
+ entryMap = new HashMap<String, MemoryContextHandler>();
+ }
+ entryChildren.add(child);
+ entryMap.put(name, child);
+ }
+
+ boolean deleteChild(MemoryContextHandler child)
+ {
+ boolean removedA = entryMap.remove(child.getName()) != null;
+ boolean removedB = entryChildren.remove(child);
+
+ return removedA && removedB;
+ }
+
+ public VirtualFileHandler findChild(String path) throws IOException
+ {
+ return structuredFindChild(path);
+ }
+
+ MemoryContextHandler getDirectChild(String name)
+ {
+ return entryMap.get(name);
+ }
+
+ public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
+ {
+ return entryChildren;
+ }
+
+ public boolean isLeaf() throws IOException
+ {
+ return contents != null;
+ }
+
+ /**
+ * Called by structuredFindChild
+ */
+ public VirtualFileHandler createChildHandler(String name) throws IOException
+ {
+ VirtualFileHandler child = entryMap.get(name);
+ if( child == null )
+ throw new FileNotFoundException(this+" has no child: "+name);
+ return child;
+ }
+
+ public boolean exists() throws IOException
+ {
+ return true;
+ }
+
+ public void setContents(byte[] contents)
+ {
+ if (entryChildren.size() > 0)
+ {
+ throw new RuntimeException("Cannot set contents for non-leaf node");
+ }
+ this.contents = contents;
+ }
+
+ protected void initCacheLastModified()
+ {
+ this.cachedLastModified = System.currentTimeMillis();
+ }
+
+ public InputStream openStream() throws IOException
+ {
+ if (contents != null)
+ {
+ return new ByteArrayInputStream(contents);
+ }
+ return new ByteArrayInputStream(new byte[0]);
+ }
+
+}
Added: 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 (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsmemory/Handler.java 2007-10-16 21:39:04 UTC (rev 66195)
@@ -0,0 +1,87 @@
+/*
+* 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.protocol.vfsmemory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+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>
+ * @version $Revision: 1.1 $
+ */
+public class Handler extends URLStreamHandler
+{
+ protected URLConnection openConnection(URL u) throws IOException
+ {
+ String host = u.getHost();
+ MemoryContext ctx = MemoryContextFactory.getInstance().find(host);
+
+ if (ctx == null) throw new IOException("vfs does not exist: " + u.toString());
+ VirtualFile vf = ctx.findChild(ctx.getRoot(), u.getPath()).getVirtualFile();
+ if (vf == null) throw new IOException("vfs does not exist: " + u.toString());
+ return new VirtualFileURLConnection(u, vf);
+
+ }
+ public static void main(String[] args) throws Exception
+ {
+ System.setProperty("java.protocol.handler.pkgs", "org.jboss.virtual.protocol");
+ //URL url = new URL("vfsfile:/c:/tmp/urlstream.java");
+ //URL url = new URL("vfsfile:/C:\\jboss\\jboss-head\\build\\output\\jboss-5.0.0.Beta\\server\\default\\lib\\jboss.jar\\schema\\xml.xsd");
+// URL url = new URL("vfsjar:file:/c:/tmp/parent.jar!/foo.jar/urlstream.java");
+
+ URL rootURL = new URL("vfsmemory://aopdomain2");
+ MemoryContextFactory.getInstance().createRoot(rootURL);
+
+ URL url = new URL("vfsmemory://aopdomain2/org/foo/Test.class");
+ MemoryContextFactory.getInstance().putFile(url, new byte[] {'a', 'b', 'c'});
+ URL url2 = new URL("vfsmemory://aopdomain2/org/bar/Test.class");
+ MemoryContextFactory.getInstance().putFile(url2, new byte[] {'d', 'e', 'f'});
+
+ System.out.println("---------");
+ InputStream is = url.openStream();
+ while (is.available() != 0)
+ {
+ System.out.print((char)is.read());
+ }
+ is.close();
+
+ System.out.println("---------");
+ MemoryContextFactory.getInstance().createRoot(rootURL);
+ InputStream is2 = url2.openStream();
+ while (is2.available() != 0)
+ {
+ System.out.print((char)is2.read());
+ }
+ is.close();
+
+ }
+}
Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContextFactoryLocator.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContextFactoryLocator.java 2007-10-16 19:10:44 UTC (rev 66194)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VFSContextFactoryLocator.java 2007-10-16 21:39:04 UTC (rev 66195)
@@ -39,6 +39,7 @@
import org.jboss.logging.Logger;
import org.jboss.virtual.plugins.context.file.FileSystemContextFactory;
import org.jboss.virtual.plugins.context.jar.JarContextFactory;
+import org.jboss.virtual.plugins.context.memory.MemoryContextFactory;
import org.jboss.virtual.plugins.context.VfsArchiveBrowserFactory;
import org.jboss.util.file.ArchiveBrowser;
@@ -244,6 +245,9 @@
// No jar protocol, use the default
if (factoryByProtocol.containsKey("jar") == false)
registerFactory(new JarContextFactory());
+
+ if (factoryByProtocol.containsKey("vfsmemory") == false)
+ registerFactory(MemoryContextFactory.getInstance());
initialized = true;
}
Added: 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 (rev 0)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/MemoryTestCase.java 2007-10-16 21:39:04 UTC (rev 66195)
@@ -0,0 +1,185 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.virtual.test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URL;
+
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.plugins.context.memory.MemoryContextFactory;
+import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.spi.VFSContextFactory;
+import org.jboss.virtual.spi.VFSContextFactoryLocator;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class MemoryTestCase extends TestCase
+{
+ public void testContextFactory()throws Exception
+ {
+ URI uri = new URI("vfsmemory://aopdomain");
+ VFSContextFactory factory = VFSContextFactoryLocator.getFactory(uri);
+ assertNotNull(factory);
+ }
+
+ public void testContext() throws Exception
+ {
+ URI uri = new URI("vfsmemory://aopdomain");
+ VFSContextFactory factory = VFSContextFactoryLocator.getFactory(uri);
+ VFSContext ctx = factory.getVFS(uri);
+ assertNotNull(ctx);
+
+ MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
+ assertNotNull(mfactory);
+ assertSame(factory, mfactory);
+
+ VFSContext mctx = mfactory.createRoot(uri.toURL());
+ assertNotNull(mctx);
+ assertSame(ctx, mctx);
+ }
+
+ public void testWriteAndReadData() throws Exception
+ {
+ MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
+ URL root = new URL("vfsmemory://aopdomain");
+ try
+ {
+ VFSContext ctx = mfactory.createRoot(root);
+ URL url = new URL("vfsmemory://aopdomain/org/acme/test/Test.class");
+ mfactory.putFile(url, new byte[] {'a', 'b', 'c'});
+
+ String read = readURL(url);
+ assertEquals("abc", read);
+
+ assertTrue(mfactory.delete(url));
+ try
+ {
+ InputStream is = url.openStream();
+ fail("Should not have found file");
+ }
+ catch(Exception expected)
+ {
+ }
+
+ ctx = mfactory.find("aopdomain");
+ assertNotNull(ctx);
+
+ assertTrue(mfactory.deleteRoot(root));
+ ctx = mfactory.find("aopdomain");
+ assertNull(ctx);
+ }
+ finally
+ {
+ mfactory.deleteRoot(root);
+ }
+ }
+
+ public void testMultipleFiles() throws Exception
+ {
+ MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
+ URL root = new URL("vfsmemory://aopdomain");
+ try
+ {
+ VFSContext ctx = mfactory.createRoot(root);
+
+ URL urlA = new URL("vfsmemory://aopdomain/org/acme/test/Test.class");
+ mfactory.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'});
+
+ String readA = readURL(urlA);
+ assertEquals("abc", readA);
+
+ String readB = readURL(urlB);
+ assertEquals("def", readB);
+ }
+ finally
+ {
+ mfactory.deleteRoot(root);
+ }
+ }
+
+ public void testNavigate() throws Exception
+ {
+ MemoryContextFactory mfactory = MemoryContextFactory.getInstance();
+ URL root = new URL("vfsmemory://aopdomain");
+ try
+ {
+ VFSContext ctx = mfactory.createRoot(root);
+ URL url = new URL("vfsmemory://aopdomain/org/acme/test/Test.class");
+ mfactory.putFile(url, new byte[] {'a', 'b', 'c'});
+
+ VFS vfs = ctx.getVFS();
+ VirtualFile file = vfs.getVirtualFile(root, "/org/acme/test/Test.class");
+ assertNotNull(file);
+
+ VirtualFile file2 = vfs.getVirtualFile(root, "/org");
+ assertNotNull(file2);
+ VirtualFile file3 = file2.findChild("/acme/test/Test.class");
+ assertNotNull(file3);
+
+ assertSame(file.getHandler(), file3.getHandler());
+ }
+ finally
+ {
+ mfactory.deleteRoot(root);
+ }
+ }
+
+ private String readURL(URL url) throws IOException
+ {
+ InputStream is = null;
+ try
+ {
+ is = url.openStream();
+ StringBuffer sb = new StringBuffer();
+ while (is.available() != 0)
+ {
+ sb.append((char)is.read());
+ }
+ return sb.toString();
+ }
+ finally
+ {
+ if (is != null)
+ {
+ try
+ {
+ is.close();
+ }
+ catch(Exception ignore)
+ {
+ }
+ }
+ }
+ }
+
+}
More information about the jboss-cvs-commits
mailing list