KickJava   Java API By Example, From Geeks To Geeks.

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


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

36
37 package jnlp.sample.JreInstaller;
38
39 import java.io.*;
40 import java.net.*;
41 import java.util.*;
42 import java.util.jar.*;
43 import java.util.zip.*;
44
45
46 class EatInput implements Runnable JavaDoc {
47     InputStream _is;
48     String JavaDoc _name;
49     
50     EatInput(String JavaDoc name, InputStream is) {
51         _name = name;
52         _is = is;
53     }
54
55     public void run() {
56         Config.trace("EatInput " + _name + " started");
57         byte[] buffer = new byte[1024];
58         try {
59             while(true) {
60                 int n = _is.read(buffer);
61             }
62         } catch(IOException ioe) {
63             Config.trace("EatInput " + _name + ": " + ioe);
64         }
65     }
66 }
67
68 /**
69  * Some installs require user intervention with the installer for it to
70  * complete. This class allows you to define the strings that are to be
71  * expected from the output of the process. When a matching string has been
72  * found, a string can then be passed back to the process.
73  * <p>The strings to wait for and respond with are defined in the properties
74  * file.
75  * <code>install.waitStringx</code> gives the wait string for the x'th
76  * pass, with <code>install.responseStringx</code> giveing the wait string
77  * for the x'th iteration (starting at 0).
78  *
79  * @version 1.13 03/04/03
80  */

81 public class SolarisInstaller {
82     
83     /**
84      * Overriden to interact with the process. After starting the
85      * process the string <code>Config.getWaitString</code> is waited for, and
86      * if <code>Config.getResponseString</code> is non null, it is passed to
87      * the process. This is repeated until <code>Config.getWaitString</code> returns
88      * null, at which point the process is waited for to complete. If
89      * successful true is returned, otherwise false is returned.
90      */

91     public static boolean execute(String JavaDoc execString) {
92         Config.trace("Executing: " + execString);
93         
94         Process JavaDoc p;
95         try {
96             p = Runtime.getRuntime().exec(execString);
97         }
98         catch (IOException ioe) {
99             return false;
100         }
101         
102         final InputStream processIS = p.getInputStream();
103         final InputStream processES = p.getErrorStream();
104         final OutputStream processOS = p.getOutputStream();
105         
106         int counter = 0;
107         String JavaDoc waitString;
108         boolean failed = false;
109         try {
110             while ((waitString = Config.getWaitString(counter)) != null) {
111                 String JavaDoc responseString = Config.getResponseString(counter);
112                 responseString += '\n';
113                 Config.trace("Waiting for: " + waitString +
114                                  " response: " + responseString);
115                 if (waitFor(waitString, processIS)) {
116                     if (responseString != null) {
117                         processOS.write(responseString.getBytes());
118                         processOS.flush();
119                     }
120                 }
121                 counter++;
122             }
123                            
124             EatInput isEatInput = new EatInput("is", processIS);
125             EatInput esEatInput = new EatInput("es", processIS);
126             new Thread JavaDoc(isEatInput).start();
127             new Thread JavaDoc(esEatInput).start();
128
129             
130             // Wait for the process to finish
131
Config.trace("Wating for process to finish");
132             try {
133                 p.waitFor();
134             } catch (InterruptedException JavaDoc ie) {
135                 Config.trace("Got interrupt exception: " + ie);
136                 return false;
137             }
138             
139             // cleanup
140
try {
141                 processOS.close();
142             }
143             catch (IOException ioe) {}
144             
145             Config.trace("Process ended");
146             
147             try {
148                 processIS.close();
149             }
150             catch (IOException ioe) {}
151             
152             
153             return true;
154         } catch (IOException ioe) {
155             Config.trace("Got IO exception: " + ioe);
156             return false;
157         }
158     }
159     
160     /**
161      * Reads from the InputStream <code>is</code>, waiting for the String
162      * matching <code>string</code> to be read. This assumes that
163      * <code>string</code> will be encountered once, and that after
164      * <code>string</code> has been read no more bytes will be able to
165      * be read.
166      */

167     static private boolean waitFor(String JavaDoc string, InputStream is)
168         throws IOException {
169         int length = string.length();
170         byte[] buff = new byte[length];
171         int read;
172         String JavaDoc content = "";
173         
174         // PENDING: we may want to not use read, but instead first use
175
// available and then read to avoid blocking if the correct response
176
// is never seen.
177
while ((read = is.read(buff)) > 0) {
178             String JavaDoc readStr = new String JavaDoc(buff, 0, 0, read);
179             int cl = content.length();
180             if (cl < length * 4) {
181                 content += readStr;
182             } else {
183                 content = content.substring(read) + readStr;
184             }
185             if (content.indexOf(string) != -1) {
186                 Config.trace("match");
187                 return true;
188             }
189         }
190         
191         Config.trace("failed to find match");
192         // failed
193
return false;
194     }
195     
196     
197     /** Creates a temp. shell script used to launch UNIX installer */
198     static File createTempShellScript() throws IOException {
199         String JavaDoc script =
200             "#!/bin/sh\n" +
201             "# This script is executed by the java Installer with the directory\n" +
202             "# to install to as well as the real install script to execute.\n" +
203             "# This script is used instead of /bin/sh cd $0; /bin/sh $1 as I couldn't\n" +
204             "# get it to work inside java.\n" +
205             "cd $1\n" +
206             "/bin/sh $2\n";
207         
208         File result = File.createTempFile("jre", "sh");
209         result.deleteOnExit();
210         PrintStream out = new PrintStream(new FileOutputStream(result));
211         out.println(script);
212         out.close();
213         return result;
214     }
215 }
216
217
218
219
Popular Tags