1 22 package org.jboss.net.protocol.file; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.FilenameFilter ; 27 import java.io.IOException ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.net.protocol.URLListerBase; 35 36 43 public class FileURLLister extends URLListerBase 44 { 45 46 private static final Logger log = Logger.getLogger(FileURLLister.class); 47 48 50 public Collection listMembers(URL baseUrl, URLFilter filter) throws IOException 51 { 52 return listMembers(baseUrl, filter, false); 53 } 54 55 public Collection listMembers(URL baseUrl, URLFilter filter, boolean scanNonDottedSubDirs) throws IOException 56 { 57 String baseUrlString = baseUrl.toString(); 59 if (!baseUrlString.endsWith("/")) 60 { 61 throw new IOException ("Does not end with '/', not a directory url: " + baseUrlString); 62 } 63 64 File dir = new File (baseUrl.getPath()); 66 if (!dir.isDirectory()) 67 { 68 throw new FileNotFoundException ("Not pointing to a directory, url: " + baseUrlString); 69 } 70 71 ArrayList resultList = new ArrayList (); 73 74 listFiles(baseUrl, filter, scanNonDottedSubDirs, resultList); 76 77 return resultList; 79 } 80 81 83 88 private void listFiles(final URL baseUrl, final URLFilter filter, boolean scanNonDottedSubDirs, ArrayList resultList) 89 throws IOException 90 { 91 final File baseDir = new File (baseUrl.getPath()); 93 String [] filenames = baseDir.list(new FilenameFilter () 94 { 95 public boolean accept(File dir, String name) 96 { 97 try 98 { 99 return filter.accept(baseUrl, name); 100 } 101 catch (Exception e) 102 { 103 log.debug("Unexpected exception filtering entry '" + name + "' in directory '" + baseDir + "'", e); 104 return true; 105 } 106 } 107 }); 108 109 if (filenames == null) 110 { 111 throw new IOException ("Could not list directory '" + baseDir + "', reason unknown"); 116 } 117 else 118 { 119 String baseUrlString = baseUrl.toString(); 120 121 for (int i = 0; i < filenames.length; i++) 122 { 123 String filename = filenames[i]; 124 125 File file = new File (baseDir, filename); 127 boolean isDir = file.isDirectory(); 128 129 URL subUrl = createURL(baseUrlString, filename, isDir); 131 132 if (scanNonDottedSubDirs && isDir && (filename.indexOf('.') == -1)) 137 { 138 listFiles(subUrl, filter, scanNonDottedSubDirs, resultList); 140 } 141 else 142 { 143 resultList.add(subUrl); 145 } 146 } 147 } 148 } 149 150 154 private URL createURL(String baseUrlString, String filename, boolean isDirectory) 155 { 156 try 157 { 158 return new URL (baseUrlString + filename + (isDirectory ? "/" : "")); 159 } 160 catch (MalformedURLException e) 161 { 162 throw new IllegalStateException (); 164 } 165 } 166 167 } 168 | Popular Tags |