001: /**
002: * JOnAS : Java(TM) OpenSource Application Server
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Initial Developer : Guillaume Sauthier
020: * --------------------------------------------------------------------------
021: * $Id: ArchiveStorer.java 6673 2005-04-28 16:53:00Z benoitf $
022: * --------------------------------------------------------------------------
023: */package org.objectweb.jonas_lib.genbase.utils;
024:
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStream;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Map;
031: import java.util.Vector;
032:
033: import org.w3c.dom.Document;
034:
035: import org.objectweb.jonas_lib.I18n;
036: import org.objectweb.jonas_lib.genbase.GenBaseException;
037: import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
038: import org.objectweb.jonas_lib.xml.XMLSerializer;
039:
040: import org.objectweb.jonas.common.Log;
041:
042: import org.objectweb.util.monolog.api.BasicLevel;
043: import org.objectweb.util.monolog.api.Logger;
044:
045: /**
046: * Store an Archive in compressed or uncompressed format
047: *
048: * @author Guillaume Sauthier
049: */
050: public abstract class ArchiveStorer {
051:
052: /** Buffer Size */
053: public static final int MAX_BUFFER_SIZE = 1024;
054:
055: /** i18n */
056: private static I18n i18n = I18n.getInstance(ArchiveStorer.class);
057:
058: /** logger */
059: private static Logger logger = Log
060: .getLogger(Log.JONAS_GENBASE_PREFIX);
061:
062: /**
063: * base J2EEArchive to be saved
064: */
065: private J2EEArchive archive;
066:
067: /**
068: * already saved filenames
069: */
070: private List already;
071:
072: /** output filename */
073: private String out = "";
074:
075: /**
076: * Creates a new ArchiveStorer object.
077: *
078: * @param archive archive to be saved
079: */
080: public ArchiveStorer(J2EEArchive archive) {
081: this .archive = archive;
082: already = new Vector();
083: // we must use Manifestof the J2EEArchive
084: already.add(convertName("META-INF/MANIFEST.MF"));
085: }
086:
087: /**
088: * Fill an OutputStream with content from an InputStream
089: *
090: * @param is InputStream
091: * @param os OutputStream
092: *
093: * @throws IOException When filling fails
094: */
095: protected static void fill(InputStream is, OutputStream os)
096: throws IOException {
097: byte[] buffer = new byte[MAX_BUFFER_SIZE];
098: int read;
099:
100: while ((read = is.read(buffer, 0, MAX_BUFFER_SIZE)) != -1) {
101: os.write(buffer, 0, read);
102: }
103: }
104:
105: /**
106: * add a file in saved archive
107: *
108: * @param name file name
109: *
110: * @throws IOException When save fails
111: */
112: protected abstract void addFile(String name) throws IOException;
113:
114: /**
115: * convert a filename from unix to windows filename and reverse
116: *
117: * @param name name to be converted
118: *
119: * @return converted filename
120: */
121: protected abstract String convertName(String name);
122:
123: /**
124: * Returns an OutputStream from the given name
125: *
126: * @param name the filename we want to open/create
127: *
128: * @return an OutputStream from the given name
129: *
130: * @throws IOException When OS cannot be created
131: */
132: protected abstract OutputStream getOutputStream(String name)
133: throws IOException;
134:
135: /**
136: * Store the content of the contained archive.
137: *
138: * @throws GenBaseException When cannot add all files
139: */
140: public void store() throws GenBaseException {
141:
142: if (logger.isLoggable(BasicLevel.DEBUG)) {
143: logger.log(BasicLevel.DEBUG, "Writing '" + out + "' ...");
144: }
145:
146: for (Iterator i = archive.getContainedFiles().iterator(); i
147: .hasNext();) {
148: String name = (String) i.next();
149:
150: try {
151: if (!archive.omit(convertName(name))
152: && !already.contains(convertName(name))) {
153:
154: addFile(name);
155: already.add(convertName(name));
156: }
157: } catch (IOException ioe) {
158: String err = i18n.getMessage(
159: "ArchiveStorer.store.addFile", name);
160: throw new GenBaseException(err, ioe);
161: }
162: }
163:
164: // add Descriptors
165: Map descs = archive.getDescriptors();
166:
167: for (Iterator i = descs.keySet().iterator(); i.hasNext();) {
168: String name = (String) i.next();
169:
170: try {
171: XMLSerializer ser = new XMLSerializer((Document) descs
172: .get(name));
173: ser.serialize(getOutputStream(name));
174: } catch (IOException ioe) {
175: String err = i18n.getMessage(
176: "ArchiveStorer.store.serialize", name);
177: throw new GenBaseException(err, ioe);
178: }
179: }
180: }
181:
182: /**
183: * @return Returns the i18n.
184: */
185: public static I18n getI18n() {
186: return i18n;
187: }
188:
189: /**
190: * @return Returns the archive.
191: */
192: public J2EEArchive getArchive() {
193: return archive;
194: }
195:
196: /**
197: * @param out The out to set.
198: */
199: public void setOut(String out) {
200: this.out = out;
201: }
202: }
|