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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Mar 3 19:21:54 EST 2010


Author: jason.greene at jboss.com
Date: 2010-03-03 19:21:53 -0500 (Wed, 03 Mar 2010)
New Revision: 101795

Added:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java
Removed:
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/SymlinkTestCase.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/VirtualFile.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java
Log:
Restructure path processing to better support operating systems with multiple roots (Windows)
Drop symlink support for now


Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java	2010-03-04 00:15:35 UTC (rev 101794)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/VFS.java	2010-03-04 00:21:53 UTC (rev 101795)
@@ -48,6 +48,7 @@
 import org.jboss.vfs.spi.FileSystem;
 import org.jboss.vfs.spi.RealFileSystem;
 import org.jboss.vfs.spi.JavaZipFileSystem;
+import org.jboss.vfs.spi.RootFileSystem;
 import org.jboss.logging.Logger;
 import org.jboss.net.protocol.URLStreamHandlerFactory;
 
@@ -67,9 +68,15 @@
     public static final boolean FORCE_CANONICAL;
 
     private static final ConcurrentMap<VirtualFile, Map<String, Mount>> mounts = new ConcurrentHashMap<VirtualFile, Map<String, Mount>>();
-    private static final VirtualFile rootVirtualFile = new VirtualFile("", null);
-    private static final Mount rootMount = new Mount(RealFileSystem.ROOT_INSTANCE, rootVirtualFile);
+    private static final VirtualFile rootVirtualFile = createDefaultRoot();
 
