/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.actions;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.preferences.PreferenceConstants;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatServerStateListener;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
public class StartWGARuntime implements IWorkbenchWindowActionDelegate {
private static class WaitTask implements IRunnableWithProgress, TomcatServerStateListener {
private boolean _startupFinished = false;;
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
monitor.beginTask("Waiting for OpenWGA startup ...", IProgressMonitor.UNKNOWN);
TomcatUtils.getInstance().addListener(this);
long startTime = System.currentTimeMillis();
while (!_startupFinished && !monitor.isCanceled()) {
Thread.sleep(500);
}
} finally {
monitor.done();
TomcatUtils.getInstance().removeListener(this);
}
}
public void stateChanged(int state) {
if (state == TomcatServerStateListener.STATE_RUNNING) {
_startupFinished = true;
}
}
}
private WGARuntime _wgaRuntime;
public void dispose() {
}
public void init(IWorkbenchWindow window) {
}
public void run(IAction action) {
call(_wgaRuntime);
}
public static void call(WGARuntime runtime) {
call(runtime, false);
}
public static void call(WGARuntime runtime, boolean joinAndWaitForStartup) {
boolean force = false;
WGARuntime current = TomcatUtils.getInstance().getCurrentRuntime();
if (TomcatUtils.getInstance().isRunning() && current != null && !current.getProject().getName().equals(runtime.getProject().getName())) {
force = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "Confirm WGA runtime change", "Another WGA runtime '" + current.getProject().getName()
+ "' is started. Are you sure you want to terminate this instance and start '" + runtime.getProject().getName() + "' instead ?");
}
if (!TomcatUtils.getInstance().isRunning() || force) {
// check preconditions
if (TomcatUtils.getInstance().isSocketInUse()) {
int port = WGADesignerPlugin.getDefault().getPreferenceStore().getInt(PreferenceConstants.TOMCAT_SERVER_PORT);
MessageDialog.openWarning(Display.getCurrent().getActiveShell(), "Unable to start WGA runtime",
"Unable to start WGA runtime. The socket '127.0.0.1:" + port + "' is used by another process. Please terminate this process first.");
return;
}
final WGARuntime temp = runtime;
Job job = new Job("WGA Runtime start") {
@Override
protected IStatus run(IProgressMonitor monitor) {
try {
monitor.beginTask("Starting WGA runtime '" + temp.getName() + "'.", 2);
TomcatUtils.getInstance().stop(new SubProgressMonitor(monitor, 1));
TomcatUtils.getInstance().start(temp, new SubProgressMonitor(monitor, 1));
return Status.OK_STATUS;
} catch (Exception e) {
return new Status(Status.ERROR, WGADesignerPlugin.PLUGIN_ID, "Unable to start WGA runtime '" + temp.getName() + "'.", e);
} finally {
monitor.done();
}
}
};
job.setPriority(Job.SHORT);
job.schedule();
}
if (joinAndWaitForStartup) {
ProgressMonitorDialog dialog = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
try {
dialog.run(true, true, new WaitTask());
}
catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void selectionChanged(IAction action, ISelection selection) {
_wgaRuntime = WGARuntime.fromSelection(selection);
WGARuntime current = TomcatUtils.getInstance().getCurrentRuntime();
boolean instanceRunning = TomcatUtils.getInstance().isRunning();
if (_wgaRuntime != null) {
if (instanceRunning && current != null && _wgaRuntime.getProject().getName().equals(current.getProject().getName())) {
action.setEnabled(false);
} else {
action.setEnabled(true);
}
} else {
action.setEnabled(false);
}
}
}