KickJava   Java API By Example, From Geeks To Geeks.

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


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: AbsArchive.java,v 1.2 2005/04/27 12:28:21 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genbase.archive;
27
28 import java.io.File JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.jar.Manifest JavaDoc;
32
33 import org.objectweb.jonas_lib.I18n;
34
35 /**
36  * An <code>AbsArchive</code> centralized commonly used methods for Jar and
37  * File support.
38  *
39  * @author Guillaume Sauthier
40  */

41 public abstract class AbsArchive implements Archive {
42
43     /** i18n */
44     private static I18n i18n = I18n.getInstance(AbsArchive.class);
45
46     /** root File of the Archive */
47     private File JavaDoc root;
48
49     /** the archive Manifest */
50     private Manifest JavaDoc manifest = null;
51
52     /** map between name and files added in the archive */
53     private Map JavaDoc files = null;
54
55     /**
56      * Create a FileArchive where the root if the given file.
57      *
58      * @param file the directory base of the archive
59      */

60     public AbsArchive(File JavaDoc file) {
61         root = file;
62         files = new Hashtable JavaDoc();
63     }
64
65     /**
66      * add the content of the given directory into the root of the archive.
67      *
68      * @param directory directory to add
69      */

70     public void addDirectory(File JavaDoc directory) {
71         addDirectoryIn("", directory);
72     }
73
74     /**
75      * add the content of the given directory into the given directory of the
76      * archive.
77      *
78      * @param dirName archive directory name.
79      * @param directory directory to add.
80      */

81     public void addDirectoryIn(String JavaDoc dirName, File JavaDoc directory) {
82         File JavaDoc[] childs = directory.listFiles();
83
84         // directory exists ?
85
if (childs != null) {
86             for (int i = 0; i < childs.length; i++) {
87                 if (childs[i].isFile()) {
88                     // File
89
addFileIn(dirName, childs[i]);
90                 } else {
91                     // Directory
92
addDirectoryIn(dirName + childs[i].getName() + File.separator, childs[i]);
93                 }
94             }
95         }
96     }
97
98     /**
99      * add a lonely file into the root directory of the archive.
100      *
101      * @param file the file to be added.
102      */

103     public void addFile(File JavaDoc file) {
104         addFileIn("", file);
105     }
106
107     /**
108      * add a file into the root directory of the archive with a specified name.
109      *
110      * @param file the file to be added.
111      * @param name filename
112      */

113     public void addFile(File JavaDoc file, String JavaDoc name) {
114         files.put(name, file);
115     }
116
117     /**
118      * add a lonely file into the given directory of the archive.
119      *
120      * @param dirName archive directory name.
121      * @param file the file to be added.
122      */

123     public void addFileIn(String JavaDoc dirName, File JavaDoc file) {
124         files.put(dirName + file.getName(), file);
125     }
126
127     /**
128      * Returns the File corresponding to the root of the archive.
129      *
130      * @return the File corresponding to the root of the archive.
131      */

132     public File JavaDoc getRootFile() {
133         return root;
134     }
135
136     /**
137      * Returns the name of the Archive.
138      *
139      * @return the name of the Archive.
140      */

141     public String JavaDoc getName() {
142         return root.getName();
143     }
144
145     /**
146      * @return Returns the manifest.
147      */

148     public Manifest JavaDoc getManifest() {
149         if (manifest == null) {
150             manifest = new Manifest JavaDoc();
151         }
152         return manifest;
153     }
154     /**
155      * @param manifest The manifest to set.
156      */

157     public void setManifest(Manifest JavaDoc manifest) {
158         this.manifest = manifest;
159     }
160     /**
161      * @return Returns the i18n.
162      */

163     public static I18n getI18n() {
164         return i18n;
165     }
166     /**
167      * @return Returns the files.
168      */

169     public Map JavaDoc getFiles() {
170         return files;
171     }
172
173     /**
174      * close this archive
175      */

176     public void close() {
177         this.files = null;
178         this.manifest = null;
179     }
180 }
Popular Tags