[Jboss-cvs] JBossAS SVN: r56853 - in projects/microcontainer/trunk/container/src: main/org/jboss/virtual/plugins/context/file tests/org/jboss/test/virtual/test

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Sep 14 11:59:05 EDT 2006


Author: scott.stark at jboss.org
Date: 2006-09-14 11:59:00 -0400 (Thu, 14 Sep 2006)
New Revision: 56853

Modified:
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/FileSystemContext.java
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/LinkHandler.java
   projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/TestFileVFS.java
Log:
First usable link implementation

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/FileSystemContext.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/FileSystemContext.java	2006-09-14 15:58:29 UTC (rev 56852)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/FileSystemContext.java	2006-09-14 15:59:00 UTC (rev 56853)
@@ -23,6 +23,7 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -198,7 +199,7 @@
       if (uri == null)
          throw new IllegalArgumentException("Null uri");
 
-      VirtualFileHandler handler;
+      VirtualFileHandler handler = null;
       if( VFSUtils.isLink(file.getName()) )
       {
          Properties props = new Properties();
@@ -227,8 +228,25 @@
             }
          }
       }
+      else if( file.exists() == false )
+      {
+         // See if we can resolve this to a link in the parent
+         List<VirtualFileHandler> children = parent.getChildren(true);
+         for(VirtualFileHandler vfh : children)
+         {
+            if( vfh.getName().equals(file.getName()) )
+            {
+               handler = vfh;
+               break;
+            }
+         }
+         if( handler == null )
+            throw new FileNotFoundException("File does not exist: " + file.getCanonicalPath());
+      }
       else
+      {
          handler = new FileHandler(this, parent, file, uri);
+      }
       return handler;
    }
    

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/LinkHandler.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/LinkHandler.java	2006-09-14 15:58:29 UTC (rev 56852)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/LinkHandler.java	2006-09-14 15:59:00 UTC (rev 56853)
@@ -21,15 +21,20 @@
 */
 package org.jboss.virtual.plugins.context.file;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 
 import org.jboss.virtual.VFSUtils;
 import org.jboss.virtual.plugins.context.AbstractURLHandler;
+import org.jboss.virtual.plugins.context.DelegatingHandler;
 import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
+import org.jboss.virtual.plugins.vfs.helpers.PathTokenizer;
 import org.jboss.virtual.spi.LinkInfo;
 import org.jboss.virtual.spi.VFSContext;
 import org.jboss.virtual.spi.VFSContextFactory;
@@ -49,8 +54,46 @@
    /** The link information */
    private List<LinkInfo> links;
    private HashMap<String, VirtualFileHandler> linkTargets =
-      new HashMap<String, VirtualFileHandler>();
-   
+      new HashMap<String, VirtualFileHandler>(3);
+
+   class ParentOfLink extends AbstractURLHandler
+      implements StructuredVirtualFileHandler
+   {
+      private static final long serialVersionUID = 1;
+      private HashMap<String, VirtualFileHandler> children = 
+         new HashMap<String, VirtualFileHandler>(1);
+
+      public ParentOfLink(VFSContext context, VirtualFileHandler parent, URL url, String name)
+      {
+         super(context, parent, url, name);
+      }
+      void addChild(VirtualFileHandler child, String name)
+      {
+         children.put(name, child);
+      }
+      public VirtualFileHandler findChild(String path) throws IOException
+      {
+         return structuredFindChild(path);
+      }
+
+      public VirtualFileHandler createChildHandler(String name) throws IOException
+      {
+         VirtualFileHandler child = children.get(name);
+         return child;
+      }
+
+      public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
+      {
+         // TODO Auto-generated method stub
+         return null;
+      }
+
+      public boolean isDirectory() throws IOException
+      {
+         return true;
+      }
+   }
+
    /**
     * Create a new LinkHandler.
     * 
@@ -64,10 +107,68 @@
     */
    public LinkHandler(FileSystemContext context, VirtualFileHandler parent, URI uri, String name,
          List<LinkInfo> links)
