Commits

Conor MacNeill committed 3d6583c

Make sure the output and input handler complete methods don't about the process handler's complete process

Comments (0)

Files changed (1)

processutils/src/main/java/com/atlassian/utils/process/PluggableProcessHandler.java

 package com.atlassian.utils.process;
 
+import org.apache.log4j.Logger;
+
 import java.io.InputStream;
 import java.io.OutputStream;
 
     private boolean complete;
     private boolean throwOnNonZeroExit = true;
 
+    private static final Logger LOGGER = Logger.getLogger(PluggableProcessHandler.class);
+
     public PluggableProcessHandler() {
     }
 
         inputHandler.process(processInput);
     }
 
-    public void complete(int exitCode, ProcessException exception) {
-        this.exitCode = exitCode;
-        this.exception = exception;
+    public void complete(int code, ProcessException pe) {
+        this.exitCode = code;
+        this.exception = pe;
         if (outputHandler != null) {
             try {
                 outputHandler.complete();
-            } catch (ProcessException e) {
-                if (this.exception == null) {
-                    this.exception = e;
-                }
+            } catch (Throwable e) {
+                setException(e);
             }
         }
         if (errorHandler != null) {
             try {
                 errorHandler.complete();
-            } catch (ProcessException e) {
-                if (this.exception == null) {
-                    this.exception = e;
-                }
+            } catch (Throwable e) {
+                setException(e);
             }
         }
         if (inputHandler != null) {
-            inputHandler.complete();
+            try {
+                inputHandler.complete();
+            } catch (Throwable e) {
+                setException(e);
+            }
         }
 
-        if (exception == null && exitCode != 0 && throwOnNonZeroExit) {
-            this.exception = new ProcessException("Non-zero exit code: " + exitCode, exitCode);
+        if (pe == null && code != 0 && throwOnNonZeroExit) {
+            setException(new ProcessException("Non-zero exit code: " + code, code));
         }
         complete = true;
     }
         return exception;
     }
 
+    public void setException(Throwable e) {
+        if (exception != null) {
+            if (e instanceof ProcessException) {
+                this.exception = (ProcessException) e;
+            } else {
+                this.exception = new ProcessException(e);
+            }
+        } else {
+            LOGGER.debug("Ignore exception as exception for handler is alredy set", e);
+        }
+    }
+
     public String getError() {
         return null;
     }