Statistics
| Revision:

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

History | View | Annotate | Download (10 KB)

1
/*
2
 *  $Id: InstallerBase.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 :               InstallerBase.java
7
 *  Description :        Utility functions shared by the GUI and headless installers.
8
 *  Author's email :     julien@izforge.com
9
 *  Author's Website :   http://www.izforge.com
10
 *
11
 *  Portions are Copyright (C) 2002 Jan Blok (jblok@profdata.nl - PDM - www.profdata.nl)
12
 *
13
 *  This program is free software; you can redistribute it and/or
14
 *  modify it under the terms of the GNU General Public License
15
 *  as published by the Free Software Foundation; either version 2
16
 *  of the License, or any later version.
17
 *
18
 *  This program is distributed in the hope that it will be useful,
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 *  GNU General Public License for more details.
22
 *
23
 *  You should have received a copy of the GNU General Public License
24
 *  along with this program; if not, write to the Free Software
25
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26
 */
27
package com.izforge.izpack.installer;
28

    
29
import java.io.File;
30
import java.io.InputStream;
31
import java.io.ObjectInputStream;
32
import java.util.ArrayList;
33
import java.util.Enumeration;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Locale;
37
import java.util.Properties;
38

    
39
import com.izforge.izpack.CustomData;
40
import com.izforge.izpack.Info;
41
import com.izforge.izpack.Pack;
42
import com.izforge.izpack.util.IoHelper;
43
import com.izforge.izpack.util.OsConstraint;
44
import com.izforge.izpack.util.OsVersion;
45

    
46
/**
47
 * Common utility functions for the GUI and text installers. (Do not import
48
 * swing/awt classes to this class.)
49
 * 
50
 * @author Jonathan Halliday
51
 * @author Julien Ponge
52
 */
