1 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 { 47 InputStream _is; 48 String _name; 49 50 EatInput(String 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 81 public class SolarisInstaller { 82 83 91 public static boolean execute(String execString) { 92 Config.trace("Executing: " + execString); 93 94 Process 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 waitString; 108 boolean failed = false; 109 try { 110 while ((waitString = Config.getWaitString(counter)) != null) { 111 String 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 (isEatInput).start(); 127 new Thread (esEatInput).start(); 128 129 130 Config.trace("Wating for process to finish"); 132 try { 133 p.waitFor(); 134 } catch (InterruptedException ie) { 135 Config.trace("Got interrupt exception: " + ie); 136 return false; 137 } 138 139 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 167 static private boolean waitFor(String string, InputStream is) 168 throws IOException { 169 int length = string.length(); 170 byte[] buff = new byte[length]; 171 int read; 172 String content = ""; 173 174 while ((read = is.read(buff)) > 0) { 178 String readStr = new String (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 return false; 194 } 195 196 197 198 static File createTempShellScript() throws IOException { 199 String 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 |