001: /*
002: * $Id: WebPackager.java 628 2004-04-15 20:20:15Z mchenryc $
003: * IzPack
004: * Copyright (C) 2001-2004 Julien Ponge
005: *
006: * File : WebPackager.java
007: * Description : The web installer packager class.
008: * Author's email : julien@izforge.com
009: * Author's Website : http://www.izforge.com
010: *
011: * This program is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU General Public License
013: * as published by the Free Software Foundation; either version 2
014: * of the License, or any later version.
015: *
016: * This program is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU General Public License for more details.
020: *
021: * You should have received a copy of the GNU General Public License
022: * along with this program; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
024: */
025: package com.izforge.izpack.compiler;
026:
027: import java.io.DataOutputStream;
028: import java.io.FileOutputStream;
029: import java.io.InputStream;
030: import java.io.ObjectOutputStream;
031: import java.util.ArrayList;
032: import java.util.Enumeration;
033: import java.util.List;
034: import java.util.Properties;
035: import java.util.jar.JarFile;
036: import java.util.jar.JarOutputStream;
037: import java.util.zip.ZipEntry;
038: import java.util.zip.ZipException;
039: import java.util.zip.ZipOutputStream;
040:
041: import com.izforge.izpack.GUIPrefs;
042: import com.izforge.izpack.Info;
043: import com.izforge.izpack.Pack;
044:
045: /**
046: * Web packager class.
047: *
048: * @author Julien Ponge
049: */
050: public class WebPackager extends Packager {
051: /** The zipped output stream. */
052: protected JarOutputStream outJar;
053:
054: /** The web jar file output stream. */
055: protected JarOutputStream webJar;
056:
057: /**
058: * The constructor.
059: *
060: * @param outputFilename The output filename.
061: * @param plistener The packager listener.
062: * @exception Exception Description of the Exception
063: */
064: public WebPackager(String outputFilename, PackagerListener plistener)
065: throws Exception {
066: packs = new ArrayList();
067: langpacks = new ArrayList();
068: setPackagerListener(plistener);
069:
070: sendStart();
071:
072: // Sets up the zipped output stream
073: FileOutputStream outFile = new FileOutputStream(outputFilename);
074: outJar = new JarOutputStream(outFile);
075: outJar.setLevel(9);
076:
077: // Sets up the web output jar stream
078: outputFilename = outputFilename.substring(0, outputFilename
079: .length() - 4)
080: + "_web.jar";
081: outFile = new FileOutputStream(outputFilename);
082: webJar = new JarOutputStream(outFile);
083: webJar.setLevel(9);
084:
085: // Copies the skeleton installer
086: sendMsg("Copying the skeleton installer ...");
087: writeSkeletonInstaller(outJar);
088: }
089:
090: public boolean allowPackFileBackReferences() {
091: return false;
092: }
093:
094: /**
095: * Adds a pack (the compiler sends the merged data).
096: *
097: * @param packNumber The pack number.
098: * @param name The pack name.
099: * @param osConstraints The target operation system(s) of this pack.
100: * @param required Is the pack required ?
101: * @param description The pack description.
102: * @return Description of the Return Value
103: * @exception Exception Description of the Exception
104: */
105: public ZipOutputStream addPack(int packNumber, String name,
106: List osConstraints, boolean required, String description,
107: boolean preselected) throws Exception {
108: sendMsg("Adding pack #" + packNumber + " : " + name + " ...");
109:
110: // Adds it in the packs array
111: Pack pack = new Pack(name, description, osConstraints,
112: required, preselected);
113: packs.add(packNumber, pack);
114:
115: // Returns the suiting output stream
116: String entryName = "packs/pack" + packNumber;
117: ZipEntry entry = new ZipEntry(entryName);
118: webJar.putNextEntry(entry);
119: return webJar;
120: }
121:
122: /**
123: * Sets the GUI preferences.
124: *
125: * @param prefs The new gUIPrefs value
126: * @exception Exception Description of the Exception
127: */
128: public void setGUIPrefs(GUIPrefs prefs) throws Exception {
129: sendMsg("Setting the GUI preferences ...");
130:
131: outJar.putNextEntry(new ZipEntry("GUIPrefs"));
132: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
133: objOut.writeObject(prefs);
134: objOut.flush();
135: outJar.closeEntry();
136: }
137:
138: /**
139: * Adds a panel.
140: *
141: * @param classFilename The class filename.
142: * @param input The stream to get the file data from.
143: * @exception Exception Description of the Exception
144: */
145: public void addPanelClass(String classFilename, InputStream input)
146: throws Exception {
147: sendMsg("Adding the (sub)classes for " + classFilename + " ...");
148:
149: outJar.putNextEntry(new ZipEntry("com/izforge/izpack/panels/"
150: + classFilename));
151: copyStream(input, outJar);
152: outJar.closeEntry();
153: }
154:
155: /**
156: * Sets the panels order.
157: *
158: * @param order The ordered list of the panels.
159: * @exception Exception Description of the Exception
160: */
161: public void setPanelsOrder(ArrayList order) throws Exception {
162: sendMsg("Setting the panels order ...");
163:
164: outJar.putNextEntry(new ZipEntry("panelsOrder"));
165: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
166: objOut.writeObject(order);
167: objOut.flush();
168: outJar.closeEntry();
169: }
170:
171: /**
172: * Sets the informations related to this installation.
173: *
174: * @param info The info section.
175: * @exception Exception Description of the Exception
176: */
177: public void setInfo(Info info) throws Exception {
178: sendMsg("Setting the installer informations ...");
179:
180: outJar.putNextEntry(new ZipEntry("info"));
181: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
182: objOut.writeObject(info);
183: objOut.flush();
184: outJar.closeEntry();
185: }
186:
187: /**
188: * Adds Variable Declaration.
189: *
190: * @param varDef The variables definitions.
191: * @exception Exception Description of the Exception
192: */
193: public void setVariables(Properties varDef) throws Exception {
194: sendMsg("Setting the variables ...");
195: outJar.putNextEntry(new ZipEntry("vars"));
196: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
197: objOut.writeObject(varDef);
198: objOut.flush();
199: outJar.closeEntry();
200: }
201:
202: /**
203: * Adds a resource.
204: *
205: * @param resId The resource Id.
206: * @param input The stream to get the data from.
207: * @exception Exception Description of the Exception
208: */
209: public void addResource(String resId, InputStream input)
210: throws Exception {
211: sendMsg("Adding resource : " + resId + " ...");
212:
213: outJar.putNextEntry(new ZipEntry("res/" + resId));
214: copyStream(input, outJar);
215: outJar.closeEntry();
216: input.close();
217: }
218:
219: /**
220: * Adds a native library.
221: *
222: * @param name The native library name.
223: * @param input The stream to get the data from.
224: * @exception Exception Description of the Exception
225: */
226: public void addNativeLibrary(String name, InputStream input)
227: throws Exception {
228: sendMsg("Adding native library : " + name + " ...");
229:
230: outJar.putNextEntry(new ZipEntry("native/" + name));
231: copyStream(input, outJar);
232: outJar.closeEntry();
233: input.close();
234: }
235:
236: /**
237: * Adds a language pack.
238: *
239: * @param iso3 The ISO3 code.
240: * @param input The stream to get the data from.
241: * @exception Exception Description of the Exception
242: */
243: public void addLangPack(String iso3, InputStream input)
244: throws Exception {
245: sendMsg("Adding langpack : " + iso3 + " ...");
246:
247: langpacks.add(iso3);
248: outJar.putNextEntry(new ZipEntry("langpacks/" + iso3 + ".xml"));
249: copyStream(input, outJar);
250: outJar.closeEntry();
251: input.close();
252: }
253:
254: /**
255: * Adds a jar file content to the installer.
256: *
257: * @param file The jar filename.
258: * @exception Exception Description of the Exception
259: */
260: public void addJarContent(String file) throws Exception {
261: sendMsg("Adding a jar file content ...");
262:
263: JarFile jar = new JarFile(file);
264: Enumeration entries = jar.entries();
265: while (entries.hasMoreElements()) {
266: // Puts a new entry
267: ZipEntry zentry = (ZipEntry) entries.nextElement();
268: try {
269: InputStream zin = jar.getInputStream(zentry);
270: outJar.putNextEntry(new ZipEntry(zentry.getName()));
271:
272: // Copy the data
273: copyStream(zin, outJar);
274: outJar.closeEntry();
275: zin.close();
276: } catch (ZipException zerr) {
277: }
278: }
279: }
280:
281: /**
282: * Tells the packager to finish the job (misc writings, cleanups, closings ,
283: * ...).
284: *
285: * @exception Exception Description of the Exception
286: */
287: public void finish() throws Exception {
288: // Usefull stuff
289: DataOutputStream datOut;
290: ObjectOutputStream objOut;
291: int size;
292: int i;
293:
294: sendMsg("Finishing the enpacking ...");
295:
296: // Writes the installation kind information
297: outJar.putNextEntry(new ZipEntry("kind"));
298: datOut = new DataOutputStream(outJar);
299: datOut.writeUTF("web");
300: datOut.flush();
301: outJar.closeEntry();
302:
303: // Writes the packs informations
304: outJar.putNextEntry(new ZipEntry("packs.info"));
305: objOut = new ObjectOutputStream(outJar);
306: size = packs.size();
307: objOut.writeInt(size);
308: for (i = 0; i < size; i++)
309: objOut.writeObject(packs.get(i));
310: objOut.flush();
311: outJar.closeEntry();
312:
313: // Writes the langpacks informations
314: outJar.putNextEntry(new ZipEntry("langpacks.info"));
315: datOut = new DataOutputStream(outJar);
316: size = langpacks.size();
317: datOut.writeInt(size);
318: for (i = 0; i < size; i++)
319: datOut.writeUTF((String) langpacks.get(i));
320: datOut.flush();
321: outJar.closeEntry();
322:
323: // Closes the stream
324: outJar.flush();
325: outJar.close();
326: webJar.flush();
327: webJar.close();
328:
329: sendStop();
330: }
331: }
|