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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Sep 9 17:17:29 EDT 2006


Author: scott.stark at jboss.org
Date: 2006-09-09 17:17:23 -0400 (Sat, 09 Sep 2006)
New Revision: 56698

Added:
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/LinkHandler.java
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/spi/LinkInfo.java
Modified:
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/VFSUtils.java
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/FileSystemContext.java
   projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/TestFileVFS.java
Log:
Checkpoint work on adding support for links

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/virtual/VFSUtils.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/virtual/VFSUtils.java	2006-09-09 16:55:15 UTC (rev 56697)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/VFSUtils.java	2006-09-09 21:17:23 UTC (rev 56698)
@@ -23,17 +23,22 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.jar.Attributes;
 import java.util.jar.JarFile;
 import java.util.jar.Manifest;
 
 import org.jboss.logging.Logger;
+import org.jboss.virtual.spi.LinkInfo;
 
 /**
  * VFS Utilities
@@ -45,7 +50,8 @@
 {
    /** The log */
    private static final Logger log = Logger.getLogger(VFSUtils.class);
-   
+   public static final String VFS_LINK_PREFIX = ".vfslink";
+
    /**
     * Get the paths string for a collection of virtual files
     * 
@@ -201,6 +207,24 @@
    }
 
    /**
+    * 
+    * @param uri
+    * @return
+    */
+   public static String getName(URI uri)
+   {
+      String name = uri.getPath();
+      if( name != null )
+      {
+         // TODO: Not correct for certain uris like jar:...!/ 
+         int lastSlash = name.lastIndexOf('/');
+         if( lastSlash > 0 )
+            name = name.substring(lastSlash+1);
+      }
+      return name;
+   }
+
+   /**
     * Take a URL.getQuery string and parse it into name=value pairs
     * 
     * @param query Possibly empty/null url query string
@@ -221,4 +245,60 @@
       }
 	   return pairsMap;
    }
+
+   /**
+    * Does a vf name match the VFS link prefix
+    * @param name - the name portion of a virtual file
+    * @return true if the name starts with VFS_LINK_PREFIX, false otherwise
+    */
+   public static boolean isLink(String name)
+   {
+      boolean isLink = name.startsWith(VFS_LINK_PREFIX);
+      return isLink;
+   }
+
+   /**
+    * Read the link information from the stream based on the type as determined
+    * from the name suffix.
+    * 
+    * @param is - input stream to the link file contents
+    * @param name - the name of the virtual file representing the link 
+    * @return a list of the links read from the stream
+    * @throws IOException on failure to read/parse the stream
+    */
+   public static List<LinkInfo> readLinkInfo(InputStream is, String name)
+      throws IOException, URISyntaxException
+   {
+      ArrayList<LinkInfo> info = new ArrayList<LinkInfo>();
+      if( name.endsWith(".properties") )
+         parseLinkProperties(is, info);
+      else
+         throw new UnsupportedEncodingException("Unknown link format: "+name);
+      return info;
+   }
+
+   /**
+    * Parse a properties link file
+    * 
+    * @param is
+    * @param info
+    * @throws IOException
+    * @throws URISyntaxException 
+    */
+   public static void parseLinkProperties(InputStream is, List<LinkInfo> info)
+      throws IOException, URISyntaxException
+   {
+      Properties props = new Properties();
+      props.load(is);
+      // Iterate over the property tuples
+      for(int n = 0; ; n ++)
+      {
+         String nameKey = "link.name." + n;
+         String name = props.getProperty(nameKey);
+         String uriKey = "link.uri." + n;
+         String uri = props.getProperty(uriKey);
+         LinkInfo link = new LinkInfo(name, new URI(uri));
+         info.add(link);
+      }
+   }
 }

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-09 16:55:15 UTC (rev 56697)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/FileSystemContext.java	2006-09-09 21:17:23 UTC (rev 56698)
@@ -189,8 +189,13 @@
          throw new IllegalArgumentException("Null file");
       if (uri == null)
          throw new IllegalArgumentException("Null uri");
-      
-      return new FileHandler(this, parent, file, uri);
+
+      VirtualFileHandler handler;
+      if( VFSUtils.isLink(file.getName()) )
+         handler = new LinkHandler(this, parent, file, uri);
+      else
+         handler = new FileHandler(this, parent, file, uri);
+      return handler;
    }
    
    @Override

