commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgoes...@apache.org
Subject svn commit: r951330 - in /commons/proper/exec/trunk/src: changes/ main/java/org/apache/commons/exec/ site/apt/ test/java/org/apache/commons/exec/
Date Fri, 04 Jun 2010 08:53:38 GMT
Author: sgoeschl
Date: Fri Jun  4 08:53:38 2010
New Revision: 951330

URL: http://svn.apache.org/viewvc?rev=951330&view=rev
Log:
[EXEC-42] Updating the tutorial and adding more tests

Modified:
    commons/proper/exec/trunk/src/changes/changes.xml
    commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java
    commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java
    commons/proper/exec/trunk/src/site/apt/tutorial.apt
    commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java

Modified: commons/proper/exec/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/changes/changes.xml?rev=951330&r1=951329&r2=951330&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/changes/changes.xml (original)
+++ commons/proper/exec/trunk/src/changes/changes.xml Fri Jun  4 08:53:38 2010
@@ -18,140 +18,144 @@
  under the License.
 -->
 <document>
-  <properties>
-    <title>commons-exec</title>
-    <author email="sgoeschl@apache.org">Siegfried Goeschl</author>
-  </properties>
-  <body>
-    <release version="1.0.2-SNAPSHOT" date="as in SVN" description="Maintenance Release">
-      <action dev="sgoeschl" type="fix" date="2010-05-31" issue="EXEC-44">
-        Because the ExecuteWatchdog is the only way to destroy asynchronous processes,
-        it should be possible to set it to an infinite timeout, for processes which
-        should not timeout, but manually destroyed under some circumstances.
-      </action>    
-    </release>
-    <release version="1.0.1" date="2009-09-28" description="Maintenance Release">
-      <action dev="henrib" type="fix" date="2009-09-25" issue="EXEC-33">
-        On a Mac, the unit tests never finish. Culprit is InputStreamPumper which 
-        sets its stop member in the run method; however, run might really be executed 
-        after the stopProcessing method is called if the process 
-        thread completes before the InputStreamPumper starts.
-      </action>
-      <action dev="sgoeschl" type="fix" due-to="Peter Henderson" issue="EXEC-40">
-        Fixes NPE in DefaultExecutor.setExitValues().
-      </action>
-      <action dev="sgoeschl" type="fix" due-to="Milos Kleint" issue="EXEC-33">
-        Copies all data from an System.input stream to an output stream of 
-        the executed process. 
-      </action>
-    </release>  
-    <release version="1.0" date="2009-03-15" description="First Public Release">
-      <action dev="sgoeschl" type="fix" due-to="Sebastien Bazley" issue="EXEC-37">
-        Removed useless synchronized statement in
-        OpenVmsProcessingEnvironment.createProcEnvironment
-      </action>
-      <action dev="sgoeschl" type="fix" issue="EXEC-33">
-        Using System.in for child process will actually hang your application -
-        see JIRA for more details. Since there is no easy fix an
-        IllegalRuntimeException is thrown when System.in is passed.
-      </action>
-      <action dev="sgoeschl" type="fix" due-to="Luc Maisonobe" issue="EXEC-35">
-        Fixing a few findbugs issues.
-      </action>
-      <action dev="sgoeschl" type="fix" due-to="Marco Ferrante" issue="EXEC-32">
-        Handle null streams consistently.
-      </action>
-      <action dev="sgoeschl" type="fix">
-        After a long discussion we decided to stick to following groupId
-        "org.apache.commons" instead of "commons-exec".
-      </action>
-      <action dev="sgoeschl" type="fix" due-to="Kevin Jackson">
-        The Ant build now works even when junit is not on the classpath
-      </action>
-      <action dev="sgoeschl" type="fix">
-        Fixed broken "groupId" from "org.apache.commons" to "commons-exec"
-      </action>
-      <action dev="sgoeschl" type="fix" issue="EXEC-27" due-to="Benjamin Bentmann">
-        Renamed EnvironmentUtil to EnvironmentUtils to align with other classes
-        in this project and commons in general. Please note that this change
-        could break existing clients (but would be rather unlikely).
-      </action>
-      <action dev="sgoeschl" type="fix" issue="EXEC-30" due-to="Benjamin Bentmann">
-         Make environment variables respect casing rules of platforms. Under Windows
-         "PATH", "Path" and "path" would access the same environment variable whereas
-         the real name is "Path".
-      </action>
-      <action dev="sgoeschl" type="fix" issue="EXEC-31" due-to="Benjamin Bentmann">
-        Invoking DefaultExecutor.execute(CommandLine command, Map environment) using 
-        a 'null' Map results in inheriting all environment variables of the current
-        process while passing an empty map implies starting the new process with no
-        environment variables. In short 'null' is not the same as an empty map. 
-      </action>
-      <action dev="sgoeschl" type="add" issue="EXEC-26" due-to="Benjamin Bentmann">
-        Added one additional test : DefaultExecutorTest.testExecuteWithFancyArg
-      </action>
-      <action dev="sgoeschl" issue="EXEC-25" type="fix">
-        Using variable substitution within CommandLine broke the regression tests
-        under Windows. Found also another bug when calling CommandLine.getExecutable()
-        the result was not substituted at all. As a general rule we do variable
-        substitution and file separator fixing on the command line executable and
-        variable substitution but NO file separator fixing for the command line
-        arguments.
-      </action>
-      <action dev="sgoeschl" type="add">
-        Added convinience method to add two parameters to the CommandLine
-        using one method invocation.
-      </action>
-      <action dev="sgoeschl" type="fix">
-        Implemented better regression test for OpenVMS affecting also
-        the Executor and CommandLauncher interface.
-      </action>
-      <action dev="sebb" type="add">
-        Added test scripts for OpenVMS - he seems to be the last human
-        having access to an OpenVMS box ... :-)
-      </action>
-      <action dev="sgoeschl" type="add" due-to="Simone Gianni,Bindul Bhowmik,Niall Pemberton,Sebastian
Bazley">
-        With the help of the Apache Commons community I added the first results
-        of cross-OS testing.
-      </action>
-      <action dev="sgoeschl" type="add">
-        The regression tests now also works on Windows - so it should
-        work now on Linux, Windows and Mac OS X
-      </action>
-      <action dev="sgoeschl" type="add">
-        Added DebugUtils to improve cross-platform testing.
-      </action>
-      <action dev="sgoeschl" type="remove">
-        Removed commons-logging integration
-      </action>
-      <action dev="sgoeschl" type="add" issue="SANDBOX-62" due-to="Jeremy Lacoste">
-        Made DefaultExecutor.launch() protected to enable mocking.
-      </action>
-      <action dev="sgoeschl" type="add" issue="SANDBOX-107" due-to="Niklas Gustavsson">
-        Made ProcessDestroyer optional and pluggable when using Executor.
-      </action>
-      <action dev="sgoeschl" type="add">
-        CommandLine can now expand the given command line by a user-suppied
-        map. This allows to execute something like "${JAVA_HOME}/bin/java -jar ${myapp}"
-      </action>
-      <action dev="sgoeschl" type="add" issue="SANDBOX-192" due-to="Reinhold Fuereder">
-        Added methods to provide pre-quoted arguments.
-      </action>
-      <action dev="sgoeschl" type="add" issue="SANDBOX-193" due-to="Reinhold Fuereder">
-        Exposing a ExecuteWatchdog.destroy() to kill an asynchrounous process
-        manually. This formalizes a workaround described in the JIRA
-      </action>
-      <action dev="sgoeschl" type="add" issue="SANDBOX-203">
-      	Extending exit value handling to support applications returning an error
-      	code.
-      </action>    
-      <action dev="sgoeschl" type="fix" issue="SANDBOX-204">
-        Cleaned up the source code to get rid of javadoc errors and
-        unused imports.
-      </action>    
-      <action dev="sgoeschl" type="add" issue="SANDBOX-204">
-        Added a few regression tests for the watchdog since they were missing.
-      </action>    
-    </release>
-  </body>
+    <properties>
+        <title>commons-exec</title>
+        <author email="sgoeschl@apache.org">Siegfried Goeschl</author>
+    </properties>
+    <body>
+        <release version="1.1-SNAPSHOT" date="as in SVN" description="Maintenance Release">
+            <action dev="sgoeschl" type="update" date="2010-06-01" issue="EXEC-42" due-to="Pablo
Hoertner">
+                Added a new section to the tutorial to show working with asynchronous
+                processes. Thanks to Pablo for providing this documentation update.
+            </action>
+            <action dev="sgoeschl" type="fix" date="2010-05-31" issue="EXEC-44">
+                Because the ExecuteWatchdog is the only way to destroy asynchronous processes,
+                it should be possible to set it to an infinite timeout, for processes which
+                should not timeout, but manually destroyed under some circumstances.
+            </action>
+        </release>
+        <release version="1.0.1" date="2009-09-28" description="Maintenance Release">
+            <action dev="henrib" type="fix" date="2009-09-25" issue="EXEC-33">
+                On a Mac, the unit tests never finish. Culprit is InputStreamPumper which
+                sets its stop member in the run method; however, run might really be executed
+                after the stopProcessing method is called if the process
+                thread completes before the InputStreamPumper starts.
+            </action>
+            <action dev="sgoeschl" type="fix" due-to="Peter Henderson" issue="EXEC-40">
+                Fixes NPE in DefaultExecutor.setExitValues().
+            </action>
+            <action dev="sgoeschl" type="fix" due-to="Milos Kleint" issue="EXEC-33">
+                Copies all data from an System.input stream to an output stream of
+                the executed process.
+            </action>
+        </release>
+        <release version="1.0" date="2009-03-15" description="First Public Release">
+            <action dev="sgoeschl" type="fix" due-to="Sebastien Bazley" issue="EXEC-37">
+                Removed useless synchronized statement in
+                OpenVmsProcessingEnvironment.createProcEnvironment
+            </action>
+            <action dev="sgoeschl" type="fix" issue="EXEC-33">
+                Using System.in for child process will actually hang your application -
+                see JIRA for more details. Since there is no easy fix an
+                IllegalRuntimeException is thrown when System.in is passed.
+            </action>
+            <action dev="sgoeschl" type="fix" due-to="Luc Maisonobe" issue="EXEC-35">
+                Fixing a few findbugs issues.
+            </action>
+            <action dev="sgoeschl" type="fix" due-to="Marco Ferrante" issue="EXEC-32">
+                Handle null streams consistently.
+            </action>
+            <action dev="sgoeschl" type="fix">
+                After a long discussion we decided to stick to following groupId
+                "org.apache.commons" instead of "commons-exec".
+            </action>
+            <action dev="sgoeschl" type="fix" due-to="Kevin Jackson">
+                The Ant build now works even when junit is not on the classpath
+            </action>
+            <action dev="sgoeschl" type="fix">
+                Fixed broken "groupId" from "org.apache.commons" to "commons-exec"
+            </action>
+            <action dev="sgoeschl" type="fix" issue="EXEC-27" due-to="Benjamin Bentmann">
+                Renamed EnvironmentUtil to EnvironmentUtils to align with other classes
+                in this project and commons in general. Please note that this change
+                could break existing clients (but would be rather unlikely).
+            </action>
+            <action dev="sgoeschl" type="fix" issue="EXEC-30" due-to="Benjamin Bentmann">
+                Make environment variables respect casing rules of platforms. Under Windows
+                "PATH", "Path" and "path" would access the same environment variable whereas
+                the real name is "Path".
+            </action>
+            <action dev="sgoeschl" type="fix" issue="EXEC-31" due-to="Benjamin Bentmann">
+                Invoking DefaultExecutor.execute(CommandLine command, Map environment) using
+                a 'null' Map results in inheriting all environment variables of the current
+                process while passing an empty map implies starting the new process with
no
+                environment variables. In short 'null' is not the same as an empty map.
+            </action>
+            <action dev="sgoeschl" type="add" issue="EXEC-26" due-to="Benjamin Bentmann">
+                Added one additional test : DefaultExecutorTest.testExecuteWithFancyArg
+            </action>
+            <action dev="sgoeschl" issue="EXEC-25" type="fix">
+                Using variable substitution within CommandLine broke the regression tests
+                under Windows. Found also another bug when calling CommandLine.getExecutable()
+                the result was not substituted at all. As a general rule we do variable
+                substitution and file separator fixing on the command line executable and
+                variable substitution but NO file separator fixing for the command line
+                arguments.
+            </action>
+            <action dev="sgoeschl" type="add">
+                Added convinience method to add two parameters to the CommandLine
+                using one method invocation.
+            </action>
+            <action dev="sgoeschl" type="fix">
+                Implemented better regression test for OpenVMS affecting also
+                the Executor and CommandLauncher interface.
+            </action>
+            <action dev="sebb" type="add">
+                Added test scripts for OpenVMS - he seems to be the last human
+                having access to an OpenVMS box ... :-)
+            </action>
+            <action dev="sgoeschl" type="add" due-to="Simone Gianni,Bindul Bhowmik,Niall
Pemberton,Sebastian Bazley">
+                With the help of the Apache Commons community I added the first results
+                of cross-OS testing.
+            </action>
+            <action dev="sgoeschl" type="add">
+                The regression tests now also works on Windows - so it should
+                work now on Linux, Windows and Mac OS X
+            </action>
+            <action dev="sgoeschl" type="add">
+                Added DebugUtils to improve cross-platform testing.
+            </action>
+            <action dev="sgoeschl" type="remove">
+                Removed commons-logging integration
+            </action>
+            <action dev="sgoeschl" type="add" issue="SANDBOX-62" due-to="Jeremy Lacoste">
+                Made DefaultExecutor.launch() protected to enable mocking.
+            </action>
+            <action dev="sgoeschl" type="add" issue="SANDBOX-107" due-to="Niklas Gustavsson">
+                Made ProcessDestroyer optional and pluggable when using Executor.
+            </action>
+            <action dev="sgoeschl" type="add">
+                CommandLine can now expand the given command line by a user-suppied
+                map. This allows to execute something like "${JAVA_HOME}/bin/java -jar ${myapp}"
+            </action>
+            <action dev="sgoeschl" type="add" issue="SANDBOX-192" due-to="Reinhold Fuereder">
+                Added methods to provide pre-quoted arguments.
+            </action>
+            <action dev="sgoeschl" type="add" issue="SANDBOX-193" due-to="Reinhold Fuereder">
+                Exposing a ExecuteWatchdog.destroy() to kill an asynchrounous process
+                manually. This formalizes a workaround described in the JIRA
+            </action>
+            <action dev="sgoeschl" type="add" issue="SANDBOX-203">
+                Extending exit value handling to support applications returning an error
+                code.
+            </action>
+            <action dev="sgoeschl" type="fix" issue="SANDBOX-204">
+                Cleaned up the source code to get rid of javadoc errors and
+                unused imports.
+            </action>
+            <action dev="sgoeschl" type="add" issue="SANDBOX-204">
+                Added a few regression tests for the watchdog since they were missing.
+            </action>
+        </release>
+    </body>
 </document>

Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java?rev=951330&r1=951329&r2=951330&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java
(original)
+++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecuteResultHandler.java
Fri Jun  4 08:53:38 2010
@@ -24,6 +24,9 @@ package org.apache.commons.exec;
  */
 public class DefaultExecuteResultHandler implements ExecuteResultHandler {
 
+    /** Keep track if the process is still running */
+    private boolean hasResult;
+
     /** The exit value of the finished process */
     private int exitValue;
 
@@ -33,29 +36,42 @@ public class DefaultExecuteResultHandler
     /**
      * @see org.apache.commons.exec.ExecuteResultHandler#onProcessComplete(int)
      */
-    public void onProcessComplete(int exitValue) {
+    synchronized public void onProcessComplete(int exitValue) {
         this.exitValue = exitValue;
+        this.hasResult = true;
     }
 
     /**
      * @see org.apache.commons.exec.ExecuteResultHandler#onProcessFailed(org.apache.commons.exec.ExecuteException)
      */
-    public void onProcessFailed(ExecuteException e) {
+    synchronized public void onProcessFailed(ExecuteException e) {
         this.exception = e;
         exitValue = e.getExitValue();
+        this.hasResult = true;
     }
 
     /**
      * @return Returns the exception.
      */
-    public ExecuteException getException() {
+    synchronized public ExecuteException getException() {
+        if(!hasResult) throw new IllegalStateException("The process has not exited yet ...");
         return exception;
     }
 
     /**
      * @return Returns the exitValue.
      */
-    public int getExitValue() {
+    synchronized public int getExitValue() {
+        if(!hasResult) throw new IllegalStateException("The process has not exited yet ...");
         return exitValue;
     }
+
+    /**
+     * Has the process exited and a result is available?
+     *
+     * @return true if the process might be still running
+     */
+    synchronized public boolean hasResult() {
+        return hasResult;
+    }
 }
\ No newline at end of file

Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java?rev=951330&r1=951329&r2=951330&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java
(original)
+++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteException.java
Fri Jun  4 08:53:38 2010
@@ -48,7 +48,7 @@ public class ExecuteException extends IO
      * @param exitValue The exit value
      */
     public ExecuteException(final String message, int exitValue) {
-        super(message + "(Exit value: " + exitValue + ")");
+        super(message + " (Exit value: " + exitValue + ")");
         this.cause = null;
         this.exitValue = exitValue;
     }

Modified: commons/proper/exec/trunk/src/site/apt/tutorial.apt
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/site/apt/tutorial.apt?rev=951330&r1=951329&r2=951330&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/site/apt/tutorial.apt (original)
+++ commons/proper/exec/trunk/src/site/apt/tutorial.apt Fri Jun  4 08:53:38 2010
@@ -69,7 +69,7 @@ int exitValue = executor.execute(command
   You happily printed for a while but now your application blocks - your printing subprocess
   hangs for some obvious or not so obvious reason. Starting is easy but what to do with a
run-away
   Acrobat Reader?! Luckily commons-exec provides a watchdog which does the work for you.
-  Here is the improved code
+  Here is the improved code which kills a run-away process after sixty seconds.
 
 +----------------------------------------------------------------------------
 String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
@@ -85,8 +85,9 @@ int exitValue = executor.execute(command
 
   Well, the code worked for quite a while until a new customer complained that
   no documents are printed. It took half a day to find out that the following file
-  'C:\Document And Settings\documents\432432.pdf' could not be printed. Due to the
-  spaces and without further quoting the command line fell literally apart.
+  'C:\\Document And Settings\\documents\\432432.pdf' could not be printed. Due to the
+  spaces and without further quoting the command line fell literally apart into
+  the following snippet
 
 +----------------------------------------------------------------------------
 > AcroRd32.exe /p /h C:\Document And Settings\documents\432432.pdf
@@ -105,7 +106,7 @@ ExecuteWatchdog watchdog = new ExecuteWa
 executor.setWatchdog(watchdog);
 int exitValue = executor.execute(commandLine);
 +----------------------------------------------------------------------------
-  
+
 * Build the Command Line Incrementally
 
   The previous problem stems from the fact that commons-exec tried to split
@@ -138,9 +139,9 @@ int exitValue = executor.execute(command
 
 +----------------------------------------------------------------------------
 CommandLine commandLine;
-HashMap params = new HashMap();
-params.put("file", "C:\Document And Settings\documents\432432.pdf");
-commandLine  = CommandLine.parse("AcroRd32.exe /p /h \"${file}\"", params);
+HashMap map = new HashMap();
+map.put("file", "C:\Document And Settings\documents\432432.pdf");
+commandLine  = CommandLine.parse("AcroRd32.exe /p /h \"${file}\"", map);
 
 // Or you can use expansion with individual parameters:
 
@@ -148,8 +149,44 @@ CommandLine commandLine = CommandLine.pa
 commandLine.addArgument("/p");
 commandLine.addArgument("/h");
 commandLine.addArgument("${file}");
-HashMap params = new HashMap();
-params.put("file", "C:\Document And Settings\documents\432432.pdf");
-commandLine.setSubstitutionMap(params);
+HashMap map = new HashMap();
+map.put("file", "C:\Document And Settings\documents\432432.pdf");
+commandLine.setSubstitutionMap(map);
++----------------------------------------------------------------------------
+
+* Unblock Your Execution
+
+  Up to now we have a working example but it would not be good enough for
+  production - because it is blocking.
+
+  At the one hand your print process might block when it tries to write to
+  'stdout' or 'stderr'. Avoiding this problem is done by setting a pump stream
+  handler which pumps the output from the print process to your own process.
+
+  On the other hand your own worker thread will block until the print process
+  has exited. Therefore executing the print job asynchronously will do the trick.
+  In this example we create an instance of 'ExecuteResultHandler' and pass it
+  to the 'Executor' instance in order to execute the process asynchronously.
+  The 'resultHandler' picks up any offending exception or the process exit
+  code.
+
++----------------------------------------------------------------------------
+CommandLine commandLine = CommandLine.parse(this.acroRd32Script.getAbsolutePath());
+commandLine.addArgument("/p");
+commandLine.addArgument("/h");
+commandLine.addArgument("${file}");
+HashMap map = new HashMap();
+map.put("file", "./pom.xml");
+commandLine.setSubstitutionMap(map);
+
+DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
+
+ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout1);
+
+Executor executor = new DefaultExecutor();
+executor.setExitValue(1);
+executor.setStreamHandler(new PumpStreamHandler());
+executor.setWatchdog(watchdog);
+executor.execute(commandLine, resultHandler);
 +----------------------------------------------------------------------------
-  
\ No newline at end of file
+  

Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java?rev=951330&r1=951329&r2=951330&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
(original)
+++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
Fri Jun  4 08:53:38 2010
@@ -40,6 +40,8 @@ public class DefaultExecutorTest extends
     private File redirectScript = TestUtil.resolveScriptForOS(testDir + "/redirect");
     private File exec41Script = TestUtil.resolveScriptForOS(testDir + "/exec41");
     private File printArgsScript = TestUtil.resolveScriptForOS(testDir + "/printargs");
+    private File acroRd32Script = TestUtil.resolveScriptForOS(testDir + "/acrord32");
+
 
     // Get suitable exit codes for the OS
     private static final int SUCCESS_STATUS; // test script successful exit code
@@ -550,4 +552,76 @@ public class DefaultExecutorTest extends
         assertTrue("The watchdog has killed the process", watchdog.killedProcess());
         assertFalse("The watchdog is no longer watching any process", watchdog.isWatching());
     }
+
+
+    /**
+     * Runs the final tutorial example. The sample demonstrates the following
+     * features of commons-exec
+     * <ul>
+     *  <li>creating a command line with parameter substitution
+     *  <li>use a watchdog to kill a run-away process after a certain timeout
+     *  <li>use a 'resultHandler' to execute the process asynchronously
+     *  <li>check if the process cleanly exited or was killed by the watchdog
+     * </ul>
+     *
+     * @throws Exception the test failed
+     */
+
+    public void testTutorialExample() throws Exception {
+
+        final long timeout1 = 5*1000;
+        final long timeout2 = 8*1000;
+
+        // build up the command line
+        CommandLine commandLine = CommandLine.parse(this.acroRd32Script.getAbsolutePath());
+        commandLine.addArgument("/p");
+        commandLine.addArgument("/h");
+        commandLine.addArgument("${file}");
+        HashMap map = new HashMap();
+        map.put("file", "./pom.xml");
+        commandLine.setSubstitutionMap(map);
+
+        // asynchronous execution is defined by using a 'resultHander'
+        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler() {
+            public void onProcessComplete(int exitValue) {
+                super.onProcessComplete(exitValue);
+                System.out.println("[resultHandler] The print process has exited with the
following value: " + exitValue);
+            }
+            public void onProcessFailed(ExecuteException e) {
+                super.onProcessFailed(e);
+                System.err.println("[resultHandler] The print process was not successfully
executed due to : " + e.getMessage());
+            }
+        };
+
+        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout1);
+
+        // execute the asynchronous process and consider '1' as success
+        Executor executor = new DefaultExecutor();
+        executor.setExitValue(1);
+        executor.setStreamHandler(new PumpStreamHandler());
+        executor.setWatchdog(watchdog);
+        executor.execute(commandLine, resultHandler);
+
+        // wait for some time (longer than the timeout for the watchdog to avoid race conditions)
+        Thread.sleep(timeout2);
+ 
+        if(resultHandler.hasResult()) {
+            if(resultHandler.getException() != null) {
+                System.err.println("[application] The document was NOT successfully printed");
+            }
+            else {
+                System.out.println("[application] The document was successfully printed");
+            }
+        }
+        else {
+            if(watchdog.killedProcess()) {
+                System.err.println("[application] The print process was killed by the watchdog");
+            }
+            else {
+                System.err.println("[application] The print process is still running");
+            }
+        }
+
+
+    }
 }



Mime
View raw message