Author: seanfinan
Date: Fri Oct 14 14:28:45 2022
New Revision: 1904591
URL: http://svn.apache.org/viewvc?rev=1904591&view=rev
Log:
External Process can now be stopped on exit and have custom env vars.
Piper File Submitter help button that launches wiki help page.
Added:
ctakes/trunk/ctakes-gui-res/src/main/resources/org/apache/ctakes/gui/pipeline/icon/StopHand.png
(with props)
Modified:
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/external/SystemUtil.java
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java
Modified:
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/external/SystemUtil.java
URL:
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/external/SystemUtil.java?rev=1904591&r1=1904590&r2=1904591&view=diff
==============================================================================
---
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/external/SystemUtil.java
(original)
+++
ctakes/trunk/ctakes-core/src/main/java/org/apache/ctakes/core/util/external/SystemUtil.java
Fri Oct 14 14:28:45 2022
@@ -10,6 +10,7 @@ import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.*;
+import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.*;
@@ -279,12 +280,15 @@ final public class SystemUtil {
static public class CommandRunner implements Callable<Boolean> {
+ private final Map<String, String> _userEnvVars = new HashMap<>();
private final String _command;
private String _dir;
private String _outLog;
private String _errLog;
private Logger _logger;
private boolean _wait;
+ private boolean _stopOnExit;
+ private InputFeeder _inputFeeder;
public CommandRunner( final String command ) {
_command = command;
@@ -308,6 +312,19 @@ final public class SystemUtil {
_wait = wait;
}
+ public void addEnvVar( final String name, final String value ) {
+ _userEnvVars.put( name, value );
+ }
+
+ public void stopOnExit( final boolean stopOnExit ) {
+ _stopOnExit = stopOnExit;
+ }
+
+ public InputFeeder createInputFeeder() {
+ _inputFeeder = new InputFeeder();
+ return _inputFeeder;
+ }
+
private String getDefaultLogFile() {
final Random randomizer = new Random();
final int spaceIndex = _command.indexOf( ' ' );
@@ -317,7 +334,7 @@ final public class SystemUtil {
return _command.substring( 0, spaceIndex ) + ".ctakes.log." +
randomizer.nextLong();
}
- static private void ensureEnvironment( final ProcessBuilder
processBuilder ) {
+ private void ensureEnvironment( final ProcessBuilder processBuilder ) {
final Map<String, String> env = processBuilder.environment();
// If the user set a variable in a piper file using "env" then add
that to the environment.
System.getProperties()
@@ -341,6 +358,7 @@ final public class SystemUtil {
env.put( "CLASSPATH", classpath );
}
}
+ env.putAll( _userEnvVars );
}
public Boolean call() throws IOException, InterruptedException {
@@ -355,10 +373,8 @@ final public class SystemUtil {
String cmd = "cmd.exe";
String cmdOpt = "/c";
final String os = System.getProperty( "os.name" );
- if ( os.toLowerCase()
- .contains( "windows" ) ) {
- command = command.replace( '/', '\\' );
- } else {
+ if ( !os.toLowerCase()
+ .contains( "windows" ) ) {
cmd = "bash";
cmdOpt = "-c";
// if ( !_wait ) {
@@ -375,17 +391,64 @@ final public class SystemUtil {
}
ensureEnvironment( processBuilder );
final Process process = processBuilder.start();
+ if ( _stopOnExit ) {
+ registerShutdownHook( process );
+ }
if ( _logger != null ) {
final ExecutorService executors = Executors.newFixedThreadPool( 2
);
executors.submit( new OutputLogger( process, _logger ) );
executors.submit( new ErrorLogger( process, _logger ) );
}
+ if ( _inputFeeder != null ) {
+ _inputFeeder.setProcess( process );
+ }
if ( _wait ) {
return process.waitFor() == 0;
}
return true;
}
+ /**
+ * Registers a shutdown hook for the process so that it shuts down when
the VM exits.
+ * This includes kill signals and user actions like "Ctrl-C".
+ */
+ private void registerShutdownHook( final Process process ) {
+ Runtime.getRuntime()
+ .addShutdownHook( new Thread( () -> {
+ try {
+ if ( process.isAlive() ) {
+ process.destroy();
+ // if destroy doesn't work then waitFor may hang this
app.
+ process.waitFor();
+ }
+ } catch ( InterruptedException multE ) {
+ LOGGER.error( "Could not stop process.", multE );
+ }
+ } ) );
+ }
+
+ }
+
+
+ static private class InputFeeder {
+
+ private OutputStream _input;
+
+ private void setProcess( final Process process ) {
+ _input = process.getOutputStream();
+ }
+
+ public void feedInput( final String input ) {
+ if ( _input == null ) {
+ LOGGER.error( "Process not started, cannot send input." );
+ }
+ try {
+ _input.write( input.getBytes() );
+ } catch ( IOException ioE ) {
+ LOGGER.error( "Could not send input to process. " +
ioE.getMessage() );
+ }
+ }
+
}
Added:
ctakes/trunk/ctakes-gui-res/src/main/resources/org/apache/ctakes/gui/pipeline/icon/StopHand.png
URL:
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-gui-res/src/main/resources/org/apache/ctakes/gui/pipeline/icon/StopHand.png?rev=1904591&view=auto
==============================================================================
Binary file - no diff available.
Propchange:
ctakes/trunk/ctakes-gui-res/src/main/resources/org/apache/ctakes/gui/pipeline/icon/StopHand.png
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Modified:
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java
URL:
http://svn.apache.org/viewvc/ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java?rev=1904591&r1=1904590&r2=1904591&view=diff
==============================================================================
---
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java
(original)
+++
ctakes/trunk/ctakes-gui/src/main/java/org/apache/ctakes/gui/pipeline/PiperRunnerPanel.java
Fri Oct 14 14:28:45 2022
@@ -6,6 +6,7 @@ import org.apache.ctakes.core.pipeline.P
import org.apache.ctakes.core.pipeline.PiperFileRunner;
import org.apache.ctakes.core.pipeline.ProgressManager;
import org.apache.ctakes.core.resource.FileLocator;
+import org.apache.ctakes.core.util.external.SystemUtil;
import org.apache.ctakes.gui.component.*;
import org.apache.ctakes.gui.pipeline.bit.parameter.ParameterCellRenderer;
import org.apache.ctakes.gui.pipeline.piper.PiperFileView;
@@ -60,6 +61,7 @@ final public class PiperRunnerPanel exte
private JButton _saveButton;
private JButton _parmButton;
private JButton _runButton;
+ private JButton _helpButton;
private final String[] STANDARD_CHARS = { "i", "o" };
private final String[] STANDARD_NAMES = { "InputDirectory",
"OutputDirectory" };
@@ -121,7 +123,8 @@ final public class PiperRunnerPanel exte
_runButton.setEnabled( false );
toolBar.addSeparator( new Dimension( 50, 0 ) );
- final JProgressBar progressBar = new JProgressBar(
ProgressManager.getInstance().getModel() ) {
+ final JProgressBar progressBar = new JProgressBar(
ProgressManager.getInstance()
+
.getModel() ) {
protected void fireStateChanged() {
super.fireStateChanged();
SwingUtilities.invokeLater( () -> setString( getValue() + " / " +
getMaximum() ) );
@@ -129,6 +132,10 @@ final public class PiperRunnerPanel exte
};
progressBar.setStringPainted( true );
toolBar.add( progressBar );
+
+ toolBar.addSeparator( new Dimension( 50, 0 ) );
+ _helpButton = addButton( toolBar, "Piper File Submitter Help" );
+ _helpButton.addActionListener( new HelpAction() );
toolBar.addSeparator( new Dimension( 10, 0 ) );
return toolBar;
@@ -138,7 +145,7 @@ final public class PiperRunnerPanel exte
final JButton button = new JButton();
button.setFocusPainted( false );
// prevents first button from having a painted border
-// button.setFocusable( false );
+ button.setFocusable( false );
button.setToolTipText( toolTip );
toolBar.add( button );
toolBar.addSeparator( new Dimension( 10, 0 ) );
@@ -544,14 +551,44 @@ final public class PiperRunnerPanel exte
executor.execute( new PiperFileRunnable() );
executor.shutdown();
}
+
}
+ private final class HelpAction implements ActionListener {
+
+ @Override
+ public void actionPerformed( final ActionEvent event ) {
+ if ( _helpButton == null ) {
+ return;
+ }
+ LOGGER.info( "Opening Web Page ..." );
+ String command = "start \"Browser\" /max https://cwiki.apache"
+ +
".org/confluence/display/CTAKES/Piper+File+Submitter+GUI";
+ final String os = System.getProperty( "os.name" );
+ if ( !os.toLowerCase()
+ .contains( "windows" ) ) {
+ command = "xdg-open
https://cwiki.apache.org/confluence/display/CTAKES/Piper+File+Submitter+GUI"
+ + " || sensible-browser"
+ + "
https://cwiki.apache.org/confluence/display/CTAKES/Piper+File+Submitter+GUI";
+ }
+ try {
+ SystemUtil.run( command );
+ } catch ( IOException e ) {
+ LOGGER.error( e.getMessage() );
+ }
+ }
+
+ }
+
+
private class PiperFileRunnable implements Runnable {
+
@Override
public void run() {
- final JFrame frame = (JFrame)SwingUtilities.getRoot(
PiperRunnerPanel.this );
+ final JFrame frame = (JFrame) SwingUtilities.getRoot(
PiperRunnerPanel.this );
frame.setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ) );
- DisablerPane.getInstance().setVisible( true );
+ DisablerPane.getInstance()
+ .setVisible( true );
final java.util.List<String> args = new ArrayList<>();
args.add( 0, "-p" );
args.add( 1, _piperPath );
@@ -594,14 +631,17 @@ final public class PiperRunnerPanel exte
final String parmPng = "BoxOfStuff.png";
final String savePng = "Package.png";
final String runPng = "RunPiper.png";
+ final String helpPng = "Help_32.png";
final Icon openIcon = IconLoader.loadIcon( dir + openPng );
final Icon parmIcon = IconLoader.loadIcon( dir + parmPng );
final Icon saveIcon = IconLoader.loadIcon( dir + savePng );
final Icon runIcon = IconLoader.loadIcon( dir + runPng );
+ final Icon helpIcon = IconLoader.loadIcon( dir + helpPng );
_openButton.setIcon( openIcon );
_parmButton.setIcon( parmIcon );
_saveButton.setIcon( saveIcon );
_runButton.setIcon( runIcon );
+ _helpButton.setIcon( helpIcon );
}
}