KickJava   Java API By Example, From Geeks To Geeks.

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


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

24
25 package org.objectweb.jonas_lib.genbase.utils;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.w3c.dom.Document JavaDoc;
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 /**
48  * Store an Archive in compressed or uncompressed format
49  *
50  * @author Guillaume Sauthier
51  */

52 public abstract class ArchiveStorer {
53
54     /** Buffer Size */
55     public static final int MAX_BUFFER_SIZE = 1024;
56
57     /** i18n */
58     private static I18n i18n = I18n.getInstance(ArchiveStorer.class);
59
60     /** logger */
61     private static Logger logger = Log.getLogger(Log.JONAS_GENBASE_PREFIX);
62
63     /**
64      * base J2EEArchive to be saved
65      */

66     private J2EEArchive archive;
67
68     /**
69      * already saved filenames
70      */

71     private List JavaDoc already;
72
73     /** output filename */
74     private String JavaDoc out = "";
75
76     /**
77      * Creates a new ArchiveStorer object.
78      *
79      * @param archive archive to be saved
80      */

81     public ArchiveStorer(J2EEArchive archive) {
82         this.archive = archive;
83         already = new Vector JavaDoc();
84         // we must use Manifestof the J2EEArchive
85
already.add(convertName("META-INF/MANIFEST.MF"));
86     }
87
88     /**
89      * Fill an OutputStream with content from an InputStream
90      *
91      * @param is InputStream
92      * @param os OutputStream
93      *
94      * @throws IOException When filling fails
95      */

96     protected static void fill(InputStream JavaDoc is, OutputStream JavaDoc os) throws IOException JavaDoc {
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     /**
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 JavaDoc name) throws IOException JavaDoc;
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 JavaDoc convertName(String JavaDoc 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 JavaDoc getOutputStream(String JavaDoc name) throws IOException JavaDoc;
133
134     /**
135      * Store the content of the contained archive.
136      *
137      * @throws GenBaseException When cannot add all files
138      */

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 JavaDoc i = archive.getContainedFiles().iterator(); i.hasNext();) {
146             String JavaDoc name = (String JavaDoc) 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 JavaDoc ioe) {
155                 String JavaDoc err = i18n.getMessage("ArchiveStorer.store.addFile", name);
156                 throw new GenBaseException(err, ioe);
157             }
158         }
159
160         // add Descriptors
161
Map JavaDoc descs = archive.getDescriptors();
162
163         for (Iterator JavaDoc i = descs.keySet().iterator(); i.hasNext();) {
164             String JavaDoc name = (String JavaDoc) i.next();
165
166             try {
167                 XMLSerializer ser = new XMLSerializer((Document JavaDoc) descs.get(name));
168                 ser.serialize(getOutputStream(name));
169             } catch (IOException JavaDoc ioe) {
170                 String JavaDoc err = i18n.getMessage("ArchiveStorer.store.serialize", name);
171                 throw new GenBaseException(err, ioe);
172             }
173         }
174     }
175     /**
176      * @return Returns the i18n.
177      */

178     public static I18n getI18n() {
179         return i18n;
180     }
181
182     /**
183      * @return Returns the archive.
184      */

185     public J2EEArchive getArchive() {
186         return archive;
187     }
188
189     /**
190      * @param out The out to set.
191      */

192     public void setOut(String JavaDoc out) {
193         this.out = out;
194     }
195 }
Popular Tags