KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jnlp > sample > JreInstaller > WindowsInstaller


1
2 /*
3  * @(#)WindowsInstaller.java 1.7 05/11/17
4  *
5  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * -Redistribution of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * -Redistribution in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
18  * be used to endorse or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
23  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
24  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
25  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
26  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
27  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
28  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
29  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
30  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32  *
33  * You acknowledge that this software is not designed, licensed or intended
34  * for use in the design, construction, operation or maintenance of any
35  * nuclear facility.
36  */

37
38 package jnlp.sample.JreInstaller;
39
40 import java.io.*;
41 import java.net.*;
42 import java.util.*;
43 import javax.swing.JOptionPane JavaDoc;
44
45 /**
46  *
47  * @version 1.14 05/15/03
48  */

49 public class WindowsInstaller {
50     
51     private static final String JavaDoc JAVAWS_REBOOT_FLAG_FILE = "jawsboot.ini";
52     
53     /**
54      * Executes the passed in string, returning true if the process
55      * succesfully completes, otherwise false.
56      */

57     public static boolean execute(String JavaDoc execString) {
58         Config.trace("execute !" + execString + "!");
59     try {
60         Process JavaDoc p = Runtime.getRuntime().exec(execString);
61         p.waitFor();
62     }
63     catch (IOException ioe) {
64         Config.trace("Got exception: " + ioe);
65         return false;
66     }
67     catch (InterruptedException JavaDoc ie) {
68         Config.trace("Got exception: " + ie);
69         return false;
70     }
71     return true;
72     }
73     
74     /** Creates a temp. ISS file used to launch Windows installer */
75     static File createTempISSScript(String JavaDoc jrepath, String JavaDoc version) throws IOException {
76         if (jrepath.endsWith("\\")) {
77             jrepath = jrepath.substring(0, jrepath.length() - 1);
78         }
79         String JavaDoc script = null;
80
81     if (version.equals("1.4.1_02")) {
82         script =
83         "[InstallShield Silent]\n" +
84         "Version=v6.00.000\n" +
85         "File=Response File\n" +
86         "[File Transfer]\n" +
87         "OverwrittenReadOnly=NoToAll\n" +
88         "[{EFCE5837-FC21-11D6-9D24-00010240CE95}-DlgOrder]\n" +
89         "Dlg0={EFCE5837-FC21-11D6-9D24-00010240CE95}-SdLicense-0\n" +
90         "Count=3\n" +
91         "Dlg1={EFCE5837-FC21-11D6-9D24-00010240CE95}-SetupType-0\n" +
92         "Dlg2={EFCE5837-FC21-11D6-9D24-00010240CE95}-SdAskDestPath-0\n" +
93         "[{EFCE5837-FC21-11D6-9D24-00010240CE95}-SdLicense-0]\n" +
94         "Result=1\n" +
95         "[{EFCE5837-FC21-11D6-9D24-00010240CE95}-SetupType-0]\n" +
96         "Result=303\n" +
97         "[{EFCE5837-FC21-11D6-9D24-00010240CE95}-SdAskDestPath-0]\n" +
98         "szDir=" + jrepath + "\n" +
99         "Result=1\n" +
100         "[Application]\n" +
101         "Name=Java 2 Runtime Environment, SE v" + version + "\n" +
102         "Version=" + version + "\n" +
103         "Company=JavaSoft\n" +
104         "Lang=0009\n";
105     } else if (Config.isHopper()) {
106         script =
107         "[InstallShield Silent]\n" +
108         "Version=v6.00.000\n" +
109         "File=Response File\n" +
110         "[File Transfer]\n" +
111         "OverwrittenReadOnly=NoToAll\n" +
112         "[{CD0159C9-17FB-11D6-A76A-00B0D079AF64}-DlgOrder]\n" +
113         "Dlg0={CD0159C9-17FB-11D6-A76A-00B0D079AF64}-SdLicense-0\n" +
114         "Count=3\n" +
115         "Dlg1={CD0159C9-17FB-11D6-A76A-00B0D079AF64}-SetupType-0\n" +
116         "Dlg2={CD0159C9-17FB-11D6-A76A-00B0D079AF64}-SdAskDestPath-0\n" +
117         "[{CD0159C9-17FB-11D6-A76A-00B0D079AF64}-SdLicense-0]\n" +
118         "Result=1\n" +
119         "[{CD0159C9-17FB-11D6-A76A-00B0D079AF64}-SetupType-0]\n" +
120         "Result=303\n" +
121         "[{CD0159C9-17FB-11D6-A76A-00B0D079AF64}-SdAskDestPath-0]\n" +
122         "szDir=" + jrepath + "\n" +
123         "Result=1\n" +
124         "[Application]\n" +
125         "Name=Java 2 Runtime Environment, SE v" + version + "\n" +
126         "Version=" + version + "\n" +
127         "Company=JavaSoft\n" +
128         "Lang=0009\n";
129     } else if (Config.isMerlin()) {
130         script =
131         "[InstallShield Silent]\n" +
132         "Version=v6.00.000\n" +
133         "File=Response File\n" +
134         "[File Transfer]\n" +
135         "OverwrittenReadOnly=NoToAll\n" +
136         "[{F4588301-0A06-11D6-A761-00B0D079AF64}-DlgOrder]\n" +
137         "Dlg0={EE21CE74-CD7F-11D5-A886-00B0D079AF64}-SdWelcome-0\n" +
138         "Count=4\n" +
139         "Dlg1={F4588301-0A06-11D6-A761-00B0D079AF64}-SdLicense-0\n" +
140         "Dlg2={F4588301-0A06-11D6-A761-00B0D079AF64}-SdAskDestPath-0\n" +
141         "Dlg3={F4588301-0A06-11D6-A761-00B0D079AF64}-SdAskOptions-0\n" +
142         "[{F4588301-0A06-11D6-A761-00B0D079AF64}-SdWelcome-0]\n" +
143         "Result=1\n" +
144         "[{F4588301-0A06-11D6-A761-00B0D079AF64}-SdLicense-0]\n" +
145         "Result=1\n" +
146         "[{F4588301-0A06-11D6-A761-00B0D079AF64}-SdAskDestPath-0]\n" +
147         "szDir=" + jrepath + "\n" +
148         "Result=1\n" +
149         "[{F4588301-0A06-11D6-A761-00B0D079AF64}-SdAskOptions-0]\n" +
150         "Component-type=string\n" +
151         "Component-count=0\n" +
152         "Result=1\n" +
153         "[Application]\n" +
154         "Name=Java 2 Runtime Environment, SE v" + version + "\n" +
155         "Version=" + version + "\n" +
156         "Company=JavaSoft\n" +
157         "Lang=0009\n";
158     } else if (version.equals("1.3.1_08")) {
159         script =
160         "[InstallShield Silent]\n" +
161         "Version=v6.00.000\n" +
162         "File=Response File\n" +
163         "[File Transfer]\n" +
164         "OverwrittenReadOnly=NoToAll\n" +
165         "[{B11CAD0E-0E0F-11D7-88BE-0050DA21757E}-DlgOrder]\n" +
166         "Dlg0={B11CAD0E-0E0F-11D7-88BE-0050DA21757E}-SdLicense-0\n" +
167         "Count=3\n" +
168         "Dlg1={B11CAD0E-0E0F-11D7-88BE-0050DA21757E}-SdAskDestPath-0\n" +
169         "Dlg2={B11CAD0E-0E0F-11D7-88BE-0050DA21757E}-SdAskOptions-0\n" +
170         "[{B11CAD0E-0E0F-11D7-88BE-0050DA21757E}-SdLicense-0]\n" +
171         "Result=1\n" +
172         "[{B11CAD0E-0E0F-11D7-88BE-0050DA21757E}-SdAskDestPath-0]\n" +
173         "szDir=" + jrepath + "\n" +
174         "Result=1\n" +
175         "[{B11CAD0E-0E0F-11D7-88BE-0050DA21757E}-SdAskOptions-0]\n" +
176         "Component-type=string\n" +
177         "Component-count=0\n" +
178         "Result=1\n" +
179         "[Application]\n" +
180         "Name=Java 2 Runtime Environment, SE v" + version + "\n" +
181         "Version=" + version + "\n" +
182         "Company=JavaSoft\n" +
183         "Lang=0009\n";
184     } else {
185         script =
186         "[InstallShield Silent]\n" +
187         "Version=v5.00.000\n" +
188         "File=Response File\n" +
189         "[DlgOrder]\n" +
190         "Dlg0=SdLicense-0\n" +
191         "Dlg2=SdFinishReboot-0\n" +
192         "Count=3\n" +
193         "Dlg1=SdAskDestPath-0\n" +
194         "[SdLicense-0]\n" +
195         "Result=1\n" +
196         "[SdAskDestPath-0]\n" +
197         "szDir=" + jrepath + "\n" +
198         "Result=1\n" +
199         "[Application]\n" +
200         "Name=Java Runtime Environment\n" +
201         "Version=" + version + "\n" +
202         "Company=JavaSoft\n" +
203         "Lang=0009\n" +
204         "[SdFinishReboot-0]\n" +
205         "Result=1\n" +
206         "BootOption=0\n";
207     }
208         
209         File result = File.createTempFile("jre", "iss");
210         result.deleteOnExit();
211         PrintStream out = new PrintStream(new FileOutputStream(result));
212         out.println(script);
213         out.close();
214         return result;
215     }
216     
217     /** Windows reboot manager */
218     
219     /**
220      * Checks the version of the MSVCRT.DLL. If the version is too old,
221      * the user is asked if they would like to reboot, otherwise JavaWS is
222      * restarted.
223      */

224     static public boolean IsRebootNecessary() {
225     long versionMS = getLongHexResource(Config.getMsvcrtVersionMS());
226     long versionLS = getLongHexResource(Config.getMsvcrtVersionLS());
227     
228     Config.trace("versions: " + versionMS + " " + versionLS);
229     
230     File tmp = null;
231     File flagFile = null;
232     try {
233         tmp = File.createTempFile("javaws", "tmp");
234         if (tmp != null) {
235         flagFile = new File(tmp.getParent() + File.separator +
236                     JAVAWS_REBOOT_FLAG_FILE);
237         tmp.delete();
238         }
239     } catch (IOException ioe) {
240     }
241     if (new WindowsInstaller().needsReboot(versionMS, versionLS) ||
242         (flagFile != null && Config.isHopper() && flagFile.exists())) {
243         Config.trace("needs reboot");
244         return true;
245     }
246     return false;
247     }
248     
249     /**
250      * Prompts the user if they would like to reboot now, or later. If they
251      * want to reboot now, <code>reboot</code>is invoked.
252      */

253     public static boolean askUserForReboot() {
254     String JavaDoc message = Config.getRebootMessage();
255     String JavaDoc title = Config.getRebootTitle();
256     Object JavaDoc[] options = new Object JavaDoc[] {
257         Config.getRebootNowString(),
258         Config.getRebootLaterString()
259     };
260     if (JOptionPane.showOptionDialog(null, message, title, -1,
261                      JOptionPane.QUESTION_MESSAGE,
262                      null, options, options[0]) == 0) {
263         WinRegistry.doReboot();
264         return true;
265     }
266     return false;
267     }
268         
269     /**
270      * Returns the resource named <code>key</code> this is a hex value
271      * as a long.
272      */

273     static private long getLongHexResource(String JavaDoc value)
274     throws MissingResourceException {
275     long retValue = 0;
276     int length = value.length();
277     for (int counter = 0; counter < length; counter++) {
278         char aChar = value.charAt(counter);
279         retValue *= 16;
280         if (Character.isDigit(aChar)) {
281         retValue += (aChar - '0');
282         }
283         else if (aChar >= 'a' && aChar <= 'f') {
284         retValue += (10 + aChar - 'a');
285         }
286         else if (aChar >= 'A' && aChar <= 'F') {
287         retValue += (10 + aChar - 'A');
288         }
289     }
290     return retValue;
291     }
292     
293     /**
294      * Native method that returns true if the current version of the JRE
295      * is less than the passed in version.
296      */

297     private native boolean needsReboot(long versionMS, long versionLS);
298     
299     /**
300      * Native method to do a reboot.
301      */

302     private native void reboot();
303 }
304
305
306
Popular Tags