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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Nov 14 05:31:15 EST 2008


Author: mstruk
Date: 2008-11-14 05:31:15 -0500 (Fri, 14 Nov 2008)
New Revision: 80994

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/DelegatingHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java
   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/zip/ZipEntryContext.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java
Log:
JBVFS-77 Initial implementetion of real URLs - limited to handlers for now - not yet exposed through VFS API

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	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/AbstractVirtualFileHandler.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -258,6 +258,11 @@
       return vfsUrl;
    }
 
+   public URL getRealURL() throws IOException, URISyntaxException
+   {
+      return toURL();
+   }
+
    /**
     * Get VFS url.
     *

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/DelegatingHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/DelegatingHandler.java	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/DelegatingHandler.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -180,6 +180,11 @@
       return getDelegate().toVfsUrl();
    }
 
+   public URL getRealURL() throws IOException, URISyntaxException
+   {
+      return getDelegate().getRealURL();
+   }
+
    public int hashCode()
    {
       if (delegate != null)

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/file/FileHandler.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -107,6 +107,11 @@
       return getVfsUrl();
    }
 
+   public URL getRealURL() throws IOException, URISyntaxException
+   {
+      return getURL();
+   }
+
    @Override
    public FileSystemContext getVFSContext()
    {

Modified: 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	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/memory/MemoryContextHandler.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -186,4 +186,9 @@
       }
       return getVfsUrl();
    }
+
+   public URL getRealURL() throws IOException, URISyntaxException
+   {
+      throw new UnsupportedOperationException("Operation not supported on handler: " + this);
+   }
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -122,6 +122,9 @@
    /** Have zip entries been navigated yet */
    private InitializationStatus initStatus = InitializationStatus.NOT_INITIALIZED;
 
+   /** RealURL of this context */
+   private URL realURL;
+
    /**
     * Create a new ZipEntryContext
     *
@@ -283,13 +286,18 @@
       if (file == null)
          throw new IOException("VFS file does not exist: " + rootPath);
 
+      RealURLInfo urlInfo = new RealURLInfo(file);
+
       if (relative != null)
       {
-         return findEntry(new FileInputStream(file), relative);
+         ZipWrapper wrapper = findEntry(new FileInputStream(file), relative, urlInfo);
+         realURL = urlInfo.toURL();
+         return wrapper;
       }
       else
       {
          boolean noReaper = Boolean.valueOf(getOptions().get(VFSUtils.NO_REAPER_QUERY));
+         realURL = urlInfo.toURL();
          return new ZipFileWrapper(file, autoClean, noReaper);
       }
    }
@@ -300,10 +308,11 @@
     *
     * @param is the input stream
     * @param relative relative path
+    * @param urlInfo
     * @return zip wrapper instance
     * @throws IOException for any error
     */
-   protected ZipWrapper findEntry(InputStream is, String relative) throws IOException
+   protected ZipWrapper findEntry(InputStream is, String relative, RealURLInfo urlInfo) throws IOException
    {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       VFSUtils.copyStreamAndClose(is, baos);
@@ -356,8 +365,11 @@
          String entryName = entry.getName();
          if (entryName.equals(longestNameMatch))
          {
+            if (urlInfo != null)
+               urlInfo.relativePath = longestNameMatch;
+
             relative = relative.substring(longestNameMatch.length() + 1);
-            return findEntry(zis, relative);
+            return findEntry(zis, relative, null);
          }
       }
       throw new IllegalArgumentException("No such entry: " + is + ", " + relative);
@@ -987,6 +999,14 @@
    }
 
    /**
+    *  Get RealURL corresponding to root handler
+    */
+   public URL getRealURL()
+   {
+      return realURL;
+   }
+
+   /**
     *  Internal data structure holding meta information of a virtual file in this context
     */
    static class EntryInfo
@@ -1215,9 +1235,35 @@
       }
    }
 