-      throws IOException
+      throws IOException, URISyntaxException
    {
+      // TODO: This URL is not consistent with the getName, but does point to the raw link file
       super(context, parent, uri.toURL(), name);
       this.links = links;
+      // Create handlers for the links and add 
+      for(LinkInfo link : links)
+      {
+         String linkName = link.getName();
+         if( linkName == null )
+            linkName = VFSUtils.getName(link.getLinkTarget());
+         if( linkName != null )
+         {
+            String[] paths = PathTokenizer.getTokens(linkName);
+            int n = 0;
+            VirtualFileHandler linkParent = this;
+            String atom = null;
+            // Look for an existing parent           
+            for(; n < paths.length-1; n ++)
+            {
+               atom = paths[n];
+               try
+               {
+                  linkParent = linkParent.findChild(atom);
+               }
+               catch(IOException e)
+               {
+                  break;
+               }
+            }
+            // Create any missing parents
+            for(; n < paths.length-1; n ++)
+            {
+               atom = paths[n];
+               URL polURL = new URL(linkParent.toURI().toURL(), atom);
+               ParentOfLink pol = new ParentOfLink(this.getVFSContext(), linkParent, polURL, atom);
+               if( linkParent == this )
+               {
+                  linkTargets.put(atom, pol);
+               }
+               else
+               {
+                  ParentOfLink prevPOL = (ParentOfLink) linkParent;
+                  prevPOL.addChild(pol, atom);
+               }
+               linkParent = pol;
+            }
+               
+            // Create the link handler
+            atom = paths[n];
+            VirtualFileHandler linkHandler = createLinkHandler(linkParent, atom, link.getLinkTarget());
+            if( linkParent == this )
+            {
+               linkTargets.put(atom, linkHandler);
+            }
+            else
+            {
+               ParentOfLink prevPOL = (ParentOfLink) linkParent;
+               prevPOL.addChild(linkHandler, atom);
+            }            
+         }
+      }
    }
 
    @Override
@@ -89,31 +190,7 @@
 
    public List<VirtualFileHandler> getChildren(boolean ignoreErrors) throws IOException
    {
-      List<VirtualFileHandler> result = new ArrayList<VirtualFileHandler>();
-      for (LinkInfo link : links)
-      {
-         try
-         {
-            String name = link.getName();
-            URI linkURI = link.getLinkTarget();
-            if( name == null )
-               name = VFSUtils.getName(linkURI);
-            VirtualFileHandler handler = linkTargets.get(name);
-            if( handler == null )
-            {
-               handler = createLinkHandler(link);
-               linkTargets.put(name, handler);
-            }
-            result.add(handler);
-         }
-         catch (IOException e)
-         {
-            if (ignoreErrors)
-               log.trace("Ignored: " + e);
-            else
-               throw e;
-         }
-      }
+      List<VirtualFileHandler> result = new ArrayList<VirtualFileHandler>(linkTargets.values());
       return result;
    }
 
@@ -126,20 +203,7 @@
       VirtualFileHandler handler = linkTargets.get(name);
       if( handler == null )
       {
-         for (LinkInfo link : links)
-         {
-            String infoName = link.getName();
-            if( infoName == null )
-            {
-               infoName = VFSUtils.getName(link.getLinkTarget());
-            }
-            
-            if( name.equals(infoName) )
-            {
-               handler = createLinkHandler(link);
-               linkTargets.put(name, handler);
-            }
-         }
+         throw new FileNotFoundException("Failed to find link for: "+name+", parent: "+this);
       }
       return handler;
    }
@@ -151,13 +215,14 @@
       links.clear();
    }
    
-   protected VirtualFileHandler createLinkHandler(LinkInfo info)
+   protected VirtualFileHandler createLinkHandler(VirtualFileHandler parent, String name, URI linkURI)
       throws IOException
    {
-      URI linkURI = info.getLinkTarget();
       VFSContextFactory factory = VFSContextFactoryLocator.getFactory(linkURI);
       VFSContext context = factory.getVFS(linkURI);
-      VirtualFileHandler handler = context.getRoot();
+      VirtualFileHandler rootHandler = context.getRoot();
+      // Wrap the handler in a delegate so we can change the parent and name
+      DelegatingHandler handler = new DelegatingHandler(this.getVFSContext(), parent, name, rootHandler);
       // TODO: if the factory caches contexts the root handler may not point to the link
       return handler;
    }

