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: JarFileLocator.java 4622 2004-04-19 13:49:54Z sauthieg $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.jonas_lib.loader.locator;
026:
027: import java.io.IOException;
028: import java.net.URL;
029: import java.util.Enumeration;
030: import java.util.List;
031: import java.util.Vector;
032: import java.util.jar.JarFile;
033: import java.util.zip.ZipEntry;
034:
035: /**
036: * A <code>JarfileLocator</code> is used to look up for a file inside a Jar
037: * archive. Works only for local jars.
038: *
039: * @author Guillaume Sauthier
040: */
041: public class JarFileLocator extends Locator {
042:
043: /**
044: * Wrapped JarFile where searchs will be performed
045: */
046: private JarFile file = null;
047:
048: /**
049: * Construct a new JarFileLocator from an URL representing a Jar.
050: *
051: * @param jar URL pointing to a Jar file
052: *
053: * @throws IOException When URL is not a JarFile.
054: */
055: public JarFileLocator(URL jar) throws IOException {
056: String filename = jar.getFile();
057: file = new JarFile(filename);
058: }
059:
060: /**
061: * Returns true when file was found.
062: *
063: * @param path the path to the file to look up
064: *
065: * @return true when file was found, otherwise false.
066: */
067: public boolean hasFile(String path) {
068:
069: ZipEntry entry = file.getEntry(path);
070: return (entry != null);
071: }
072:
073: /**
074: * Returns true when directory was found.
075: *
076: * @param path the path to the directory to look up
077: *
078: * @return true when directory was found, otherwise false.
079: */
080: public boolean hasDirectory(String path) {
081:
082: boolean found = false;
083: for (Enumeration e = file.entries(); e.hasMoreElements()
084: && !found;) {
085: ZipEntry entry = (ZipEntry) e.nextElement();
086: if (entry.getName().startsWith(path)) {
087: return true;
088: }
089: }
090:
091: return false;
092: }
093:
094: /**
095: * Returns a list of filename stored in path.
096: *
097: * @param path the path to the directory where looking for files
098: *
099: * @return a list of filename stored in path.
100: */
101: public List listContent(String path) {
102:
103: List libs = new Vector();
104: for (Enumeration e = file.entries(); e.hasMoreElements();) {
105: ZipEntry entry = (ZipEntry) e.nextElement();
106: if (entry.getName().startsWith(path)) {
107: libs.add(entry.getName());
108: }
109: }
110:
111: return libs;
112: }
113:
114: }
|