Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / installer / AutomatedInstaller.java @ 15280

History | View | Annotate | Download (9.1 KB)

1
/*
2
 *  $Id: AutomatedInstaller.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 *  IzPack
4
 *  Copyright (C) 2001-2003 Jonathan Halliday, Julien Ponge
5
 *
6
 *  File :               AutomatedInstaller.java
7
 *  Description :        The silent (headless) installer.
8
 *  Author's email :     jonathan.halliday@arjuna.com
9
 *  Author's Website :   http://www.arjuna.com
10
 *
11
 *  This program is free software; you can redistribute it and/or
12
 *  modify it under the terms of the GNU General Public License
13
 *  as published by the Free Software Foundation; either version 2
14
 *  of the License, or any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program; if not, write to the Free Software
23
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
 */
25
package com.izforge.izpack.installer;
26

    
27
import java.io.BufferedWriter;
28
import java.io.File;
29
import java.io.FileInputStream;
30
import java.io.InputStream;
31
import java.io.ObjectOutputStream;
32
import java.io.OutputStreamWriter;
33
import java.util.Iterator;
34
import java.util.List;
35
import java.util.TreeMap;
36
import java.util.Vector;
37
import java.util.zip.ZipEntry;
38
import java.util.zip.ZipOutputStream;
39

    
40
import net.n3.nanoxml.NonValidator;
41
import net.n3.nanoxml.StdXMLBuilder;
42
import net.n3.nanoxml.StdXMLParser;
43
import net.n3.nanoxml.StdXMLReader;
44
import net.n3.nanoxml.XMLElement;
45

    
46
import com.izforge.izpack.ExecutableFile;
47
import com.izforge.izpack.LocaleDatabase;
48
import com.izforge.izpack.Panel;
49
import com.izforge.izpack.util.Housekeeper;
50
import com.izforge.izpack.util.OsConstraint;
51

    
52
/**
53
 *  Runs the install process in text only (no GUI) mode.
54
 *
55
 * @author Jonathan Halliday <jonathan.halliday@arjuna.com>
56
 * @author Julien Ponge <julien@izforge.com>
57
 * @author Johannes Lehtinen <johannes.lehtinen@iki.fi>
58
 */
