3333import java .io .Reader ;
3434import java .lang .Thread .UncaughtExceptionHandler ;
3535import java .util .Arrays ;
36+ import java .util .HashMap ;
3637import java .util .List ;
3738import java .util .Map ;
3839import java .util .Optional ;
@@ -67,6 +68,7 @@ public class Executor {
6768 private byte [] stdout ;
6869 private byte [] stderr ;
6970 private int timeout ; // in milliseconds, 0 means no timeout
71+ private final Map <String , String > environ ;
7072
7173 /**
7274 * Create a new instance of the Executor.
@@ -88,38 +90,44 @@ public Executor(List<String> cmdList) {
8890 * Create a new instance of the Executor with default command timeout value.
8991 * The timeout value will be based on the running context (indexer or web application).
9092 * @param cmdList A list containing the command to execute
91- * @param workingDirectory The directory the process should have as the
92- * working directory
93+ * @param workingDirectory The directory the process should have as the working directory
9394 */
9495 public Executor (List <String > cmdList , File workingDirectory ) {
95- RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
96- int timeoutSec = env .isIndexer () ? env .getIndexerCommandTimeout () : env .getInteractiveCommandTimeout ();
97-
98- this .cmdList = cmdList ;
99- this .workingDirectory = workingDirectory ;
100- this .timeout = timeoutSec * 1000 ;
96+ this (cmdList , workingDirectory , new HashMap <>());
10197 }
10298
10399 /**
104100 * Create a new instance of the Executor with specific timeout value.
105101 * @param cmdList A list containing the command to execute
106- * @param workingDirectory The directory the process should have as the
107- * working directory
102+ * @param workingDirectory The directory the process should have as the working directory
108103 * @param timeout If the command runs longer than the timeout (seconds),
109104 * it will be terminated. If the value is 0, no timer
110105 * will be set up.
111106 */
112107 public Executor (List <String > cmdList , File workingDirectory , int timeout ) {
108+ this (cmdList , workingDirectory , new HashMap <>());
109+
110+ this .timeout = timeout * 1000 ;
111+ }
112+
113+ /**
114+ * Create a new instance of the Executor with default command timeout value.
115+ * The timeout value will be based on the running context (indexer or web application).
116+ * @param cmdList A list containing the command to execute
117+ * @param workingDirectory The directory the process should have as the working directory
118+ */
119+ public Executor (List <String > cmdList , File workingDirectory , Map <String , String > environ ) {
113120 this .cmdList = cmdList ;
114121 this .workingDirectory = workingDirectory ;
115- this .timeout = timeout * 1000 ;
122+ this .environ = environ ;
123+
124+ setDefaultTimeout ();
116125 }
117126
118127 /**
119128 * Create a new instance of the Executor with or without timeout.
120129 * @param cmdList A list containing the command to execute
121- * @param workingDirectory The directory the process should have as the
122- * working directory
130+ * @param workingDirectory The directory the process should have as the working directory
123131 * @param useTimeout terminate the process after default timeout or not
124132 */
125133 public Executor (List <String > cmdList , File workingDirectory , boolean useTimeout ) {
@@ -129,6 +137,13 @@ public Executor(List<String> cmdList, File workingDirectory, boolean useTimeout)
129137 }
130138 }
131139
140+ private void setDefaultTimeout () {
141+ RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
142+ int timeoutSec = env .isIndexer () ? env .getIndexerCommandTimeout () : env .getInteractiveCommandTimeout ();
143+
144+ this .timeout = timeoutSec * 1000 ;
145+ }
146+
132147 /**
133148 * Execute the command and collect the output. All exceptions will be
134149 * logged.
@@ -180,6 +195,8 @@ public int exec(final boolean reportExceptions, StreamHandler handler) {
180195 .map (File ::toString )
181196 .orElseGet (() -> System .getProperty ("user.dir" ));
182197
198+ processBuilder .environment ().putAll (environ );
199+
183200 String envStr = "" ;
184201 if (LOGGER .isLoggable (Level .FINER )) {
185202 Map <String , String > envMap = processBuilder .environment ();
0 commit comments