KickJava   Java API By Example, From Geeks To Geeks.

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


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: J2EEArchive.java,v 1.4 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.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.jar.Manifest JavaDoc;
34
35 import org.objectweb.jonas_lib.I18n;
36 import org.objectweb.jonas_lib.genbase.GenBaseException;
37 import org.objectweb.jonas_lib.genbase.generator.Config;
38 import org.objectweb.jonas_lib.genbase.generator.GeneratorFactories;
39 import org.objectweb.jonas_lib.genbase.generator.GeneratorFactory;
40
41 import org.objectweb.jonas.common.Log;
42
43 import org.objectweb.util.monolog.api.Logger;
44
45
46 /**
47  * Base Class for all J2EE modules (application, ejbjar, client, webapp).
48  *
49  * @author Guillaume Sauthier
50  * @see org.objectweb.jonas_lib.genbase.archive.Archive
51  */

52 public abstract class J2EEArchive implements Archive {
53     /** i18n */
54     private static I18n i18n = I18n.getInstance(J2EEArchive.class);
55
56     /** logger */
57     private static Logger logger = Log.getLogger(Log.JONAS_GENBASE_PREFIX);
58
59     /** encapsulated archive */
60     private Archive archive;
61
62     /** J2EE Archive inner ClassLoader */
63     private ClassLoader JavaDoc moduleClassloader = null;
64
65     /**
66      * Creates a new J2EEArchive object.
67      *
68      * @param archive Archive containing files
69      */

70     public J2EEArchive(Archive archive) {
71         this.archive = archive;
72     }
73
74     /**
75      * add the content of the given directory into the root of the archive.
76      *
77      * @param directory directory to add
78      */

79     public void addDirectory(File JavaDoc directory) {
80         archive.addDirectory(directory);
81     }
82
83     /**
84      * add the content of the given directory into the given directory of the
85      * archive.
86      *
87      * @param dirName archive directory name.
88      * @param directory directory to add.
89      */

90     public void addDirectoryIn(String JavaDoc dirName, File JavaDoc directory) {
91         archive.addDirectoryIn(dirName, directory);
92     }
93
94     /**
95      * add a lonely file into the root directory of the archive.
96      *
97      * @param file the file to be added.
98      */

99     public void addFile(File JavaDoc file) {
100         archive.addFile(file);
101     }
102
103     /**
104      * add a file into the root directory of the archive with a specified name.
105      *
106      * @param file the file to be added.
107      * @param name filename
108      */

109     public void addFile(File JavaDoc file, String JavaDoc name) {
110         archive.addFile(file, name);
111     }
112
113     /**
114      * add a lonely file into the given directory of the archive.
115      *
116      * @param dirName archive directory name.
117      * @param file the file to be added.
118      */

119     public void addFileIn(String JavaDoc dirName, File JavaDoc file) {
120         archive.addFileIn(dirName, file);
121     }
122
123     /**
124      * Returns the File corresponding to the root of the archive.
125      *
126      * @return the File corresponding to the root of the archive.
127      */

128     public File JavaDoc getRootFile() {
129         return archive.getRootFile();
130     }
131
132     /**
133      * Returns the Manifest of the Archive.
134      *
135      * @return the Manifest of the Archive.
136      */

137     public Manifest JavaDoc getManifest() {
138         return archive.getManifest();
139     }
140
141     /**
142      * Returns an InputStream corresponding to the given filename.
143      *
144      * @param filename file name source of the InputStream
145      *
146      * @return the InputStream corresponding to the given filename.
147      *
148      * @throws IOException When Cannot get InputStream from filename
149      */

150     public InputStream JavaDoc getInputStream(String JavaDoc filename)
151         throws IOException JavaDoc {
152         return archive.getInputStream(filename);
153     }
154
155     /**
156      * Returns a List of all files contained in this archive. Original files in
157      * jar, added Files are all included as String in this Enumeration.
158      *
159      * @return a List of all files contained in this archive.
160      */

161     public List JavaDoc getContainedFiles() {
162         return archive.getContainedFiles();
163     }
164
165     /**
166      * Returns true if archive is packed or false if archive is unpacked.
167      *
168      * @return true if archive is packed or false if archive is unpacked.
169      */

170     public boolean isPacked() {
171         return archive.isPacked();
172     }
173
174     /**
175      * Returns the name of the Archive.
176      *
177      * @return the name of the Archive.
178      */

179     public String JavaDoc getName() {
180         return archive.getName();
181     }
182
183     /**
184      * Returns a Map of name to Document for each modified Descriptor of the
185      * archive.
186      *
187      * @return a Map of name to Document
188      */

189     public abstract Map JavaDoc getDescriptors();
190
191     /**
192      * Returns true if filename must be omitted in the archive.
193      *
194      * @param name filename to be tested
195      *
196      * @return true if filename must be omitted.
197      */

198     public abstract boolean omit(String JavaDoc name);
199
200     /**
201      * @return Returns the i18n.
202      */

203     public static I18n getI18n() {
204         return i18n;
205     }
206     /**
207      * @return Returns the logger.
208      */

209     public static Logger getLogger() {
210         return logger;
211     }
212     /**
213      * @return Returns the archive.
214      */

215     public Archive getArchive() {
216         return archive;
217     }
218     /**
219      * @param archive The archive to set.
220      */

221     public void setArchive(Archive archive) {
222         this.archive = archive;
223     }
224
225     /**
226      * Initialize the Archive.
227      * @throws GenBaseException When initialization fails.
228      */

229     public abstract void initialize() throws GenBaseException;
230
231     /**
232      * @return Returns the module inner ClassLoader
233      */

234     public ClassLoader JavaDoc getModuleClassloader() {
235         return moduleClassloader;
236     }
237
238     /**
239      * @param moduleClassloader The moduleClassloader to set.
240      */

241     public void setModuleClassloader(ClassLoader JavaDoc moduleClassloader) {
242         this.moduleClassloader = moduleClassloader;
243     }
244
245     /**
246      * @return true if the use of DTDs is allowed or if we have to use only web services
247      */

248     protected boolean isDTDsAllowed() {
249         GeneratorFactory gf = GeneratorFactories.getCurrentFactory();
250         if (gf == null) {
251             throw new IllegalStateException JavaDoc(i18n.getMessage("J2EEArchive.isDTDsAllowed.noFactory"));
252         }
253         Config config = gf.getConfiguration();
254         if (config == null) {
255             throw new IllegalStateException JavaDoc(i18n.getMessage("J2EEArchive.isDTDsAllowed.noConfig"));
256         }
257         return config.isDTDsAllowed();
258     }
259
260     /**
261      * Close this archive
262      */

263     public void close() {
264         try {
265             this.archive.close();
266         } catch (IOException JavaDoc ioe) {
267             throw new RuntimeException JavaDoc("Cannot close file '" + archive + "'", ioe);
268         }
269         archive = null;
270     }
271 }
272
Popular Tags