53
public class InstallerBase
54
{
55

    
56
  /**
57
   *  Loads the installation data. Also sets environment variables to
58
   * <code>installdata</code>. All system properties are available as
59
   * $SYSTEM_<variable> where <variable> is the actual name _BUT_ with
60
   * all separators replaced by '_'. Properties with null values 
61
   * are never stored.
62
   * Example: $SYSTEM_java_version or $SYSTEM_os_name
63
   * 
64
   * @param installdata Where to store the installation data.
65
   * 
66
   * @exception Exception Description of the Exception
67
   */
68
  public void loadInstallData(AutomatedInstallData installdata)
69
      throws Exception
70
  {
71
    // Usefull variables
72
    InputStream in;
73
    ObjectInputStream objIn;
74
    int size;
75
    int i;
76

    
77
    // We load the variables
78
    Properties variables = null;
79
    in = InstallerBase.class.getResourceAsStream("/vars");
80
    if (null != in)
81
    {
82
      objIn = new ObjectInputStream(in);
83
      variables = (Properties) objIn.readObject();
84
      objIn.close();
85
    }
86

    
87
    // We load the Info data
88
    in = InstallerBase.class.getResourceAsStream("/info");
89
    objIn = new ObjectInputStream(in);
90
    Info inf = (Info) objIn.readObject();
91
    objIn.close();
92

    
93
    // We put the Info data as variables
94
    installdata.setVariable(ScriptParser.APP_NAME, inf.getAppName());
95
    installdata.setVariable(ScriptParser.APP_URL, inf.getAppURL());
96
    installdata.setVariable(ScriptParser.APP_VER, inf.getAppVersion());
97

    
98
    // We read the panels order data
99
    in = InstallerBase.class.getResourceAsStream("/panelsOrder");
100
    objIn = new ObjectInputStream(in);
101
    List panelsOrder = (List) objIn.readObject();
102
    objIn.close();
103

    
104
    // We read the packs data
105
    in = InstallerBase.class.getResourceAsStream("/packs.info");
106
    objIn = new ObjectInputStream(in);
107
    size = objIn.readInt();
108
    ArrayList availablePacks = new ArrayList();
109
    ArrayList allPacks = new ArrayList();
110
    for (i = 0; i < size; i++)
111
    {
112
      Pack pk = (Pack) objIn.readObject();
113
      allPacks.add(pk);
114
      if (OsConstraint.oneMatchesCurrentSystem(pk.osConstraints))
115
          availablePacks.add(pk);
116
    }
117
    objIn.close();
118

    
119
    // We determine the operating system and the initial installation path
120
    String dir;
121
    String installPath;
122
    if (OsVersion.IS_WINDOWS)
123
    {
124
      dir = buildWindowsDefaultPath();
125
    }
126
    else if (OsVersion.IS_OSX)
127
    {
128
      dir = "/Applications" + File.separator;
129
    }
130
    else
131
    {
132
      if (new File("/usr/local/").canWrite())
133
      {
134
        dir = "/usr/local" + File.separator;
135
      }
136
      else
137
      {
138
        dir = System.getProperty("user.home") + File.separator;
139
      }
140
    }
141
    installdata.setVariable(ScriptParser.JAVA_HOME, System
142
        .getProperty("java.home"));
143
    installdata.setVariable(ScriptParser.USER_HOME, System
144
        .getProperty("user.home"));
145
    installdata.setVariable(ScriptParser.USER_NAME, System
146
        .getProperty("user.name"));
147
    installdata.setVariable(ScriptParser.FILE_SEPARATOR, File.separator);
148
    
149
    Enumeration e = System.getProperties().keys();
150
    while (e.hasMoreElements())
151
    {
152
      String varName = (String) e.nextElement();
153
      String varValue = System.getProperty(varName);
154
      if (varValue != null) {
155
              varName = varName.replace('.', '_');
156
        installdata.setVariable("SYSTEM_" + varName, varValue);
157
      }
158
    }
159
    
160
    if (null != variables)
161
    {
162
      Enumeration enum = variables.keys();
163
      String varName;
164
      String varValue;
165
      while (enum.hasMoreElements())
166
      {
167
        varName = (String) enum.nextElement();
168
        varValue = variables.getProperty(varName);
169
        installdata.setVariable(varName, varValue);
170
      }
171
    }
172
    
173
    installdata.info = inf;
174
    installdata.panelsOrder = panelsOrder;
175
    installdata.availablePacks = availablePacks;
176
    installdata.allPacks = allPacks;
177

    
178
    // get list of preselected packs
179
    Iterator pack_it = availablePacks.iterator();
180
    while (pack_it.hasNext())
181
    {
182
      Pack pack = (Pack) pack_it.next();
183
      if (pack.preselected) installdata.selectedPacks.add(pack);
184
    }
185
    // Set the installation path in a default manner
186
    installPath = dir + inf.getAppName();
187
    if( inf.getInstallationSubPath() != null )
188
    { // A subpath was defined, use it.
189
      installPath = IoHelper.translatePath(dir + inf.getInstallationSubPath(), 
190
        new VariableSubstitutor(installdata.getVariables()));
191
    }
192
    installdata.setInstallPath(installPath);
193
    // Load custom action data.
194
    loadCustomData(installdata);
195

    
196
  }
197

    
198
  /**
199
   * Builds the default path for Windows (i.e Program Files/...).
200
   * 
201
   * @return The Windows default installation path.
202
   */
203
  private String buildWindowsDefaultPath()
204
  {
205
    StringBuffer dpath = new StringBuffer("");
206
    try
207
    {
208
      // We load the properties
209
      Properties props = new Properties();
210
      props
211
          .load(InstallerBase.class
212
              .getResourceAsStream("/com/izforge/izpack/installer/win32-defaultpaths.properties"));
213

    
214
      // We look for the drive mapping
215
      String drive = System.getProperty("user.home");
216
      if (drive.length() > 3) drive = drive.substring(0, 3);
217

    
218
      // Now we have it :-)
219
      dpath.append(drive);
220

    
221
      // Ensure that we have a trailing backslash (in case drive was something
222
      // like "C:")
223
      if (drive.length() == 2) dpath.append("\\");
224

    
225
      String language = Locale.getDefault().getLanguage();
226
      String country = Locale.getDefault().getCountry();
227
      String language_country = language + "_" + country;
228

    
229
      // Try the most specific combination first
230
      if (null != props.getProperty(language_country))
231
      {
232
        dpath.append(props.getProperty(language_country));
233
      }
234
      else if (null != props.getProperty(language))
235
      {
236
        dpath.append(props.getProperty(language));
237
      }
238
      else
239
      {
240
        dpath.append(props.getProperty(Locale.ENGLISH.getLanguage()));
241
      }
242
      dpath.append("\\");
243
    }
244
    catch (Exception err)
245
    {
246
      dpath = new StringBuffer("C:\\Program Files\\");
247
    }
248

    
249
    return dpath.toString();
250
  }
251

    
252
  /**
253
   * Loads custom data like listener and lib references if exist and
254
   * fills the installdata.
255
   * 
256
   * @param installdata installdata into which the custom action data should be
257
   * stored
258
   * @throws Exception
259
   */
260
  private void loadCustomData(AutomatedInstallData installdata)
261
      throws Exception
262
  {
263
    // Usefull variables
264
    InputStream in;
265
    ObjectInputStream objIn;
266
    int i;
267
    // Load listeners if exist.
268
    String[] streamNames = AutomatedInstallData.CUSTOM_ACTION_TYPES;
269
    List[] out = new List[streamNames.length];
270
    for (i = 0; i < streamNames.length; ++i)
271
      out[i] = new ArrayList();
272
    in = InstallerBase.class.getResourceAsStream("/customData");
273
    if (in != null)
274
    {
275
      objIn = new ObjectInputStream(in);
276
      Object listeners = objIn.readObject();
277
      objIn.close();
278
      Iterator keys = ((List) listeners).iterator();
279
      while (keys != null && keys.hasNext())
280
      {
281
        CustomData ca = (CustomData) keys.next();
282

    
283
        if (ca.osConstraints != null
284
            && !OsConstraint.oneMatchesCurrentSystem(ca.osConstraints))
285
        { // OS constraint defined, but not matched; therefore ignore it.
286
          continue;
287
        }
288
        switch (ca.type)
289
        {
290
        case CustomData.INSTALLER_LISTENER:
291
          Class clazz = Class.forName(ca.listenerName);
292
          if( clazz == null )
293
            throw new InstallerException("Custom action " + ca.listenerName + " not bound!");
294
          out[ca.type].add(clazz.newInstance());
295
          break;
296
        case CustomData.UNINSTALLER_LISTENER:
297
        case CustomData.UNINSTALLER_JAR:
298
          out[ca.type].add(ca);
299
          break;
300
        case CustomData.UNINSTALLER_LIB:
301
          out[ca.type].add(ca.contents);
302
          break;
303
        }
304

    
305
      }
306
      // Add the current custem action data to the installdata hash map.
307
      for (i = 0; i < streamNames.length; ++i)
308
        installdata.customData.put(streamNames[i], out[i]);
309
    }
310
    // uninstallerLib list if exist
311

    
312
  }
313
}