KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > loader > locator > DirLocator


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Guillaume SAUTHIER
22  * --------------------------------------------------------------------------
23  * $Id: DirLocator.java,v 1.3 2004/04/19 13:49:09 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas_lib.loader.locator;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * A <code>DirLocator</code> is used to look up for a file
36  * inside a directory.
37  *
38  * @author Guillaume Sauthier
39  */

40 public class DirLocator extends Locator {
41
42     /**
43      * Wrapped File where search will be performed.
44      */

45     private File JavaDoc file = null;
46
47     /**
48      * Construct a new DirLocator from an URL pointing to a directory.
49      *
50      * @param jar URL pointing to a directory.
51      *
52      * @throws IOException When
53      */

54     public DirLocator(URL JavaDoc jar) throws IOException JavaDoc {
55         String JavaDoc filename = jar.getFile();
56         file = new File JavaDoc(filename);
57         if (!file.exists()) {
58             throw new IOException JavaDoc("File " + file + " does not exists.");
59         }
60         if (!file.isDirectory()) {
61             throw new IOException JavaDoc("File " + file + " is not a directory.");
62         }
63     }
64
65     /**
66      * Returns true when file was found.
67      *
68      * @param path the path to the file to look up
69      *
70      * @return true when file was found, otherwise false.
71      */

72     public boolean hasFile(String JavaDoc path) {
73
74         File JavaDoc child = new File JavaDoc(file, path);
75         return (child.exists() && child.isFile());
76     }
77
78     /**
79      * Returns true when directory was found.
80      *
81      * @param path the path to the directory to look up
82      *
83      * @return true when directory was found, otherwise false.
84      */

85     public boolean hasDirectory(String JavaDoc path) {
86
87         File JavaDoc child = new File JavaDoc(file, path);
88         return (child.exists() && child.isDirectory());
89     }
90
91     /**
92      * Returns a list of filename stored in path.
93      *
94      * @param path the path to the directory where looking for files
95      *
96      * @return a list of filename stored in path.
97      */

98     public List JavaDoc listContent(String JavaDoc path) {
99
100         File JavaDoc child = new File JavaDoc(file, path);
101         List JavaDoc libs = new Vector JavaDoc();
102         // List directory content
103
if (child.isDirectory()) {
104             addContent(child, libs);
105         }
106
107         return libs;
108     }
109
110     /**
111      * Add only files in the given List.
112      * Recursive!
113      *
114      * @param f base directory to explore
115      * @param l file list to be filled
116      */

117     private void addContent(File JavaDoc f, List JavaDoc l) {
118         File JavaDoc[] childs = f.listFiles();
119         if (childs != null) {
120             for (int i = 0; i < childs.length; i++) {
121                 if (childs[i].isDirectory()) {
122                     addContent(childs[i], l);
123                 } else if (childs[i].isFile()) {
124                     l.add(childs[i].getPath().substring(file.getPath().length()));
125                 }
126             }
127         }
128     }
129
130 }
131
Popular Tags