Source:
public static Job startOrRestartServer(final IServer server, final int timeout, final TimeUnit unit) throws TimeoutException, InterruptedException, ExecutionException { if (server == null) { return null; } final boolean needsRestart = (server.getServerState() == IServer.STATE_STARTED); return new Job((needsRestart ? "Restarting " : "Starting ") + server.getName() + "...") { @Override protected IStatus run(IProgressMonitor monitor) { try { final Long limitTime = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(timeout, unit); if (needsRestart) { server.stop(true); while (server.getServerState() != IServer.STATE_STOPPED && System.currentTimeMillis() < limitTime) { TimeUnit.SECONDS.sleep(1); } } server.start(ILaunchManager.RUN_MODE, new NullProgressMonitor()); while (server.getServerState() != IServer.STATE_STARTED && System.currentTimeMillis() < limitTime) { TimeUnit.SECONDS.sleep(1); } if (server.getServerState() != IServer.STATE_STARTED) { if (needsRestart) { Logger.error("Failed to restart " + server.getName() + " before timeout"); } else { Logger.error("Failed to start " + server.getName() + " before timeout"); } } return Status.OK_STATUS; } catch (Exception e) { if (needsRestart) { Logger.error("Failed to restart " + server.getName() + " before timeout", e); } else { Logger.error("Failed to start " + server.getName() + " before timeout", e); } return Status.CANCEL_STATUS; } } }; }

Target:
public static Job startOrRestartServer(final IServer server, final int timeout, final TimeUnit unit) throws TimeoutException, InterruptedException, ExecutionException { if (server == null) { return null; } final boolean isStopping = (server.getServerState() == IServer.STATE_STOPPING); final boolean needsStop = (server.getServerState() == IServer.STATE_STARTING || server.getServerState() == IServer.STATE_STARTED ); final boolean needsRestart = isStopping || needsStop; return new Job((needsRestart ? "Restarting " : "Starting ") + server.getName() + "...") { @Override protected IStatus run(IProgressMonitor monitor) { try { final Long limitTime = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(timeout, unit); if (needsStop) { server.stop(true); } if(needsStop || isStopping) { while (server.getServerState() != IServer.STATE_STOPPED && System.currentTimeMillis() < limitTime && !monitor.isCanceled()) { TimeUnit.SECONDS.sleep(1); } } if(monitor.isCanceled()) { return Status.CANCEL_STATUS; } final int ticks = (int)TimeUnit.SECONDS.convert(timeout, unit); final SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, ticks); server.start(ILaunchManager.RUN_MODE, subMonitor); while (server.getServerState() != IServer.STATE_STARTED && System.currentTimeMillis() < limitTime && !subMonitor.isCanceled() && !monitor.isCanceled()) { TimeUnit.SECONDS.sleep(1); subMonitor.worked(1); } subMonitor.done(); if (server.getServerState() != IServer.STATE_STARTED) { if (needsRestart) { Logger.error("Failed to restart " + server.getName() + " before timeout"); } else { Logger.error("Failed to start " + server.getName() + " before timeout"); } } return Status.OK_STATUS; } catch (Exception e) { if (needsRestart) { Logger.error("Failed to restart " + server.getName() + " before timeout", e); } else { Logger.error("Failed to start " + server.getName() + " before timeout", e); } return Status.CANCEL_STATUS; } } }; }