00001
00025 package org.objectweb.jonas_lib.genbase.utils;
00026
00027 import java.io.IOException;
00028 import java.io.InputStream;
00029 import java.io.OutputStream;
00030 import java.util.Iterator;
00031 import java.util.List;
00032 import java.util.Map;
00033 import java.util.Vector;
00034
00035 import org.w3c.dom.Document;
00036
00037 import org.objectweb.jonas_lib.I18n;
00038 import org.objectweb.jonas_lib.genbase.GenBaseException;
00039 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
00040 import org.objectweb.jonas_lib.xml.XMLSerializer;
00041
00042 import org.objectweb.jonas.common.Log;
00043
00044 import org.objectweb.util.monolog.api.BasicLevel;
00045 import org.objectweb.util.monolog.api.Logger;
00046
00052 public abstract class ArchiveStorer {
00053
00055 public static final int MAX_BUFFER_SIZE = 1024;
00056
00058 private static I18n i18n = I18n.getInstance(ArchiveStorer.class);
00059
00061 private static Logger logger = Log.getLogger(Log.JONAS_GENBASE_PREFIX);
00062
00066 private J2EEArchive archive;
00067
00071 private List already;
00072
00074 private String out = "";
00075
00081 public ArchiveStorer(J2EEArchive archive) {
00082 this.archive = archive;
00083 already = new Vector();
00084
00085 already.add(convertName("META-INF/MANIFEST.MF"));
00086 }
00087
00096 protected static void fill(InputStream is, OutputStream os) throws IOException {
00097 byte[] buffer = new byte[MAX_BUFFER_SIZE];
00098 int read;
00099
00100 while ((read = is.read(buffer, 0, MAX_BUFFER_SIZE)) != -1) {
00101 os.write(buffer, 0, read);
00102 }
00103 }
00104
00112 protected abstract void addFile(String name) throws IOException;
00113
00121 protected abstract String convertName(String name);
00122
00132 protected abstract OutputStream getOutputStream(String name) throws IOException;
00133
00139 public void store() throws GenBaseException {
00140
00141 logger.log(BasicLevel.DEBUG, "Writing '" + out + "' ...");
00142
00143 for (Iterator i = archive.getContainedFiles().iterator(); i.hasNext();) {
00144 String name = (String) i.next();
00145
00146 try {
00147 if (!archive.omit(convertName(name)) && !already.contains(convertName(name))) {
00148
00149 addFile(name);
00150 already.add(convertName(name));
00151 }
00152 } catch (IOException ioe) {
00153 String err = i18n.getMessage("ArchiveStorer.store.addFile", name);
00154 throw new GenBaseException(err, ioe);
00155 }
00156 }
00157
00158
00159 Map descs = archive.getDescriptors();
00160
00161 for (Iterator i = descs.keySet().iterator(); i.hasNext();) {
00162 String name = (String) i.next();
00163
00164 try {
00165 XMLSerializer ser = new XMLSerializer((Document) descs.get(name));
00166 ser.serialize(getOutputStream(name));
00167 } catch (IOException ioe) {
00168 String err = i18n.getMessage("ArchiveStorer.store.serialize", name);
00169 throw new GenBaseException(err, ioe);
00170 }
00171 }
00172 }
00176 public static I18n getI18n() {
00177 return i18n;
00178 }
00179
00183 public J2EEArchive getArchive() {
00184 return archive;
00185 }
00186
00190 public void setOut(String out) {
00191 this.out = out;
00192 }
00193 }