KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.net.InetAddress JavaDoc;
34
35 import com.izforge.izpack.CustomData;
36 import com.izforge.izpack.Info;
37 import com.izforge.izpack.Pack;
38 import com.izforge.izpack.util.Debug;
39 import com.izforge.izpack.util.IoHelper;
40 import com.izforge.izpack.util.OsConstraint;
41 import com.izforge.izpack.util.OsVersion;
42 import com.izforge.izpack.util.VariableSubstitutor;
43
44 /**
45  * Common utility functions for the GUI and text installers. (Do not import swing/awt classes to
46  * this class.)
47  *
48  * @author Jonathan Halliday
49  * @author Julien Ponge
50  */

51 public class InstallerBase
52 {
53
54     /**
55      * The base name of the XML file that specifies the custom langpack. Searched is for the file
56      * with the name expanded by _ISO3.
57      */

58     protected static final String JavaDoc LANG_FILE_NAME = "CustomLangpack.xml";
59
60     /**
61      * Loads the installation data. Also sets environment variables to <code>installdata</code>.
62      * All system properties are available as $SYSTEM_<variable> where <variable> is the actual
63      * name _BUT_ with all separators replaced by '_'. Properties with null values are never stored.
64      * Example: $SYSTEM_java_version or $SYSTEM_os_name
65      *
66      * @param installdata Where to store the installation data.
67      *
68      * @exception Exception Description of the Exception
69      */

70     public void loadInstallData(AutomatedInstallData installdata) throws Exception JavaDoc
71     {
72         // Usefull variables
73
InputStream JavaDoc in;
74         ObjectInputStream JavaDoc objIn;
75         int size;
76         int i;
77
78         // We load the variables
79
Properties JavaDoc variables = null;
80         in = InstallerBase.class.getResourceAsStream("/vars");
81         if (null != in)
82         {
83             objIn = new ObjectInputStream JavaDoc(in);
84             variables = (Properties JavaDoc) objIn.readObject();
85             objIn.close();
86         }
87
88         // We load the Info data
89
in = InstallerBase.class.getResourceAsStream("/info");
90         objIn = new ObjectInputStream JavaDoc(in);
91         Info inf = (Info) objIn.readObject();
92         objIn.close();
93
94         // We put the Info data as variables
95
installdata.setVariable(ScriptParser.APP_NAME, inf.getAppName());
96         if (inf.getAppURL() != null)
97             installdata.setVariable(ScriptParser.APP_URL, inf.getAppURL());
98         installdata.setVariable(ScriptParser.APP_VER, inf.getAppVersion());
99
100         // We read the panels order data
101
in = InstallerBase.class.getResourceAsStream("/panelsOrder");
102         objIn = new ObjectInputStream JavaDoc(in);
103         List JavaDoc panelsOrder = (List JavaDoc) objIn.readObject();
104         objIn.close();
105
106         // We read the packs data
107
in = InstallerBase.class.getResourceAsStream("/packs.info");
108         objIn = new ObjectInputStream JavaDoc(in);
109         size = objIn.readInt();
110         ArrayList JavaDoc availablePacks = new ArrayList JavaDoc();
111         ArrayList JavaDoc allPacks = new ArrayList JavaDoc();
112         for (i = 0; i < size; i++)
113         {
114             Pack pk = (Pack) objIn.readObject();
115             allPacks.add(pk);
116             if (OsConstraint.oneMatchesCurrentSystem(pk.osConstraints)) availablePacks.add(pk);
117         }
118         objIn.close();
119
120         // We determine the operating system and the initial installation path
121
String JavaDoc dir;
122         String JavaDoc installPath;
123         if (OsVersion.IS_WINDOWS)
124         {
125             dir = buildWindowsDefaultPath();
126         }
127         else if (OsVersion.IS_OSX)
128         {
129             dir = "/Applications";
130         }
131         else
132         {
133             if (new File JavaDoc("/usr/local/").canWrite())
134             {
135                 dir = "/usr/local";
136             }
137             else
138             {
139                 dir = System.getProperty("user.home");
140             }
141         }
142         
143         // We determine the hostname and IPAdress
144
String JavaDoc hostname;
145         String JavaDoc IPAddress;
146         
147         try {
148             InetAddress JavaDoc addr = InetAddress.getLocalHost();
149     
150                 // Get IP Address
151
IPAddress = addr.getHostAddress();
152             
153                 // Get hostname
154
hostname = addr.getHostName();
155         } catch (Exception JavaDoc e) {
156             hostname = "";
157             IPAddress = "";
158         }
159         
160                 
161
162         installdata.setVariable("APPLICATIONS_DEFAULT_ROOT", dir);
163         dir += File.separator;
164         installdata.setVariable(ScriptParser.JAVA_HOME, System.getProperty("java.home"));
165         installdata.setVariable(ScriptParser.CLASS_PATH, System.getProperty("java.class.path"));
166         installdata.setVariable(ScriptParser.USER_HOME, System.getProperty("user.home"));
167         installdata.setVariable(ScriptParser.USER_NAME, System.getProperty("user.name"));
168         installdata.setVariable(ScriptParser.IP_ADDRESS, IPAddress);
169         installdata.setVariable(ScriptParser.HOST_NAME, hostname);
170         installdata.setVariable(ScriptParser.FILE_SEPARATOR, File.separator);
171
172         Enumeration JavaDoc e = System.getProperties().keys();
173         while (e.hasMoreElements())
174         {
175             String JavaDoc varName = (String JavaDoc) e.nextElement();
176             String JavaDoc varValue = System.getProperty(varName);
177             if (varValue != null)
178             {
179                 varName = varName.replace('.', '_');
180                 installdata.setVariable("SYSTEM_" + varName, varValue);
181             }
182         }
183
184         if (null != variables)
185         {
186             Enumeration JavaDoc enumeration = variables.keys();
187             String JavaDoc varName;
188             String JavaDoc varValue;
189             while (enumeration.hasMoreElements())
190             {
191                 varName = (String JavaDoc) enumeration.nextElement();
192                 varValue = variables.getProperty(varName);
193                 installdata.setVariable(varName, varValue);
194             }
195         }
196
197         installdata.info = inf;
198         installdata.panelsOrder = panelsOrder;
199         installdata.availablePacks = availablePacks;
200         installdata.allPacks = allPacks;
201
202         // get list of preselected packs
203
Iterator JavaDoc pack_it = availablePacks.iterator();
204         while (pack_it.hasNext())
205         {
206             Pack pack = (Pack) pack_it.next();
207             if (pack.preselected) installdata.selectedPacks.add(pack);
208         }
209         // Set the installation path in a default manner
210
installPath = dir + inf.getAppName();
211         if (inf.getInstallationSubPath() != null)
212         { // A subpath was defined, use it.
213
installPath = IoHelper.translatePath(dir + inf.getInstallationSubPath(),
214                     new VariableSubstitutor(installdata.getVariables()));
215         }
216         installdata.setInstallPath(installPath);
217         // Load custom action data.
218
loadCustomData(installdata);
219
220     }
221
222     /**
223      * Add the contents of a custom langpack (if exist) to the previos loaded comman langpack. If
224      * not exist, trace an info and do nothing more.
225      *
226      * @param idata install data to be used
227      */

228     protected void addCustomLangpack(AutomatedInstallData idata)
229     {
230         // We try to load and add a custom langpack.
231
try
232         {
233             idata.langpack.add(ResourceManager.getInstance().getInputStream(LANG_FILE_NAME));
234         }
235         catch (Throwable JavaDoc exception)
236         {
237             Debug.trace("No custom langpack available.");
238             return;
239         }
240         Debug.trace("Custom langpack for " + idata.localeISO3 + " available.");
241     }
242
243     /**
244      * Get the default path for Windows (i.e Program Files/...).
245      * Windows has a Setting for this in the environment and in the registry.
246      * Just try to use the setting in the environment. If it fails for whatever reason, we take the former solution (buildWindowsDefaultPathFromProps).
247      * @return The Windows default installation path for applications.
248      */

249     private String JavaDoc buildWindowsDefaultPath()
250     {
251       try{
252         //get value from environment...
253
String JavaDoc prgFilesPath = IoHelper.getenv("ProgramFiles");
254         if (prgFilesPath!=null && prgFilesPath.length()>0){
255           return prgFilesPath;
256         }else{
257           return buildWindowsDefaultPathFromProps();
258         }
259       }catch(Exception JavaDoc x){
260         x.printStackTrace();
261         return buildWindowsDefaultPathFromProps();
262       }
263     }
264     /**
265      * just plain wrong in case the programfiles are not stored where the developer expects them.
266      * E.g. in custom installations of large companies or if used internationalized version of windows with a language pack.
267      * @return
268      */

269     private String JavaDoc buildWindowsDefaultPathFromProps()
270     {
271         StringBuffer JavaDoc dpath = new StringBuffer JavaDoc("");
272         try
273         {
274             // We load the properties
275
Properties JavaDoc props = new Properties JavaDoc();
276             props
277                     .load(InstallerBase.class
278                             .getResourceAsStream("/com/izforge/izpack/installer/win32-defaultpaths.properties"));
279
280             // We look for the drive mapping
281
String JavaDoc drive = System.getProperty("user.home");
282             if (drive.length() > 3) drive = drive.substring(0, 3);
283
284             // Now we have it :-)
285
dpath.append(drive);
286
287             // Ensure that we have a trailing backslash (in case drive was
288
// something
289
// like "C:")
290
if (drive.length() == 2) dpath.append("\\");
291
292             String JavaDoc language = Locale.getDefault().getLanguage();
293             String JavaDoc country = Locale.getDefault().getCountry();
294             String JavaDoc language_country = language + "_" + country;
295
296             // Try the most specific combination first
297
if (null != props.getProperty(language_country))
298             {
299                 dpath.append(props.getProperty(language_country));
300             }
301             else if (null != props.getProperty(language))
302             {
303                 dpath.append(props.getProperty(language));
304             }
305             else
306             {
307                 dpath.append(props.getProperty(Locale.ENGLISH.getLanguage()));
308             }
309         }
310         catch (Exception JavaDoc err)
311         {
312             dpath = new StringBuffer JavaDoc("C:\\Program Files");
313         }
314
315         return dpath.toString();
316     }
317
318     /**
319      * Loads custom data like listener and lib references if exist and fills the installdata.
320      *
321      * @param installdata installdata into which the custom action data should be stored
322      * @throws Exception
323      */

324     private void loadCustomData(AutomatedInstallData installdata) throws Exception JavaDoc
325     {
326         // Usefull variables
327
InputStream JavaDoc in;
328         ObjectInputStream JavaDoc objIn;
329         int i;
330         // Load listeners if exist.
331
String JavaDoc[] streamNames = AutomatedInstallData.CUSTOM_ACTION_TYPES;
332         List JavaDoc[] out = new List JavaDoc[streamNames.length];
333         for (i = 0; i < streamNames.length; ++i)
334             out[i] = new ArrayList JavaDoc();
335         in = InstallerBase.class.getResourceAsStream("/customData");
336         if (in != null)
337         {
338             objIn = new ObjectInputStream JavaDoc(in);
339             Object JavaDoc listeners = objIn.readObject();
340             objIn.close();
341             Iterator JavaDoc keys = ((List JavaDoc) listeners).iterator();
342             while (keys != null && keys.hasNext())
343             {
344                 CustomData ca = (CustomData) keys.next();
345
346                 if (ca.osConstraints != null
347                         && !OsConstraint.oneMatchesCurrentSystem(ca.osConstraints))
348                 { // OS constraint defined, but not matched; therefore ignore
349
// it.
350
continue;
351                 }
352                 switch (ca.type)
353                 {
354                 case CustomData.INSTALLER_LISTENER:
355                     Class JavaDoc clazz = Class.forName(ca.listenerName);
356                     if (clazz == null)
357                         throw new InstallerException("Custom action " + ca.listenerName
358                                 + " not bound!");
359                     out[ca.type].add(clazz.newInstance());
360                     break;
361                 case CustomData.UNINSTALLER_LISTENER:
362                 case CustomData.UNINSTALLER_JAR:
363                     out[ca.type].add(ca);
364                     break;
365                 case CustomData.UNINSTALLER_LIB:
366                     out[ca.type].add(ca.contents);
367                     break;
368                 }
369
370             }
371             // Add the current custem action data to the installdata hash map.
372
for (i = 0; i < streamNames.length; ++i)
373                 installdata.customData.put(streamNames[i], out[i]);
374         }
375         // uninstallerLib list if exist
376

377     }
378 }
379
Popular Tags