Added: 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-09 16:55:15 UTC (rev 56697)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/LinkHandler.java	2006-09-09 21:17:23 UTC (rev 56698)
@@ -0,0 +1,179 @@
+/*
+* 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.file;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+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.StructuredVirtualFileHandler;
+import org.jboss.virtual.plugins.context.jar.JarUtils;
+import org.jboss.virtual.spi.LinkInfo;
+import org.jboss.virtual.spi.VFSContext;
+import org.jboss.virtual.spi.VFSContextFactory;
+import org.jboss.virtual.spi.VFSContextFactoryLocator;
+import org.jboss.virtual.spi.VirtualFileHandler;
+
+/**
+ * A handler for link directories.
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision: 1.1 $
+ */
+public class LinkHandler extends FileHandler
+   implements StructuredVirtualFileHandler
+{
+   private static final long serialVersionUID = 1;
+   /** The link information */
+   private List<LinkInfo> links;
+   private HashMap<String, VirtualFileHandler> linkTargets =
+      new HashMap<String, VirtualFileHandler>();
+   
+   /**
+    * Create a new LinkHandler.
+    * 
+    * @param context the context
+    * @param parent the parent
+    * @param file the file
+    * @param url the url
+    * @throws IOException for an error accessing the file system
+    * @throws IllegalArgumentException for a null context, url
+    */
+   public LinkHandler(FileSystemContext context, VirtualFileHandler parent, File file, URI uri)
+      throws IOException
+   {
+      super(context, parent, file, uri);
+      // Read the link info from the file
+      FileInputStream fis = new FileInputStream(file);
+      try
+      {
+          links = VFSUtils.readLinkInfo(fis, file.getName());
+      }
+      catch (URISyntaxException e)
+      {
+         IOException ex = new IOException();
+         ex.initCause(e);
+         throw ex;
+      }
+   }
+
+   @Override
+   public boolean isArchive()
+   {
+      return false;
+   }
+
+   @Override
+   public boolean isDirectory()
+   {
+      return true;
+   }
+
+   @Override
+   public boolean isFile()
+   {
+      return false;
+   }
+
+   @Override
+   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;
+         }
+      }
+      return result;
+   }
+
+   public VirtualFileHandler createChildHandler(String name) throws IOException
+   {
+      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);
+            }
+         }
+      }
+      return handler;
+   }
+
+   @Override
+   protected void doClose()
+   {
+      super.doClose();
+      links.clear();
+   }
+   
+   protected VirtualFileHandler createLinkHandler(LinkInfo info)
+      throws IOException
+   {
+      URI linkURI = info.getLinkTarget();
+      VFSContextFactory factory = VFSContextFactoryLocator.getFactory(linkURI);
+      VFSContext context = factory.getVFS(linkURI);
+      VirtualFileHandler handler = context.getRoot();
+      // TODO: if the factory caches contexts the root handler may not point to the link
+      return handler;
+   }
+}

Added: projects/microcontainer/trunk/container/src/main/org/jboss/virtual/spi/LinkInfo.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/virtual/spi/LinkInfo.java	2006-09-09 16:55:15 UTC (rev 56697)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/spi/LinkInfo.java	2006-09-09 21:17:23 UTC (rev 56698)
@@ -0,0 +1,64 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, 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.spi;
+
+import java.io.Serializable;
+import java.net.URI;
+
+/**
+ * A class representing the information for a VFS link.
+ * 
+ * @author Scott.Stark at jboss.org
+ * @version $Revision:$
+ */
+public class LinkInfo implements Serializable
+{
+   private static final long serialVersionUID = 1L;
+   /** Optional name of the link which defines its name to the parent */
+   private String name;
+   /** Required URI for the link target */
+   private final URI linkTarget;
+
+   /**
+    * Create a LinkInfo
+    * 
+    * @param name - the simple name of the target link. If null the linkTarget
+    *    name will be used.
+    * @param linkTarget - the URI of the target of the link.
+    */
+   public LinkInfo(String name, URI linkTarget)
+   {
+      this.name = name;
+      this.linkTarget = linkTarget;
+   }
+
+   public URI getLinkTarget()
+   {
+      return linkTarget;
+   }
+
+   public String getName()
+   {
+      return name;
+   }
+
+}

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-09 16:55:15 UTC (rev 56697)
+++ projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/TestFileVFS.java	2006-09-09 21:17:23 UTC (rev 56698)
@@ -522,6 +522,33 @@
       mfIS.close();
    }
 
+   public void testMountPoint()
+      throws Exception
+   {
+      File mpProps = new File("output/mp.properties");
+      URI mp = new URI("mount:" + mpProps.toURL() + "?x=y");
+      log.debug("getScheme: "+mp.getScheme());
+      log.debug("isAbsolute: "+mp.isAbsolute());
+      log.debug("isOpaque: "+mp.isOpaque());
+      log.debug("getHost: "+mp.getHost());
+      log.debug("getQuery: "+mp.getQuery());
+      log.debug("getPath: "+mp.getPath());
+      log.debug("getHost: "+mp.getHost());
+      log.debug("getSchemeSpecificPart: "+mp.getSchemeSpecificPart());
+
+      String locationInfo = mp.getSchemeSpecificPart();
+      URL locationURL = new URL(locationInfo);
+      log.debug("locationURL.getFile(): "+locationURL.getFile());
+      log.debug("locationURL.getPath(): "+locationURL.getPath());
+      log.debug("locationURL.getQuery(): "+locationURL.getQuery());
+      // Drop the query portion of the file
+      URL testURL = new URL(locationURL.getProtocol(), locationURL.getHost(),
+            locationURL.getPath());
+      assertEquals("mpProps.toURL", mpProps.toURL(), testURL);
+
+      
+   }
+
   /**
     * Test that the URL of a VFS corresponding to a directory ends in '/' so that
     * URLs created relative to it are under the directory.




More information about the jboss-cvs-commits mailing list