[jbosstools-commits] JBoss Tools SVN: r41391 - in trunk: openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/job and 1 other directories.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Thu May 24 19:12:33 EDT 2012


Author: adietish
Date: 2012-05-24 19:12:29 -0400 (Thu, 24 May 2012)
New Revision: 41391

Modified:
   trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java
   trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobUtils.java
   trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/job/CreateApplicationJob.java
   trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/OpenShiftExpressApplicationWizard.java
Log:
[JBIDE-11314] added timeout dialog for app creation

Modified: trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java	2012-05-24 21:25:18 UTC (rev 41390)
+++ trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobResultFuture.java	2012-05-24 23:12:29 UTC (rev 41391)
@@ -29,20 +29,31 @@
  */
 public class JobResultFuture implements Future<IStatus> {
 
-	private AtomicBoolean done = new AtomicBoolean();
-	private AtomicBoolean cancelled = new AtomicBoolean();
+	private AtomicBoolean done = new AtomicBoolean(false);
+	private AtomicBoolean cancelled = new AtomicBoolean(false);
 	private ArrayBlockingQueue<IStatus> queue = new ArrayBlockingQueue<IStatus>(1);
+	private Job job;
 
 	public JobResultFuture(Job job) {
+		this.job = job;
 		addJobFinishedListener(job);
 	}
 
 	@Override
 	public boolean cancel(boolean mayInterruptIfRunning) {
-		cancelled.set(true);
-		return true;
+		if ((isRunning(job)
+				&& mayInterruptIfRunning)
+				|| !isRunning(job)) {
+			cancelled.set(true);
+			job.cancel();
+		}
+		return isRunning(job);
 	}
 
+	private boolean isRunning(Job job) {
+		return job.getState() == Job.RUNNING;
+	}
+
 	@Override
 	public boolean isCancelled() {
 		return cancelled.get();
@@ -50,6 +61,9 @@
 
 	@Override
 	public boolean isDone() {
+		if (isCancelled()) {
+			return false;
+		}
 		return done.get();
 	}
 

Modified: trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobUtils.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobUtils.java	2012-05-24 21:25:18 UTC (rev 41390)
+++ trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/JobUtils.java	2012-05-24 23:12:29 UTC (rev 41391)
@@ -35,4 +35,10 @@
 		return status != null
 				&& status.isOK();
 	}
+
+	public static boolean isCancel(IStatus status) {
+		return status != null
+				&& status.getSeverity() == IStatus.CANCEL;
+	}
+
 }

Modified: trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java	2012-05-24 21:25:18 UTC (rev 41390)
+++ trunk/common/plugins/org.jboss.tools.common.ui/src/org/jboss/tools/common/ui/WizardUtils.java	2012-05-24 23:12:29 UTC (rev 41391)
@@ -12,7 +12,6 @@
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
 import org.eclipse.core.databinding.DataBindingContext;
