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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Nov 28 17:28:20 EST 2006


Author: bill.burke at jboss.com
Date: 2006-11-28 17:28:16 -0500 (Tue, 28 Nov 2006)
New Revision: 58713

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/jar/AbstractJarHandler.java
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/JarHandler.java
   projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
   projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/FileVFSUnitTestCase.java
Log:
JarHandlers should not return "jar:" based urls.  Let them return an actual pointer to their real file.

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-11-28 18:03:50 UTC (rev 58712)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/file/FileSystemContext.java	2006-11-28 22:28:16 UTC (rev 58713)
@@ -173,11 +173,10 @@
       URI fileURL = getFileURI(file);
       if (file.isFile() && JarUtils.isArchive(file.getName()))
       {
-         URL url = JarUtils.createJarURL(file.toURL());
          String name = file.getName();
          try
          {
-            return new JarHandler(this, parent, url, name);
+            return new JarHandler(this, parent, file, file.toURL(), name);
          }
          catch (IOException e)
          {

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/AbstractJarHandler.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/AbstractJarHandler.java	2006-11-28 18:03:50 UTC (rev 58712)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/AbstractJarHandler.java	2006-11-28 22:28:16 UTC (rev 58713)
@@ -24,6 +24,7 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.ObjectInputStream;
+import java.io.File;
 import java.net.JarURLConnection;
 import java.net.MalformedURLException;
 import java.net.URISyntaxException;
@@ -46,27 +47,33 @@
 
 /**
  * AbstractJarHandler.
- * 
+ *
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
  * @author Scott.Stark at jboss.org
  * @version $Revision: 1.1 $
  */
 public class AbstractJarHandler extends AbstractURLHandler
-   implements StructuredVirtualFileHandler
+        implements StructuredVirtualFileHandler
 {
-   /** serialVersionUID */
+   /**
+    * serialVersionUID
+    */
    private static final long serialVersionUID = 1;
 
-   /** The jar file */
+   /**
+    * The jar file
+    */
    private transient JarFile jar;
 
-   /** The jar entries */
+   /**
+    * The jar entries
+    */
    private transient List<VirtualFileHandler> entries;
    private transient Map<String, VirtualFileHandler> entryMap;
 
    /**
     * Get a jar entry name
-    * 
+    *
     * @param entry the entry
     * @return the name
     * @throws IllegalArgumentException for a null entry
@@ -77,15 +84,15 @@
          throw new IllegalArgumentException("Null entry");
       return entry.getName();
    }
-   
+
    /**
     * Create a new JarHandler.
-    * 
+    *
     * @param context the context
-    * @param parent the parent
-    * @param url the url
-    * @param name the name
-    * @throws IOException for an error accessing the file system
+    * @param parent  the parent
+    * @param url     the url
+    * @param name    the name
+    * @throws IOException              for an error accessing the file system
     * @throws IllegalArgumentException for a null context, url or vfsPath
     */
    protected AbstractJarHandler(VFSContext context, VirtualFileHandler parent, URL url, String name) throws IOException
@@ -95,7 +102,7 @@
 
    /**
     * Get the jar.
-    * 
+    *
     * @return the jar.
     */
    public JarFile getJar()
@@ -105,9 +112,9 @@
 
    /**
     * Initialise the jar file
-    * 
+    *
     * @param jarFile the jar file
-    * @throws IOException for any error reading the jar file
+    * @throws IOException              for any error reading the jar file
     * @throws IllegalArgumentException for a null jarFile
     */
    protected void initJarFile(JarFile jarFile) throws IOException
@@ -138,64 +145,64 @@
          JarEntry entry = enumeration.nextElement();
          String[] paths = entry.getName().split("/");
          int depth = paths.length;
-         if( depth >= levelMapList.size() )
+         if (depth >= levelMapList.size())
          {
-            for(int n = levelMapList.size(); n <= depth; n ++)
+            for (int n = levelMapList.size(); n <= depth; n++)
                levelMapList.add(new ArrayList<JarEntry>());
          }
          ArrayList<JarEntry> levelMap = levelMapList.get(depth);
          levelMap.add(entry);
-         if( trace )
-            log.trace("added "+entry.getName()+" at depth "+depth);
+         if (trace)
+            log.trace("added " + entry.getName() + " at depth " + depth);
       }
       // Process each level to build the handlers in parent first order
       int level = 0;
-      for(ArrayList<JarEntry> levels : levelMapList)
+      for (ArrayList<JarEntry> levels : levelMapList)
       {
-         if( trace )
-            log.trace("Level("+level++ +"): "+levels);
-         for(JarEntry entry : levels)
+         if (trace)
+            log.trace("Level(" + level++ + "): " + levels);
+         for (JarEntry entry : levels)
          {
             String name = entry.getName();
-            int slash = entry.isDirectory() ? name.lastIndexOf('/', name.length()-2) :
-               name.lastIndexOf('/', name.length()-1);
+            int slash = entry.isDirectory() ? name.lastIndexOf('/', name.length() - 2) :
+                    name.lastIndexOf('/', name.length() - 1);
             VirtualFileHandler parent = this;
             String entryName = name;
-            if( slash >= 0 )
+            if (slash >= 0)
             {
                // Need to include the slash in the name to match the JarEntry.name
-               String parentName = name.substring(0, slash+1);
+               String parentName = name.substring(0, slash + 1);
                parent = parentMap.get(parentName);
-               if( parent == null )
+               if (parent == null)
                {
                   // Build up the parent(s) 
                   parent = buildParents(parentName, parentMap, entry);
                }
             }
             // Get the entry name without any directory '/' ending
-            int start = slash+1;
-            int end = entry.isDirectory() ? name.length()-1 : name.length();
+            int start = slash + 1;
+            int end = entry.isDirectory() ? name.length() - 1 : name.length();
             entryName = name.substring(start, end);
             VirtualFileHandler handler = this.createVirtualFileHandler(parent, entry, entryName);
-            if( entry.isDirectory() )
+            if (entry.isDirectory())
             {
                parentMap.put(name, handler);
-               if( trace )
-                  log.trace("Added parent: "+name);
+               if (trace)
+                  log.trace("Added parent: " + name);
             }
-            if( parent == this )
+            if (parent == this)
             {
                // This is an immeadiate child of the jar handler
                entries.add(handler);
                entryMap.put(entryName, handler);
             }
-            else if( parent instanceof JarEntryHandler )
+            else if (parent instanceof JarEntryHandler)
             {
                // This is a child of the jar entry handler
                JarEntryHandler ehandler = (JarEntryHandler) parent;
                ehandler.addChild(handler);
             }
-            else if( parent instanceof SynthenticDirEntryHandler )
+            else if (parent instanceof SynthenticDirEntryHandler)
             {
                // This is a child of the jar entry handler
                SynthenticDirEntryHandler ehandler = (SynthenticDirEntryHandler) parent;
@@ -207,21 +214,21 @@
 
    /**
     * Create any missing parents.
-    * 
+    *
     * @param parentName full vfs path name of parent
-    * @param parentMap initJarFile parentMap
-    * @param entry JarEntry missing a parent
+    * @param parentMap  initJarFile parentMap
+    * @param entry      JarEntry missing a parent
     * @return the VirtualFileHandler for the parent
     * @throws IOException
     */
    protected VirtualFileHandler buildParents(String parentName,
-         Map<String, VirtualFileHandler> parentMap, JarEntry entry)
-      throws IOException
+                                             Map<String, VirtualFileHandler> parentMap, JarEntry entry)
+           throws IOException
    {
       VirtualFileHandler parent = this;
       String[] paths = PathTokenizer.getTokens(parentName);
       StringBuilder pathName = new StringBuilder();
-      for(String path : paths)
+      for (String path : paths)
       {
          VirtualFileHandler next = null;
          pathName.append(path);
@@ -235,21 +242,21 @@
             // Create a synthetic parent
             URL url = getURL(parent, path);
             next = new SynthenticDirEntryHandler(getVFSContext(), parent, path,
-                  entry.getTime(), url);
+                    entry.getTime(), url);
             parentMap.put(pathName.toString(), next);
-            if( parent == this )
+            if (parent == this)
             {
                // This is an immeadiate child of the jar handler
                entries.add(next);
                entryMap.put(path, next);
             }
-            else if( parent instanceof JarEntryHandler )
+            else if (parent instanceof JarEntryHandler)
             {
                // This is a child of the jar entry handler
                JarEntryHandler ehandler = (JarEntryHandler) parent;
                ehandler.addChild(next);
             }
-            else if( parent instanceof SynthenticDirEntryHandler )
+            else if (parent instanceof SynthenticDirEntryHandler)
             {
                // This is a child of the jar entry handler
                SynthenticDirEntryHandler ehandler = (SynthenticDirEntryHandler) parent;
@@ -262,17 +269,17 @@
    }
 
    protected URL getURL(VirtualFileHandler parent, String path)
-      throws MalformedURLException
+           throws MalformedURLException
    {
       StringBuilder buffer = new StringBuilder();
       try
       {
          buffer.append(parent.toURL());
-         if (buffer.charAt(buffer.length()-1) != '/')
+         if (buffer.charAt(buffer.length() - 1) != '/')
             buffer.append('/');
          buffer.append(path);
       }
-      catch(URISyntaxException e)
+      catch (URISyntaxException e)
       {
          // Should not happen
          throw new MalformedURLException(e.getMessage());
@@ -315,33 +322,54 @@
    public VirtualFileHandler createChildHandler(String name) throws IOException
    {
       VirtualFileHandler child = entryMap.get(name);
-      if( child == null )
-         throw new FileNotFoundException(this+" has no child: "+name);
+      if (child == null)
+         throw new FileNotFoundException(this + " has no child: " + name);
       return child;
    }
 
    /**
     * Create a new virtual file handler
-    * 
+    *
     * @param parent the parent
-    * @param entry the entry
+    * @param entry  the entry
     * @return the handler
-    * @throws IOException for any error accessing the file system
+    * @throws IOException              for any error accessing the file system
     * @throws IllegalArgumentException for a null parent or entry
     */
    protected VirtualFileHandler createVirtualFileHandler(VirtualFileHandler parent, JarEntry entry,
-         String entryName)
-      throws IOException
+                                                         String entryName)
+           throws IOException
    {
       if (parent == null)
          throw new IllegalArgumentException("Null parent");
       if (entry == null)
          throw new IllegalArgumentException("Null entry");
 
-      // Question: Why doesn't this work properly?
-      // URL url = new URL(parent.toURL(), entry.getName());
-      URL url = getURL(parent, entryName);
 
+      StringBuilder buffer = new StringBuilder();
+      try
+      {
+         String parentUrl = parent.toURL().toString();
+         if (parent instanceof JarEntryHandler || parent instanceof SynthenticDirEntryHandler)
+         {
+            buffer.append(parentUrl);
+         }
+         else
+         {
+            buffer.append("jar:").append(parentUrl).append("!/");
+         }
+
+         if (buffer.charAt(buffer.length() - 1) != '/')
+            buffer.append('/');
+         buffer.append(entryName);
+      }
+      catch (URISyntaxException e)
+      {
+         // Should not happen
+         throw new MalformedURLException(e.getMessage());
+      }
+      URL url = new URL(buffer.toString());
+
       VFSContext context = parent.getVFSContext();
 
       VirtualFileHandler vfh;
@@ -350,10 +378,10 @@
          String flag = context.getOptions().get("useNoCopyJarHandler");
          boolean useNoCopyJarHandler = Boolean.valueOf(flag);
 
-         if( useNoCopyJarHandler )
+         if (useNoCopyJarHandler)
             vfh = new NoCopyNestedJarHandler(context, parent, jar, entry, url);
          else
-            vfh = new NestedJarHandler(context, parent, jar, entry, url, entryName);
+            vfh = NestedJarHandler.create(context, parent, jar, entry, url, entryName);
       }
       else
       {
@@ -368,27 +396,35 @@
 
    /**
     * Restore the jar file from the jar URL
-    * 
+    *
     * @param in
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void readObject(ObjectInputStream in)
-      throws IOException, ClassNotFoundException
+           throws IOException, ClassNotFoundException
    {
       in.defaultReadObject();
       // Initialize the transient values
       URL jarURL = super.getURL();
-      URLConnection conn = jarURL.openConnection();
-      if( conn instanceof JarURLConnection )
+      String jarAsString = jarURL.toString();
+      if (jarAsString.startsWith("file:"))
       {
-         JarURLConnection jconn = (JarURLConnection) conn;
-         jar = jconn.getJarFile();
-         // initJarFile(jar) must be called by subclasses readObject
+         File fp = new File(jarAsString.substring(6));
+         jar = new JarFile(fp);
       }
       else
       {
-         throw new IOException("Cannot restore from non-JarURLConnection, url: "+jarURL);
+         URLConnection conn = jarURL.openConnection();
+         if (conn instanceof JarURLConnection)
+         {
+            JarURLConnection jconn = (JarURLConnection) conn;
+            jar = jconn.getJarFile();
+         }
+         else
+         {
+            throw new IOException("Cannot restore from non-JarURLConnection, url: " + jarURL);
+         }
       }
    }
 

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/JarHandler.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/JarHandler.java	2006-11-28 18:03:50 UTC (rev 58712)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/JarHandler.java	2006-11-28 22:28:16 UTC (rev 58713)
@@ -23,8 +23,11 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.File;
+import java.io.ObjectInputStream;
 import java.net.JarURLConnection;
 import java.net.URL;
+import java.util.jar.JarFile;
 
 import org.jboss.virtual.spi.VFSContext;
 import org.jboss.virtual.spi.VirtualFileHandler;
@@ -68,19 +71,36 @@
       }
    }
 
+   public JarHandler(VFSContext context, VirtualFileHandler parent, File file, URL url, String name) throws IOException
+   {
+      super(context, parent, url, name);
+
+      try
+      {
+         initJarFile(new JarFile(file));
+      }
+      catch (IOException original)
+      {
+         // Fix the context of the error message
+         IOException e = new IOException("Error opening jar file: " + url + " reason=" + original.getMessage());
+         e.setStackTrace(original.getStackTrace());
+         throw e;
+      }
+   }
+
    /**
-    * Override to return the input stream of the underlying url rather than
-    * a JarInputStream for the jar as this is not usable for copying the
-    * jar.
+    * Restore the jar file from the parent jar and entry name
+    *
+    * @param in
+    * @throws IOException
+    * @throws ClassNotFoundException
     */
-   @Override
-   public InputStream openStream() throws IOException
+   private void readObject(ObjectInputStream in)
+      throws IOException, ClassNotFoundException
    {
-      checkClosed();
-      String jarURL = getURL().toString();
-      String underlyingURL = jarURL.substring(4, jarURL.length()-2);
-      URL realURL = new URL(underlyingURL);
-      InputStream is = realURL.openStream();
-      return is;
+      JarFile parentJar = super.getJar();
+      // Initial the parent jar entries
+      initJarFile(parentJar);
    }
+
 }

Modified: projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java	2006-11-28 18:03:50 UTC (rev 58712)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/virtual/plugins/context/jar/NestedJarHandler.java	2006-11-28 22:28:16 UTC (rev 58713)
@@ -54,6 +54,8 @@
    /** The temporary file */
    private transient File temp;
 
+   private transient URL original;
+
    /**
     * Create a temporary jar
     * 
@@ -98,6 +100,26 @@
       return new JarFile(temp);
    }
 
+   public static NestedJarHandler create(VFSContext context, VirtualFileHandler parent,
+         JarFile parentJar, JarEntry entry, URL url, String entryName) throws IOException
+   {
+      File temp = null;
+
+      try
+      {
+         temp = File.createTempFile("nestedjar", null);
+         temp.deleteOnExit();
+      }
+      catch (IOException original)
+      {
+         // Fix the context of the error message
+         IOException e = new IOException("Error opening jar file: " + url + " reason=" + original.getMessage());
+         e.setStackTrace(original.getStackTrace());
+         throw e;
+      }
+      return new NestedJarHandler(context, parent, parentJar, entry, url, temp, entryName);
+   }
+
    /**
     * Create a new NestedJarHandler.
     * 
@@ -105,29 +127,27 @@
     * @param parent the parent
     * @param parentJar the parent jar file
     * @param entry the jar entry
-    * @param url the url
     * @throws IOException for an error accessing the file system
     * @throws IllegalArgumentException for a null context, url or vfsPath
     */
-   public NestedJarHandler(VFSContext context, VirtualFileHandler parent,
-         JarFile parentJar, JarEntry entry, URL url, String entryName)
+   protected NestedJarHandler(VFSContext context, VirtualFileHandler parent,
+         JarFile parentJar, JarEntry entry, URL original, File temp, String entryName)
       throws IOException
    {
-      super(context, parent, url, entryName);
+      super(context, parent, temp.toURL(), entryName);
 
+      this.temp = temp;
+      this.original = original;
       
       try
       {
-         temp = File.createTempFile("nestedjar", null);
-         temp.deleteOnExit();
-
          initJarFile(createTempJar(temp, parentJar, entry));
       }
-      catch (IOException original)
+      catch (IOException old)
       {
          // Fix the context of the error message
-         IOException e = new IOException("Error opening jar file: " + url + " reason=" + original.getMessage());
-         e.setStackTrace(original.getStackTrace());
+         IOException e = new IOException("Error opening jar file: " + original + " reason=" + old.getMessage());
+         e.setStackTrace(old.getStackTrace());
          throw e;
       }
       
@@ -167,12 +187,6 @@
       return fis;
    }
 
-   @Override
-   public URL toURL() throws MalformedURLException, URISyntaxException
-   {
-      return new URL("jar:" + temp.toURL() + "!/");
-   }
-
    /**
     * Restore the jar file from the parent jar and entry name
     * 
@@ -184,11 +198,6 @@
       throws IOException, ClassNotFoundException
    {
       JarFile parentJar = super.getJar();
-      // Initialize the transient values
-      entry = parentJar.getJarEntry(getName());
-      temp = File.createTempFile("nestedjar", null);
-      temp.deleteOnExit();
-      createTempJar(temp, parentJar, entry);
       // Initial the parent jar entries
       super.initJarFile(parentJar);
    }

Modified: projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/FileVFSUnitTestCase.java
===================================================================
--- projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/FileVFSUnitTestCase.java	2006-11-28 18:03:50 UTC (rev 58712)
+++ projects/microcontainer/trunk/container/src/tests/org/jboss/test/virtual/test/FileVFSUnitTestCase.java	2006-11-28 22:28:16 UTC (rev 58713)
@@ -594,7 +594,7 @@
       String vfsPath = tmpJar.getPath();
       vfsPath = vfsPath.substring(tmpRoot.getPath().length()+1);
       URL url = tmpJar.toURL();
-      url = JarUtils.createJarURL(url);
+      //url = JarUtils.createJarURL(url);
       log.debug("name: "+name);
       log.debug("vfsPath: "+vfsPath);
       log.debug("url: "+url);
@@ -784,10 +784,10 @@
       log.debug("outerURL: "+outerURL);
       assertTrue(outerURL+" ends in '/'", outerURL.getPath().endsWith("/"));
       // Validate that jar1 is under unpacked-outer.jar
-      URL jar1URL = JarUtils.createJarURL(new URL(outerURL, "jar1.jar"));
+      URL jar1URL = new URL(outerURL, "jar1.jar");
       log.debug("jar1URL: "+jar1URL+", path="+jar1URL.getPath());
       assertTrue("jar1URL path ends in unpacked-outer.jar/jar1.jar!/",
-            jar1URL.getPath().endsWith("unpacked-outer.jar/jar1.jar!/"));
+            jar1URL.getPath().endsWith("unpacked-outer.jar/jar1.jar"));
       VirtualFile jar1 = outerJar.findChild("jar1.jar");
       assertEquals(jar1URL, jar1.toURL());
 
@@ -942,7 +942,7 @@
       VirtualFile tstjar = vfs.findChild("path with spaces/tst.jar");
       assertNotNull("tstjar != null", tstjar);
       URI uri = tstjar.toURI();
-      URI expectedURI = new URI("jar:"+rootURL.toString()+"/path%20with%20spaces/tst.jar!/");
+      URI expectedURI = new URI(rootURL.toString()+"/path%20with%20spaces/tst.jar");
       assertEquals(uri, expectedURI);
    }
 




More information about the jboss-cvs-commits mailing list