-   static enum InitializationStatus {
+   static enum InitializationStatus
+   {
       NOT_INITIALIZED,
       INITIALIZING,
       INITIALIZED
    }
+
+   private static class RealURLInfo
+   {
+      String rootURL;
+      String relativePath;
+
+      RealURLInfo(File file) throws MalformedURLException
+      {
+         String url = file.toURL().toExternalForm();
+         if (url.endsWith("/"))
+            url = url.substring(0, url.length()-1);
+         rootURL = "jar:" + url + "!/";
+      }
+
+      URL toURL() throws MalformedURLException
+      {
+         if (relativePath == null || relativePath.length() == 0)
+            return new URL(rootURL);
+
+         if (relativePath.startsWith("/"))
+            relativePath = relativePath.substring(1);
+
+         return new URL(rootURL + relativePath);
+      }
+   }
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryHandler.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -23,7 +23,6 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -31,7 +30,9 @@
 
 import org.jboss.virtual.VFSUtils;
 import org.jboss.virtual.plugins.context.AbstractVirtualFileHandler;
+import org.jboss.virtual.plugins.context.file.FileSystemContext;
 import org.jboss.virtual.plugins.context.StructuredVirtualFileHandler;
+import org.jboss.virtual.spi.VFSContext;
 import org.jboss.virtual.spi.VirtualFileHandler;
 
 /**
@@ -192,4 +193,38 @@
       init();
       return super.getVfsUrl();
    }
+
+   public URL getRealURL() throws IOException, URISyntaxException
+   {
+      // see how far the parent contexts go
+      // if there is no parent context or it is of type FileSystemContext
+      // ZipEntryContext -> jar!/
+      // ZipEntryContext / ZipEntryContext -> jar!/jar
+      // ZipEntryConteyt / ZipEntryContext / ZipEntryContext ... -> jar!/jar
+      VFSContext ctx = getLocalVFSContext();
+      VirtualFileHandler peer = getLocalVFSContext().getRootPeer();
+
+      if (peer == null)
+         return getZipEntryContext().getRealURL();
+
+      if (peer instanceof AbstractVirtualFileHandler
+         && ((AbstractVirtualFileHandler)peer).getLocalVFSContext() instanceof FileSystemContext)
+      {
+         String lpath = getLocalPathName();
+         return new URL("jar:file:" + ctx.getRootURI().getPath() + "!" + (lpath.length() == 0 ? "/" : lpath));
+      }
+
+      if (peer instanceof AbstractVirtualFileHandler)
+      {
+         AbstractVirtualFileHandler aPeer =(AbstractVirtualFileHandler) peer;
+         URL realUrl = aPeer.getLocalVFSContext().getRoot().getRealURL();
+         String urlStr = realUrl.toExternalForm();
+         if (urlStr.endsWith("!/"))
+            return new URL(urlStr + aPeer.getLocalPathName());
+         else
+            return realUrl;
+      }
+       
+      throw new RuntimeException("Operation not supported for handler: " + this);
+   }
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/spi/VirtualFileHandler.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -72,6 +72,22 @@
     */
    URL toVfsUrl() throws MalformedURLException, URISyntaxException;
    
+   /**
+    * Get a file: or jar:file: URL representing a resource as precisely as possible.
+    * file: urls can represent files in the file system  (i.e.: file:/classes/MyClass.class)
+    * jar:file: urls can represent entries within zip archives in the filesystem
+    * (i.e.: jar:file:/lib/classes.jar!/MyClass.class)
+    * There is no standard URL handler to represent entries within archives that are themselves
+    * entries within archives.
+    * (i.e.: this doesn't work: jar:file:/lib/app.ear!/classes.jar!/MyClass.class
+    * In this case the most precise supported resource locator is: jar:file:/lib/app.ear!/classes.jar
+    * )
+    *
+    * @return the url
+    * @throws URISyntaxException for an error parsing the URI
+    * @throws MalformedURLException for any error constructing the URL
+    */
+   URL getRealURL() throws IOException, URISyntaxException;
 
    /**
     * Get the VF URI (file://root/org/jboss/X.java)

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java	2008-11-14 09:52:21 UTC (rev 80993)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/virtual/test/ZipEntryVFSContextUnitTestCase.java	2008-11-14 10:31:15 UTC (rev 80994)
@@ -27,6 +27,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
+import java.util.List;
 
 import junit.framework.Test;
 import org.jboss.virtual.VFS;
@@ -152,6 +153,52 @@
    }
 
    /**
+    * Real URL test
+    *
+    * @throws Exception
+    */
+   public void testRealURL() throws Exception
+   {
+      URL url = getResource("/vfs/context/jar/");
+      FileSystemContext ctx = new FileSystemContext(url);
+
+      // valid archive
+      VirtualFileHandler root = ctx.getRoot();
+      assertEquals("Context Real URL", url.toExternalForm(), root.getRealURL().toExternalForm());
+      String jarName = "archive.jar";
+      VirtualFileHandler archive = root.getChild(jarName);
+      assertEquals("Child Real URL", "jar:" + url.toExternalForm() + jarName + "!/", archive.getRealURL().toExternalForm());
+
+      url = getResource("/vfs/test/");
+      ctx = new FileSystemContext(url);
+      root = ctx.getRoot();
+
+      jarName = "level1.zip";
+      archive = root.getChild(jarName);
+      String nestedName = "level2.zip";
+      VirtualFileHandler nested = archive.getChild(nestedName);
+
+      String jarURL = "jar:" + url.toExternalForm() + jarName + "!/" + nestedName;
+      assertEquals("First level nested Real URL", jarURL, nested.getRealURL().toExternalForm());
+
+      nested = nested.getChild("level3.zip");
+      assertEquals("Second level nested Real URL", jarURL, nested.getRealURL().toExternalForm());
+
+      // nested root test
+      url = getResource("/vfs/test/");
+      ZipEntryContext zctx = new ZipEntryContext(new URL("vfszip:" + url.getPath() + "/level1.zip/level2.zip/level3.zip"));
+
+      VirtualFileHandler handler = zctx.getRoot();
+      assertEquals("Nested root Real URL", jarURL, handler.getRealURL().toExternalForm());
+
+      List<VirtualFileHandler> children = handler.getChildren(false);
+      for (VirtualFileHandler child: children)
+      {
+         assertEquals("Nested root Real URL", jarURL, child.getRealURL().toExternalForm());
+      }
+   }
+
+   /**
     * Handler representing a directory must return a recomposed zip archive
     * that has requested entry as its root.
     *




More information about the jboss-cvs-commits mailing list