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: JarArchive.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.FileInputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.util.Enumeration;
031: import java.util.List;
032: import java.util.Vector;
033: import java.util.jar.JarFile;
034: import java.util.zip.ZipEntry;
035:
036: import org.objectweb.jonas_lib.genbase.GenBaseException;
037:
038: /**
039: * A <code>JarArchive</code> is a wrapper for jar file.
040: *
041: * @author Guillaume Sauthier
042: */
043: public class JarArchive extends AbsArchive {
044:
045: /** encapsulated Jar File */
046: private JarFile jar;
047:
048: /**
049: * Creates a new JarArchive object.
050: *
051: * @param jar the File corresponding to a JarFile
052: *
053: * @throws GenBaseException When Manifest cannot be found
054: */
055: public JarArchive(File jar) throws GenBaseException {
056: super (jar);
057:
058: try {
059: this .jar = new JarFile(jar);
060: setManifest(this .jar.getManifest());
061: } catch (IOException ioe) {
062: String err = getI18n().getMessage("JarArchive.constr.jar",
063: jar);
064: throw new GenBaseException(err, ioe);
065: }
066: }
067:
068: /**
069: * Returns an InputStream corresponding to the given filename.
070: *
071: * @param filename file name source of the InputStream
072: *
073: * @return the InputStream corresponding to the given filename.
074: *
075: * @throws IOException when InputStream of the filename cannot be found in
076: * the archive
077: */
078: public InputStream getInputStream(String filename)
079: throws IOException {
080: // try get the file from the map
081: File file = (File) getFiles().get(filename);
082:
083: if (file == null) {
084: // filename not found in added files
085: // try jar search
086: ZipEntry ze = jar.getEntry(filename);
087:
088: // Entry found ?
089: if (ze == null) {
090: return null;
091: } else {
092: return jar.getInputStream(ze);
093: }
094: } else {
095: // file exists (in added files)
096: return new FileInputStream(file);
097: }
098: }
099:
100: /**
101: * Returns a List of all files contained in this archive. Original files in
102: * jar, added Files are all included as String in this Enumeration.
103: *
104: * @return a List of all files contained in this archive.
105: */
106: public List getContainedFiles() {
107: List list = new Vector(getFiles().keySet());
108:
109: // add files of the original archive
110: for (Enumeration e = jar.entries(); e.hasMoreElements();) {
111: ZipEntry ze = (ZipEntry) e.nextElement();
112: list.add(ze.getName());
113: }
114:
115: return list;
116: }
117:
118: /**
119: * Returns true if archive is packed or false if archive is unpacked.
120: *
121: * @return true if archive is packed or false if archive is unpacked.
122: */
123: public boolean isPacked() {
124: return true;
125: }
126:
127: /**
128: * Close this archive
129: */
130: public void close() {
131: super .close();
132: try {
133: jar.close();
134: } catch (IOException ioe) {
135: throw new RuntimeException("Cannot close file '" + jar
136: + "'", ioe);
137: }
138: jar = null;
139: }
140: }
|