@@ -25,6 +24,7 @@
 import org.eclipse.jface.wizard.IWizardContainer;
 import org.eclipse.jface.wizard.IWizardPage;
 import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.widgets.Shell;
 
 /**
@@ -32,6 +32,9 @@
  */
 public class WizardUtils {
 
+	private static final long THREAD_SLEEP = 1 * 1000;
+	private static final int DEFAULT_TIMEOUT = 120 * 1000;
+
 	private WizardUtils() {
 		// inhibit instantiation
 	}
@@ -80,7 +83,7 @@
 	 */
 	public static IStatus runInWizard(final Job job, final DelegatingProgressMonitor delegatingMonitor,
 			final IWizardContainer container) throws InvocationTargetException, InterruptedException {
-		return runInWizard(job, delegatingMonitor, container, 120);
+		return runInWizard(job, delegatingMonitor, container, DEFAULT_TIMEOUT);
 	}
 
 	/**
@@ -122,10 +125,11 @@
 				if (delegatingMonitor != null) {
 					delegatingMonitor.add(monitor);
 				}
+
 				monitor.beginTask(job.getName(), IProgressMonitor.UNKNOWN);
 				job.schedule();
 				try {
-					future.get(timeout, TimeUnit.SECONDS);
+					waitForFuture(timeout, future, monitor);
 				} catch (ExecutionException e) {
 				} catch (TimeoutException e) {
 				} finally {
@@ -133,12 +137,37 @@
 				}
 			}
 		});
+
+		return getStatus(job, future);
+	}
+
+	private static void waitForFuture(long timeout, JobResultFuture future, IProgressMonitor monitor)
+			throws InterruptedException, ExecutionException, TimeoutException {
+		long startTime = System.currentTimeMillis();
+		while (!future.isDone()
+				&& (System.currentTimeMillis() - startTime) < timeout) {
+			if (monitor.isCanceled()) {
+				future.cancel(true);
+				break;
+			}
+			Thread.sleep(THREAD_SLEEP);
+		}
+	}
+
+	private static IStatus getStatus(final Job job, final JobResultFuture future) {
+
+		if (future.isCancelled()) {
+			String message = NLS.bind("The operation ''{0}'' was cancelled", job.getName());
+			CommonUIPlugin.getDefault().logError(message);
+			return new Status(IStatus.CANCEL, CommonUIPlugin.PLUGIN_ID, message);
+		}
 		if (future.isDone()) {
 			return job.getResult();
 		}
-		CommonUIPlugin.getDefault().logError("Operation did not complete in a reasonnable amount of time");
-		return new Status(IStatus.ERROR, CommonUIPlugin.PLUGIN_ID,
-				"Operation did not complete in a reasonnable amount of time");
+		String message =
+				NLS.bind("The operation ''{0}'' did not complete in a reasonnable amount of time", job.getName());
+		CommonUIPlugin.getDefault().logError(message);
+		return new Status(IStatus.ERROR, CommonUIPlugin.PLUGIN_ID, message);
 	}
 
 	/**

Modified: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/job/CreateApplicationJob.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/job/CreateApplicationJob.java	2012-05-24 21:25:18 UTC (rev 41390)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/job/CreateApplicationJob.java	2012-05-24 23:12:29 UTC (rev 41391)
@@ -10,12 +10,17 @@
  ******************************************************************************/
 package org.jboss.tools.openshift.express.internal.ui.job;
 
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.ReentrantLock;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.osgi.util.NLS;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
 import org.jboss.tools.openshift.express.internal.core.console.UserDelegate;
 import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator;
 import org.jboss.tools.openshift.express.internal.ui.messages.OpenShiftExpressUIMessages;
@@ -23,7 +28,10 @@
 import com.openshift.client.ApplicationScale;
 import com.openshift.client.IApplication;
 import com.openshift.client.ICartridge;
+import com.openshift.client.IDomain;
 import com.openshift.client.IGearProfile;
+import com.openshift.client.OpenShiftException;
+import com.openshift.client.OpenShiftTimeoutException;
 
 /**
  * @author Andre Dietisheim
@@ -52,8 +60,17 @@
 	protected IStatus doRun(IProgressMonitor monitor) {
 		try {
 			lock.lock();
-			this.application = user.createApplication(name, cartridge, scale, gear);
-			return Status.OK_STATUS;
+			try {
+				this.application = user.createApplication(name, cartridge, scale, gear);
+			} catch (OpenShiftTimeoutException e) {
+				this.application = refreshAndCreateApplication(monitor);
+			}
+			if (application == null) {
+				return OpenShiftUIActivator.createCancelStatus(NLS.bind(
+						"User cancelled creation of application {0}", name));
+			} else {
+				return Status.OK_STATUS;
+			}
 		} catch (Exception e) {
 			return OpenShiftUIActivator.createErrorStatus(
 					OpenShiftExpressUIMessages.COULD_NOT_CREATE_APPLICATION, e, name);
@@ -62,6 +79,26 @@
 		}
 	}
 
+	private IApplication refreshAndCreateApplication(IProgressMonitor monitor) throws OpenShiftException {
+		IApplication application = null;
+		do {
+			try {
+				IDomain domain = user.getDefaultDomain();
+				domain.refresh();
+				application = domain.getApplicationByName(name);
+				if (application == null) {
+					// app is not created yet, try again
+					application = user.createApplication(name, cartridge, scale, gear);
+				}
+			} catch (OpenShiftTimeoutException ex) {
+				// ignore
+			}
+		} while (application == null
+				&& openKeepTryingDialog()
+				&& !monitor.isCanceled());
+		return application;
+	}
+
 	public IApplication getApplication() {
 		try {
 			lock.lock();
@@ -70,4 +107,30 @@
 			lock.unlock();
 		}
 	}
+
+	protected boolean openKeepTryingDialog() {
+		final AtomicBoolean keepTrying = new AtomicBoolean(false);
+		final Display display = Display.getDefault();
+		Display.getDefault().syncExec(new Runnable() {
+
+			@Override
+			public void run() {
+				MessageDialog dialog =
+						new MessageDialog(display.getActiveShell()
+								, NLS.bind("Creating {0}", name)
+								, display.getSystemImage(SWT.ICON_QUESTION)
+								, NLS.bind("Could not create application {0}. Connection timed out.\n\nKeep trying?",
+										name)
+								, MessageDialog.QUESTION
+								, new String[] { "Keep trying",
+										OpenShiftExpressUIMessages.BTN_CLOSE_WIZARD }
+								, MessageDialog.QUESTION);
+				// style &= SWT.SHEET;
+				// dialog.setShellStyle(dialog.getShellStyle() | style);
+				keepTrying.set(dialog.open() == IDialogConstants.OK_ID);
+			}
+		});
+		return keepTrying.get();
+	}
+
 }

Modified: trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/OpenShiftExpressApplicationWizard.java
===================================================================
--- trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/OpenShiftExpressApplicationWizard.java	2012-05-24 21:25:18 UTC (rev 41390)
+++ trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/OpenShiftExpressApplicationWizard.java	2012-05-24 23:12:29 UTC (rev 41391)
@@ -143,18 +143,26 @@
 	public boolean performFinish() {
 		boolean success = getWizardModel().isUseExistingApplication();
 		if (!success) {
-			if (createApplication()) {
-				if (success = waitForApplication(wizardModel.getApplication())) {
-					success = addRemoveCartridges(
-							getWizardModel().getApplication(), getWizardModel().getSelectedEmbeddableCartridges());
-				} else {
-					getContainer().getShell().close();
-				}
+
+			IStatus status = createApplication();
+			if (JobUtils.isCancel(status)) {
+				getContainer().getShell().close();
+			} else if (!JobUtils.isOk(status)) {
+				return false;
 			}
+
+			if (success = waitForApplication(wizardModel.getApplication())) {
+				success = addRemoveCartridges(
+						getWizardModel().getApplication(), getWizardModel().getSelectedEmbeddableCartridges());
+			} else {
+				getContainer().getShell().close();
+			}
 		}
+
 		if (success) {
 			success = importProject();
 		}
+
 		wizardModel.addUserToModel();
 		return success;
 	}
@@ -183,7 +191,7 @@
 		}
 	}
 
-	private boolean createApplication() {
+	private IStatus createApplication() {
 		try {
 			CreateApplicationJob job = new CreateApplicationJob(
 					wizardModel.getApplicationName()
@@ -194,9 +202,10 @@
 			IStatus status = WizardUtils.runInWizard(
 					job, job.getDelegatingProgressMonitor(), getContainer(), APP_CREATE_TIMEOUT);
 			wizardModel.setApplication(job.getApplication());
-			return status.isOK();
+			return status;
 		} catch (Exception e) {
-			return false;
+			return OpenShiftUIActivator.createErrorStatus(
+					NLS.bind("Could not create application {0}", wizardModel.getApplicationName()), e);
 		}
 	}
 



More information about the jbosstools-commits mailing list