1 24 25 package org.objectweb.jonas_lib.genbase.utils; 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Vector ; 34 35 import org.w3c.dom.Document ; 36 37 import org.objectweb.jonas_lib.I18n; 38 import org.objectweb.jonas_lib.genbase.GenBaseException; 39 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive; 40 import org.objectweb.jonas_lib.xml.XMLSerializer; 41 42 import org.objectweb.jonas.common.Log; 43 44 import org.objectweb.util.monolog.api.BasicLevel; 45 import org.objectweb.util.monolog.api.Logger; 46 47 52 public abstract class ArchiveStorer { 53 54 55 public static final int MAX_BUFFER_SIZE = 1024; 56 57 58 private static I18n i18n = I18n.getInstance(ArchiveStorer.class); 59 60 61 private static Logger logger = Log.getLogger(Log.JONAS_GENBASE_PREFIX); 62 63 66 private J2EEArchive archive; 67 68 71 private List already; 72 73 74 private String out = ""; 75 76 81 public ArchiveStorer(J2EEArchive archive) { 82 this.archive = archive; 83 already = new Vector (); 84 already.add(convertName("META-INF/MANIFEST.MF")); 86 } 87 88 96 protected static void fill(InputStream is, OutputStream os) throws IOException { 97 byte[] buffer = new byte[MAX_BUFFER_SIZE]; 98 int read; 99 100 while ((read = is.read(buffer, 0, MAX_BUFFER_SIZE)) != -1) { 101 os.write(buffer, 0, read); 102 } 103 } 104 105 112 protected abstract void addFile(String name) throws IOException ; 113 114 121 protected abstract String convertName(String name); 122 123 132 protected abstract OutputStream getOutputStream(String name) throws IOException ; 133 134 139 public void store() throws GenBaseException { 140 141 if (logger.isLoggable(BasicLevel.DEBUG)) { 142 logger.log(BasicLevel.DEBUG, "Writing '" + out + "' ..."); 143 } 144 145 for (Iterator i = archive.getContainedFiles().iterator(); i.hasNext();) { 146 String name = (String ) i.next(); 147 148 try { 149 if (!archive.omit(convertName(name)) && !already.contains(convertName(name))) { 150 151 addFile(name); 152 already.add(convertName(name)); 153 } 154 } catch (IOException ioe) { 155 String err = i18n.getMessage("ArchiveStorer.store.addFile", name); 156 throw new GenBaseException(err, ioe); 157 } 158 } 159 160 Map descs = archive.getDescriptors(); 162 163 for (Iterator i = descs.keySet().iterator(); i.hasNext();) { 164 String name = (String ) i.next(); 165 166 try { 167 XMLSerializer ser = new XMLSerializer((Document ) descs.get(name)); 168 ser.serialize(getOutputStream(name)); 169 } catch (IOException ioe) { 170 String err = i18n.getMessage("ArchiveStorer.store.serialize", name); 171 throw new GenBaseException(err, ioe); 172 } 173 } 174 } 175 178 public static I18n getI18n() { 179 return i18n; 180 } 181 182 185 public J2EEArchive getArchive() { 186 return archive; 187 } 188 189 192 public void setOut(String out) { 193 this.out = out; 194 } 195 } | Popular Tags |