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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Apr 2 20:54:34 EDT 2007


Author: bill.burke at jboss.com
Date: 2007-04-02 20:54:33 -0400 (Mon, 02 Apr 2007)
New Revision: 62010

Added:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContext.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContextFactory.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectory.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledFileHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledUrlStreamHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileUrlStreamHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfs/
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfs/Handler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/AssembledContextTest.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileURLConnection.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java
Log:
Added new context type.  "vfs".  "vfs" is a true virtual file assembly.  Allows you to constructure a file system programmatically from any abitrary file or resource.

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java	2007-04-02 22:41:48 UTC (rev 62009)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/VFS.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -65,7 +65,9 @@
          System.setProperty("java.protocol.handler.pkgs", pkgs);
       }
       // keep this until AOP and HEM uses VFS internally instead of the stupid ArchiveBrowser crap.
-      ArchiveBrowser.factoryFinder.put("vfsfile", new VfsArchiveBrowserFactory());      
+      ArchiveBrowser.factoryFinder.put("vfsfile", new VfsArchiveBrowserFactory());
+      ArchiveBrowser.factoryFinder.put("vfsjar", new VfsArchiveBrowserFactory());
+      ArchiveBrowser.factoryFinder.put("vfs", new VfsArchiveBrowserFactory());      
    }
 
    /**

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java	2007-04-02 22:41:48 UTC (rev 62009)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -210,7 +210,7 @@
     * 
     * @return the resulting count
     */
-   private int increment()
+   protected int increment()
    {
       return references.incrementAndGet();
    }
@@ -220,7 +220,7 @@
     * 
     * @return the resulting count
     */
