KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > installer > AutomatedInstaller


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2003 Jonathan Halliday
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.installer;
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.TreeMap JavaDoc;
33 import java.util.Vector JavaDoc;
34 import java.util.zip.ZipEntry JavaDoc;
35 import java.util.zip.ZipOutputStream JavaDoc;
36
37 import net.n3.nanoxml.NonValidator;
38 import net.n3.nanoxml.StdXMLBuilder;
39 import net.n3.nanoxml.StdXMLParser;
40 import net.n3.nanoxml.StdXMLReader;
41 import net.n3.nanoxml.XMLElement;
42
43 import com.izforge.izpack.ExecutableFile;
44 import com.izforge.izpack.LocaleDatabase;
45 import com.izforge.izpack.Panel;
46 import com.izforge.izpack.util.Debug;
47 import com.izforge.izpack.util.Housekeeper;
48 import com.izforge.izpack.util.OsConstraint;
49
50 /**
51  * Runs the install process in text only (no GUI) mode.
52  *
53  * @author Jonathan Halliday <jonathan.halliday@arjuna.com>
54  * @author Julien Ponge <julien@izforge.com>
55  * @author Johannes Lehtinen <johannes.lehtinen@iki.fi>
56  */

57 public class AutomatedInstaller extends InstallerBase
58 {
59
60     // there are panels which can be instantiated multiple times
61
// we therefore need to select the right XML section for each
62
// instance
63
private TreeMap JavaDoc panelInstanceCount;
64
65     /** The automated installation data. */
66     private AutomatedInstallData idata = new AutomatedInstallData();
67
68     /** The result of the installation. */
69     private boolean result = false;
70     
71     /**
72      * Constructing an instance triggers the install.
73      *
74      * @param inputFilename Name of the file containing the installation data.
75      * @exception Exception Description of the Exception
76      */

77     public AutomatedInstaller(String JavaDoc inputFilename) throws Exception JavaDoc
78     {
79         super();
80
81         File JavaDoc input = new File JavaDoc(inputFilename);
82
83         // Loads the installation data
84
loadInstallData(this.idata);
85
86         // Loads the xml data
87
this.idata.xmlData = getXMLData(input);
88
89         // Loads the langpack
90
this.idata.localeISO3 = this.idata.xmlData.getAttribute("langpack", "eng");
91         InputStream JavaDoc in = getClass().getResourceAsStream("/langpacks/" + this.idata.localeISO3 + ".xml");
92         this.idata.langpack = new LocaleDatabase(in);
93         this.idata.setVariable(ScriptParser.ISO3_LANG, this.idata.localeISO3);
94
95         // create the resource manager singleton
96
ResourceManager.create(this.idata);
97
98         // Load custom langpack if exist.
99
addCustomLangpack(this.idata);
100
101         this.panelInstanceCount = new TreeMap JavaDoc();
102     }
103
104     /**
105      * Writes the uninstalldata.
106      *
107      * Unfortunately, Java doesn't allow multiple inheritance, so <code>AutomatedInstaller</code>
108      * and <code>InstallerFrame</code> can't share this code ... :-/
109      *
110      * TODO: We should try to fix this in the future.
111      */

112     private boolean writeUninstallData()
113     {
114         try
115         {
116             // We get the data
117
UninstallData udata = UninstallData.getInstance();
118             List JavaDoc files = udata.getFilesList();
119             ZipOutputStream JavaDoc outJar = this.idata.uninstallOutJar;
120
121             if (outJar == null)
122                 return true; // it is allowed not to have an installer
123

124             System.out.println("[ Writing the uninstaller data ... ]");
125
126             // We write the files log
127
outJar.putNextEntry(new ZipEntry JavaDoc("install.log"));
128             BufferedWriter JavaDoc logWriter = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(outJar));
129             logWriter.write(this.idata.getInstallPath());
130             logWriter.newLine();
131             Iterator JavaDoc iter = files.iterator();
132             while (iter.hasNext())
133             {
134                 logWriter.write((String JavaDoc) iter.next());
135                 if (iter.hasNext()) logWriter.newLine();
136             }
137             logWriter.flush();
138             outJar.closeEntry();
139
140             // We write the uninstaller jar file log
141
outJar.putNextEntry(new ZipEntry JavaDoc("jarlocation.log"));
142             logWriter = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(outJar));
143             logWriter.write(udata.getUninstallerJarFilename());
144             logWriter.newLine();
145             logWriter.write(udata.getUninstallerPath());
146             logWriter.flush();
147             outJar.closeEntry();
148
149             // Write out executables to execute on uninstall
150
outJar.putNextEntry(new ZipEntry JavaDoc("executables"));
151             ObjectOutputStream JavaDoc execStream = new ObjectOutputStream JavaDoc(outJar);
152             iter = udata.getExecutablesList().iterator();
153             execStream.writeInt(udata.getExecutablesList().size());
154             while (iter.hasNext())
155             {
156                 ExecutableFile file = (ExecutableFile) iter.next();
157                 execStream.writeObject(file);
158             }
159             execStream.flush();
160             outJar.closeEntry();
161
162             // Cleanup
163
outJar.flush();
164             outJar.close();
165             return true;
166         }
167         catch (Exception JavaDoc err)
168         {
169             err.printStackTrace();
170             return false;
171         }
172     }
173
174     /**
175      * Runs the automated installation logic for each panel in turn.
176      *
177      * @throws Exception
178      */

