001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Guillaume SAUTHIER
022: * --------------------------------------------------------------------------
023: * $Id: DirLocator.java 4622 2004-04-19 13:49:54Z sauthieg $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.loader.locator;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.net.URL;
030: import java.util.List;
031: import java.util.Vector;
032:
033: /**
034: * A <code>DirLocator</code> is used to look up for a file
035: * inside a directory.
036: *
037: * @author Guillaume Sauthier
038: */
039: public class DirLocator extends Locator {
040:
041: /**
042: * Wrapped File where search will be performed.
043: */
044: private File file = null;
045:
046: /**
047: * Construct a new DirLocator from an URL pointing to a directory.
048: *
049: * @param jar URL pointing to a directory.
050: *
051: * @throws IOException When
052: */
053: public DirLocator(URL jar) throws IOException {
054: String filename = jar.getFile();
055: file = new File(filename);
056: if (!file.exists()) {
057: throw new IOException("File " + file + " does not exists.");
058: }
059: if (!file.isDirectory()) {
060: throw new IOException("File " + file
061: + " is not a directory.");
062: }
063: }
064:
065: /**
066: * Returns true when file was found.
067: *
068: * @param path the path to the file to look up
069: *
070: * @return true when file was found, otherwise false.
071: */
072: public boolean hasFile(String path) {
073:
074: File child = new File(file, path);
075: return (child.exists() && child.isFile());
076: }
077:
078: /**
079: * Returns true when directory was found.
080: *
081: * @param path the path to the directory to look up
082: *
083: * @return true when directory was found, otherwise false.
084: */
085: public boolean hasDirectory(String path) {
086:
087: File child = new File(file, path);
088: return (child.exists() && child.isDirectory());
089: }
090:
091: /**
092: * Returns a list of filename stored in path.
093: *
094: * @param path the path to the directory where looking for files
095: *
096: * @return a list of filename stored in path.
097: */
098: public List listContent(String path) {
099:
100: File child = new File(file, path);
101: List libs = new Vector();
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 f, List l) {
118: File[] 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(
125: file.getPath().length()));
126: }
127: }
128: }
129: }
130:
131: }
|