Modified: projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/TestFileVFS.java
===================================================================
--- projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/TestFileVFS.java	2006-09-14 15:58:29 UTC (rev 56852)
+++ projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/TestFileVFS.java	2006-09-14 15:59:00 UTC (rev 56853)
@@ -526,7 +526,11 @@
    }
 
    /**
-    * Test parsing of a vfs link properties file
+    * Test parsing of a vfs link properties file. It contains test.classes.url
+    * and test.lib.url system property references that are configured to
+    * point to the CodeSource location of this class and /vfs/sundry/jar/
+    * respectively.
+    * 
     * @throws Exception
     */
    public void testVfsLinkProperties()
@@ -538,7 +542,7 @@
       URL classesURL = getClass().getProtectionDomain().getCodeSource().getLocation();
       assertNotNull("classesURL", classesURL);
       System.setProperty("test.classes.url", classesURL.toString());
-      URL libURL = super.getResource("/vfs/sundry/jar/archive.jar");
+      URL libURL = super.getResource("/vfs/sundry/jar");
       assertNotNull("libURL", libURL);      
       System.setProperty("test.lib.url", libURL.toString());
 
@@ -562,6 +566,57 @@
       assertEquals("libInfo.target", libURL.toURI(), libInfo.getLinkTarget());
    }
 
+   /**
+    * Test the test-link.war link
+    * @throws Exception
+    */
+   public void testWarLink()
+      throws Exception
+   {
+      // Find resources to use as the WEB-INF/{classes,lib} link targets
+      URL classesURL = getClass().getProtectionDomain().getCodeSource().getLocation();
+      assertNotNull("classesURL", classesURL);
+      System.setProperty("test.classes.url", classesURL.toString());
+      URL libURL = super.getResource("/vfs/sundry/jar");
+      assertNotNull("libURL", libURL);      
+      System.setProperty("test.lib.url", libURL.toString());
+
+      // Root the vfs at the link file parent directory
+      URL linkURL = super.getResource("/vfs/links/war1.vfslink.properties");
+      File linkFile = new File(linkURL.toURI());
+      File vfsRoot = linkFile.getParentFile();
+      assertNotNull("vfs/links/war1.vfslink.properties", linkURL);
+      VFS vfs = VFS.getVFS(vfsRoot.toURI());
+
+      // We should find the test-link.war the link represents
+      VirtualFile war = vfs.findChildFromRoot("test-link.war");
+      assertNotNull("war", war);
+
+      // Validate the WEB-INF/classes child link
+      VirtualFile classes = war.findChild("WEB-INF/classes");
+      String classesName = classes.getName();
+      String classesPathName = classes.getPathName();
+      boolean classesIsDirectory = classes.isDirectory();
+      assertEquals("classes.name", "classes", classesName);
+      assertEquals("classes.pathName", "test-link.war/WEB-INF/classes", classesPathName);
+      assertEquals("classes.isDirectory", true, classesIsDirectory);
+      // Should be able to find this class since classes points to out codesource
+      VirtualFile thisClass = classes.findChild("org/jboss/test/virtual/test/TestFileVFS.class");
+      assertEquals("TestFileVFS.class", thisClass.getName());
+
+      // Validate the WEB-INF/lib child link
+      VirtualFile lib = war.findChild("WEB-INF/lib");
+      String libName = lib.getName();
+      String libPathName = lib.getPathName();
+      boolean libIsDirectory = lib.isDirectory();
+      assertEquals("lib.name", "lib", libName);
+      assertEquals("lib.pathName", "test-link.war/WEB-INF/lib", libPathName);
+      assertEquals("lib.isDirectory", true, libIsDirectory);
+      // Should be able to find archive.jar under lib
+      VirtualFile archiveJar = lib.findChild("archive.jar");
+      assertEquals("archive.jar", archiveJar.getName());
+   }
+
    public void testURIBehavior()
       throws Exception
    {




More information about the jboss-cvs-commits mailing list