59
public class AutomatedInstaller extends InstallerBase
60
{
61
  // there are panels which can be instantiated multiple times
62
  // we therefore need to select the right XML section for each
63
  // instance
64
  private TreeMap panelInstanceCount;
65
  
66
        /** The automated installation data. */
67
        private AutomatedInstallData idata = new AutomatedInstallData();
68

    
69
  /**
70
   *  Constructing an instance triggers the install.
71
   *
72
   * @param inputFilename Name of the file containing the installation data.
73
   * @exception Exception Description of the Exception
74
   */
75
  public AutomatedInstaller(String inputFilename) throws Exception
76
  {
77
    super();
78

    
79
    File input = new File(inputFilename);
80

    
81
    // Loads the installation data
82
    loadInstallData(idata);
83

    
84
    // Loads the xml data
85
    idata.xmlData = getXMLData(input);
86

    
87
    // Loads the langpack
88
    idata.localeISO3 = idata.xmlData.getAttribute("langpack", "eng");
89
    InputStream in =
90
      getClass().getResourceAsStream("/langpacks/" + idata.localeISO3 + ".xml");
91
    idata.langpack = new LocaleDatabase(in);
92
    idata.setVariable(ScriptParser.ISO3_LANG, idata.localeISO3);
93

    
94
    // create the resource manager singleton
95
    ResourceManager.create(idata);
96

    
97
    this.panelInstanceCount = new TreeMap();
98

    
99
    doInstall(idata);
100
  }
101

    
102
  /**
103
   * Writes the uninstalldata.
104
   *
105
   * Unfortunately, Java doesn't allow multiple inheritance, so
106
   * <code>AutomatedInstaller</code> and <code>InstallerFrame</code> can't
107
   * share this code ... :-/
108
   * 
109
   * TODO: We should try to fix this in the future. 
110
   */
111
  private void writeUninstallData()
112
  {
113
    try
114
    {
115
      // We get the data
116
      UninstallData udata = UninstallData.getInstance();
117
      List files = udata.getFilesList();
118
      ZipOutputStream outJar = idata.uninstallOutJar;
119

    
120
      if (outJar == null)
121
        return;
122

    
123
      System.out.println("[ Writing the uninstaller data ... ]");
124

    
125
      // We write the files log
126
      outJar.putNextEntry(new ZipEntry("install.log"));
127
      BufferedWriter logWriter =
128
        new BufferedWriter(new OutputStreamWriter(outJar));
129
      logWriter.write(idata.getInstallPath());
130
      logWriter.newLine();
131
      Iterator iter = files.iterator();
132
      while (iter.hasNext())
133
      {
134
        logWriter.write((String) iter.next());
135
        if (iter.hasNext())
136
          logWriter.newLine();
137
      }
138
      logWriter.flush();
139
      outJar.closeEntry();
140

    
141
      // We write the uninstaller jar file log
142
      outJar.putNextEntry(new ZipEntry("jarlocation.log"));
143
      logWriter = new BufferedWriter(new OutputStreamWriter(outJar));
144
      logWriter.write(udata.getUninstallerJarFilename());
145
      logWriter.newLine();
146
      logWriter.write(udata.getUninstallerPath());
147
      logWriter.flush();
148
      outJar.closeEntry();
149

    
150
      // Write out executables to execute on uninstall
151
      outJar.putNextEntry(new ZipEntry("executables"));
152
      ObjectOutputStream execStream = new ObjectOutputStream(outJar);
153
      iter = udata.getExecutablesList().iterator();
154
      execStream.writeInt(udata.getExecutablesList().size());
155
      while (iter.hasNext())
156
      {
157
        ExecutableFile file = (ExecutableFile) iter.next();
158
        execStream.writeObject(file);
159
      }
160
      execStream.flush();
161
      outJar.closeEntry();
162

    
163
      // Cleanup
164
      outJar.flush();
165
      outJar.close();
166
    } catch (Exception err)
167
    {
168
      err.printStackTrace();
169
    }
170
  }
171

    
172
  /**
173
   * Runs the automated installation logic for each panel in turn.
174
   *
175
   * @param installdata the installation data.
176
   * @throws Exception
177
   */
178
  private void doInstall(AutomatedInstallData installdata) throws Exception
179
  {
180
    // TODO: i18n
181
    System.out.println("[ Starting automated installation ]");
182

    
183
    // walk the panels in order
184
    Iterator panelsIterator = installdata.panelsOrder.iterator();
185
    while (panelsIterator.hasNext())
186
    {
187
      Panel p = (Panel) panelsIterator.next();
188
      String praefix = "com.izforge.izpack.panels.";
189
      if( p.className.compareTo(".") > -1 )
190
        // Full qualified class name
191
        praefix = "";
192
      if (!OsConstraint.oneMatchesCurrentSystem(p.osConstraints))
193
        continue;
194

    
195
      String panelClassName = p.className;
196
      String automationHelperClassName =
197
        praefix + panelClassName + "AutomationHelper";
198
      Class automationHelperClass = null;
199
      // determine if the panel supports automated install
200
      try
201
      {
202
        automationHelperClass = Class.forName(automationHelperClassName);
203
      } catch (ClassNotFoundException e)
204
      {
205
        // this is OK - not all panels have/need automation support.
206
        continue;
207
      }
208

    
209
      // instantiate the automation logic for the panel
210
      PanelAutomation automationHelperInstance = null;
211
      if (automationHelperClass != null)
212
      {
213
        try
214
        {
215
          automationHelperInstance =
216
            (PanelAutomation) automationHelperClass.newInstance();
217
        } catch (Exception e)
218
        {
219
          System.err.println(
220
            "ERROR: no default constructor for "
221
              + automationHelperClassName
222
              + ", skipping...");
223
          continue;
224
        }
225
      }
226

    
227
      // We get the panels root xml markup
228
      Vector panelRoots = installdata.xmlData.getChildrenNamed(panelClassName);
229
      int panelRootNo = 0;
230

    
231
      if (this.panelInstanceCount.containsKey(panelClassName))
232
      {
233
        // get number of panel instance to process
234
        panelRootNo =
235
          ((Integer) this.panelInstanceCount.get(panelClassName)).intValue();
236
      }
237

    
238
      XMLElement panelRoot = (XMLElement) panelRoots.elementAt(panelRootNo);
239

    
240
      this.panelInstanceCount.put(panelClassName, new Integer(panelRootNo + 1));
241

    
242
      // execute the installation logic for the current panel, if it has any:
243
      if (automationHelperInstance != null)
244
      {
245
        try
246
        {
247
          automationHelperInstance.runAutomated(installdata, panelRoot);
248
        } catch (Exception e)
249
        {
250
          System.err.println(
251
            "ERROR: automated installation failed for panel " + panelClassName);
252
          e.printStackTrace();
253
          continue;
254
        }
255

    
256
      }
257

    
258
    }
259

    
260
    // this does nothing if the uninstaller was not included
261
    writeUninstallData();
262

    
263
    System.out.println("[ Automated installation done ]");
264

    
265
    // Bye
266
    Housekeeper.getInstance().shutDown(0);
267
  }
268

    
269
  /**
270
   *  Loads the xml data for the automated mode.
271
   *
272
   * @param  input          The file containing the installation data.
273
   * @exception  Exception  thrown if there are problems reading the file.
274
   */
275
  public XMLElement getXMLData(File input) throws Exception
276
  {
277
    FileInputStream in = new FileInputStream(input);
278

    
279
    // Initialises the parser
280
    StdXMLParser parser = new StdXMLParser();
281
    parser.setBuilder(new StdXMLBuilder());
282
    parser.setReader(new StdXMLReader(in));
283
    parser.setValidator(new NonValidator());
284

    
285
    XMLElement rtn = (XMLElement) parser.parse();
286
    in.close();
287

    
288
    return rtn;
289
  }
290
}