KickJava   Java API By Example, From Geeks To Geeks.

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


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: JarFileLocator.java,v 1.2 2004/04/19 13:49:09 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26 package org.objectweb.jonas_lib.loader.locator;
27
28 import java.io.IOException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34 import java.util.zip.ZipEntry JavaDoc;
35
36 /**
37  * A <code>JarfileLocator</code> is used to look up for a file inside a Jar
38  * archive. Works only for local jars.
39  *
40  * @author Guillaume Sauthier
41  */

42 public class JarFileLocator extends Locator {
43
44     /**
45      * Wrapped JarFile where searchs will be performed
46      */

47     private JarFile JavaDoc file = null;
48
49     /**
50      * Construct a new JarFileLocator from an URL representing a Jar.
51      *
52      * @param jar URL pointing to a Jar file
53      *
54      * @throws IOException When URL is not a JarFile.
55      */

56     public JarFileLocator(URL JavaDoc jar) throws IOException JavaDoc {
57         String JavaDoc filename = jar.getFile();
58         file = new JarFile JavaDoc(filename);
59     }
60
61     /**
62      * Returns true when file was found.
63      *
64      * @param path the path to the file to look up
65      *
66      * @return true when file was found, otherwise false.
67      */

68     public boolean hasFile(String JavaDoc path) {
69
70         ZipEntry JavaDoc entry = file.getEntry(path);
71         return (entry != null);
72     }
73
74     /**
75      * Returns true when directory was found.
76      *
77      * @param path the path to the directory to look up
78      *
79      * @return true when directory was found, otherwise false.
80      */

81     public boolean hasDirectory(String JavaDoc path) {
82
83         boolean found = false;
84         for (Enumeration JavaDoc e = file.entries(); e.hasMoreElements() && !found;) {
85             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) e.nextElement();
86             if (entry.getName().startsWith(path)) {
87                 return true;
88             }
89         }
90
91         return false;
92     }
93
94     /**
95      * Returns a list of filename stored in path.
96      *
97      * @param path the path to the directory where looking for files
98      *
99      * @return a list of filename stored in path.
100      */

101     public List JavaDoc listContent(String JavaDoc path) {
102
103         List JavaDoc libs = new Vector JavaDoc();
104         for (Enumeration JavaDoc e = file.entries(); e.hasMoreElements();) {
105             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) e.nextElement();
106             if (entry.getName().startsWith(path)) {
107                 libs.add(entry.getName());
108             }
109         }
110
111         return libs;
112     }
113
114 }
Popular Tags