[jboss-cvs] JBossAS SVN: r57271 - in branches/JBoss_4_0_2_CP: common/src/main/org/jboss/net/protocol/file testsuite/src/main/org/jboss/test/util/test testsuite/src/resources/util testsuite/src/resources/util/fileurllister testsuite/src/resources/util/fileurllister/deploy testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Sep 28 22:16:37 EDT 2006


Author: ryan.campbell at jboss.com
Date: 2006-09-28 22:16:35 -0400 (Thu, 28 Sep 2006)
New Revision: 57271

Added:
   branches/JBoss_4_0_2_CP/testsuite/src/main/org/jboss/test/util/test/FileURLListerUnitTestCase.java
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir/
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir/four.xml
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir/
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir/three.xml
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/one.xml
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/two.xml
Removed:
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir/
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir/four.xml
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir/
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir/three.xml
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/one.xml
   branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/two.xml
Modified:
   branches/JBoss_4_0_2_CP/common/src/main/org/jboss/net/protocol/file/FileURLLister.java
Log:
ASPATCH-47: JBAS-2099: Patch request for: NPE in FileURLListener - JBAS-1905

Modified: branches/JBoss_4_0_2_CP/common/src/main/org/jboss/net/protocol/file/FileURLLister.java
===================================================================
--- branches/JBoss_4_0_2_CP/common/src/main/org/jboss/net/protocol/file/FileURLLister.java	2006-09-28 18:20:24 UTC (rev 57270)
+++ branches/JBoss_4_0_2_CP/common/src/main/org/jboss/net/protocol/file/FileURLLister.java	2006-09-29 02:16:35 UTC (rev 57271)
@@ -1,132 +1,150 @@
-/***************************************
- *                                     *
- *  JBoss: The OpenSource J2EE WebOS   *
- *                                     *
- *  Distributable under LGPL license.  *
- *  See terms of license at gnu.org.   *
- *                                     *
- ***************************************/
-
-
+/*  
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
 package org.jboss.net.protocol.file;
 
-import java.io.*;
 import java.io.File;
-import java.io.FileFilter;
+import java.io.FileNotFoundException;
+import java.io.FilenameFilter;
 import java.io.IOException;
-import java.io.FileNotFoundException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 
+import org.jboss.logging.Logger;
 import org.jboss.net.protocol.URLListerBase;
 
+/**
+ * FileURLLister
+ *
+ * @author jboynes at users.sf.net
+ * @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
+ * @version $Revision$
+ */
 public class FileURLLister extends URLListerBase
 {
-   public Collection listMembers (final URL baseUrl, final URLFilter filter) throws IOException   
+   /** The Logger */
+   private static final Logger log = Logger.getLogger(FileURLLister.class);
+   
+   // Public --------------------------------------------------------
+   
+   public Collection listMembers(URL baseUrl, URLFilter filter) throws IOException   
    {
-      return listMembers (baseUrl, filter, false);
+      return listMembers(baseUrl, filter, false);
    }
 
-   public Collection listMembers (final URL baseUrl, final URLFilter filter, boolean scanNonDottedSubDirs) throws IOException
+   public Collection listMembers(URL baseUrl, URLFilter filter, boolean scanNonDottedSubDirs) throws IOException
    {
-      File directory = new File (baseUrl.getPath ());
-      if (directory.exists () == false)
+      // Make sure this is a directory URL
+      String baseUrlString = baseUrl.toString();
+      if (!baseUrlString.endsWith("/"))
       {
-         throw new FileNotFoundException (directory.toString ());
+         throw new IOException("Does not end with '/', not a directory url: " + baseUrlString);
       }
-      return listFiles (baseUrl, filter, scanNonDottedSubDirs);
       
+      // Verify the directory actually exists
+      File dir = new File(baseUrl.getPath());
+      if (!dir.isDirectory())
+      {
+         throw new FileNotFoundException("Not pointing to a directory, url: " + baseUrlString);
+      }
+      
+      // The list of URLs to return
+      ArrayList resultList = new ArrayList();
+
+      // Do the actual job
+      listFiles(baseUrl, filter, scanNonDottedSubDirs, resultList);
+      
+      // Done
+      return resultList;
    }
    
-   protected Collection listFiles (URL baseUrl, final URLFilter filter, boolean scanNonDottedSubDirs)
+   // Private -------------------------------------------------------
+   
+   /**
+    * Starting from baseUrl, that should point to a directory, populate the
+    * resultList with the contents that pass the filter (in the form of URLs)
+    * and possibly recurse into subdris not containing a '.' in their name.
+    */
+   private void listFiles(final URL baseUrl, final URLFilter filter, boolean scanNonDottedSubDirs, ArrayList resultList)
    {      
-      // List files at the current dir level
-      //
-      final File directory = new File (baseUrl.getPath ());      
-      File[] files = directory.listFiles (new FileFilter ()
+      // List the files at the current dir level, using the provided filter
+      final File baseDir = new File(baseUrl.getPath());      
+      String[] filenames = baseDir.list(new FilenameFilter()
       {
-         public boolean accept (File file)
+         public boolean accept(File dir, String name)
          {
             try
             {
-               return filter.accept (directory.toURL (), file.getName ());
+               return filter.accept(baseUrl, name);
             }
-            catch (Exception forgetIt)
+            catch (Exception e)
             {
-               forgetIt.printStackTrace ();
+               log.debug("Unexpected exception filtering entry '" + name + "' in directory '" + baseDir + "'", e);
                return true;
             }
          }
-      });            
+      });
       
-      // Do we have to scan sub-dirs as well?
-      //
-      if (scanNonDottedSubDirs)
+      if (filenames == null)
       {
-         ArrayList result = new ArrayList ();
+         // This happens only when baseDir not a directory, or some unknown
+         // IOException happens internally (e.g. run out of file descriptors?)
+         // Unfortunately the File API doesn't provide a way to know.
+         log.warn("Could not list directory '" + baseDir + "', reason unknown");
+      }      
+      else
+      {
+         String baseUrlString = baseUrl.toString();
          
-         for (int i = 0; i < files.length; i++)
+         for (int i = 0; i < filenames.length; i++)
          {
-            File file = files[i];
+            String filename = filenames[i];
             
-            if (doesDirTriggersRecursiveSearch (file))
-            {  
-               result.addAll (listFiles (prependDirToUrl (baseUrl, file.getName (), true), filter, scanNonDottedSubDirs));
+            // Find out if this is a directory
+            File file = new File(baseDir, filename);
+            boolean isDir = file.isDirectory();
+            
+            // The subUrl
+            URL subUrl = createURL(baseUrlString, filename, isDir);
+            
+            // If scanning subdirs and we have a directory, not containing a '.' in
+            // the name, recurse into it. This is to allow recursing into grouping
+            // dirs like ./deploy/jms, ./deploy/management, etc., avoiding
+            // at the same time exploded packages, like .sar, .war, etc.
+            if (scanNonDottedSubDirs && isDir && (filename.indexOf('.') == -1))
+            {
+               // recurse into it
+               listFiles(subUrl, filter, scanNonDottedSubDirs, resultList);
             }
             else
-            {               
-               result.add ( fileToURL (baseUrl, file) );
+            {
+               // just add to the list
+               resultList.add(subUrl);                
             }
-         }         
-         
-         return result;
+         }
       }
-      else
-      {
-         // We don't scan sub-dirs ==> we simply add the URL to the result (after transformation)
-         //
-         return filesToURLs(baseUrl, files);
-      }
-            
-   }
+   }  
    
