1 11 package org.eclipse.help.internal.standalone; 12 13 import java.io.*; 14 import java.net.*; 15 import java.nio.channels.*; 16 17 21 public class EclipseController implements EclipseLifeCycleListener { 22 public static final String CMD_INSTALL = "install"; 24 public static final String CMD_UPDATE = "update"; 26 public static final String CMD_ENABLE = "enable"; 28 public static final String CMD_DISABLE = "disable"; 30 public static final String CMD_UNINSTALL = "uninstall"; 32 public static final String CMD_SEARCH = "search"; 34 public static final String CMD_LIST = "listFeatures"; 36 public static final String CMD_ADDSITE = "addSite"; 38 public static final String CMD_REMOVESITE = "removeSite"; 40 public static final String CMD_APPLY = "apply"; 42 private static final String CONTROL_SERVLET_PATH = "/help/control"; 45 protected String applicationId; 47 48 protected EclipseConnection connection; 50 51 public Eclipse eclipse = null; 52 53 private FileLock lock; 55 56 private boolean eclipseEnded = false; 57 58 70 public EclipseController(String applicationId, String [] args) { 71 72 this.applicationId = applicationId; 73 Options.init(applicationId, args); 74 connection = new EclipseConnection(); 75 } 76 77 80 public final synchronized void shutdown() throws Exception { 81 try { 82 obtainLock(); 83 sendHelpCommandInternal("shutdown", new String [0]); } catch (MalformedURLException mue) { 85 mue.printStackTrace(); 86 } catch (InterruptedException ie) { 87 } finally { 88 releaseLock(); 89 } 90 } 91 92 95 public final synchronized void start() throws Exception { 96 try { 97 obtainLock(); 98 startEclipse(); 99 } finally { 100 releaseLock(); 101 } 102 103 } 104 105 110 protected final synchronized void sendHelpCommand(String command, 111 String [] parameters) throws Exception { 112 try { 113 obtainLock(); 114 sendHelpCommandInternal(command, parameters); 115 } finally { 116 releaseLock(); 117 } 118 119 } 120 121 124 private void startEclipse() throws Exception { 125 boolean fullyRunning = isApplicationRunning(); 126 if (fullyRunning) { 127 return; 128 } 129 if (Options.isDebug()) { 130 System.out 131 .println("Using workspace " + Options.getWorkspace().getAbsolutePath()); } 133 Options.getConnectionFile().delete(); 135 connection.reset(); 136 137 if (Options.isDebug()) { 138 System.out 139 .println("Ensured old .connection file is deleted. Launching Eclipse."); } 141 eclipseEnded = false; 142 eclipse = new Eclipse(this); 143 eclipse.start(); 144 fullyRunning = isApplicationRunning(); 145 while (!eclipseEnded && !fullyRunning) { 146 try { 147 Thread.sleep(250); 148 } catch (InterruptedException ie) { 149 } 150 fullyRunning = isApplicationRunning(); 151 } 152 if (eclipseEnded) { 153 if (eclipse.getStatus() == Eclipse.STATUS_ERROR) { 154 throw eclipse.getException(); 155 } 156 return; 157 } 158 if (Options.isDebug()) { 159 System.out.println("Eclipse launched"); } 161 Runtime.getRuntime().addShutdownHook(new EclipseCleaner()); 163 } 164 165 private void sendHelpCommandInternal(String command, String [] parameters) 166 throws Exception { 167 if (!"shutdown".equalsIgnoreCase(command)) { startEclipse(); 169 } 170 if (!isApplicationRunning()) { 171 return; 172 } 173 if (!connection.isValid()) { 174 connection.renew(); 175 } 176 try { 177 String trustStoreLocation = Options.getTrustStoreLocation(); 178 if (trustStoreLocation != null) { 179 System.setProperty("javax.net.ssl.trustStore", trustStoreLocation); } 181 String trustStorePassword = Options.getTrustStorePassword(); 182 if (trustStorePassword != null) { 183 System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword); } 185 URL url = createCommandURL(command, parameters); 186 if ("shutdown".equalsIgnoreCase(command) && Options.getConnectionFile().exists()) { 188 connection.connect(url); 189 long timeLimit = System.currentTimeMillis() + 60 * 1000; 190 while (Options.getConnectionFile().exists()) { 191 Thread.sleep(200); 192 if (System.currentTimeMillis() > timeLimit) { 193 System.out 194 .println("Shutting down is taking too long. Will not wait."); break; 196 } 197 } 198 199 } else { 200 connection.connect(url); 201 } 202 } catch (MalformedURLException mue) { 203 mue.printStackTrace(); 204 } catch (InterruptedException ie) { 205 } 206 } 207 208 218 private URL createCommandURL(String command, String [] parameters) 219 throws MalformedURLException { 220 StringBuffer urlStr = new StringBuffer (); 221 urlStr.append("http://"); urlStr.append(connection.getHost()); 223 urlStr.append(":"); urlStr.append(connection.getPort()); 225 urlStr.append(CONTROL_SERVLET_PATH); 226 urlStr.append("?command="); urlStr.append(command); 228 for (int i = 0; i < parameters.length; i++) { 229 urlStr.append("&"); urlStr.append(parameters[i]); 231 } 232 if (Options.isDebug()) { 233 System.out.println("Control servlet URL=" + urlStr.toString()); } 235 return new URL(urlStr.toString()); 236 } 237 238 public void eclipseEnded() { 239 eclipseEnded = true; 240 connection.reset(); 241 } 242 243 private void obtainLock() throws IOException { 244 if (lock != null) { 245 return; 247 } 248 if (!Options.getLockFile().exists()) { 249 Options.getLockFile().getParentFile().mkdirs(); 250 } 251 RandomAccessFile raf = new RandomAccessFile(Options.getLockFile(), "rw"); lock = raf.getChannel().lock(); 253 if (Options.isDebug()) { 254 System.out.println("Lock obtained."); } 256 } 257 258 private void releaseLock() { 259 if (lock != null) { 260 try { 261 lock.channel().close(); 262 if (Options.isDebug()) { 263 System.out.println("Lock released."); } 265 lock = null; 266 } catch (IOException ioe) { 267 } 268 } 269 } 270 271 275 private boolean isApplicationRunning() { 276 File applicationLockFile = new File(Options.getLockFile() 277 .getParentFile(), ".applicationlock"); RandomAccessFile randomAccessFile = null; 279 FileLock applicationLock = null; 280 try { 281 randomAccessFile = new RandomAccessFile(applicationLockFile, "rw"); applicationLock = randomAccessFile.getChannel().tryLock(); 283 } catch (IOException ioe) { 284 } finally { 285 if (applicationLock != null) { 286 try { 287 applicationLock.release(); 288 } catch (IOException ioe) { 289 } 290 } 291 if (randomAccessFile != null) { 292 try { 293 randomAccessFile.close(); 294 } catch (IOException ioe) { 295 } 296 } 297 if (Options.isDebug()) { 298 System.out 299 .println("isApplicationRunning? " + (applicationLock == null)); } 301 } 302 return applicationLock == null; 303 } 304 305 public class EclipseCleaner extends Thread { 306 public void run() { 307 if (eclipse != null) { 308 eclipse.killProcess(); 309 } 310 } 311 } 312 313 316 protected boolean executeUpdateCommand(String updateCommand) 317 throws Exception { 318 String [] parameters = Options.getUpdateParameters(); 319 sendHelpCommandInternal(updateCommand, parameters); 320 return true; 321 } 322 } 323 | Popular Tags |