KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genbase > archive > FileArchive


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  * Copyright (C) 1999-2004 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  * --------------------------------------------------------------------------
22  * $Id: FileArchive.java,v 1.1 2004/10/11 13:16:14 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genbase.archive;
27
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Vector JavaDoc;
34 import java.util.jar.Manifest JavaDoc;
35
36 import org.objectweb.jonas_lib.genbase.GenBaseException;
37
38
39 /**
40  * A <code>FileArchive</code> is a wrapper for directory structured as a jar.
41  *
42  * @author Guillaume Sauthier
43  */

44 public class FileArchive extends AbsArchive {
45
46     /**
47      * Creates a new FileArchive object.
48      *
49      * @param archive directory structured as a jar
50      *
51      * @throws GenBaseException When manifest cannot be loaded
52      */

53     public FileArchive(File JavaDoc archive) throws GenBaseException {
54         super(archive);
55
56         try {
57             File JavaDoc mf = new File JavaDoc(archive, "META-INF" + File.separator + "MANIFEST.MF");
58
59             if (mf.exists()) {
60                 setManifest(new Manifest JavaDoc(new FileInputStream JavaDoc(mf)));
61             } else {
62                 setManifest(new Manifest JavaDoc());
63             }
64         } catch (Exception JavaDoc e) {
65             String JavaDoc err = getI18n().getMessage("FileArchive.constr.manifest", getRootFile());
66             throw new GenBaseException(err, e);
67         }
68     }
69
70     /**
71      * Returns an InputStream corresponding to the given filename.
72      *
73      * @param filename file name source of the InputStream
74      *
75      * @return the InputStream corresponding to the given filename.
76      *
77      * @throws IOException When InputStream corersponding to the given filename
78      * cannot be found.
79      */

80     public InputStream JavaDoc getInputStream(String JavaDoc filename) throws IOException JavaDoc {
81         File JavaDoc file = (File JavaDoc) getFiles().get(filename);
82
83         if (file == null) {
84             // filename not found in added files
85
// try root search
86
file = new File JavaDoc(getRootFile(), filename);
87
88             if (!file.exists()) {
89                 return null;
90             }
91         }
92
93         // file exists (in added files or in original archive)
94
return new FileInputStream JavaDoc(file);
95     }
96
97     /**
98      * Returns a List of all files contained in this archive. Original files in
99      * jar, added Files are all included as String in this Enumeration.
100      *
101      * @return a List of all files contained in this archive.
102      */

103     public List JavaDoc getContainedFiles() {
104         List JavaDoc list = new Vector JavaDoc(getFiles().keySet());
105
106         // add files of the original archive
107
traverse("", getRootFile(), list);
108
109         return list;
110     }
111
112     /**
113      * Add all files contained in the given directory into a list.
114      *
115      * @param dirName current directory name
116      * @param base directory where file are listed
117      * @param map list of filename
118      */

119     private static void traverse(String JavaDoc dirName, File JavaDoc base, List JavaDoc map) {
120         File JavaDoc[] childs = base.listFiles();
121
122         // directory exists ?
123
if (childs != null) {
124             for (int i = 0; i < childs.length; i++) {
125                 if (childs[i].isFile()) {
126                     // File
127
map.add(dirName + childs[i].getName());
128                 } else {
129                     // Directory
130
traverse(dirName + childs[i].getName() + File.separator, childs[i], map);
131                 }
132             }
133         }
134     }
135
136     /**
137      * Returns true if archive is packed or false if archive is unpacked.
138      *
139      * @return true if archive is packed or false if archive is unpacked.
140      */

141     public boolean isPacked() {
142         return false;
143     }
144 }
Popular Tags