[jboss-cvs] JBossAS SVN: r74537 - in projects/vfs/trunk/src/main/java/org/jboss/virtual: protocol and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Jun 13 10:34:51 EDT 2008


Author: alesj
Date: 2008-06-13 10:34:51 -0400 (Fri, 13 Jun 2008)
New Revision: 74537

Added:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java
Removed:
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractZipHandler.java
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/ZipEntryContextFactory.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsfile/Handler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfszip/Handler.java
   projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/zip/Handler.java
Log:
Change Handlers.
Remove ctxCache.

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-06-13 14:18:41 UTC (rev 74536)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContext.java	2008-06-13 14:34:51 UTC (rev 74537)
@@ -108,7 +108,7 @@
    private boolean autoClean = false;
 
    /** Registry of everything that zipSource contains */
-   private ConcurrentHashMap<String, EntryInfo> entries = new ConcurrentHashMap<String, EntryInfo>();
+   private Map<String, EntryInfo> entries = new ConcurrentHashMap<String, EntryInfo>();
 
    /**
     * Create a new ZipEntryContext
@@ -203,9 +203,7 @@
 
          // initialize rootEntryPath and get archive file path
          String rootPath = initRootAndPath(localRootURL);
-
-         String noReaper = getOptions().get(VFSUtils.NO_REAPER_QUERY);
-         zipSource = new ZipFileWrapper(VFSUtils.toURI(new URL(rootPath)), autoClean, Boolean.valueOf(noReaper));
+         zipSource = createZipSource(rootPath);
       }
       else
       {
@@ -234,9 +232,45 @@
       entries.put("", new EntryInfo(new ZipEntryHandler(this, null, name, true), null));
 
       initEntries();
-      ZipEntryContextFactory.registerContext(this);
    }
 
+   protected ZipWrapper createZipSource(String rootPath) throws IOException
+   {
+      File file = null;
+      String relative;
+      File fp = new File(rootPath);
+      if (fp.exists())
+      {
+         file = fp;
+         relative = "";
+      }
+      else
+      {
+         File curr = fp;
+         relative = fp.getName();
+         while ((curr = curr.getParentFile()) != null)
+         {
+            if (curr.exists())
+            {
+               file = curr;
+               break;
+            }
+            else
+            {
+               relative = curr.getName() + "/" + relative;
+            }
+         }
+      }
+
+      if (file == null)
+         throw new IOException("VFS file does not exist: " + rootPath);
+
+      // TODO - use relative to create the right context
+
+      String noReaper = getOptions().get(VFSUtils.NO_REAPER_QUERY);
+      return new ZipFileWrapper(file, autoClean, Boolean.valueOf(noReaper));
+   }
+
    /**
     * Returns archive file name - if this is a top-level ZipEntryContext.
     * Otherwise it returns the last component of URL.
@@ -442,14 +476,14 @@
 
       // find where url protocol ends - i.e. jar:file:/ ...
       pos= zipPath.indexOf(":/");
-      filePath = zipPath.substring(pos+1);
+      filePath = zipPath.substring(pos + 2);
 
       // cut out url query part if present
       int queryStart = filePath.indexOf("?");
       if (queryStart != -1)
          filePath = filePath.substring(0, queryStart);
-       
-      return "file:" + filePath;
+
+      return filePath;
    }
 
    /**

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContextFactory.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContextFactory.java	2008-06-13 14:18:41 UTC (rev 74536)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/plugins/context/zip/ZipEntryContextFactory.java	2008-06-13 14:34:51 UTC (rev 74537)
@@ -26,8 +26,6 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 
 import org.jboss.virtual.plugins.context.AbstractContextFactory;
 import org.jboss.virtual.spi.VFSContext;
@@ -40,9 +38,6 @@
  */
 public class ZipEntryContextFactory extends AbstractContextFactory
 {
-   /** registry of all ZipEntryContext instances */
-   private static Map<String, ZipEntryContext> ctxCache = new ConcurrentHashMap<String, ZipEntryContext>();
-
    /** singleton */
    private static ZipEntryContextFactory instance = new ZipEntryContextFactory();
 
@@ -62,32 +57,12 @@
    /**
     * Find a best matching existing ZipEntryContext, or create a new one if none matches.
     *
-    * @param rootURL
-    * @return
-    * @throws IOException
+    * @param rootURL the root url
+    * @return new zip context
+    * @throws IOException for any error
     */
    public VFSContext getVFS(URL rootURL) throws IOException
    {
-      String key = rootURL.toString();
-      int cutPos = key.indexOf(":/");
-      key = key.substring(cutPos+1);
-
-      String longestMatchingKey = null;
-      ZipEntryContext longestMatchingCtx = null;
-      for(Map.Entry<String, ZipEntryContext> ent : ctxCache.entrySet())
-      {
-         if(key.startsWith(ent.getKey()))
-         {
-            if(longestMatchingCtx == null || ent.getKey().length() > longestMatchingKey.length())
-            {
-               longestMatchingKey = ent.getKey();
-               longestMatchingCtx = ent.getValue();
-            }
-         }
-      }
-      if(longestMatchingCtx != null)
-         return longestMatchingCtx;
-
       try
       {
          return new ZipEntryContext(rootURL);
@@ -104,14 +79,4 @@
    {
       return instance;
    }
-
-   public static void registerContext(ZipEntryContext ctx)
-   {
-      String key = ctx.getRootURI().toString();
-      int cutPos = key.indexOf(":/");
-      key = key.substring(cutPos+1);
-      if("".equals(key))
-         throw new RuntimeException("Derived key for ZipEntryContext registration is empty: " + ctx.getRootURI());
-      ctxCache.put(key, ctx);
-   }
 }

Copied: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java (from rev 74373, projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractZipHandler.java)
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractVFSHandler.java	2008-06-13 14:34:51 UTC (rev 74537)
@@ -0,0 +1,99 @@
+/*
+* 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;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import org.jboss.virtual.plugins.vfs.VirtualFileURLConnection;
+
+/**
+ * VFS's file URL handler.
+ *
+ * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public abstract class AbstractVFSHandler extends URLStreamHandler
+{
+   private static Map<Class, Integer> lengths = new WeakHashMap<Class, Integer>();
+
+   /**
+    * Get protocol name length.
+    * e.g. vfsfile - 7, vfszip - 6, ...
+    *
+    * @return
+    */
+   protected int getProtocolNameLength()
+   {
+      Class<?> clazz = getClass();
+      Integer length = lengths.get(clazz);
+      if (length == null)
+      {
+         Package pck = clazz.getPackage();
+         String pckName = pck.getName();
+         int p = pckName.lastIndexOf('.');
+         length = pckName.substring(p + 1).length();
+         lengths.put(clazz, length);
+      }
+      return length;
+   }
+
+   protected URLConnection openConnection(URL url) throws IOException
+   {
+      String file = url.toExternalForm().substring(getProtocolNameLength() + 1); // strip out vfs protocol + :
+      URL vfsurl = null;
+      String relative;
+      File fp = new File(file);
+      if (fp.exists())
+      {
+         vfsurl = fp.getParentFile().toURL();
+         relative = fp.getName();
+      }
+      else
+      {
+         File curr = fp;
+         relative = fp.getName();
+         while ((curr = curr.getParentFile()) != null)
+         {
+            if (curr.exists())
+            {
+               vfsurl = curr.toURL();
+               break;
+            }
+            else
+            {
+               relative = curr.getName() + "/" + relative;
+            }
+         }
+      }
+
+      if (vfsurl == null)
+         throw new IOException("VFS file does not exist: " + url);
+
+      return new VirtualFileURLConnection(url, vfsurl, relative);
+   }
+}
\ No newline at end of file

Deleted: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractZipHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractZipHandler.java	2008-06-13 14:18:41 UTC (rev 74536)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/AbstractZipHandler.java	2008-06-13 14:34:51 UTC (rev 74537)
@@ -1,57 +0,0 @@
-/*
-* 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;
-
-import java.io.IOException;
-import java.net.URL;
-import java.net.URLConnection;
-import java.net.URLStreamHandler;
-
-import org.jboss.virtual.plugins.context.zip.ZipEntryContextFactory;
-import org.jboss.virtual.plugins.vfs.VirtualFileURLConnection;
-import org.jboss.virtual.spi.VFSContext;
-import org.jboss.virtual.spi.VirtualFileHandler;
-
-/**
- * VFS's zip URL handler.
- *
- * @author <a href="strukelj at parsek.net">Marko Strukelj</a>
- * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
- */
-public abstract class AbstractZipHandler extends URLStreamHandler
-{
-   protected URLConnection openConnection(URL url) throws IOException
-   {
-      VFSContext ctx = ZipEntryContextFactory.getInstance().getVFS(url);
-      if (ctx == null)
-         throw new IOException("No VFS context found for URL: " + url);
-
-      String rootPath = ctx.getRootURI().getPath();
-      String entryPath = url.getFile().substring(rootPath.length());
-
-      VirtualFileHandler child = ctx.getChild(ctx.getRoot(), entryPath);
-      if (child == null)
-         throw new IOException("No VFS file found for URL: " + url);
-
-      return new VirtualFileURLConnection(url, child.getVirtualFile());
-   }
-}

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsfile/Handler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsfile/Handler.java	2008-06-13 14:18:41 UTC (rev 74536)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfsfile/Handler.java	2008-06-13 14:34:51 UTC (rev 74537)
@@ -21,54 +21,15 @@
 */
 package org.jboss.virtual.protocol.vfsfile;
 
-import java.io.File;
-import java.io.IOException;
-import java.net.URL;
-import java.net.URLConnection;
-import java.net.URLStreamHandler;
+import org.jboss.virtual.protocol.AbstractVFSHandler;
 
-import org.jboss.virtual.plugins.vfs.VirtualFileURLConnection;
-
 /**
  * URLStreamHandler for VFS
  *
  * @author <a href="bill at jboss.com">Bill Burke</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
-public class Handler extends URLStreamHandler
+public class Handler extends AbstractVFSHandler
 {
-   protected URLConnection openConnection(URL url) throws IOException
-   {
-      String file = url.toString().substring(8); // strip out vfsfile:
-      URL vfsurl = null;
-      String relative;
-      File fp = new File(file);
-      if (fp.exists())
-      {
-         vfsurl = fp.getParentFile().toURL();
-         relative = fp.getName();
-      }
-      else
-      {
-         File curr = fp;
-         relative = fp.getName();
-         while ((curr = curr.getParentFile()) != null)
-         {
-            if (curr.exists())
-            {
-               vfsurl = curr.toURL();
-               break;
-            }
-            else
-            {
-               relative = curr.getName() + "/" + relative;
-            }
-         }
-      }
-
-      if (vfsurl == null)
-         throw new IOException("VFS file does not exist: " + url);
-
-      return new VirtualFileURLConnection(url, vfsurl, relative);
-   }
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfszip/Handler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfszip/Handler.java	2008-06-13 14:18:41 UTC (rev 74536)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/vfszip/Handler.java	2008-06-13 14:34:51 UTC (rev 74537)
@@ -1,6 +1,6 @@
 package org.jboss.virtual.protocol.vfszip;
 
-import org.jboss.virtual.protocol.AbstractZipHandler;
+import org.jboss.virtual.protocol.AbstractVFSHandler;
 
 /**
  * URLStreamHandler for VFS
@@ -8,6 +8,6 @@
  * @author <a href="strukelj at parsek.net">Marko Strukelj</a>
  * @version $Revision: 1.0 $
  */
-public class Handler extends AbstractZipHandler
+public class Handler extends AbstractVFSHandler
 {
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/zip/Handler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/zip/Handler.java	2008-06-13 14:18:41 UTC (rev 74536)
+++ projects/vfs/trunk/src/main/java/org/jboss/virtual/protocol/zip/Handler.java	2008-06-13 14:34:51 UTC (rev 74537)
@@ -21,12 +21,8 @@
 */
 package org.jboss.virtual.protocol.zip;
 
-import java.io.IOException;
-import java.net.URL;
-import java.net.URLConnection;
+import org.jboss.virtual.protocol.AbstractVFSHandler;
 
-import org.jboss.virtual.protocol.AbstractZipHandler;
-
 /**
  * Zip url handler.
  *
@@ -36,11 +32,6 @@
  *
  * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
  */
-public class Handler extends AbstractZipHandler
+public class Handler extends AbstractVFSHandler
 {
-   protected URLConnection openConnection(URL url) throws IOException
-   {
-      url = new URL("vfs" + url.toExternalForm());
-      return super.openConnection(url);
-   }
 }




More information about the jboss-cvs-commits mailing list