Index: src/org/jboss/tools/openshift/express/internal/core/portforward/ApplicationPortForwardingWizardPage.java =================================================================== --- src/org/jboss/tools/openshift/express/internal/core/portforward/ApplicationPortForwardingWizardPage.java (revision 41507) +++ src/org/jboss/tools/openshift/express/internal/core/portforward/ApplicationPortForwardingWizardPage.java (working copy) @@ -331,7 +331,7 @@ @Override protected void onPageActivated(DataBindingContext dbc) { - final Job j = new Job("Retrieving application's forwardable ports...") { + final Job j = new Job("Loading application's forwardable ports...") { @Override protected IStatus run(IProgressMonitor monitor) { try { @@ -350,18 +350,11 @@ } }; - getContainer().getShell().getDisplay().asyncExec(new Runnable() { - public void run() { - try { - IStatus status = WizardUtils.runInWizard(j, getContainer(), getDataBindingContext()); - if(!status.isOK()) { - getWizard().getContainer().getShell().close(); - } - } catch(Exception e) { - // ignore - } - } - }); + try { + WizardUtils.runInWizard(j, getContainer(), getDataBindingContext()); + } catch (Exception e) { + Logger.error("Failed to load application's forwardable ports", e); + } } private void refreshViewerInput() { Index: src/org/jboss/tools/openshift/express/internal/ui/action/AbstractSSHAction.java =================================================================== --- src/org/jboss/tools/openshift/express/internal/ui/action/AbstractSSHAction.java (revision 41507) +++ src/org/jboss/tools/openshift/express/internal/ui/action/AbstractSSHAction.java (working copy) @@ -1,43 +0,0 @@ -package org.jboss.tools.openshift.express.internal.ui.action; - -import org.eclipse.jface.resource.ImageDescriptor; -import org.jboss.tools.openshift.express.internal.ui.utils.Logger; -import org.jboss.tools.openshift.express.internal.ui.utils.OpenShiftSshSessionFactory; - -import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.Session; -import com.openshift.client.IApplication; -import com.openshift.client.OpenShiftSSHOperationException; - -public abstract class AbstractSSHAction extends AbstractAction { - - public AbstractSSHAction(String text) { - super(text); - } - - public AbstractSSHAction(String text, boolean enableForSingleElement) { - super(text, enableForSingleElement); - } - - public AbstractSSHAction(String text, ImageDescriptor image) { - super(text, image); - } - - /** - * @param monitor - * @throws OpenShiftSSHOperationException - * @throws JSchException - */ - protected boolean verifyApplicationSSHSession(final IApplication application) throws OpenShiftSSHOperationException { - final boolean hasAlreadySSHSession = application.hasSSHSession(); - if (!hasAlreadySSHSession) { - Logger.debug("Opening a new SSH Session for application '" + application.getName() + "'"); - final Session session = OpenShiftSshSessionFactory.getInstance().createSession( - application); - application.setSSHSession(session); - } - // now, check if the session is valid (ie, not null and still connected) - return application.hasSSHSession(); - } - -} \ No newline at end of file Index: src/org/jboss/tools/openshift/express/internal/ui/action/ApplicationPortForwardingAction.java =================================================================== --- src/org/jboss/tools/openshift/express/internal/ui/action/ApplicationPortForwardingAction.java (revision 41507) +++ src/org/jboss/tools/openshift/express/internal/ui/action/ApplicationPortForwardingAction.java (working copy) @@ -1,25 +1,21 @@ package org.jboss.tools.openshift.express.internal.ui.action; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.core.runtime.jobs.IJobChangeEvent; +import org.eclipse.core.runtime.jobs.JobChangeAdapter; import org.eclipse.debug.ui.DebugUITools; import org.eclipse.debug.ui.IDebugUIConstants; import org.eclipse.jface.viewers.ITreeSelection; import org.eclipse.jface.wizard.WizardDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.wst.server.core.IServer; -import org.jboss.tools.openshift.express.internal.core.behaviour.ExpressServerUtils; import org.jboss.tools.openshift.express.internal.core.portforward.ApplicationPortForwardingWizard; import org.jboss.tools.openshift.express.internal.core.portforward.ApplicationPortForwardingWizardDialog; -import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator; -import org.jboss.tools.openshift.express.internal.ui.utils.Logger; +import org.jboss.tools.openshift.express.internal.ui.job.RetrieveApplicationJob; +import org.jboss.tools.openshift.express.internal.ui.job.VerifySSHSessionJob; import com.openshift.client.IApplication; -import com.openshift.client.OpenShiftSSHOperationException; -public class ApplicationPortForwardingAction extends AbstractSSHAction { +public class ApplicationPortForwardingAction extends AbstractAction { public ApplicationPortForwardingAction() { super("Port forwarding...", DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_LCL_DISCONNECT)); @@ -35,9 +31,9 @@ if (selection != null && selection instanceof ITreeSelection) { Object sel = ((ITreeSelection) selection).getFirstElement(); if (sel instanceof IApplication) { - openPortForwardingDialog((IApplication) sel); + openPortForwardingDialogFor((IApplication) sel); } else if (sel instanceof IServer) { - openPortForwardingDialog((IServer) sel); + openPortForwardingDialogFor((IServer) sel); } } } @@ -49,62 +45,54 @@ * * @param server */ - private void openPortForwardingDialog(final IServer server) { - Job job = new Job("Identifying OpenShift Application from selected Server...") { + private void openPortForwardingDialogFor(final IServer server) { + final RetrieveApplicationJob job = new RetrieveApplicationJob(server); + job.addJobChangeListener(new JobChangeAdapter() { @Override - protected IStatus run(IProgressMonitor monitor) { - final IApplication application = ExpressServerUtils.getApplication(server); - Display.getDefault().asyncExec(new Runnable() { - public void run() { - openPortForwardingDialog(application); - } - }); - return Status.OK_STATUS; + public void done(IJobChangeEvent event) { + if (event.getResult().isOK()) { + final IApplication application = job.getApplication(); + openPortForwardingDialogFor(application); + } } - }; + }); job.setUser(true); job.schedule(); } - private void openPortForwardingDialog(final IApplication application) { - Job job = new Job("Retrieving application's forwardable ports...") { + private void openPortForwardingDialogFor(final IApplication application) { + final VerifySSHSessionJob job = new VerifySSHSessionJob(application); + job.addJobChangeListener(new JobChangeAdapter() { @Override - protected IStatus run(IProgressMonitor monitor) { - try { - verifyApplicationSSHSession(application); - Display.getDefault().asyncExec(new Runnable() { - public void run() { - runAsync(application); - } - }); - return Status.OK_STATUS; - } catch (OpenShiftSSHOperationException e) { - return OpenShiftUIActivator.createErrorStatus(e.getMessage(), e.getCause()); + public void done(IJobChangeEvent event) { + if (event.getResult().isOK() && job.isValidSession()) { + openWizardDialog(application); } } - }; + }); + job.setUser(true); job.schedule(); } /** + * Opens the Port Forwarding dialog for good... + * * @param application */ - private void runAsync(final IApplication application) { - try { - ApplicationPortForwardingWizard wizard = new ApplicationPortForwardingWizard( - application); - WizardDialog dialog = new ApplicationPortForwardingWizardDialog(Display.getCurrent() - .getActiveShell(), - wizard); - dialog.setMinimumPageSize(700, 300); - dialog.create(); - dialog.open(); - } catch (Exception e) { - Logger.error( - "Failed to perform 'port-forwarding' for application '" + application.getName() - + "'", e); - } + private void openWizardDialog(final IApplication application) { + Display.getDefault().syncExec(new Runnable() { + @Override + public void run() { + ApplicationPortForwardingWizard wizard = new ApplicationPortForwardingWizard( + application); + WizardDialog dialog = new ApplicationPortForwardingWizardDialog(Display.getCurrent() + .getActiveShell(), + wizard); + dialog.setMinimumPageSize(700, 300); + dialog.create(); + dialog.open(); + } + }); } - } Index: src/org/jboss/tools/openshift/express/internal/ui/action/ShowEnvironmentAction.java =================================================================== --- src/org/jboss/tools/openshift/express/internal/ui/action/ShowEnvironmentAction.java (revision 41507) +++ src/org/jboss/tools/openshift/express/internal/ui/action/ShowEnvironmentAction.java (working copy) @@ -12,28 +12,26 @@ import java.util.List; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.core.runtime.jobs.IJobChangeEvent; +import org.eclipse.core.runtime.jobs.JobChangeAdapter; import org.eclipse.jface.viewers.ITreeSelection; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.console.MessageConsole; import org.eclipse.ui.console.MessageConsoleStream; import org.eclipse.wst.server.core.IServer; -import org.jboss.tools.openshift.express.internal.core.behaviour.ExpressServerUtils; import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator; import org.jboss.tools.openshift.express.internal.ui.console.ConsoleUtils; +import org.jboss.tools.openshift.express.internal.ui.job.RetrieveApplicationJob; +import org.jboss.tools.openshift.express.internal.ui.job.VerifySSHSessionJob; import org.jboss.tools.openshift.express.internal.ui.messages.OpenShiftExpressUIMessages; import org.jboss.tools.openshift.express.internal.ui.utils.OpenShiftSshSessionFactory; import com.openshift.client.IApplication; -import com.openshift.client.OpenShiftSSHOperationException; /** * @author Xavier Coulon */ -public class ShowEnvironmentAction extends AbstractSSHAction { +public class ShowEnvironmentAction extends AbstractAction { public ShowEnvironmentAction() { super(OpenShiftExpressUIMessages.SHOW_ENVIRONMENT_ACTION, true); @@ -49,10 +47,10 @@ final ITreeSelection treeSelection = (ITreeSelection) selection; if (selection instanceof ITreeSelection && treeSelection.getFirstElement() instanceof IApplication) { final IApplication application = (IApplication) treeSelection.getFirstElement(); - showEnvironmentProperties(application); + showEnvironmentPropertiesFor(application); } else if (selection instanceof ITreeSelection && treeSelection.getFirstElement() instanceof IServer) { final IServer server = (IServer) treeSelection.getFirstElement(); - showEnvironmentProperties(server); + showEnvironmentPropertiesFor(server); } } @@ -64,40 +62,37 @@ * * @param server */ - private void showEnvironmentProperties(final IServer server) { - Job job = new Job("Identifying OpenShift Application from selected Server...") { + private void showEnvironmentPropertiesFor(final IServer server) { + final RetrieveApplicationJob job = new RetrieveApplicationJob(server); + job.addJobChangeListener(new JobChangeAdapter() { @Override - protected IStatus run(IProgressMonitor monitor) { - final IApplication application = ExpressServerUtils.getApplication(server); - Display.getDefault().asyncExec(new Runnable() { - public void run() { - showEnvironmentProperties(application); - } - }); - return Status.OK_STATUS; + public void done(IJobChangeEvent event) { + if (event.getResult().isOK()) { + final IApplication application = job.getApplication(); + Display.getDefault().asyncExec(new Runnable() { + @Override + public void run() { + showEnvironmentPropertiesFor(application); + } + }); + } } - }; + }); job.setUser(true); job.schedule(); } - private void showEnvironmentProperties(final IApplication application) { - Job job = new Job("Retrieving selected OpenShift Application's environment variables...") { + private void showEnvironmentPropertiesFor(final IApplication application) { + final VerifySSHSessionJob job = new VerifySSHSessionJob(application); + job.addJobChangeListener(new JobChangeAdapter() { @Override - protected IStatus run(IProgressMonitor monitor) { - try { - verifyApplicationSSHSession(application); - Display.getDefault().asyncExec(new Runnable() { - public void run() { - runAsync(application); - } - }); - return Status.OK_STATUS; - } catch (OpenShiftSSHOperationException e) { - return OpenShiftUIActivator.createErrorStatus(e.getMessage(), e.getCause()); + public void done(IJobChangeEvent event) { + if(event.getResult().isOK() && job.isValidSession()) { + showEnvironmentProperties(application); } } - }; + }); + job.setUser(true); job.schedule(); } @@ -105,22 +100,27 @@ /** * @param application */ - private void runAsync(final IApplication application) { - try { - if (!application.hasSSHSession()) { - application.setSSHSession(OpenShiftSshSessionFactory.getInstance().createSession(application)); - } - List props = application.getEnvironmentProperties(); - final MessageConsole console = ConsoleUtils.findMessageConsole(getMessageConsoleName(application)); - console.clearConsole(); - MessageConsoleStream stream = console.newMessageStream(); - for (String prop : props) { - stream.println(prop); + private void showEnvironmentProperties(final IApplication application) { + Display.getDefault().syncExec(new Runnable() { + @Override + public void run() { + try { + if (!application.hasSSHSession()) { + application.setSSHSession(OpenShiftSshSessionFactory.getInstance().createSession(application)); + } + List props = application.getEnvironmentProperties(); + final MessageConsole console = ConsoleUtils.findMessageConsole(getMessageConsoleName(application)); + console.clearConsole(); + MessageConsoleStream stream = console.newMessageStream(); + for (String prop : props) { + stream.println(prop); + } + ConsoleUtils.displayConsoleView(console); + } catch (Exception e) { + OpenShiftUIActivator.createErrorStatus("Failed to display remote environment variables", e); + } } - ConsoleUtils.displayConsoleView(console); - } catch (Exception e) { - OpenShiftUIActivator.createErrorStatus("Failed to display remote environment variables", e); - } + }); } /** Index: src/org/jboss/tools/openshift/express/internal/ui/job/RetrieveApplicationJob.java =================================================================== --- src/org/jboss/tools/openshift/express/internal/ui/job/RetrieveApplicationJob.java (revision 0) +++ src/org/jboss/tools/openshift/express/internal/ui/job/RetrieveApplicationJob.java (revision 0) @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (c) 2012 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is 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: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.jboss.tools.openshift.express.internal.ui.job; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.wst.server.core.IServer; +import org.jboss.tools.openshift.express.internal.core.behaviour.ExpressServerUtils; +import org.jboss.tools.openshift.express.internal.core.console.UserDelegate; +import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator; + +import com.openshift.client.IApplication; +import com.openshift.client.OpenShiftException; + +/** + * @author Xavier Coulon + * + */ +public class RetrieveApplicationJob extends Job { + + private IApplication application = null; + + private final IServer server; + + public RetrieveApplicationJob(final IServer server) { + super("Identifying OpenShift Application from selected Server..."); + this.server = server; + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + UserDelegate userDelegate = ExpressServerUtils.getUserDelegate(server); + if (userDelegate != null && userDelegate.checkForPassword()) { + final String applicationName = ExpressServerUtils.getExpressApplicationName(server); + try { + this.application = userDelegate.getApplicationByName(applicationName); + } catch (OpenShiftException e) { + return OpenShiftUIActivator + .createErrorStatus("Failed to retrieve Application from the selected Server.", e); + } + if (application == null) { + return OpenShiftUIActivator + .createErrorStatus("Failed to retrieve Application from the selected Server.\n" + + "Please verify that the associated OpenShift Application still exists."); + } + return Status.OK_STATUS; + } + return Status.CANCEL_STATUS; + } + + /** + * @return the application + */ + public final IApplication getApplication() { + return application; + } + +} Index: src/org/jboss/tools/openshift/express/internal/ui/job/VerifySSHSessionJob.java =================================================================== --- src/org/jboss/tools/openshift/express/internal/ui/job/VerifySSHSessionJob.java (revision 0) +++ src/org/jboss/tools/openshift/express/internal/ui/job/VerifySSHSessionJob.java (revision 0) @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2012 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is 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: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.jboss.tools.openshift.express.internal.ui.job; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.jobs.Job; +import org.jboss.tools.openshift.express.internal.ui.OpenShiftUIActivator; +import org.jboss.tools.openshift.express.internal.ui.utils.Logger; +import org.jboss.tools.openshift.express.internal.ui.utils.OpenShiftSshSessionFactory; + +import com.jcraft.jsch.Session; +import com.openshift.client.IApplication; +import com.openshift.client.OpenShiftSSHOperationException; + +/** + * @author Xavier Coulon + * + */ +public class VerifySSHSessionJob extends Job { + + private final IApplication application; + + private boolean validSession = false; + + public VerifySSHSessionJob(final IApplication application) { + super("Verifying SSH session to retrieve Application's ports..."); + this.application = application; + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + final boolean hasAlreadySSHSession = application.hasSSHSession(); + if (!hasAlreadySSHSession) { + Logger.debug("Opening a new SSH Session for application '" + application.getName() + "'"); + Session session; + session = OpenShiftSshSessionFactory.getInstance().createSession( + application); + application.setSSHSession(session); + } + // now, check if the session is valid (ie, not null and still + // connected) + this.validSession = application.hasSSHSession(); + return Status.OK_STATUS; + } catch (OpenShiftSSHOperationException e) { + return OpenShiftUIActivator.createErrorStatus(e.getMessage(), e.getCause()); + } + } + + /** + * @return the hasSession + */ + public final boolean isValidSession() { + return validSession; + } + +}