179     protected void doInstall() throws Exception JavaDoc
180     {
181         // TODO: i18n
182
System.out.println("[ Starting automated installation ]");
183         Debug.log("[ Starting automated installation ]");
184
185         try
186         {
187             // assume that installation will succeed
188
this.result = true;
189             
190             // walk the panels in order
191
Iterator JavaDoc panelsIterator = this.idata.panelsOrder.iterator();
192             while (panelsIterator.hasNext())
193             {
194                 Panel p = (Panel) panelsIterator.next();
195                 
196                 String JavaDoc praefix = "com.izforge.izpack.panels.";
197                 if (p.className.compareTo(".") > -1)
198                 // Full qualified class name
199
praefix = "";
200                 if (!OsConstraint.oneMatchesCurrentSystem(p.osConstraints)) continue;
201     
202                 String JavaDoc panelClassName = p.className;
203                 String JavaDoc automationHelperClassName = praefix + panelClassName + "AutomationHelper";
204                 Class JavaDoc automationHelperClass = null;
205                 
206                 Debug.log( "AutomationHelper:" + automationHelperClassName );
207                 // determine if the panel supports automated install
208
try
209                 {
210                     
211                     automationHelperClass = Class.forName(automationHelperClassName);
212                     
213                 }
214                 catch (ClassNotFoundException JavaDoc e)
215                 {
216                     // this is OK - not all panels have/need automation support.
217
Debug.log( "ClassNotFoundException-skip :" + automationHelperClassName );
218                     continue;
219                 }
220     
221                 // instantiate the automation logic for the panel
222
PanelAutomation automationHelperInstance = null;
223                 if (automationHelperClass != null)
224                 {
225                     try
226                     {
227                         Debug.log( "Instantiate :" + automationHelperClassName );
228                         automationHelperInstance = (PanelAutomation) automationHelperClass
229                                 .newInstance();
230                     }
231                     catch (Exception JavaDoc e)
232                     {
233                         Debug.log("ERROR: no default constructor for "
234                                 + automationHelperClassName + ", skipping...");
235                         continue;
236                     }
237                 }
238     
239                 // We get the panels root xml markup
240
Vector JavaDoc panelRoots = this.idata.xmlData.getChildrenNamed(panelClassName);
241                 int panelRootNo = 0;
242     
243                 if (this.panelInstanceCount.containsKey(panelClassName))
244                 {
245                     // get number of panel instance to process
246
panelRootNo = ((Integer JavaDoc) this.panelInstanceCount.get(panelClassName)).intValue();
247                 }
248     
249                 XMLElement panelRoot = (XMLElement) panelRoots.elementAt(panelRootNo);
250     
251                 this.panelInstanceCount.put(panelClassName, new Integer JavaDoc(panelRootNo + 1));
252     
253                 // execute the installation logic for the current panel, if it has
254
// any:
255
if (automationHelperInstance != null)
256                 {
257                     try
258                     {
259                         Debug.log( "automationHelperInstance.runAutomated :" + automationHelperClassName + " entered." );
260                         if (! automationHelperInstance.runAutomated(this.idata, panelRoot))
261                         {
262                             // make installation fail instantly
263
this.result = false;
264                             return;
265                         }
266                         else
267                         {
268                           Debug.log( "automationHelperInstance.runAutomated :" + automationHelperClassName + " successfully done." );
269                         }
270                     }
271                     catch (Exception JavaDoc e)
272                     {
273                         Debug.log( "ERROR: automated installation failed for panel "
274                                 + panelClassName );
275                         e.printStackTrace();
276                         this.result = false;
277                         continue;
278                     }
279     
280                 }
281     
282             }
283     
284             // this does nothing if the uninstaller was not included
285
writeUninstallData();
286     
287             if (this.result)
288                 System.out.println("[ Automated installation done ]");
289             else
290                 System.out.println("[ Automated installation FAILED! ]");
291         }
292         catch (Exception JavaDoc e)
293         {
294             this.result = false;
295             System.err.println(e.toString());
296             e.printStackTrace();
297             System.out.println("[ Automated installation FAILED! ]");
298         }
299         finally
300         {
301             // Bye
302
Housekeeper.getInstance().shutDown(this.result ? 0 : 1);
303         }
304     }
305
306     /**
307      * Loads the xml data for the automated mode.
308      *
309      * @param input The file containing the installation data.
310      *
311      * @return The root of the XML file.
312      *
313      * @exception Exception thrown if there are problems reading the file.
314      */

315     public XMLElement getXMLData(File JavaDoc input) throws Exception JavaDoc
316     {
317         FileInputStream JavaDoc in = new FileInputStream JavaDoc(input);
318
319         // Initialises the parser
320
StdXMLParser parser = new StdXMLParser();
321         parser.setBuilder(new StdXMLBuilder());
322         parser.setReader(new StdXMLReader(in));
323         parser.setValidator(new NonValidator());
324
325         XMLElement rtn = (XMLElement) parser.parse();
326         in.close();
327
328         return rtn;
329     }
330     
331     /**
332      * Get the result of the installation.
333      *
334      * @return True if the installation was successful.
335      */

336     public boolean getResult()
337     {
338         return this.result;
339     }
340 }
341
Popular Tags