Search in sources :

Example 1 with MutableBoolean

use of org.knime.core.util.MutableBoolean in project knime-core by knime.

the class CommandExecution method execute.

/**
 * Executes the commands via the Runtime and streams the stdout as well as
 * the stderr to the logger and notifies the registered observer if a msg
 * occurs.
 *
 * @see Runtime#exec(String[], String[], File)
 *
 * @param exec The <code>ExecutionMonitor</code> to monitor the status of
 *            execution and to check for user cancellations.
 *
 * @return The exit value of the subprocess.
 *
 * @throws Exception If the execution terminates abnormally.
 */
public int execute(final ExecutionMonitor exec) throws Exception {
    // execute the command
    int exitVal;
    try {
        exec.setProgress("Starting command");
        Runtime rt = Runtime.getRuntime();
        LOGGER.debug("Launching command: '" + getCmdString() + "'");
        exec.setProgress("External command is running...");
        final Process proc;
        if (m_cmds != null) {
            assert m_cmd == null;
            proc = rt.exec(m_cmds, m_envps, m_dir);
        } else {
            assert m_cmds == null;
            proc = rt.exec(m_cmd, m_envps, m_dir);
        }
        // create a thread that periodically checks for user cancellation
        final MutableBoolean procDone = new MutableBoolean(false);
        ThreadUtils.threadWithContext(new CheckCanceledRunnable(proc, procDone, exec)).start();
        // pick up the output to std err from the executable
        Thread stdErrThread = ThreadUtils.threadWithContext(new StdErrCatchRunnable(proc, exec, m_extErrout));
        stdErrThread.setName("ExtTool StdErr collector");
        stdErrThread.start();
        // pick up the output to std out from the executable
        Thread stdOutThread = ThreadUtils.threadWithContext(new StdOutCatchRunnable(proc, exec, m_extOutput));
        stdOutThread.setName("ExtTool StdOut collector");
        stdOutThread.start();
        // wait until the external process finishes.
        exitVal = proc.waitFor();
        synchronized (procDone) {
            // this should terminate the check cancel thread
            procDone.setValue(true);
        }
        exec.checkCanceled();
        exec.setProgress("External command done.");
        String message = "External commands terminated with exit code: " + exitVal;
        if (exitVal == 0) {
            LOGGER.debug(message);
        } else {
            LOGGER.info(message);
        }
    } catch (InterruptedException ie) {
        throw ie;
    } catch (Exception e) {
        LOGGER.error("Execution failed (with exception): " + e.getMessage(), e);
        throw e;
    } catch (Throwable t) {
        LOGGER.fatal("Execution failed (with error): " + t.getMessage(), t);
        throw new Exception(t);
    }
    return exitVal;
}
Also used : MutableBoolean(org.knime.core.util.MutableBoolean) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 2 with MutableBoolean

use of org.knime.core.util.MutableBoolean in project knime-core by knime.

the class BatchExecutor method executeWorkflow.

/**
 * Executes a workflow.
 *
 * @param wfm the workflow manager
 * @param config the corresponding workflow configuration
 * @return <code>true</code> if the workflow executed successfully, <code>false</code> otherwise
 * @throws CanceledExecutionException if execution has been canceled by the user
 * @throws BatchException may be thrown by subclass implementations; the real exception is available via the cause
 *             of the batch exception
 * @since 2.7
 */
protected boolean executeWorkflow(final WorkflowManager wfm, final WorkflowConfiguration config) throws CanceledExecutionException, BatchException {
    LOGGER.debug("Status of workflow before execution:");
    LOGGER.debug("------------------------------------");
    dumpWorkflowToDebugLog(wfm);
    LOGGER.debug("------------------------------------");
    boolean successful = true;
    final MutableBoolean executionCanceled = new MutableBoolean(false);
    if (!config.noExecute) {
        // get workspace dir
        File wsFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
        // file to be checked for
        final File cancelFile = new File(wsFile, ".cancel");
        // create new timer task
        TimerTask task = new TimerTask() {

            /**
             * {@inheritDoc}
             */
            @Override
            public void run() {
                if (cancelFile.exists()) {
                    // CANCEL workflow manager
                    wfm.cancelExecution();
                    // delete cancel file
                    cancelFile.delete();
                    executionCanceled.setValue(true);
                    // cancel this timer
                    this.cancel();
                }
            }
        };
        KNIMETimer.getInstance().schedule(task, 1000, 1000);
        successful = wfm.executeAllAndWaitUntilDone();
        task.cancel();
    }
    if (executionCanceled.booleanValue()) {
        throw new CanceledExecutionException();
    }
    return successful;
}
Also used : TimerTask(java.util.TimerTask) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) MutableBoolean(org.knime.core.util.MutableBoolean) File(java.io.File)