-   private int decrement()
+   protected int decrement()
    {
       return references.decrementAndGet();
    }

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContext.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContext.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,60 @@
+/*
+* 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.plugins.context.vfs;
+
+import org.jboss.virtual.plugins.context.AbstractVFSContext;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+import java.net.URISyntaxException;
+import java.net.URI;
+import java.io.IOException;
+
+/**
+ * JarContext.
+ *
+ * @author Scott.Stark at jboss.org
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class AssembledContext extends AbstractVFSContext
+{
+   private String name;
+   /** The root file */
+   private final VirtualFileHandler root;
+
+   public AssembledContext(String name, String rootName) throws IOException, URISyntaxException
+   {
+      super(new URI("vfs", name, null, null));
+      this.name = name;
+      root = new AssembledDirectoryHandler(this, null, rootName);
+   }
+
+   public VirtualFileHandler getRoot() throws IOException
+   {
+      return root;
+   }
+
+   public String getName()
+   {
+      return name;
+   }
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContextFactory.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContextFactory.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledContextFactory.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,93 @@
+/*
+* 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.plugins.context.vfs;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.net.URISyntaxException;
+import java.io.IOException;
+
+/**
+ * comment
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @version $Revision: 1.1 $
+ */
+public class AssembledContextFactory
+{
+   private ConcurrentHashMap<String, AssembledDirectory> registry = new ConcurrentHashMap<String, AssembledDirectory>();
+   private volatile int count;
+   private static  AssembledContextFactory singleton = new AssembledContextFactory();
+
+   public AssembledDirectory create(String name, String rootName)
+   {
+      if (registry.containsKey(name)) throw new RuntimeException("Assembled context already exists for name: " + name);
+      try
+      {
+         AssembledContext context = new AssembledContext(name, rootName);
+         AssembledDirectory directory = (AssembledDirectory)context.getRoot().getVirtualFile();
+         registry.put(name, directory);
+         return directory;
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+      catch (URISyntaxException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public AssembledDirectory find(String name)
+   {
+      return registry.get(name);
+   }
+
+   public AssembledDirectory create(String rootName)
+   {
+      String name = "" + System.currentTimeMillis() + "" + count++;
+      return create(name, rootName);
+   }
+
+   public void remove(AssembledDirectory directory)
+   {
+      try
+      {
+         if (directory.getParent() != null) throw new RuntimeException("This is not the root of assembly");
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+      registry.remove(((AssembledContext)directory.getHandler().getVFSContext()).getName());
+   }
+
+   public static AssembledContextFactory getSingleton()
+   {
+      return singleton;
+   }
+
+   public static void setSingleton(AssembledContextFactory singleton)
+   {
+      AssembledContextFactory.singleton = singleton;
+   }
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectory.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectory.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectory.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,300 @@
+/*
+* 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.plugins.context.vfs;
+
+import org.jboss.virtual.VFS;
+import org.jboss.virtual.VirtualFile;
+import org.jboss.virtual.VirtualFileFilter;
+import org.jboss.virtual.VisitorAttributes;
+import org.jboss.virtual.plugins.context.jar.JarUtils;
+import org.jboss.virtual.plugins.vfs.helpers.FilterVirtualFileVisitor;
+import org.jboss.virtual.plugins.vfs.helpers.SuffixesExcludeFilter;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.regex.Pattern;
+import java.util.List;
+
+/**
+ * comment
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @version $Revision: 1.1 $
+ */
+public class AssembledDirectory extends VirtualFile
+{
+   private AssembledDirectoryHandler directory;
+
+   public AssembledDirectory(VirtualFileHandler handler)
+   {
+      super(handler);
+      directory = (AssembledDirectoryHandler) handler;
+   }
+
+   public void addClass(Class clazz)
+   {
+      addClass(clazz.getName(), clazz.getClassLoader());
+   }
+
+   public void addClass(String className)
+   {
+      addClass(className, Thread.currentThread().getContextClassLoader());
+   }
+
+   public void addClass(String className, ClassLoader loader)
+   {
+      String resource = className.replace('.', '/') + ".class";
+      URL url = loader.getResource(resource);
+      if (url == null) throw new RuntimeException("Could not find resource: " + resource);
+      AssembledDirectory p = mkdirs(resource);
+      p.addResource(resource, loader);
+   }
+
+   /**
+    * Make any directories for the give path to a file.
+    *
+    * @param path must be a path to a file as last element in path does not have a directory created
+    * @return directory file will live in
+    */
+   public AssembledDirectory mkdirs(String path)
+   {
+      String[] pkgs = path.split("/");
+      AssembledDirectoryHandler dir = directory;
+      for (int i = 0; i < pkgs.length - 1; i++)
+      {
+         AssembledDirectoryHandler next = (AssembledDirectoryHandler) dir.getChild(pkgs[i]);
+         if (next == null)
+         {
+            try
+            {
+               next = new AssembledDirectoryHandler((AssembledContext) dir.getVFSContext(), dir, pkgs[i]);
+            }
+            catch (IOException e)
+            {
+               throw new RuntimeException(e);
+            }
+            dir.addChld(next);
+         }
+         dir = next;
+      }
+      AssembledDirectory p = (AssembledDirectory) dir.getVirtualFile();
+      return p;
+   }
+
+   public void addResources(Class baseResource, String[] includes, String[] excludes)
+   {
+      String resource = baseResource.getName().replace('.', '/') + ".class";
+      addResources(resource, includes, excludes);
+   }
+
+   public void addResources(String baseResource, final String[] includes, final String[] excludes)
+   {
+      addResources(baseResource, includes, excludes, Thread.currentThread().getContextClassLoader());   
+   }
+
+   public void addResources(String baseResource, final String[] includes, final String[] excludes, ClassLoader loader)
+   {
+      URL url = loader.getResource(baseResource);
+      if (url == null) throw new RuntimeException("Could not find baseResource: " + baseResource);
+      String urlString = url.toString();
+      int idx = urlString.lastIndexOf(baseResource);
+      urlString = urlString.substring(0, idx);
+      try
+      {
+         url = new URL(urlString);
+         VirtualFile parent = VFS.getRoot(url);
+
+         VisitorAttributes va = new VisitorAttributes();
+         va.setLeavesOnly(true);
+         SuffixesExcludeFilter noJars = new SuffixesExcludeFilter(JarUtils.getSuffixes());
+         va.setRecurseFilter(noJars);
+
+         VirtualFileFilter filter = new VirtualFileFilter()
+         {
+
+            public boolean accepts(VirtualFile file)
+            {
+               boolean matched = false;
+               String path = file.getPathName();
+               for (String include : includes)
+               {
+                  if (antMatch(path, include))
+                  {
+                     matched = true;
+                     break;
+                  }
+               }
+               if (!matched) return false;
+               if (excludes != null)
+               {
+                  for (String exclude : excludes)
+                  {
+                     if (antMatch(path, exclude)) return false;
+                  }
+               }
+               return true;
+            }
+
+         };
+
+         FilterVirtualFileVisitor visitor = new FilterVirtualFileVisitor(filter, va);
+         parent.visit(visitor);
+         List<VirtualFile> files = visitor.getMatched();
+         for (VirtualFile vf : files)
+         {
+            mkdirs(vf.getPathName()).addChild(vf);
+         }
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public static Pattern getPattern(String matcher)
+   {
+      matcher = matcher.replace(".", "\\.");
+      matcher = matcher.replace("*", ".*");
+      matcher = matcher.replace("?", ".{1}");
+      return Pattern.compile(matcher);
+
+   }
+
+   public static boolean antMatch(String path, String expression)
+   {
+      if (path.startsWith("/")) path = path.substring(1);
+      if (expression.endsWith("/")) expression += "**";
+      String[] paths = path.split("/");
+      String[] expressions = expression.split("/");
+
+      int x = 0, p = 0;
+      Pattern pattern = getPattern(expressions[0]);
+
+      for (p = 0; p < paths.length && x < expressions.length; p++)
+      {
+         if (expressions[x].equals("**"))
+         {
+            do
+            {
+               x++;
+            } while (x < expressions.length && expressions[x].equals("**"));
+            if (x == expressions.length) return true; // "**" with nothing after it
+            pattern = getPattern(expressions[x]);
+         }
+         String element = paths[p];
+         if (pattern.matcher(element).matches())
+         {
+            x++;
+            if (x < expressions.length)
+            {
+               pattern = getPattern(expressions[x]);
+            }
+         }
+         else if (!(x > 0 && expressions[x - 1].equals("**"))) // our previous isn't "**"
+         {
+            return false;
+         }
+      }
+      if (p < paths.length) return false;
+      if (x < expressions.length) return false;
+      return true;
+   }
+
+   public void addChild(VirtualFile vf)
+   {
+      directory.addChld(vf.getHandler());
+   }
+
+   public void addChild(VirtualFile vf, String newName)
+   {
+      try
+      {
+         directory.addChld(new AssembledFileHandler((AssembledContext) directory.getVFSContext(), directory, newName, vf.getHandler()));
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public void addResource(String resource)
+   {
+      addResource(resource, Thread.currentThread().getContextClassLoader());
+   }
+
+   public void addResource(String resource, String newName)
+   {
+      addResource(resource, Thread.currentThread().getContextClassLoader(), newName);
+   }
+
+   public void addResource(String resource, ClassLoader loader)
+   {
+      URL url = loader.getResource(resource);
+      if (url == null) throw new RuntimeException("Could not find resource: " + resource);
+
+      addResource(url);
+   }
+
+   public void addResource(URL url)
+   {
+      try
+      {
+         VirtualFile vf = VFS.getRoot(url);
+         addChild(vf);
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public void addResource(String resource, ClassLoader loader, String newName)
+   {
+      URL url = loader.getResource(resource);
+      if (url == null) throw new RuntimeException("Could not find resource: " + resource);
+      try
+      {
+         VirtualFile vf = VFS.getRoot(url);
+         addChild(vf, newName);
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
+
+   public AssembledDirectory mkdir(String name)
+   {
+      AssembledDirectoryHandler handler = null;
+      try
+      {
+         handler = new AssembledDirectoryHandler((AssembledContext) directory.getVFSContext(), directory, name);
+         directory.addChld(handler);
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+      return new AssembledDirectory(handler);
+   }
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledDirectoryHandler.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,151 @@
+/*
+* 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.plugins.context.vfs;
+
+import org.jboss.virtual.plugins.context.AbstractVirtualFileHandler;
+import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
+import org.jboss.virtual.spi.VirtualFileHandler;
+import org.jboss.virtual.VirtualFile;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+
+/**
+ * comment
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @version $Revision: 1.1 $
+ */
+public class AssembledDirectoryHandler extends AbstractVirtualFileHandler implements StructuredVirtualFileHandler
+{
+   private long lastModified = System.currentTimeMillis();
+   private List<VirtualFileHandler> children = new ArrayList<VirtualFileHandler>();
+   private Map<String, VirtualFileHandler> childrenMap = new HashMap<String, VirtualFileHandler>();
+
+
+   public AssembledDirectoryHandler(AssembledContext context, AssembledDirectoryHandler parent, String name) throws IOException
+   {
+      super(context, parent, name);
+      String path = getPathName();
+      if (!path.endsWith("/")) path += "/";
+      vfsUrl = new URL("vfs", context.getName(), -1, path, new AssembledUrlStreamHandler(context));
+   }
+
+   public void addChld(VirtualFileHandler handler)
+   {
+      if (!(handler instanceof AssembledDirectoryHandler || handler instanceof AssembledFileHandler))
+      {
+         try
+         {
+            handler = new AssembledFileHandler((AssembledContext)getVFSContext(), this, handler.getName(), handler);
+         }
+         catch (IOException e)
+         {
+            throw new RuntimeException(e);
+         }
+      }
+      children.add(handler);
+      childrenMap.put(handler.getName(), handler);
+      lastModified = System.currentTimeMillis();
+   }
+
+   public VirtualFileHandler getChild(String name)
+   {
+      return childrenMap.get(name);
+   }
+
+   public URI toURI() throws URISyntaxException
+   {
+      return vfsUrl.toURI();
+   }
+
+   public long getLastModified() throws IOException
+   {
+      return lastModified;
+   }
+
+   public long getSize() throws IOException
+   {
+      return 0;
+   }
+
+   public boolean exists() throws IOException
+   {
+      return true;
+   }
+
+   public boolean isLeaf() throws IOException
+   {
+      return false;
+   }
+
+   public boolean isHidden() throws IOException
+   {
+      return false;
+   }
+
+   public InputStream openStream() throws IOException
+   {
+      throw new RuntimeException("Cannot open stream");
+   }
+
+   public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
+   {
+      return children;
+   }
+
+
+   public VirtualFileHandler createChildHandler(String name) throws IOException
+   {
+      VirtualFileHandler handler = childrenMap.get(name);
+      if (handler == null) throw new IOException("Could not locate child: " + name);
+      return handler;
+   }
+
+   public VirtualFileHandler findChild(String path) throws IOException
+   {
+      return structuredFindChild(path);
+   }
+
+
+   @Override
+   public VirtualFile getVirtualFile()
+   {
+      checkClosed();
+      increment();
+      return new AssembledDirectory(this);
+   }
+
+   @Override
+   public URL toURL() throws MalformedURLException, URISyntaxException
+   {
+      return vfsUrl;
+   }
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledFileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledFileHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledFileHandler.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,117 @@
+/*
+* 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.plugins.context.vfs;
+
+import org.jboss.virtual.plugins.context.AbstractVirtualFileHandler;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+import java.net.URI;
+import java.util.List;
+
+/**
+ * comment
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @version $Revision: 1.1 $
+ */
+public class AssembledFileHandler extends AbstractVirtualFileHandler
+{
+   private VirtualFileHandler delegate;
+
+   public AssembledFileHandler(AssembledContext context, AssembledDirectoryHandler parent, String name, VirtualFileHandler delegate) throws IOException
+   {
+      super(context, parent, name);
+      this.delegate = delegate;
+      vfsUrl = new URL("vfs", context.getName(), -1, getPathName(), new AssembledUrlStreamHandler(context));
+
+   }
+
+
+   public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
+   {
+      throw new IOException("File cannot have children: " + this);
+   }
+
+   public VirtualFileHandler findChild(String path) throws IOException
+   {
+      throw new IOException("File cannot have children: " + this);
+   }
+
+   public URL toURL()
+           throws MalformedURLException, URISyntaxException
+   {
+      return delegate.toURL();
+   }
+
+   public long getLastModified()
+           throws IOException
+   {
+      return delegate.getLastModified();
+   }
+
+   public boolean hasBeenModified()
+           throws IOException
+   {
+      return delegate.hasBeenModified();
+   }
+
+   public long getSize()
+           throws IOException
+   {
+      return delegate.getSize();
+   }
+
+   public boolean exists()
+           throws IOException
+   {
+      return delegate.exists();
+   }
+
+   public boolean isLeaf()
+           throws IOException
+   {
+      return delegate.isLeaf();
+   }
+
+   public boolean isHidden()
+           throws IOException
+   {
+      return delegate.isHidden();
+   }
+
+   public InputStream openStream()
+           throws IOException
+   {
+      return delegate.openStream();
+   }
+
+   public URI toURI()
+           throws URISyntaxException
+   {
+      return delegate.toURI();
+   }
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledUrlStreamHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledUrlStreamHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/vfs/AssembledUrlStreamHandler.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,55 @@
+/*
+* 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.plugins.context.vfs;
+
+import org.jboss.virtual.spi.VirtualFileHandler;
+import org.jboss.virtual.plugins.vfs.VirtualFileURLConnection;
+
+import java.net.URLStreamHandler;
+import java.net.URLConnection;
+import java.net.URL;
+import java.io.IOException;
+
+/**
+ * Used when creating VFS urls so we don't have to go through the handlers all the time
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @version $Revision: 1.1 $
+ */
+public class AssembledUrlStreamHandler extends URLStreamHandler
+{
+   private final AssembledContext context;
+
+
+   public AssembledUrlStreamHandler(AssembledContext context)
+   {
+      this.context = context;
+   }
+
+   protected URLConnection openConnection(URL u) throws IOException
+   {
+      String path = u.getPath();
+      VirtualFileHandler vf = context.getRoot().findChild(path);
+      if (vf == null) throw new IOException(path + " was not found in Assembled VFS context " + context.getName());
+      return new VirtualFileURLConnection(u, vf.getVirtualFile());
+   }
+}

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileURLConnection.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileURLConnection.java	2007-04-02 22:41:48 UTC (rev 62009)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileURLConnection.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -54,6 +54,12 @@
       this.relativePath = relativePath;
    }
 
+   public VirtualFileURLConnection(URL url, VirtualFile file)
+   {
+      super(url);
+      this.file = file;
+   }
+
    public void connect() throws IOException
    {
    }

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileUrlStreamHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileUrlStreamHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/vfs/VirtualFileUrlStreamHandler.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,73 @@
+/*
+* 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.plugins.vfs;
+
+import org.jboss.virtual.spi.VirtualFileHandler;
+import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.VirtualFile;
+
+import java.net.URLStreamHandler;
+import java.net.URLConnection;
+import java.net.URL;
+import java.net.URISyntaxException;
+import java.io.IOException;
+
+/**
+ * Used when creating VFS urls so we don't have to go through the handlers all the time
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @version $Revision: 1.1 $
+ */
+public class VirtualFileUrlStreamHandler extends URLStreamHandler
+{
+   private final VFSContext context;
+
+
+   public VirtualFileUrlStreamHandler(VirtualFileHandler handler)
+   {
+      this.context = handler.getVFSContext();
+   }
+
+   protected URLConnection openConnection(URL u) throws IOException
+   {
+      String baseRootUrl = null;
+      try
+      {
+         baseRootUrl = context.getRoot().toVfsUrl().toString();
+      }
+      catch (URISyntaxException e)
+      {
+         throw new RuntimeException(e);
+      }
+      catch (IOException e)
+      {
+         throw new RuntimeException(e);
+      }
+      String urlString = u.toString();
+      int idx = urlString.indexOf(baseRootUrl);
+      if (idx == -1) throw new IOException(u.toString() + " does not belong to the same VFS context as " + baseRootUrl);
+      String path = urlString.substring(baseRootUrl.length());
+      VirtualFileHandler vf = context.getRoot().findChild(path);
+      if (vf == null) throw new IOException(path + " was not found in VFS context " + baseRootUrl);
+      return new VirtualFileURLConnection(u, vf.getVirtualFile());
+   }
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfs/Handler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfs/Handler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfs/Handler.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,52 @@
+/*
+* 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.vfs;
+
+import org.jboss.virtual.plugins.vfs.VirtualFileURLConnection;
+import org.jboss.virtual.plugins.context.vfs.AssembledContextFactory;
+import org.jboss.virtual.plugins.context.vfs.AssembledDirectory;
+import org.jboss.virtual.VirtualFile;
+
+import java.net.URLStreamHandler;
+import java.net.URLConnection;
+import java.net.URL;
+import java.io.IOException;
+
+/**
+ * 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();
+      AssembledDirectory directory = AssembledContextFactory.getSingleton().find(host);
+      if (directory == null) throw new IOException("vfs does not exist: " + u.toString());
+      VirtualFile vf = directory.findChild(u.getPath());
+      if (vf == null) throw new IOException("vfs does not exist: " + u.toString());
+      return new VirtualFileURLConnection(u, vf);
+   }
+
+}

Added: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/AssembledContextTest.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/AssembledContextTest.java	                        (rev 0)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/AssembledContextTest.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -0,0 +1,262 @@
+/*
+* 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.test.virtual.test;
+
+import junit.framework.TestCase;
+
+import java.util.regex.Pattern;
+import java.util.List;
+
+import org.jboss.virtual.plugins.context.vfs.AssembledDirectory;
+import org.jboss.virtual.plugins.context.vfs.AssembledContextFactory;
+import org.jboss.virtual.VirtualFile;
+
+/**
+ * comment
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @version $Revision: 1.1 $
+ */
+public class AssembledContextTest extends TestCase
+{
+   public void testRegex()
+   {
+      String[] files = {".java", "x.java", "FooBar.java"};
+      String expression = "*.java";
+      Pattern p = AssembledDirectory.getPattern(expression);
+      System.out.println("pattern: " + p.pattern());
+      for (String file : files)
+      {
+         assertTrue(p.matcher(file).matches());
+      }
+      System.out.println("no matches");
+      p  = AssembledDirectory.getPattern("?.java");
+      assertTrue(p.matcher("x.java").matches());
+      assertFalse(p.matcher("xyz.java").matches());
+      assertFalse(p.matcher(".java").matches());
+
+      p = AssembledDirectory.getPattern("x?z*.java");
+      assertTrue(p.matcher("xyz.java").matches());
+      assertTrue(p.matcher("xyzasdfasdf.java").matches());
+      assertFalse(p.matcher("xyzadasdfasdf").matches());
+      assertFalse(p.matcher("xzadasdfasdf").matches());
+      System.out.println("done it");
+   }
+
+   public void testAntMatching()
+   {
+      String file;
+      String exp;
+      file = "xabc/foobar/test.java";
+      exp = "?abc/*/*.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "abc/foobar/test.java";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+      file = "xabc/x/test.xml";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+      file = "xabc/test.java";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+
+
+      exp = "org/jboss/Test.java";
+      file = "org/jboss/Test.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+
+      exp = "org/jboss/Test.java";
+      file = "org/wrong.java";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+
+      exp = "test/**";
+      file = "test/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "test/foo/bar/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "x.java";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+
+      exp = "**/CVS/*";
+      file = "CVS/Repository";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "org/apache/CVS/Entries";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "org/apache/jakarta/tools/ant/CVS/Entries";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "org/apache/CVS/foo/bar/Entries";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+
+      exp = "org/apache/jakarta/**";
+      file ="org/apache/jakarta/tools/ant/docs/index.html";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file ="org/apache/jakarta/test.xml";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "org/apache/xyz.java";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+
+      exp = "org/apache/**/CVS/*";
+      file ="org/apache/CVS/Entries";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file ="org/apache/jakarta/tools/ant/CVS/Entries";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "org/apache/CVS/foo/bar/Entries";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+      file = "org/apache/nada/foo/bar/Entries";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+
+      exp = "**/test/**";
+      file = "test/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "test/bar/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "test/bar/foo/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "foo/test/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "foo/bar/test/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "foo/test/bar/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "foo/bar/test/bar/foo/x.java";
+      assertTrue(AssembledDirectory.antMatch(file, exp));
+      file = "foo/bar/flah.java";
+      assertFalse(AssembledDirectory.antMatch(file, exp));
+   }
+
+   public void testAddClass() throws Exception
+   {
+      AssembledDirectory directory = AssembledContextFactory.getSingleton().create("foo.jar");
+      directory.addClass(VirtualFile.class);
+
+
+      List<VirtualFile> children = directory.getChildren();
+      assertEquals(children.size(), 1);
+      VirtualFile curr = children.get(0);
+      System.out.println("test org/");
+      assertEquals("org", curr.getName());
+
+      System.out.println("test org/jboss");
+      children = curr.getChildren();
+      assertEquals(children.size(), 1);
+      curr = children.get(0);
+      assertEquals("jboss", curr.getName());
+
+      System.out.println("test org/jboss/virtual");
+      children = curr.getChildren();
+      assertEquals(children.size(), 1);
+      curr = children.get(0);
+      assertEquals("virtual", curr.getName());
+      children = curr.getChildren();
+      boolean found;
+      found = false;
+      for (VirtualFile child: children)
+      {
+         if (child.getName().equals("VirtualFile.class"))
+         {
+            found = true;
+            assertEquals("org/jboss/virtual/VirtualFile.class", child.getPathName());
+            break;
+         }
+      }
+      assertTrue("VirtualFile.class was found", found);
+   }
+
+   public void testAddResources() throws Exception
+   {
+      AssembledDirectory directory = AssembledContextFactory.getSingleton().create("foo.jar");
+      String[] includes = {"org/jboss/virtual/*.class", "org/jboss/virtual/**/context/jar/*.class"};
+      String[] excludes = {"**/Nested*"};
+      directory.addResources("org/jboss/virtual/VirtualFile.class", includes, excludes, Thread.currentThread().getContextClassLoader());
+      List<VirtualFile> children = directory.getChildren();
+      assertEquals(children.size(), 1);
+      VirtualFile curr = children.get(0);
+      System.out.println("test org/");
+      assertEquals("org", curr.getName());
+
+      System.out.println("test org/jboss");
+      children = curr.getChildren();
+      assertEquals(children.size(), 1);
+      curr = children.get(0);
+      assertEquals("jboss", curr.getName());
+
+      System.out.println("test org/jboss/virtual");
+      children = curr.getChildren();
+      assertEquals(children.size(), 1);
+      curr = children.get(0);
+      assertEquals("virtual", curr.getName());
+      children = curr.getChildren();
+      boolean found;
+      found = false;
+      for (VirtualFile child: children)
+      {
+         if (child.getName().equals("VFS.class"))
+         {
+            found = true;
+            break;
+         }
+      }
+      assertTrue("VFS.class was found", found);
+
+      found = false;
+      for (VirtualFile child: children)
+      {
+         if (child.getName().equals("VirtualFile.class"))
+         {
+            found = true;
+            assertEquals("org/jboss/virtual/VirtualFile.class", child.getPathName());
+            break;
+         }
+      }
+      assertTrue("VirtualFile.class was found", found);
+
+      found = false;
+      VirtualFile plugins = null;
+      for (VirtualFile child: children)
+      {
+         if (child.getName().equals("plugins"))
+         {
+            plugins = child;
+            found = true;
+            break;
+         }
+      }
+      assertTrue("plugins/", found);
+
+      System.out.println("Test org/jboss/virtual/plugins/context/jar");
+      VirtualFile jar = directory.findChild("org/jboss/virtual/plugins/context/jar");
+      assertNotNull(jar);
+      assertEquals("jar", jar.getName());
+
+      children = jar.getChildren();
+      for (VirtualFile child: children)
+      {
+         if (child.getName().startsWith("Nested")) throw new RuntimeException("did not exclude propertly");
+      }
+      AssembledContextFactory.getSingleton().remove(directory);
+   }
+
+   public void testMkDir() throws Exception
+   {
+      AssembledDirectory directory = AssembledContextFactory.getSingleton().create("foo.jar");
+      directory.mkdir("META-INF");
+      assertNotNull(directory.findChild("META-INF"));
+
+   }
+}

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java	2007-04-02 22:41:48 UTC (rev 62009)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/VFSAllTestSuite.java	2007-04-03 00:54:33 UTC (rev 62010)
@@ -55,7 +55,8 @@
       suite.addTest(FileVirtualFileHandlerUnitTestCase.suite());
       suite.addTest(JARVFSContextUnitTestCase.suite());
       suite.addTest(JARVirtualFileHandlerUnitTestCase.suite());
-      
+      suite.addTest(new TestSuite(AssembledContextTest.class));
+
       return suite;
    }
 }




More information about the jboss-cvs-commits mailing list