001: /*
002: * $Id: StdPackager.java 588 2004-02-20 09:01:39Z jponge $
003: * IzPack
004: * Copyright (C) 2001-2004 Julien Ponge
005: *
006: * File : StdPackager.java
007: * Description : The standard 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.Deflater;
038: import java.util.zip.ZipEntry;
039: import java.util.zip.ZipException;
040: import java.util.zip.ZipOutputStream;
041:
042: import com.izforge.izpack.GUIPrefs;
043: import com.izforge.izpack.Info;
044: import com.izforge.izpack.Pack;
045:
046: /**
047: * Standard packager.
048: *
049: * @author Julien Ponge
050: */
051: public class StdPackager extends Packager {
052: /** The zipped output stream. */
053: protected JarOutputStream outJar;
054:
055: /**
056: * The constructor.
057: *
058: * @param outputFilename The output filename.
059: * @param plistener The packager listener.
060: * @exception Exception Description of the Exception
061: */
062: public StdPackager(String outputFilename, PackagerListener plistener)
063: throws Exception {
064: packs = new ArrayList();
065: langpacks = new ArrayList();
066: setPackagerListener(plistener);
067:
068: sendStart();
069:
070: // Sets up the zipped output stream
071: FileOutputStream outFile = new FileOutputStream(outputFilename);
072: outJar = new JarOutputStream(outFile);
073: outJar.setLevel(Deflater.BEST_COMPRESSION);
074:
075: // Copies the skeleton installer
076: sendMsg("Copying the skeleton installer ...");
077:
078: writeSkeletonInstaller(outJar);
079: }
080:
081: public boolean allowPackFileBackReferences() {
082: return true;
083: }
084:
085: /**
086: * Adds a pack (the compiler sends the merged data).
087: *
088: * @param packNumber The pack number.
089: * @param name The pack name.
090: * @param required Is the pack required ?
091: * @param osConstraints The target operation system(s) of this pack.
092: * @param description The pack description.
093: * @return Description of the Return Value
094: * @exception Exception Description of the Exception
095: */
096: public ZipOutputStream addPack(int packNumber, String name,
097: List osConstraints, boolean required, String description,
098: boolean preselected) throws Exception {
099: sendMsg("Adding pack #" + packNumber + " : " + name + " ...");
100:
101: // Adds it in the packs array
102: Pack pack = new Pack(name, description, osConstraints,
103: required, preselected);
104: packs.add(packNumber, pack);
105:
106: // Returns the suiting output stream
107: String entryName = "packs/pack" + packNumber;
108: ZipEntry entry = new ZipEntry(entryName);
109: outJar.putNextEntry(entry);
110: return outJar;
111: }
112:
113: /**
114: * Sets the GUI preferences.
115: *
116: * @param prefs The new gUIPrefs value
117: * @exception Exception Description of the Exception
118: */
119: public void setGUIPrefs(GUIPrefs prefs) throws Exception {
120: sendMsg("Setting the GUI preferences ...");
121:
122: outJar.putNextEntry(new ZipEntry("GUIPrefs"));
123: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
124: objOut.writeObject(prefs);
125: objOut.flush();
126: outJar.closeEntry();
127: }
128:
129: /**
130: * Adds a panel.
131: *
132: * @param classFilename The class filename.
133: * @param input The stream to get the file data from.
134: * @exception Exception Description of the Exception
135: */
136: public void addPanelClass(String classFilename, InputStream input)
137: throws Exception {
138: sendMsg("Adding the (sub)classes for " + classFilename + " ...");
139:
140: outJar.putNextEntry(new ZipEntry("com/izforge/izpack/panels/"
141: + classFilename));
142: copyStream(input, outJar);
143: outJar.closeEntry();
144: }
145:
146: /**
147: * Sets the panels order.
148: *
149: * @param order The ordered list of the panels.
150: * @exception Exception Description of the Exception
151: */
152: public void setPanelsOrder(ArrayList order) throws Exception {
153: sendMsg("Setting the panels order ...");
154:
155: outJar.putNextEntry(new ZipEntry("panelsOrder"));
156: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
157: objOut.writeObject(order);
158: objOut.flush();
159: outJar.closeEntry();
160: }
161:
162: /**
163: * Sets the informations related to this installation.
164: *
165: * @param info The info section.
166: * @exception Exception Description of the Exception
167: */
168: public void setInfo(Info info) throws Exception {
169: sendMsg("Setting the installer informations ...");
170:
171: outJar.putNextEntry(new ZipEntry("info"));
172: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
173: objOut.writeObject(info);
174: objOut.flush();
175: outJar.closeEntry();
176: }
177:
178: /**
179: * Adds Variable Declaration.
180: *
181: * @param varDef The variables definitions.
182: * @exception Exception Description of the Exception
183: */
184: public void setVariables(Properties varDef) throws Exception {
185: sendMsg("Setting the variables ...");
186: outJar.putNextEntry(new ZipEntry("vars"));
187: ObjectOutputStream objOut = new ObjectOutputStream(outJar);
188: objOut.writeObject(varDef);
189: objOut.flush();
190: outJar.closeEntry();
191: }
192:
193: /**
194: * Adds a resource.
195: *
196: * @param resId The resource Id.
197: * @param input The stream to get the data from.
198: * @exception Exception Description of the Exception
199: */
200: public void addResource(String resId, InputStream input)
201: throws Exception {
202: sendMsg("Adding resource : " + resId + " ...");
203:
204: outJar.putNextEntry(new ZipEntry("res/" + resId));
205: copyStream(input, outJar);
206: outJar.closeEntry();
207: }
208:
209: /**
210: * Adds a native library.
211: *
212: * @param name The native library name.
213: * @param input The stream to get the data from.
214: * @exception Exception Description of the Exception
215: */
216: public void addNativeLibrary(String name, InputStream input)
217: throws Exception {
218: sendMsg("Adding native library : " + name + " ...");
219:
220: outJar.putNextEntry(new ZipEntry("native/" + name));
221: copyStream(input, outJar);
222: outJar.closeEntry();
223: input.close();
224: }
225:
226: /**
227: * Adds a language pack.
228: *
229: * @param iso3 The ISO3 code.
230: * @param input The stream to get the data from.
231: * @exception Exception Description of the Exception
232: */
233: public void addLangPack(String iso3, InputStream input)
234: throws Exception {
235: sendMsg("Adding langpack : " + iso3 + " ...");
236:
237: langpacks.add(iso3);
238:
239: outJar.putNextEntry(new ZipEntry("langpacks/" + iso3 + ".xml"));
240: copyStream(input, outJar);
241: outJar.closeEntry();
242: input.close();
243: }
244:
245: /**
246: * Adds a jar file content to the installer.
247: *
248: * @param file The jar filename.
249: * @exception Exception Description of the Exception
250: */
251: public void addJarContent(String file) throws Exception {
252: sendMsg("Adding a jar file content ...");
253: JarFile jar = new JarFile(file);
254: Enumeration entries = jar.entries();
255: while (entries.hasMoreElements()) {
256: // Puts a new entry
257: ZipEntry zentry = (ZipEntry) entries.nextElement();
258: try {
259: InputStream zin = jar.getInputStream(zentry);
260: outJar.putNextEntry(new ZipEntry(zentry.getName()));
261:
262: // Copy the data
263: copyStream(zin, outJar);
264: outJar.closeEntry();
265: zin.close();
266: } catch (ZipException zerr) {
267: }
268: }
269: }
270:
271: /**
272: * Tells the packager to finish the job (misc writings, cleanups, closings ,
273: * ...).
274: *
275: * @exception Exception Description of the Exception
276: */
277: public void finish() throws Exception {
278: // Usefull stuff
279: DataOutputStream datOut;
280: ObjectOutputStream objOut;
281: int size;
282: int i;
283:
284: sendMsg("Finishing the enpacking ...");
285:
286: // Writes the installation kind information
287: outJar.putNextEntry(new ZipEntry("kind"));
288: datOut = new DataOutputStream(outJar);
289: datOut.writeUTF("standard");
290: datOut.flush();
291: outJar.closeEntry();
292:
293: // Writes the packs informations
294: outJar.putNextEntry(new ZipEntry("packs.info"));
295: objOut = new ObjectOutputStream(outJar);
296: size = packs.size();
297: objOut.writeInt(size);
298: for (i = 0; i < size; i++)
299: objOut.writeObject(packs.get(i));
300: objOut.flush();
301: outJar.closeEntry();
302:
303: // Writes the langpacks informations
304: outJar.putNextEntry(new ZipEntry("langpacks.info"));
305: datOut = new DataOutputStream(outJar);
306: size = langpacks.size();
307: datOut.writeInt(size);
308: for (i = 0; i < size; i++)
309: datOut.writeUTF((String) langpacks.get(i));
310: datOut.flush();
311: outJar.closeEntry();
312:
313: // Closes the stream
314: outJar.flush();
315: outJar.close();
316:
317: sendStop();
318: }
319: }
|