JarStorer.java

00001 /*
00002  * JOnAS : Java(TM) OpenSource Application Server
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License as published by the Free
00006  * Software Foundation; either version 2.1 of the License, or any later version.
00007  *
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
00011  * details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00016  *
00017  * Initial Developer : Guillaume Sauthier
00018  * --------------------------------------------------------------------------
00019  * $Id: JarStorer.java,v 1.1 2004/10/11 13:16:14 benoitf Exp $
00020  * --------------------------------------------------------------------------
00021  */
00022 
00023 package org.objectweb.jonas_lib.genbase.utils;
00024 
00025 import java.io.File;
00026 import java.io.FileOutputStream;
00027 import java.io.IOException;
00028 import java.io.InputStream;
00029 import java.io.OutputStream;
00030 import java.util.jar.JarOutputStream;
00031 import java.util.zip.ZipEntry;
00032 
00033 import org.objectweb.jonas_lib.genbase.GenBaseException;
00034 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
00035 
00041 public class JarStorer extends ArchiveStorer {
00042 
00046     private JarOutputStream jos;
00047 
00056     public JarStorer(J2EEArchive archive, File jar) throws GenBaseException {
00057         super(archive);
00058 
00059         setOut(jar.getAbsolutePath());
00060 
00061         try {
00062             if (!jar.getParentFile().exists()) {
00063                 if (!jar.getParentFile().mkdirs()) {
00064                     String err = getI18n().getMessage("JarStorer.constr.create", jar.getParentFile());
00065                     throw new GenBaseException(err);
00066                 }
00067             }
00068 
00069             jos = new JarOutputStream(new FileOutputStream(jar), archive.getManifest());
00070         } catch (IOException ioe) {
00071             String err = getI18n().getMessage("JarStorer.constr.ioe", jar);
00072             throw new GenBaseException(err, ioe);
00073         }
00074     }
00075 
00083     protected String convertName(String name) {
00084         return name.replace('\\', '/');
00085     }
00086 
00094     protected void addFile(String name) throws IOException {
00095         ZipEntry ze = new ZipEntry(convertName(name));
00096         jos.putNextEntry(ze);
00097 
00098         InputStream is = getArchive().getInputStream(name);
00099         fill(is, jos);
00100         is.close();
00101     }
00102 
00108     public void store() throws GenBaseException {
00109         super.store();
00110 
00111         try {
00112             jos.close();
00113         } catch (IOException ioe) {
00114             String err = getI18n().getMessage("JarStorer.store.close");
00115             throw new GenBaseException(err, ioe);
00116         }
00117     }
00118 
00128     protected OutputStream getOutputStream(String name) throws IOException {
00129         ZipEntry ze = new ZipEntry(convertName(name));
00130         jos.putNextEntry(ze);
00131 
00132         return jos;
00133     }
00134 }

Generated on Tue Feb 15 15:05:54 2005 for JOnAS by  doxygen 1.3.9.1