+    private static VirtualFile createDefaultRoot() {
+       return isWindows() ? getChild("/") : new VirtualFile("/", null);
+    }
+            
+    // Note that rootVirtualFile is ignored by RootFS
+    private static final Mount rootMount = new Mount(RootFileSystem.ROOT_INSTANCE, rootVirtualFile);
+
     // todo - LRU VirtualFiles?
     // todo - LRU String intern?
     static {
@@ -170,6 +177,12 @@
     public static VirtualFile getChild(URL url) throws URISyntaxException {
         return getChild(url.toURI());
     }
+    
+    private static boolean isWindows()
+    {
+        // Not completely accurate, but good enough
+        return File.separatorChar == '\\';
+    }
 
     /**
      * Find a virtual file.
@@ -196,31 +209,43 @@
     public static VirtualFile getChild(String path) {
         if (path == null)
             throw new IllegalArgumentException("Null path");
-        return getRootVirtualFile().getChild(canonicalize(path));
+        
+        VirtualFile root = null;
+        
+        if (isWindows()) {
+            // Normalize the path using java.io.File
+            //   TODO Consider creating our own normalization routine, which would
+            //   allow for testing on non-Windows
+            String absolute = new File(path).getAbsolutePath();
+            
+            if (absolute.charAt(1) == ':') {
+                // Drive form
+                root = new VirtualFile("/" + absolute.charAt(0) + ":/", null);
+                path = absolute.substring(2).replace('\\', '/');
+            } else if (absolute.charAt(0) == '\\' && absolute.charAt(1) == '\\') {
+                // UNC form 
+                for (int i = 2; i < absolute.length(); i++) {
+                    if (absolute.charAt(i) == '\\') {
+                        // Switch \\ to // just like java file URLs.
+                        // Note, it turns out that File.toURL puts this portion
+                        // in the path portion of the URL, which is actually not
+                        // correct, since // is supposed to signify the authority.
+                        root = new VirtualFile("//" + absolute.substring(0,i), null);
+                        path = absolute.substring(i).replace('\\', '/');
+                        break;
+                    }
+                }               
+            } 
+            
+            if (root == null)
+                throw new IllegalArgumentException("Invalid Win32 path: " + path);       
+        } else {
+            root = rootVirtualFile;
+        }
+        
+        return root.getChild(path);
     }
 
-   /**
-    * Canonicalize path.
-    *
-    * @param path the path to canonicalize
-    * @return canonical path of given @param path
-    */
-    private static String canonicalize(String path)
-    {
-       if (FORCE_CANONICAL)
-       {
-          try
-          {
-             return new File(path).getCanonicalPath();
-          }
-          catch (IOException e)
-          {
-             throw new RuntimeException("Cannot get canonical file for path: " + path, e);
-          }
-       }
-       return path;
-    }
-
     /**
      * Get the root virtual file for this VFS instance.
      *

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/VirtualFile.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/VirtualFile.java	2010-03-04 00:15:35 UTC (rev 101794)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/VirtualFile.java	2010-03-04 00:21:53 UTC (rev 101795)
@@ -134,7 +134,7 @@
         final StringBuilder builder = new StringBuilder(160);
         final VirtualFile parent = this.parent;
         if (parent == null) {
-            return "/";
+            return name;
         } else {
             builder.append(parent.getPathName());
             if (parent.parent != null) {
@@ -177,6 +177,17 @@
         final VFS.Mount mount = VFS.getMount(this);
         return mount.getFileSystem().exists(mount.getMountPoint(), this);
     }
+    
+    /**
+     * Determines whether this virtual file represents a true root of a file system.
+     * On UNIX, there is only one root "/". Howevever, on Windows there are an infinite
+     * number of roots that correspond to drives, or UNC paths.
+     * 
+     * @return {@code true} if this represents a root.
+     */ 
+    public boolean isRoot() {
+        return parent == null;
+    }
 
     /**
      * Whether it is a simple leaf of the VFS, i.e. whether it can contain other files

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java	2010-03-04 00:15:35 UTC (rev 101794)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RealFileSystem.java	2010-03-04 00:21:53 UTC (rev 101795)
@@ -44,7 +44,7 @@
     /**
      * The root real filesystem (singleton instance).
      */
-    public static final RealFileSystem ROOT_INSTANCE = new RealFileSystem(new File(""));
+   
 
     private static final boolean NEEDS_CONVERSION = File.separatorChar != '/';
 

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java	2010-03-04 00:21:53 UTC (rev 101795)
@@ -0,0 +1,133 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.spi;
+
+import org.jboss.vfs.VirtualFile;
+import org.jboss.logging.Logger;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.FileInputStream;
+import java.security.CodeSigner;
+import java.util.List;
+import java.util.Arrays;
+import java.util.Collections;
+
+/**
+ * A special FileSystem which supports multiple roots.
+ * 
+ * This is currently accomplished by requiring that VirtualFile.getPathName()
+ * produce output that is consumable by java.io.File as a path.
+ */
+public final class RootFileSystem implements FileSystem {
+
+    private static final Logger log = Logger.getLogger("org.jboss.vfs.root");
+    
+    public static final RootFileSystem ROOT_INSTANCE = new RootFileSystem();
+    
+    private RootFileSystem(){}
+
+    /**
+     * {@inheritDoc}
+     */
+    public InputStream openInputStream(VirtualFile mountPoint, VirtualFile target) throws IOException {
+        return new FileInputStream(getFile(mountPoint, target));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isReadOnly() {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public File getFile(VirtualFile mountPoint, VirtualFile target) {
+        return new File(target.getPathName());
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean delete(VirtualFile mountPoint, VirtualFile target) {
+        return getFile(mountPoint, target).delete();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getSize(VirtualFile mountPoint, VirtualFile target) {
+        return getFile(mountPoint, target).length();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public long getLastModified(VirtualFile mountPoint, VirtualFile target) {
+        return getFile(mountPoint, target).lastModified();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean exists(VirtualFile mountPoint, VirtualFile target) {
+        return getFile(mountPoint, target).exists();
+    }
+
+    /** {@inheritDoc} */
+    public boolean isFile(final VirtualFile mountPoint, final VirtualFile target) {
+        return getFile(mountPoint, target).isFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isDirectory(VirtualFile mountPoint, VirtualFile target) {
+        return getFile(mountPoint, target).isDirectory();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public List<String> getDirectoryEntries(VirtualFile mountPoint, VirtualFile target) {
+        final String[] names = getFile(mountPoint, target).list();
+        return names == null ? Collections.<String>emptyList() : Arrays.asList(names);
+    }
+    
+   /**
+    * {@inheritDoc}
+    */
+   public CodeSigner[] getCodeSigners(VirtualFile mountPoint, VirtualFile target) {
+      return null;
+   }
+
+   /**
+     * {@inheritDoc}
+     */
+    public void close() throws IOException {
+        // no operation - the root FS can't be closed
+    }
+}


Property changes on: projects/vfs/trunk/src/main/java/org/jboss/vfs/spi/RootFileSystem.java
___________________________________________________________________
Name: svn:executable
   + *

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java	2010-03-04 00:15:35 UTC (rev 101794)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/AssemblyFileSystemTest.java	2010-03-04 00:21:53 UTC (rev 101795)
@@ -2,6 +2,7 @@
 
 import java.io.Closeable;
 import java.io.File;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.List;
@@ -104,7 +105,7 @@
 
          assertFalse(directoryEntries.isEmpty());
          for (VirtualFile child : directoryEntries) {
-            assertTrue(child.getPathName().startsWith("/assembly/jar1.jar"));
+            assertTrue(child.getPathName().startsWith(winFriendlyAbsolutePath("/assembly/jar1.jar")));
          }
 
       }
@@ -113,6 +114,10 @@
       }
    }
    
+   private String winFriendlyAbsolutePath(String path) {
+       return (File.separatorChar == '\\') ? "/" + new File("\\").getAbsolutePath().replace('\\', '/') + path.substring(1) : path; 
+   }
+
    @Test
    public void testFileExistence() throws Exception {
       VirtualFileAssembly assembly = new VirtualFileAssembly();

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java	2010-03-04 00:15:35 UTC (rev 101794)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/FileVFSUnitTestCase.java	2010-03-04 00:21:53 UTC (rev 101795)
@@ -783,8 +783,9 @@
       long lastModified = tmp.lastModified();
       long size = tmp.length();
       String name = tmp.getName();
-      String vfsPath = tmp.getPath();
+      
       URL url = tmp.toURI().toURL();
+      String vfsPath = url.getPath();
       log.debug("name: " + name);
       log.debug("vfsPath: " + vfsPath);
       log.debug("url: " + url);
@@ -851,8 +852,8 @@
       long lastModified = tmpJar.lastModified();
       long size = tmpJar.length();
       String name = tmpJar.getName();
-      String vfsPath = tmpJar.getPath();
       URL url = tmpJar.toURL();
+      String vfsPath = url.getPath();
       //url = JarUtils.createJarURL(url);
       log.debug("name: " + name);
       log.debug("vfsPath: " + vfsPath);

Deleted: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/SymlinkTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/SymlinkTestCase.java	2010-03-04 00:15:35 UTC (rev 101794)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/SymlinkTestCase.java	2010-03-04 00:21:53 UTC (rev 101795)
@@ -1,149 +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.test.vfs;
-
-import junit.framework.Test;
-import org.jboss.util.StringPropertyReplacer;
-import org.jboss.util.propertyeditor.URLEditor;
-import org.jboss.vfs.VFS;
-import org.jboss.vfs.VFSUtils;
-import org.jboss.vfs.VirtualFile;
-
-import java.io.Closeable;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.List;
-
-/**
- * Symlink test.
- *
- * @author <a href="ales.justin at jboss.org">Ales Justin</a>
- */
-public class SymlinkTestCase extends AbstractVFSTest
-{
-   private String testOuterJar;
-   private String testInnerJar;
-   private String testInnerFile;
-   private boolean useEditor;
-
-   public SymlinkTestCase(String name)
-   {
-      super(name);
-   }
-
-   public static Test suite()
-   {
-      return suite(SymlinkTestCase.class);
-   }
-
-   // enable this to run the test -- no Winz though :-)
-
-   private static boolean supportSymlinks()
-   {
-      return false;
-
-//      String os = System.getProperty("os.name");
-//      return os.contains("Win") == false;
-   }
-
-   @Override
-   protected void setUp() throws Exception
-   {
-      super.setUp();
-
-      // set force canonical
-      System.setProperty("jboss.vfs.forceCanonical", Boolean.TRUE.toString());
-
-      // setup symlink dir and test path!
-
-//      System.setProperty("test.dir", "/Users/alesj/projects/jboss6/trunk"); // plain path
-      System.setProperty("test.dir", "/Users/alesj/jboss"); // -- this is symlink
-
-      testOuterJar = "/testsuite/output/lib/jboss-seam-booking.ear";
-      testInnerJar = "jboss-seam.jar";
-      testInnerFile = "org/jboss/seam/Seam.class";
-      useEditor = true;
-   }
-
-   @Override
-   protected void tearDown() throws Exception
-   {
-      System.clearProperty("jboss.vfs.forceCanonical");
-      System.clearProperty("test.dir");
-
-      testOuterJar = null;
-      testInnerJar = null;
-      testInnerFile = null;
-
-      super.tearDown();
-   }
-
-   public void testSmoke() throws Exception
-   {
-      if (supportSymlinks() == false)
-         return;
-
-      assertNotNull(testOuterJar);
-      assertNotNull(testInnerJar);
-      assertNotNull(testInnerFile);
-
-      String rootText = StringPropertyReplacer.replaceProperties("${test.dir}");
-      URL rootURL;
-      if (useEditor)
-      {
-         URLEditor editor = new URLEditor();
-         editor.setAsText(rootText);
-         rootURL = (URL) editor.getValue();
-      }
-      else
-      {
-         rootURL = new URL("file://" + rootText);
-      }
-
-      VirtualFile root = VFS.getChild(rootURL);
-      VirtualFile outerJar = root.getChild(testOuterJar);
-      List<Closeable> closables = recursiveMount(outerJar);
-      try
-      {
-         VirtualFile innerJar = outerJar.getChild(testInnerJar);
-
-         VirtualFile file = innerJar.getChild(testInnerFile);
-         assertNotNull(file);
-         assertTrue(file.exists());
-         assertTrue(file.getSize() > 0);
-         URL url = file.toURL();
-         URLConnection conn = url.openConnection();
-         long expected = file.getLastModified();
-         long actual1 = conn.getLastModified();
-         assertEquals(expected, actual1);
-
-         URL directRootURL = new URL("file://" + rootText + testOuterJar + "/" + testInnerJar + "/" + testInnerFile);
-         conn = directRootURL.openConnection();
-         long actual2 = conn.getLastModified();
-         assertEquals(expected, actual2);
-      }
-      finally
-      {
-         VFSUtils.safeClose(closables);
-      }
-   }
-}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list