[jboss-cvs] JBossAS SVN: r102017 - in projects/vfs/trunk/src: main/java/org/jboss/vfs/protocol/file and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Mar 6 00:55:02 EST 2010


Author: johnbailey
Date: 2010-03-06 00:55:00 -0500 (Sat, 06 Mar 2010)
New Revision: 102017

Added:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractLocalURLStreamHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLStreamHandler.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLStreamHandler.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/file/Handler.java
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java
Log:
[JBVFS-146] - Updated file: URL support to delegate to the real filesystem

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractLocalURLStreamHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractLocalURLStreamHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractLocalURLStreamHandler.java	2010-03-06 05:55:00 UTC (rev 102017)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.vfs.protocol;
+
+import java.io.IOException;
+import java.net.Proxy;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Abstract URLStreamHandler that can be used as a base for other URLStreamHandlers that
+ * require the URL to be local.
+ *
+ * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
+ * @version $Revision$
+ */
+public abstract class AbstractLocalURLStreamHandler extends URLStreamHandler {
+
+    private static final Set<String> locals;
+
+    static {
+        Set<String> set = new HashSet<String>();
+        set.add(null);
+        set.add("");
+        set.add("~");
+        set.add("localhost");
+        locals = set;
+    }
+
+    @Override
+    protected URLConnection openConnection(URL u, Proxy p) throws IOException {
+        return openConnection(u);
+    }
+
+    @Override
+    protected boolean hostsEqual(URL url1, URL url2) {
+        return locals.contains(toLower(url1.getHost())) && locals.contains(toLower(url2.getHost())) || super.hostsEqual(url1, url2);
+    }
+
+    private static String toLower(String str) {
+        return str == null ? null : str.toLowerCase();
+    }
+
+    protected void ensureLocal(URL url) throws IOException {
+       if (!locals.contains(toLower(url.getHost())))   
+          throw new IOException("Remote host access not supported for URLs of type \"" + url.getProtocol() + "\"");
+    }
+
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java	2010-03-06 05:55:00 UTC (rev 102017)
@@ -0,0 +1,94 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.vfs.protocol;
+
+import org.jboss.vfs.VFS;
+import org.jboss.vfs.VFSUtils;
+import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.spi.RootFileSystem;
+
+import java.io.File;
+import java.io.FilePermission;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.*;
+import java.security.Permission;
+
+/**
+ * Implementation URLConnection that will delegate to the VFS RootFileSystem.
+ *
+ * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
+ * @version $Revision$
+ */
+public class FileURLConnection extends URLConnection {
+
+   private final RootFileSystem rootFileSystem = RootFileSystem.ROOT_INSTANCE;
+
+   private final VirtualFile mountPoint = VFS.getRootVirtualFile();
+    
+   private final VirtualFile file;
+
+   public FileURLConnection(URL url) throws IOException
+   {
+      super(url);
+      file = VFS.getChild(toURI(url));
+   }
+
+   public File getContent() throws IOException
+   {
+      return rootFileSystem.getFile(mountPoint, file);
+   }
+
+   public int getContentLength()
+   {
+      final long size = rootFileSystem.getSize(mountPoint, file);
+      return size > (long) Integer.MAX_VALUE ? -1 : (int) size;
+   }
+
+   public long getLastModified()
+   {
+      return rootFileSystem.getLastModified(mountPoint, file);
+   }
+
+   public InputStream getInputStream() throws IOException
+   {
+      return rootFileSystem.openInputStream(mountPoint, file);
+   }
+
+    @Override
+    public void connect() throws IOException {
+    }
+
+    private static URI toURI(URL url) throws IOException
+   {
+      try
+      {
+         return VFSUtils.toURI(url);
+      }
+      catch (URISyntaxException e)
+      {
+         IOException ioe = new IOException();
+         ioe.initCause(e);
+         throw ioe;
+      }
+   }
+}

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLStreamHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLStreamHandler.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLStreamHandler.java	2010-03-06 05:55:00 UTC (rev 102017)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.vfs.protocol;
+
+import java.io.IOException;
+import java.net.Proxy;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * A File based URLStreamHandler
+ *
+ * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
+ * @version $Revision$
+ */
+public class FileURLStreamHandler extends AbstractLocalURLStreamHandler {
+    @Override
+    protected URLConnection openConnection(final URL url) throws IOException {
+        ensureLocal(url);
+        return new FileURLConnection(url);
+    }
+}

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLStreamHandler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLStreamHandler.java	2010-03-06 03:31:31 UTC (rev 102016)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLStreamHandler.java	2010-03-06 05:55:00 UTC (rev 102017)
@@ -33,37 +33,11 @@
 /**
  * The VFS URL stream handler.
  */
-public abstract class VirtualFileURLStreamHandler extends URLStreamHandler {
+public abstract class VirtualFileURLStreamHandler extends AbstractLocalURLStreamHandler {
 
-    private static final Set<String> locals;
-
-    static {
-        Set<String> set = new HashSet<String>();
-        set.add(null);
-        set.add("");
-        set.add("~");
-        set.add("localhost");
-        locals = set;
-    }
-
-    protected URLConnection openConnection(URL u) throws IOException {
-        if (locals.contains(toLower(u.getHost()))) {
-            // the URL is a valid local URL
-            return new VirtualFileURLConnection(u);
-        }
-        throw new IOException("Remote host access not supported for URLs of type \"" + u.getProtocol() + "\"");
-    }
-
-    protected URLConnection openConnection(URL u, Proxy p) throws IOException {
-        return openConnection(u);
-    }
-
     @Override
-    protected boolean hostsEqual(URL url1, URL url2) {
-        return locals.contains(toLower(url1.getHost())) && locals.contains(toLower(url2.getHost())) || super.hostsEqual(url1, url2);
+    protected URLConnection openConnection(URL url) throws IOException {
+        ensureLocal(url);
+        return new VirtualFileURLConnection(url);
     }
-
-    private static String toLower(String str) {
-        return str == null ? null : str.toLowerCase();
-    }
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/file/Handler.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/file/Handler.java	2010-03-06 03:31:31 UTC (rev 102016)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/file/Handler.java	2010-03-06 05:55:00 UTC (rev 102017)
@@ -22,11 +22,16 @@
 
 package org.jboss.vfs.protocol.file;
 
+import org.jboss.vfs.protocol.AbstractLocalURLStreamHandler;
+import org.jboss.vfs.protocol.FileURLStreamHandler;
 import org.jboss.vfs.protocol.VirtualFileURLStreamHandler;
 
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+
 /**
  * Stub handler class.
  */
-public final class Handler extends VirtualFileURLStreamHandler {
-
+public final class Handler extends FileURLStreamHandler {
 }

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java	2010-03-06 03:31:31 UTC (rev 102016)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java	2010-03-06 05:55:00 UTC (rev 102017)
@@ -21,20 +21,21 @@
 */
 package org.jboss.test.vfs;
 
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.concurrent.Executors;
 
 import junit.framework.Test;
 
+import org.jboss.vfs.TempFileProvider;
 import org.jboss.vfs.VFS;
 import org.jboss.vfs.VFSUtils;
 import org.jboss.vfs.VirtualFile;
+import org.jboss.vfs.protocol.FileURLConnection;
 
 /**
  * Basic tests of URL connection
@@ -158,6 +159,41 @@
       assertEquals(file.lastModified(), conn.getLastModified());
    }
 
+   public void testFileUrl() throws Exception
+   {
+      // Hack to ensure VFS.init has been called and has taken over the file: protocol
+      VFS.getChild("");
+      URL resourceUrl = getResource("/vfs/test/outer.jar");
+      // Hack to ensure the URL handler is not passed down by the parent URL context
+      URL url = new URL("file", resourceUrl.getHost(), resourceUrl.getFile());
+
+      // Make sure we are using our handler
+      URLConnection urlConn = url.openConnection();
+      assertTrue(urlConn instanceof FileURLConnection); 
+
+      File file = new File(url.toURI());
+      assertNotNull(file);
+
+      VirtualFile vf = VFS.getChild(url);
+      assertTrue(vf.isFile());
+       // Mount a temp dir over the jar location in VFS
+      TempFileProvider provider = null;
+      Closeable handle = null;
+      try {
+         provider = TempFileProvider.create("temp", Executors.newSingleThreadScheduledExecutor());
+         handle = VFS.mountTemp(vf, provider);
+         assertTrue(vf.isDirectory());
+
+         File vfsDerivedFile = vf.getPhysicalFile();
+         File urlDerivedFile = (File)url.getContent();
+         // Make sure the file returned by the file: URL is not the VFS File (In other words, make sure it does not use the mounts)
+         assertTrue(urlDerivedFile.isFile());
+         assertFalse(vfsDerivedFile.equals(urlDerivedFile));
+      } finally {
+         VFSUtils.safeClose(handle, provider);
+      }
+   }
+
    protected static byte[] readBytes(InputStream inputStream) throws Exception
    {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();




More information about the jboss-cvs-commits mailing list