001: /**
002: * JOnAS : Java(TM) OpenSource Application Server
003: * Copyright (C) 1999-2004 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: * --------------------------------------------------------------------------
022: * $Id: J2EEArchive.java 6657 2005-04-27 12:28:21Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_lib.genbase.archive;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.jar.Manifest;
032:
033: import org.objectweb.jonas_lib.I18n;
034: import org.objectweb.jonas_lib.genbase.GenBaseException;
035: import org.objectweb.jonas_lib.genbase.generator.Config;
036: import org.objectweb.jonas_lib.genbase.generator.GeneratorFactories;
037: import org.objectweb.jonas_lib.genbase.generator.GeneratorFactory;
038:
039: import org.objectweb.jonas.common.Log;
040:
041: import org.objectweb.util.monolog.api.Logger;
042:
043: /**
044: * Base Class for all J2EE modules (application, ejbjar, client, webapp).
045: *
046: * @author Guillaume Sauthier
047: * @see org.objectweb.jonas_lib.genbase.archive.Archive
048: */
049: public abstract class J2EEArchive implements Archive {
050: /** i18n */
051: private static I18n i18n = I18n.getInstance(J2EEArchive.class);
052:
053: /** logger */
054: private static Logger logger = Log
055: .getLogger(Log.JONAS_GENBASE_PREFIX);
056:
057: /** encapsulated archive */
058: private Archive archive;
059:
060: /** J2EE Archive inner ClassLoader */
061: private ClassLoader moduleClassloader = null;
062:
063: /**
064: * Creates a new J2EEArchive object.
065: *
066: * @param archive Archive containing files
067: */
068: public J2EEArchive(Archive archive) {
069: this .archive = archive;
070: }
071:
072: /**
073: * add the content of the given directory into the root of the archive.
074: *
075: * @param directory directory to add
076: */
077: public void addDirectory(File directory) {
078: archive.addDirectory(directory);
079: }
080:
081: /**
082: * add the content of the given directory into the given directory of the
083: * archive.
084: *
085: * @param dirName archive directory name.
086: * @param directory directory to add.
087: */
088: public void addDirectoryIn(String dirName, File directory) {
089: archive.addDirectoryIn(dirName, directory);
090: }
091:
092: /**
093: * add a lonely file into the root directory of the archive.
094: *
095: * @param file the file to be added.
096: */
097: public void addFile(File file) {
098: archive.addFile(file);
099: }
100:
101: /**
102: * add a file into the root directory of the archive with a specified name.
103: *
104: * @param file the file to be added.
105: * @param name filename
106: */
107: public void addFile(File file, String name) {
108: archive.addFile(file, name);
109: }
110:
111: /**
112: * add a lonely file into the given directory of the archive.
113: *
114: * @param dirName archive directory name.
115: * @param file the file to be added.
116: */
117: public void addFileIn(String dirName, File file) {
118: archive.addFileIn(dirName, file);
119: }
120:
121: /**
122: * Returns the File corresponding to the root of the archive.
123: *
124: * @return the File corresponding to the root of the archive.
125: */
126: public File getRootFile() {
127: return archive.getRootFile();
128: }
129:
130: /**
131: * Returns the Manifest of the Archive.
132: *
133: * @return the Manifest of the Archive.
134: */
135: public Manifest getManifest() {
136: return archive.getManifest();
137: }
138:
139: /**
140: * Returns an InputStream corresponding to the given filename.
141: *
142: * @param filename file name source of the InputStream
143: *
144: * @return the InputStream corresponding to the given filename.
145: *
146: * @throws IOException When Cannot get InputStream from filename
147: */
148: public InputStream getInputStream(String filename)
149: throws IOException {
150: return archive.getInputStream(filename);
151: }
152:
153: /**
154: * Returns a List of all files contained in this archive. Original files in
155: * jar, added Files are all included as String in this Enumeration.
156: *
157: * @return a List of all files contained in this archive.
158: */
159: public List getContainedFiles() {
160: return archive.getContainedFiles();
161: }
162:
163: /**
164: * Returns true if archive is packed or false if archive is unpacked.
165: *
166: * @return true if archive is packed or false if archive is unpacked.
167: */
168: public boolean isPacked() {
169: return archive.isPacked();
170: }
171:
172: /**
173: * Returns the name of the Archive.
174: *
175: * @return the name of the Archive.
176: */
177: public String getName() {
178: return archive.getName();
179: }
180:
181: /**
182: * Returns a Map of name to Document for each modified Descriptor of the
183: * archive.
184: *
185: * @return a Map of name to Document
186: */
187: public abstract Map getDescriptors();
188:
189: /**
190: * Returns true if filename must be omitted in the archive.
191: *
192: * @param name filename to be tested
193: *
194: * @return true if filename must be omitted.
195: */
196: public abstract boolean omit(String name);
197:
198: /**
199: * @return Returns the i18n.
200: */
201: public static I18n getI18n() {
202: return i18n;
203: }
204:
205: /**
206: * @return Returns the logger.
207: */
208: public static Logger getLogger() {
209: return logger;
210: }
211:
212: /**
213: * @return Returns the archive.
214: */
215: public Archive getArchive() {
216: return archive;
217: }
218:
219: /**
220: * @param archive The archive to set.
221: */
222: public void setArchive(Archive archive) {
223: this .archive = archive;
224: }
225:
226: /**
227: * Initialize the Archive.
228: * @throws GenBaseException When initialization fails.
229: */
230: public abstract void initialize() throws GenBaseException;
231:
232: /**
233: * @return Returns the module inner ClassLoader
234: */
235: public ClassLoader getModuleClassloader() {
236: return moduleClassloader;
237: }
238:
239: /**
240: * @param moduleClassloader The moduleClassloader to set.
241: */
242: public void setModuleClassloader(ClassLoader moduleClassloader) {
243: this .moduleClassloader = moduleClassloader;
244: }
245:
246: /**
247: * @return true if the use of DTDs is allowed or if we have to use only web services
248: */
249: protected boolean isDTDsAllowed() {
250: GeneratorFactory gf = GeneratorFactories.getCurrentFactory();
251: if (gf == null) {
252: throw new IllegalStateException(i18n
253: .getMessage("J2EEArchive.isDTDsAllowed.noFactory"));
254: }
255: Config config = gf.getConfiguration();
256: if (config == null) {
257: throw new IllegalStateException(i18n
258: .getMessage("J2EEArchive.isDTDsAllowed.noConfig"));
259: }
260: return config.isDTDsAllowed();
261: }
262:
263: /**
264: * Close this archive
265: */
266: public void close() {
267: try {
268: this .archive.close();
269: } catch (IOException ioe) {
270: throw new RuntimeException("Cannot close file '" + archive
271: + "'", ioe);
272: }
273: archive = null;
274: }
275: }
|