1 11 package org.eclipse.help.internal.standalone; 12 13 import java.io.*; 14 import java.util.List ; 15 16 20 public class Eclipse extends Thread { 21 private static final int NEEDS_RESTART = 23; 23 public static final int STATUS_INIT = 0; 25 public static final int STATUS_STARTED = 1; 26 public static final int STATUS_ERROR = 2; 27 28 File dir; 29 String [] cmdarray; 30 private int status = STATUS_INIT; 31 private Exception exception; 32 Process pr; 33 private EclipseLifeCycleListener lifeCycleListener; 34 37 public Eclipse(EclipseLifeCycleListener listener) { 38 super(); 39 this.lifeCycleListener = listener; 40 this.setName("Eclipse"); this.dir = Options.getEclipseHome(); 42 } 43 private void prepareCommand() throws Exception { 44 if (Options.useExe()) { 45 prepareEclipseCommand(); 46 ensureEclipseExeExists(); 47 } else { 48 prepareJavaCommand(); 49 } 50 ensureVmExists(); 51 } 52 private void prepareEclipseCommand() { 53 List vmArgs = Options.getVmArgs(); 54 List eclipseArgs = Options.getEclipseArgs(); 55 cmdarray = new String [3 + vmArgs.size() + 1 + eclipseArgs.size()]; 56 cmdarray[0] = new File(Options.getEclipseHome(), "eclipse").getAbsolutePath(); cmdarray[1] = "-vm"; cmdarray[2] = Options.getVm(); 59 for (int i = 0; i < eclipseArgs.size(); i++) { 60 cmdarray[3 + i] = (String ) eclipseArgs.get(i); 61 } 62 cmdarray[3 + eclipseArgs.size()] = "-vmargs"; for (int i = 0; i < vmArgs.size(); i++) { 64 cmdarray[4 + eclipseArgs.size() + i] = (String ) vmArgs.get(i); 65 } 66 } 67 private void prepareJavaCommand() throws Exception { 68 List vmArgs = Options.getVmArgs(); 69 List eclipseArgs = Options.getEclipseArgs(); 70 cmdarray = new String [1 + vmArgs.size() + 3 + eclipseArgs.size()]; 71 cmdarray[0] = Options.getVm(); 72 for (int i = 0; i < vmArgs.size(); i++) { 73 cmdarray[1 + i] = (String ) vmArgs.get(i); 74 } 75 cmdarray[1 + vmArgs.size()] = "-cp"; cmdarray[2 + vmArgs.size()] = getStartupJar(); 77 cmdarray[3 + vmArgs.size()] = "org.eclipse.core.launcher.Main"; for (int i = 0; i < eclipseArgs.size(); i++) { 79 cmdarray[4 + vmArgs.size() + i] = (String ) eclipseArgs.get(i); 80 } 81 } 82 85 public void run() { 86 try { 87 prepareCommand(); 88 if (Options.isDebug()) { 89 printCommand(); 90 } 91 do { 92 pr = Runtime.getRuntime().exec(cmdarray, (String []) null, dir); 93 (new StreamConsumer(pr.getInputStream())).start(); 94 (new StreamConsumer(pr.getErrorStream())).start(); 95 if (status == STATUS_INIT) { 96 status = STATUS_STARTED; 98 } 99 try { 100 pr.waitFor(); 101 } catch (InterruptedException e) { 102 } 103 if (Options.isDebug()) { 104 System.out 105 .println("Eclipse exited with status code " + pr.exitValue()); if (pr.exitValue() == NEEDS_RESTART) { 107 System.out 108 .println("Updates are installed, Eclipse will be restarted."); } 110 } 111 } while (pr.exitValue() == NEEDS_RESTART); 112 } catch (Exception exc) { 113 exception = exc; 114 status = STATUS_ERROR; 115 } finally { 116 if (status == STATUS_INIT) { 117 status = STATUS_ERROR; 118 } 119 if (status == STATUS_ERROR && exception == null) { 120 exception = new Exception ("Unknown exception."); } 122 lifeCycleListener.eclipseEnded(); 123 } 124 } 125 128 public class StreamConsumer extends Thread { 129 BufferedReader bReader; 130 public StreamConsumer(InputStream inputStream) { 131 super(); 132 this.setName("Eclipse out/err consumer"); this.setDaemon(true); 134 bReader = new BufferedReader(new InputStreamReader(inputStream)); 135 } 136 public void run() { 137 try { 138 String line; 139 while (null != (line = bReader.readLine())) { 140 System.out.println(line); 141 } 142 bReader.close(); 143 } catch (IOException ioe) { 144 ioe.printStackTrace(); 145 } 146 } 147 } 148 private void ensureVmExists() throws Exception { 149 File vmExe = new File(Options.getVm()); 150 if (vmExe.exists() && !vmExe.isDirectory()) { 151 return; 152 } 153 vmExe = new File(Options.getVm() + ".exe"); if (vmExe.exists() && !vmExe.isDirectory()) { 155 return; 156 } 157 throw new Exception ("File " + vmExe.getAbsolutePath() + " does not exists. Pass a correct -vm option"); } 160 private void ensureEclipseExeExists() throws Exception { 161 File eclipseExe = new File(Options.getEclipseHome(), "eclipse" + (System.getProperty("os.name").startsWith("Win") ? ".exe" : "")); if (eclipseExe.exists() && !eclipseExe.isDirectory()) { 166 return; 167 } 168 throw new Exception ("File " + eclipseExe.getAbsolutePath() + " does not exists. Pass a correct -eclipsehome option"); } 171 private String getStartupJar() throws Exception { 172 File pluginsDir = new File(Options.getEclipseHome(), "plugins/"); if (!pluginsDir.exists() || !pluginsDir.isDirectory()) 174 throw new Exception ("Plugins directory " + pluginsDir.getAbsolutePath() + " does not exists. Pass a correct -eclipsehome option"); File[] plugins = pluginsDir.listFiles(); 177 for (int i = 0; i < plugins.length; i++) { 178 String file = plugins[i].getName(); 179 if (file.startsWith("org.eclipse.equinox.launcher") && file.endsWith(".jar") && !plugins[i].isDirectory()) return "plugins/" + file; } 182 throw new Exception ("Plugins directory " + pluginsDir.getAbsolutePath() + " does not contain a valid startup jar. Pass a correct -eclipsehome option"); } 185 188 public Exception getException() { 189 return exception; 190 } 191 192 195 public int getStatus() { 196 return status; 197 } 198 private void printCommand() { 199 System.out.println("Launch command is:"); for (int i = 0; i < cmdarray.length; i++) { 201 System.out.println(" " + cmdarray[i]); } 203 204 } 205 208 public void killProcess() { 209 if (pr != null) { 210 pr.destroy(); 211 } 212 } 213 } 214 | Popular Tags |