KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > protocol > file > FileURLLister


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.net.protocol.file;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FilenameFilter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collection JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.net.protocol.URLListerBase;
35
36 /**
37  * FileURLLister
38  *
39  * @author jboynes@users.sf.net
40  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
41  * @version $Revision: 1958 $
42  */

43 public class FileURLLister extends URLListerBase
44 {
45    /** The Logger */
46    private static final Logger log = Logger.getLogger(FileURLLister.class);
47    
48    // Public --------------------------------------------------------
49

50    public Collection JavaDoc listMembers(URL JavaDoc baseUrl, URLFilter filter) throws IOException JavaDoc
51    {
52       return listMembers(baseUrl, filter, false);
53    }
54
55    public Collection JavaDoc listMembers(URL JavaDoc baseUrl, URLFilter filter, boolean scanNonDottedSubDirs) throws IOException JavaDoc
56    {
57       // Make sure this is a directory URL
58
String JavaDoc baseUrlString = baseUrl.toString();
59       if (!baseUrlString.endsWith("/"))
60       {
61          throw new IOException JavaDoc("Does not end with '/', not a directory url: " + baseUrlString);
62       }
63       
64       // Verify the directory actually exists
65
File JavaDoc dir = new File JavaDoc(baseUrl.getPath());
66       if (!dir.isDirectory())
67       {
68          throw new FileNotFoundException JavaDoc("Not pointing to a directory, url: " + baseUrlString);
69       }
70       
71       // The list of URLs to return
72
ArrayList JavaDoc resultList = new ArrayList JavaDoc();
73
74       // Do the actual job
75
listFiles(baseUrl, filter, scanNonDottedSubDirs, resultList);
76       
77       // Done
78
return resultList;
79    }
80    
81    // Private -------------------------------------------------------
82

83    /**
84     * Starting from baseUrl, that should point to a directory, populate the
85     * resultList with the contents that pass the filter (in the form of URLs)
86     * and possibly recurse into subdris not containing a '.' in their name.
87     */

88    private void listFiles(final URL JavaDoc baseUrl, final URLFilter filter, boolean scanNonDottedSubDirs, ArrayList JavaDoc resultList)
89       throws IOException JavaDoc
90    {
91       // List the files at the current dir level, using the provided filter
92
final File JavaDoc baseDir = new File JavaDoc(baseUrl.getPath());
93       String JavaDoc[] filenames = baseDir.list(new FilenameFilter JavaDoc()
94       {
95          public boolean accept(File JavaDoc dir, String JavaDoc name)
96          {
97             try
98             {
99                return filter.accept(baseUrl, name);
100             }
101             catch (Exception JavaDoc 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          // This happens only when baseDir not a directory (but this is already
112
// checked by the caller) or some unknown IOException happens internally
113
// (e.g. run out of file descriptors?). Unfortunately the File API
114
// doesn't provide a way to know.
115
throw new IOException JavaDoc("Could not list directory '" + baseDir + "', reason unknown");
116       }
117       else
118       {
119          String JavaDoc baseUrlString = baseUrl.toString();
120          
121          for (int i = 0; i < filenames.length; i++)
122          {
123             String JavaDoc filename = filenames[i];
124             
125             // Find out if this is a directory
126
File JavaDoc file = new File JavaDoc(baseDir, filename);
127             boolean isDir = file.isDirectory();
128             
129             // The subUrl
130
URL JavaDoc subUrl = createURL(baseUrlString, filename, isDir);
131             
132             // If scanning subdirs and we have a directory, not containing a '.' in
133
// the name, recurse into it. This is to allow recursing into grouping
134
// dirs like ./deploy/jms, ./deploy/management, etc., avoiding
135
// at the same time exploded packages, like .sar, .war, etc.
136
if (scanNonDottedSubDirs && isDir && (filename.indexOf('.') == -1))
137             {
138                // recurse into it
139
listFiles(subUrl, filter, scanNonDottedSubDirs, resultList);
140             }
141             else
142             {
143                // just add to the list
144
resultList.add(subUrl);
145             }
146          }
147       }
148    }
149    
150    /**
151     * Create a URL by concatenating the baseUrlString that should end at '/',
152     * the filename, and a trailing slash, if it points to a directory
153     */

154    private URL JavaDoc createURL(String JavaDoc baseUrlString, String JavaDoc filename, boolean isDirectory)
155    {
156       try
157       {
158          return new URL JavaDoc(baseUrlString + filename + (isDirectory ? "/" : ""));
159       }
160       catch (MalformedURLException JavaDoc e)
161       {
162          // shouldn't happen
163
throw new IllegalStateException JavaDoc();
164       }
165    }
166    
167 }
168
Popular Tags