Example 3 with MutableBoolean

use of org.knime.core.util.MutableBoolean in project knime-core by knime.

the class KNIMEApplication method checkForUpdates.

private boolean checkForUpdates() {
    final IPreferenceStore prefStore = ProductPlugin.getDefault().getPreferenceStore();
    if (prefStore.getBoolean(JUSTUPDATED)) {
        prefStore.setValue(JUSTUPDATED, false);
        return false;
    }
    final ProvisioningUI provUI = ProvisioningUI.getDefaultUI();
    if (provUI.getRepositoryTracker() == null) {
        LogHelper.log(new Status(IStatus.WARNING, ProductPlugin.getDefault().getBundle().getSymbolicName(), "Updating is not possible of KNIME is started from " + "within the IDE."));
        return false;
    }
    final IProvisioningAgent agent = (IProvisioningAgent) ServiceHelper.getService(ProductPlugin.getDefault().getBundle().getBundleContext(), IProvisioningAgent.SERVICE_NAME);
    if (agent == null) {
        LogHelper.log(new Status(IStatus.ERROR, ProductPlugin.getDefault().getBundle().getSymbolicName(), "No provisioning agent found. This application is " + "not set up for updates."));
    }
    final MutableBoolean restart = new MutableBoolean(false);
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        @Override
        public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            ProvisioningSession session = new ProvisioningSession(agent);
            UpdateOperation operation = new UpdateOperation(session);
            SubMonitor sub = SubMonitor.convert(monitor, "Checking for application updates...", 200);
            IStatus status = operation.resolveModal(sub.newChild(100));
            if (status.getSeverity() == IStatus.CANCEL) {
                throw new OperationCanceledException();
            } else if (status.getSeverity() != IStatus.ERROR) {
                ProvisioningJob job = operation.getProvisioningJob(null);
                status = job.runModal(sub.newChild(100));
                if (status.getSeverity() == IStatus.CANCEL) {
                    throw new OperationCanceledException();
                }
                if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
                    // ok, just proceed
                    LogHelper.log(new Status(IStatus.INFO, ProductPlugin.getDefault().getBundle().getSymbolicName(), "No updates found."));
                } else if (status.getSeverity() != IStatus.ERROR) {
                    prefStore.setValue(JUSTUPDATED, true);
                    restart.setValue(true);
                } else {
                    LogHelper.log(status);
                }
            } else {
                LogHelper.log(status);
            }
        }
    };
    try {
        new ProgressMonitorDialog(null).run(true, true, runnable);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
    }
    return restart.booleanValue();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ProvisioningSession(org.eclipse.equinox.p2.operations.ProvisioningSession) IStatus(org.eclipse.core.runtime.IStatus) IProvisioningAgent(org.eclipse.equinox.p2.core.IProvisioningAgent) MutableBoolean(org.knime.core.util.MutableBoolean) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) SubMonitor(org.eclipse.core.runtime.SubMonitor) ProvisioningJob(org.eclipse.equinox.p2.operations.ProvisioningJob) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProvisioningUI(org.eclipse.equinox.p2.ui.ProvisioningUI) UpdateOperation(org.eclipse.equinox.p2.operations.UpdateOperation) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Aggregations

MutableBoolean (org.knime.core.util.MutableBoolean)3 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 TimerTask (java.util.TimerTask)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 Status (org.eclipse.core.runtime.Status)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 IProvisioningAgent (org.eclipse.equinox.p2.core.IProvisioningAgent)1 ProvisioningJob (org.eclipse.equinox.p2.operations.ProvisioningJob)1 ProvisioningSession (org.eclipse.equinox.p2.operations.ProvisioningSession)1 UpdateOperation (org.eclipse.equinox.p2.operations.UpdateOperation)1 ProvisioningUI (org.eclipse.equinox.p2.ui.ProvisioningUI)1 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)1 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1