KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genbase > utils > JarStorer


1 /*
2  * JOnAS : Java(TM) OpenSource Application Server
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 2.1 of the License, or any later version.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Initial Developer : Guillaume Sauthier
18  * --------------------------------------------------------------------------
19  * $Id: JarStorer.java,v 1.1 2004/10/11 13:16:14 benoitf Exp $
20  * --------------------------------------------------------------------------
21  */

22
23 package org.objectweb.jonas_lib.genbase.utils;
24
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.jar.JarOutputStream JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32
33 import org.objectweb.jonas_lib.genbase.GenBaseException;
34 import org.objectweb.jonas_lib.genbase.archive.J2EEArchive;
35
36 /**
37  * Store a J2EEArchive in a Jar file.
38  *
39  * @author Guillaume Sauthier
40  */

41 public class JarStorer extends ArchiveStorer {
42
43     /**
44      * Jar file filled as OutputStream
45      */

46     private JarOutputStream JavaDoc jos;
47
48     /**
49      * Creates a new JarStorer object.
50      *
51      * @param archive archive to be stored
52      * @param jar outout jar file
53      *
54      * @throws GenBaseException When cannot create output jar
55      */

56     public JarStorer(J2EEArchive archive, File JavaDoc jar) throws GenBaseException {
57         super(archive);
58
59         setOut(jar.getAbsolutePath());
60
61         try {
62             if (!jar.getParentFile().exists()) {
63                 if (!jar.getParentFile().mkdirs()) {
64                     String JavaDoc err = getI18n().getMessage("JarStorer.constr.create", jar.getParentFile());
65                     throw new GenBaseException(err);
66                 }
67             }
68
69             jos = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jar), archive.getManifest());
70         } catch (IOException JavaDoc ioe) {
71             String JavaDoc err = getI18n().getMessage("JarStorer.constr.ioe", jar);
72             throw new GenBaseException(err, ioe);
73         }
74     }
75
76     /**
77      * Convert a name from any format in Jar filename format
78      *
79      * @param name filename to be converted
80      *
81      * @return converted filename
82      */

83     protected String JavaDoc convertName(String JavaDoc name) {
84         return name.replace('\\', '/');
85     }
86
87     /**
88      * Add a file in Jar
89      *
90      * @param name file name to be added
91      *
92      * @throws IOException when filling fails.
93      */

94     protected void addFile(String JavaDoc name) throws IOException JavaDoc {
95         ZipEntry JavaDoc ze = new ZipEntry JavaDoc(convertName(name));
96         jos.putNextEntry(ze);
97
98         InputStream JavaDoc is = getArchive().getInputStream(name);
99         fill(is, jos);
100         is.close();
101     }
102
103     /**
104      * Store the archive and close the streams.
105      *
106      * @throws GenBaseException When Output Jar cannot be closed
107      */

108     public void store() throws GenBaseException {
109         super.store();
110
111         try {
112             jos.close();
113         } catch (IOException JavaDoc ioe) {
114             String JavaDoc err = getI18n().getMessage("JarStorer.store.close");
115             throw new GenBaseException(err, ioe);
116         }
117     }
118
119     /**
120      * returns the OuputStream corresponding to the given filename.
121      *
122      * @param name filename to create
123      *
124      * @return An OutputStream
125      *
126      * @throws IOException When file creation fails.
127      */

128     protected OutputStream JavaDoc getOutputStream(String JavaDoc name) throws IOException JavaDoc {
129         ZipEntry JavaDoc ze = new ZipEntry JavaDoc(convertName(name));
130         jos.putNextEntry(ze);
131
132         return jos;
133     }
134 }
Popular Tags