-   protected boolean doesDirTriggersRecursiveSearch (File file)
+   /**
+    * Create a URL by concatenating the baseUrlString that should end at '/',
+    * the filename, and a trailing slash, if it points to a directory
+    */
+   private URL createURL(String baseUrlString, String filename, boolean isDirectory)
    {
-      // File is a directory and contains NO "." (which would means it 
-      // could be an exploded package that needs to be deployed i.e. .war, etc.)
-      //
-      return file.isDirectory () && (file.getName ().indexOf (".") == -1);
-   }
-   
-   protected URL prependDirToUrl (URL baseUrl, String addenda, boolean isDirectory)
-   {
       try
       {
-         String base = baseUrl.toString ();
-         return new URL (base + (base.endsWith ("/") ? "" : "/" ) + addenda + (isDirectory ? "/" : "") );
+         return new URL(baseUrlString + filename + (isDirectory ? "/" : ""));
       } 
       catch (MalformedURLException e)
       {
          // shouldn't happen
-         throw new IllegalStateException ();
+         throw new IllegalStateException();
       }
    }
    
-   protected Collection filesToURLs (final URL baseUrl, File[] files)
-   {
-      ArrayList result = new ArrayList (files.length);
-      
-      for (int i = 0; i < files.length; i++)
-         result.add (fileToURL (baseUrl, files[i]));
-      
-      return result;
-   }
-      
-   protected URL fileToURL (final URL baseUrl, File file)
-   {
-      return prependDirToUrl (baseUrl, file.getName (), file.isDirectory ());         
-   }
 }

Copied: branches/JBoss_4_0_2_CP/testsuite/src/main/org/jboss/test/util/test/FileURLListerUnitTestCase.java (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/main/org/jboss/test/util/test/FileURLListerUnitTestCase.java)

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister)

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister/deploy)

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir)

Deleted: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir/four.xml
===================================================================

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir/four.xml (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister/deploy/dotted.sub.dir/four.xml)

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir)

Deleted: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir/three.xml
===================================================================

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir/three.xml (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister/deploy/nondottedsubdir/three.xml)

Deleted: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/one.xml
===================================================================

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/one.xml (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister/deploy/one.xml)

Deleted: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/two.xml
===================================================================

Copied: branches/JBoss_4_0_2_CP/testsuite/src/resources/util/fileurllister/deploy/two.xml (from rev 57270, branches/JBoss_4_0_2_JBAS-2099/testsuite/src/resources/util/fileurllister/deploy/two.xml)




More information about the jboss-cvs-commits mailing list