From 29f5fedfebe189d524546724daec9d891f3f0e71 Mon Sep 17 00:00:00 2001 From: itaiag Date: Mon, 16 Feb 2015 18:47:48 +0200 Subject: [PATCH] Added cancel button to the wait for VM and remote debugging dialogs. Issue #142 --- .../main/java/jsystem/runner/WaitDialog.java | 55 ++++++++++++------- .../runner/remote/RemoteExecutorImpl.java | 50 ++++++++++++++--- 2 files changed, 79 insertions(+), 26 deletions(-) diff --git a/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/WaitDialog.java b/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/WaitDialog.java index b1c17894..e9622f13 100644 --- a/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/WaitDialog.java +++ b/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/WaitDialog.java @@ -34,27 +34,34 @@ public class WaitDialog extends JDialog { private String title; - private JButton cancel = new JButton("Cancel"); - + private JButton cancelBtn = new JButton("Cancel"); + private static Object staticLock = new Object(); + + private final WaitDialogListner listner; - /** - * Default constructor - * - * @param title - * the message to be displayed in the window's titlebar - * - */ - public WaitDialog(Frame parent, String title) { + public WaitDialog(Frame parent, String title, WaitDialogListner listener) { super(parent, title); this.parent = parent; this.title = title; + this.listner = listener; /* * Set the dialog to be modal */ setModalityType(ModalityType.DOCUMENT_MODAL); initComponents(); } + + /** + * Default constructor + * + * @param title + * the message to be displayed in the window's titlebar + * + */ + public WaitDialog(Frame parent, String title) { + this(parent,title,null); + } public WaitDialog(Dialog parent, String title) { super(parent, title); @@ -65,6 +72,7 @@ public WaitDialog(Dialog parent, String title) { */ setModalityType(ModalityType.DOCUMENT_MODAL); initComponents(); + this.listner = null; } public WaitDialog(String title) { @@ -75,6 +83,7 @@ public WaitDialog(String title) { */ setModalityType(ModalityType.DOCUMENT_MODAL); initComponents(); + this.listner = null; } public WaitDialog() { @@ -83,6 +92,7 @@ public WaitDialog() { */ setModalityType(ModalityType.DOCUMENT_MODAL); initComponents(); + this.listner = null; } private void initComponents() { @@ -91,7 +101,7 @@ private void initComponents() { panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); JProgressBar bar = new JProgressBar(); - bar.setPreferredSize(new Dimension(200, 20)); + bar.setPreferredSize(new Dimension(150 + (title != null ? title.length() : 0) * 4, 20)); bar.setIndeterminate(true); bar.setStringPainted(true); bar.setString(title); @@ -102,9 +112,13 @@ private void initComponents() { buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); buttons.add(Box.createHorizontalGlue()); buttons.add(Box.createHorizontalGlue()); + buttons.add(cancelBtn); panel.add(buttons, BorderLayout.SOUTH); - cancel.addActionListener(new ActionListener() { + cancelBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { + if (listner != null){ + listner.cancel(); + } dispose(); } }); @@ -117,14 +131,11 @@ public void actionPerformed(ActionEvent e) { private static WaitDialog dialog = null; - public synchronized static void launchWaitDialog(final String title) { - if (dialog != null) { // probebly sum kind of error - return; - } + public synchronized static void launchWaitDialog(final String title, WaitDialogListner listner) { /* * Execute the open of the dialog in a thread as the dialog is modal */ - dialog = new WaitDialog(TestRunnerFrame.guiMainFrame, title); + dialog = new WaitDialog(TestRunnerFrame.guiMainFrame, title, listner); (new Thread() { public void run() { dialog.setVisible(true); @@ -137,7 +148,7 @@ public void run() { } } } - + public static void endWaitDialog() { synchronized(staticLock){ if (dialog == null) { @@ -149,7 +160,7 @@ public static void endWaitDialog() { } public static void main(String[] args) { - launchWaitDialog("Just wait"); + launchWaitDialog("Just wait",null); try { Thread.sleep(4000); } catch (Exception e) { @@ -159,4 +170,10 @@ public static void main(String[] args) { endWaitDialog(); } + public interface WaitDialogListner { + void cancel(); + } + + + } diff --git a/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/remote/RemoteExecutorImpl.java b/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/remote/RemoteExecutorImpl.java index 60c7461a..9f7c6c29 100644 --- a/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/remote/RemoteExecutorImpl.java +++ b/jsystem-core-projects/jsystemAgent/src/main/java/jsystem/runner/remote/RemoteExecutorImpl.java @@ -11,6 +11,7 @@ import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; +import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; @@ -31,6 +32,7 @@ import jsystem.framework.scenario.ScenariosManager; import jsystem.runner.ErrorLevel; import jsystem.runner.WaitDialog; +import jsystem.runner.WaitDialog.WaitDialogListner; import jsystem.runner.agent.publisher.Publisher; import jsystem.runner.agent.publisher.PublisherManager; import jsystem.runner.agent.tests.PublishTest; @@ -149,20 +151,49 @@ public void run(String antFile, String[] targets, Properties additional) throws * (test vm) manually with the information as execution parameters: * -port 1999 -host 127.0.0.1 */ + String vmParams = JSystemProperties.getInstance().getPreference(FrameworkOptions.TEST_VM_PARMS); boolean testDebug = "true".equals(JSystemProperties.getInstance().getPreference(FrameworkOptions.TESTS_DEBUG)); String waitMessage; - if (testDebug) { - waitMessage = "Run Eclipse remote debugger and connect it to port 8787"; + if (testDebug || (vmParams != null && vmParams.contains("-Xdebug"))) { + waitMessage = "Waiting for remote debugging connection"; } else { waitMessage = "Wait for remote VM"; } File antHome = CommonResources.getAntDirectory(); - WaitDialog.launchWaitDialog(waitMessage); + WaitDialog.launchWaitDialog(waitMessage, new WaitDialogListner() { + + @Override + public void cancel() { + interrupted = true; + if (ss != null) { + try { + // ITAI: This will cause the socket to throw exception + // and + // release the blocking state + ss.close(); + } catch (IOException e) { + // Don't care + } + } + try { + // ITAI: Needs to give time for the ScenarioExecutor to get + // to the + // waitForRunEnd method. + Thread.sleep(100); + } catch (InterruptedException e) { + } + + // ITAI: Telling everyone that the execution was ended. + runEndListener.endRun(); + runEndListener.remoteExit(); + // Let's close everything. + close(); + } + }); File antLauncher = new File(antHome + File.separator + "lib", "ant-launcher.jar"); - String vmParams = JSystemProperties.getInstance().getPreference(FrameworkOptions.TEST_VM_PARMS); String[] vmParamsArr = new String[0]; if (vmParams != null) { // replaces socket number templates with free socket numbers @@ -228,15 +259,20 @@ public void run(String antFile, String[] targets, Properties additional) throws cmd.setDir(new File(System.getProperty("user.dir"))); Execute.execute(cmd, false, true, false); + try { + socket = ss.accept(); - socket = ss.accept(); + } catch (SocketException e) { + // ITAI: This will happen if the execution was cancelled by th user. + log.info("Execution was cancelled by the user "); + return; + } in = new ObjectInputStream(socket.getInputStream()); out = new ObjectOutputStream(socket.getOutputStream()); running = true; closed = false; reader = new ReaderThread(); reader.start(); - } private class ReaderThread extends Thread { @@ -485,7 +521,7 @@ public void run() { // SystemObjectCheckWindow.getInstance().setSysObjStatus(m.getField(0), // SOCheckStatus.valueOf(m.getField(1)), m.getField(2)); break; - + default: System.out.println("Unkown message type local: " + m.getType()); }