Skip to content

The Unified and Autonomous Development Platform

Add commandline utility.

robin shine7a4fbe6a95b6
diff --git a/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/Bootstrap.java b/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/Bootstrap.java
index 86768e5c45..9eebc81cfa 100644
--- a/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/Bootstrap.java
+++ b/commons.bootstrap/src/main/java/com/pmease/commons/bootstrap/Bootstrap.java
@@ -246,6 +246,10 @@ public class Bootstrap {
SLF4JBridgeHandler.install();
}
+ public static File getBinDir() {
+ return new File(installDir, "bin");
+ }
+
public static File getLibDir() {
return new File(installDir, "lib");
}
diff --git a/commons.util/pom.xml b/commons.util/pom.xml
index 039d77818b..c2c611373a 100644
--- a/commons.util/pom.xml
+++ b/commons.util/pom.xml
@@ -81,6 +81,10 @@
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.ant</groupId>
+ <artifactId>ant</artifactId>
+ </dependency>
</dependencies>
<repositories>
<repository>
diff --git a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
index b23154f5dc..35efc44d82 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/FileUtils.java
@@ -1,14 +1,16 @@
package com.pmease.commons.util;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map.Entry;
+import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
-import java.util.Properties;
import org.apache.commons.io.IOUtils;
@@ -111,4 +113,13 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
return null;
}
}
+
+ /**
+ * Get default file encoding of underlying OS
+ * @return
+ */
+ public static String getDefaultEncoding() {
+ return new InputStreamReader(new ByteArrayInputStream(new byte[0])).getEncoding();
+ }
+
}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/StringUtils.java b/commons.util/src/main/java/com/pmease/commons/util/StringUtils.java
index 2ead21cbad..1316afc746 100644
--- a/commons.util/src/main/java/com/pmease/commons/util/StringUtils.java
+++ b/commons.util/src/main/java/com/pmease/commons/util/StringUtils.java
@@ -1,8 +1,13 @@
package com.pmease.commons.util;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StreamTokenizer;
+import java.io.StringReader;
import java.security.spec.KeySpec;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
@@ -68,4 +73,48 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
return fields;
}
+ /**
+ * Parse specified string into tokens. Content surrounded with &quot; character
+ * is considered as a single token. For example: echo "hello world" will be parsed
+ * into two tokens, respectively [echo], and [hello world]. The quote character
+ * itself can be quoted and escaped in order to return as ordinary character. For
+ * example: echo "hello \" world" will be parsed into two tokens: [echo] and
+ * [hello " world].
+ * @param string
+ * @return
+ */
+ public static String[] parseQuoteTokens(String string) {
+ List<String> commandTokens = new ArrayList<String>();
+
+ StreamTokenizer st = new StreamTokenizer(
+ new BufferedReader(new StringReader(string)));
+ st.resetSyntax();
+ st.wordChars(0, 255);
+ st.ordinaryChar(' ');
+ st.ordinaryChar('\n');
+ st.ordinaryChar('\t');
+ st.ordinaryChar('\r');
+ st.quoteChar('"');
+
+ try {
+ String token = null;
+ while (st.nextToken() != StreamTokenizer.TT_EOF) {
+ if (st.ttype == '"' || st.ttype == StreamTokenizer.TT_WORD) {
+ if (token == null)
+ token = st.sval;
+ else
+ token += st.sval;
+ } else if (token != null) {
+ commandTokens.add(token);
+ token = null;
+ }
+ }
+ if (token != null)
+ commandTokens.add(token);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ return commandTokens.toArray(new String[commandTokens.size()]);
+ }
}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java b/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java
new file mode 100644
index 0000000000..7f4971b355
--- /dev/null
+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/Commandline.java
@@ -0,0 +1,397 @@
+package com.pmease.commons.util.execution;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import org.apache.tools.ant.types.Environment;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.pmease.commons.util.StringUtils;
+
+public class Commandline {
+
+ static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool();
+
+ private static final int MAX_COMMAND_LENGTH = 1024;
+
+ private static final Logger logger = LoggerFactory.getLogger(Commandline.class);
+
+ private List<Argument> arguments = new ArrayList<Argument>();
+
+ private String executable = null;
+
+ public Commandline(String command) {
+ if (command != null) {
+ String[] parts = StringUtils.parseQuoteTokens(command);
+ if (parts != null && parts.length > 0) {
+ setExecutable(parts[0]);
+ for (int i = 1; i < parts.length; i++)
+ addArgValue(parts[i]);
+ }
+ }
+ }
+
+ public Argument createArgument() {
+ return createArgument(false);
+ }
+
+ public Commandline addArgValue(String argValue) {
+ createArgument().setValue(argValue);
+ return this;
+ }
+
+ public Commandline addArgLine(String argLine) {
+ createArgument().setLine(argLine);
+ return this;
+ }
+
+ public Argument createArgument(boolean insertAtStart) {
+ Argument argument = new Argument();
+ if (insertAtStart) {
+ arguments.add(0, argument);
+ } else {
+ arguments.add(argument);
+ }
+ return argument;
+ }
+
+ public Commandline setExecutable(String executable) {
+ if (executable == null || executable.length() == 0)
+ return this;
+ this.executable = executable.replace('/', File.separatorChar).replace('\\', File.separatorChar);
+
+ return this;
+ }
+
+ public String getExecutable() {
+ return executable;
+ }
+
+ public Commandline addArguments(String[] argumentValues) {
+ for (int i = 0; i < argumentValues.length; i++) {
+ createArgument().setValue(argumentValues[i]);
+ }
+ return this;
+ }
+
+ public String[] getCommandParts() {
+ List<String> parts = new LinkedList<String>();
+ if (executable != null)
+ parts.add(executable);
+ parts.addAll(Arrays.asList(getArgumentParts()));
+ return parts.toArray(new String[parts.size()]);
+ }
+
+ public boolean willOverflow(String argValue) {
+ int length = 0;
+ if (executable != null) {
+ // executable length plus trailing space
+ length = executable.length() + 1;
+ // if contains whitespaces, surrounding quote will be used
+ if (executable.indexOf(' ') != -1 || executable.indexOf('\t') != -1)
+ length += 2;
+ }
+ for (int i = 0; i < arguments.size(); i++) {
+ Argument arg = arguments.get(i);
+ String[] s = arg.getParts();
+ if (s != null) {
+ for (int j = 0; j < s.length; j++) {
+ length += s[j].length() + 1;
+ if (s[j].indexOf(' ') != -1 || s[j].indexOf('\t') != -1)
+ length += 2;
+ }
+ }
+ }
+ length += argValue.length();
+ if (argValue.indexOf(' ') != -1 || argValue.indexOf('\t') != -1)
+ length += 2;
+
+ return length > MAX_COMMAND_LENGTH;
+ }
+
+ public String[] getArgumentParts() {
+ List<String> parts = new ArrayList<String>(arguments.size() * 2);
+ for (int i = 0; i < arguments.size(); i++) {
+ Argument arg = arguments.get(i);
+ String[] s = arg.getParts();
+ if (s != null) {
+ for (int j = 0; j < s.length; j++) {
+ parts.add(s[j]);
+ }
+ }
+ }
+ return parts.toArray(new String[parts.size()]);
+ }
+
+ public String toString() {
+ return describe();
+ }
+
+ public Commandline clear() {
+ executable = null;
+ arguments.clear();
+ return this;
+ }
+
+ public Commandline clearArgs() {
+ arguments.clear();
+ return this;
+ }
+
+ public String describe() {
+ String[] args = getCommandParts();
+ if (args == null || args.length == 0) {
+ return "";
+ }
+ StringBuffer buf = new StringBuffer();
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].contains(" ") || args[i].contains("\t")) {
+ buf.append("\"").append(StringUtils.replace(
+ args[i], "\n", "\\n")).append("\"").append(" ");
+ } else {
+ buf.append(StringUtils.replace(
+ args[i], "\n", "\\n")).append(" ");
+ }
+ }
+ return buf.toString();
+ }
+
+ public ExecuteResult execute(File workingDir, Environment execEnv, OutputStream stdoutConsumer,
+ LineConsumer stderrConsumer) {
+ return execute(workingDir, execEnv, stdoutConsumer, stderrConsumer, null);
+ }
+
+ public ExecuteResult execute(File workingDir, OutputStream stdoutConsumer, LineConsumer stderrConsumer) {
+ return execute(workingDir, null, stdoutConsumer, stderrConsumer, null);
+ }
+
+ public ExecuteResult execute(OutputStream stdoutConsumer, LineConsumer stderrConsumer) {
+ return execute(null, null, stdoutConsumer, stderrConsumer, null);
+ }
+
+ public ExecuteResult execute(Environment execEnv, OutputStream stdoutConsumer, LineConsumer stderrConsumer) {
+ return execute(null, execEnv, stdoutConsumer, stderrConsumer, null);
+ }
+
+ public ExecuteResult execute(OutputStream stdoutConsumer, LineConsumer stderrConsumer, String stdinString) {
+ return execute(null, null, stdoutConsumer, stderrConsumer, stdinString);
+ }
+
+ public ExecuteResult execute(Environment execEnv, OutputStream stdoutConsumer, LineConsumer stderrConsumer,
+ String stdinString) {
+ return execute(null, execEnv, stdoutConsumer, stderrConsumer, stdinString);
+ }
+
+ private ProcessBuilder getProcessBuilder(File workingDir, Environment execEnv) {
+ if (workingDir == null)
+ workingDir = com.pmease.commons.bootstrap.Bootstrap.getBinDir();
+ String[] cmdParts = getCommandParts();
+ if (!new File(cmdParts[0]).isAbsolute()) {
+ if (new File(workingDir, cmdParts[0]).isFile())
+ cmdParts[0] = new File(workingDir, cmdParts[0]).getAbsolutePath();
+ else if (new File(workingDir, cmdParts[0] + ".exe").isFile())
+ cmdParts[0] = new File(workingDir, cmdParts[0] + ".exe").getAbsolutePath();
+ else if (new File(workingDir, cmdParts[0] + ".bat").isFile())
+ cmdParts[0] = new File(workingDir, cmdParts[0] + ".bat").getAbsolutePath();
+ }
+
+ ProcessBuilder processBuilder = new ProcessBuilder(cmdParts);
+ processBuilder.directory(workingDir);
+
+ if (execEnv != null) {
+ Map<String, String> procEnv = processBuilder.environment();
+ for (Environment.Variable var: (Vector<Environment.Variable>)execEnv.getVariablesVector()) {
+ if (var.getKey() != null && var.getValue() != null)
+ procEnv.put(var.getKey(), var.getValue());
+ }
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("Executing command: " + describe());
+ logger.debug("Command working directory: " +
+ processBuilder.directory().getAbsolutePath());
+ StringBuffer buffer = new StringBuffer();
+ for (Map.Entry<String, String> entry: processBuilder.environment().entrySet())
+ buffer.append(" " + entry.getKey() + "=" + entry.getValue() + "\n");
+ logger.trace("Command execution environments:\n" +
+ StringUtils.stripEnd(buffer.toString(), "\n"));
+ }
+
+ return processBuilder;
+ }
+
+ public ExecuteResult execute(File workingDir, Environment execEnv, OutputStream stdoutConsumer,
+ final LineConsumer stderrConsumer, String stdinString) {
+ Process process;
+ try {
+ ProcessBuilder processBuilder = getProcessBuilder(workingDir, execEnv);
+ process = processBuilder.redirectErrorStream(stderrConsumer == null).start();
+ } catch (IOException e) {
+ throw new RuntimeException(e.getMessage());
+ }
+
+ ByteArrayInputStream inputStream = null;
+ if (stdinString != null && stdinString.length() != 0)
+ inputStream = new ByteArrayInputStream(stdinString.getBytes());
+
+ final StringBuffer errorMessage = new StringBuffer();
+ OutputStream errorMessageCollector = null;
+ if (stderrConsumer != null) {
+ errorMessageCollector = new LineConsumer(stderrConsumer.getEncoding()) {
+
+ @Override
+ public void consume(String line) {
+ if (errorMessage.length() != 0)
+ errorMessage.append("\n");
+ errorMessage.append(line);
+ stderrConsumer.consume(line);
+ }
+
+ };
+ }
+
+ ProcessStreamPumper streamPumper = new ProcessStreamPumper(process, stdoutConsumer,
+ errorMessageCollector, inputStream);
+ streamPumper.start();
+
+ ExecuteResult result = new ExecuteResult();
+ try {
+ while (true) {
+ try {
+ result.setReturnCode(process.waitFor());
+ break;
+ } catch (InterruptedException e) {
+ process.destroy();
+ throw new RuntimeException(e);
+ }
+ }
+ } finally {
+ streamPumper.join();
+ Thread.interrupted();
+ }
+ if (errorMessage.length() != 0)
+ result.setErrorMessage(errorMessage.toString());
+
+ return result;
+ }
+
+ public void executeWithoutWait() {
+ executeWithoutWait(null, null);
+ }
+
+ public void executeWithoutWait(File workingDir) {
+ executeWithoutWait(workingDir, null);
+ }
+
+ public void executeWithoutWait(File workingDir, Environment execEnv) {
+ executeWithoutWait(workingDir, execEnv, null);
+ }
+
+ public void executeWithoutWait(Environment execEnv) {
+ executeWithoutWait(null, execEnv);
+ }
+
+ public void executeWithoutWait(File workingDir, Environment execEnv, String stdinString) {
+ ByteArrayInputStream stdinStream = null;
+ if (stdinString != null && stdinString.length() != 0)
+ stdinStream = new ByteArrayInputStream(stdinString.getBytes());
+
+ final Process process;
+ try {
+ process = getProcessBuilder(workingDir, execEnv).start();
+ new ProcessStreamPumper(process, null, null, stdinStream).start();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+
+ EXECUTOR_SERVICE.submit(new Runnable() {
+
+ @Override
+ public void run() {
+ try {
+ process.waitFor();
+ } catch (InterruptedException e) {
+ }
+ }
+
+ });
+
+ }
+
+ public class ExecuteResult {
+
+ private int returnCode;
+
+ private String errorMessage;
+
+ public int getReturnCode() {
+ return returnCode;
+ }
+
+ public void setReturnCode(int returnCode) {
+ this.returnCode = returnCode;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ /**
+ * Build an exception object with command description, stderr output and return code.
+ * @return
+ */
+ public RuntimeException buildException() {
+ if (errorMessage != null) {
+ throw new RuntimeException("Failed to run command: " + describe() +
+ "\nCommand return code: " + getReturnCode() +
+ "\nCommand error output: " + errorMessage);
+ } else {
+ throw new RuntimeException("Failed to run command: " + describe() +
+ "\nCommand return code: " + getReturnCode());
+ }
+
+ }
+
+ /**
+ * Check return code and throw exception if it does not equal to 0.
+ */
+ public void checkReturnCode() {
+ if (getReturnCode() != 0)
+ throw buildException();
+ }
+ }
+
+ public static class Argument {
+
+ private String[] parts;
+
+ public void setValue(String value) {
+ parts = new String[] {value};
+ }
+
+ public void setLine(String line) {
+ parts = StringUtils.parseQuoteTokens(line);
+ }
+
+ public String[] getParts() {
+ return parts;
+ }
+
+ }
+
+}
diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java b/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java
new file mode 100644
index 0000000000..871d034c7c
--- /dev/null
+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/LineConsumer.java
@@ -0,0 +1,214 @@
+/*
+ * Copyright 2003-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package com.pmease.commons.util.execution;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * heavily inspired from LogOutputStream
+ * this stream class calls back the P4Handler on each line of stdout or stderr read
+ * @author : <a href="mailto:[email protected]">Antoine Levy-Lambert</a>
+ *
+ * Modified by <a href="mailto:[email protected]">robin shine</a> to seperate
+ * output and error handling
+ *
+ */
+public abstract class LineConsumer extends OutputStream {
+ private static final Logger logger = LoggerFactory.getLogger(LineConsumer.class);
+
+ /**
+ * If this property is not empty, output will be re-directed to this stream
+ * instead of the ant log stream
+ */
+ private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+
+ private boolean skip = false;
+
+ private String encoding;
+
+ public LineConsumer(String encoding) {
+ this.encoding = encoding;
+ }
+
+ public LineConsumer() {
+
+ }
+
+ public String getEncoding() {
+ return encoding;
+ }
+
+ /**
+ * Write the data to the buffer and flush the buffer, if a line
+ * separator is detected.
+ *
+ * @param cc data to log (byte).
+ * @throws IOException IOException if an I/O error occurs. In particular,
+ * an <code>IOException</code> may be thrown if the
+ * output stream has been closed.
+ */
+ @Override
+ public void write(int cc) throws IOException {
+ final byte c = (byte) cc;
+ if ((c == '\n') || (c == '\r')) {
+ if (!skip) {
+ processBuffer();
+ }
+ } else {
+ buffer.write(cc);
+ }
+ skip = (c == '\r');
+ }
+
+ /**
+ * Converts the buffer to a string and sends it to <code>processLine</code>
+ */
+ protected void processBuffer() {
+ try {
+ consume(encoding!=null?buffer.toString(encoding):buffer.toString());
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ } finally {
+ buffer.reset();
+ }
+ }
+
+ /**
+ * Sub class must implement this method to handle a line of output
+ * @param line
+ */
+ public abstract void consume(String line);
+
+ @Override
+ public void flush() throws IOException {
+ if (buffer.size() > 0) {
+ processBuffer();
+ }
+ super.flush();
+ }
+
+ /**
+ * Writes all remaining
+ * @throws IOException if an I/O error occurs.
+ */
+ @Override
+ public void close() throws IOException {
+ flush();
+ super.close();
+ }
+
+ public static class InfoLogger extends LineConsumer {
+
+ public InfoLogger(String encoding) {
+ super(encoding);
+ }
+
+ public InfoLogger() {
+ }
+
+ @Override
+ public void consume(String line) {
+ logger.info(line);
+ }
+
+ }
+
+ public static class DebugLogger extends LineConsumer {
+
+ public DebugLogger(String encoding) {
+ super(encoding);
+ }
+
+ public DebugLogger() {
+
+ }
+
+ @Override
+ public void consume(String line) {
+ logger.debug(line);
+ }
+
+ }
+
+ public static class TraceLogger extends LineConsumer {
+
+ public TraceLogger(String encoding) {
+ super(encoding);
+ }
+
+ public TraceLogger() {
+
+ }
+
+ @Override
+ public void consume(String line) {
+ logger.trace(line);
+ }
+
+ }
+
+ public static class WarnLogger extends LineConsumer {
+
+ public WarnLogger(String encoding) {
+ super(encoding);
+ }
+
+ public WarnLogger() {
+
+ }
+
+ @Override
+ public void consume(String line) {
+ logger.warn(line);
+ }
+
+ }
+
+ public static class ErrorLogger extends LineConsumer {
+
+ public ErrorLogger(String encoding) {
+ super(encoding);
+ }
+
+ public ErrorLogger() {
+
+ }
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }
+
+ public static class NoOp extends LineConsumer {
+
+ @Override
+ public void consume(String line) {
+ }
+
+ }
+}
+
+
diff --git a/commons.util/src/main/java/com/pmease/commons/util/execution/ProcessStreamPumper.java b/commons.util/src/main/java/com/pmease/commons/util/execution/ProcessStreamPumper.java
new file mode 100644
index 0000000000..96705a8f83
--- /dev/null
+++ b/commons.util/src/main/java/com/pmease/commons/util/execution/ProcessStreamPumper.java
@@ -0,0 +1,105 @@
+package com.pmease.commons.util.execution;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.concurrent.Future;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class ProcessStreamPumper {
+
+ private static final int BUFFER_SIZE = 64000;
+
+ private static final Logger logger = LoggerFactory.getLogger(ProcessStreamPumper.class);
+
+ private Future<?> stdoutPumper;
+
+ private Future<?> stderrPumper;
+
+ private Future<?> stdinPumper;
+
+ private OutputStream stdoutStream;
+
+ private OutputStream stderrStream;
+
+ private InputStream stdinStream;
+
+ private Process process;
+
+ /**
+ * @param process not null
+ * @param stdoutStream might be null
+ * @param stderrStream might be null
+ * @param stdinStream might be null
+ */
+ public ProcessStreamPumper(Process process, OutputStream stdoutStream, OutputStream stderrStream,
+ InputStream stdinStream) {
+ this.process = process;
+
+ this.stdoutStream = stdoutStream;
+ this.stderrStream = stderrStream;
+ this.stdinStream = stdinStream;
+ }
+
+ public void start() {
+ stdoutPumper = createPump(process.getInputStream(), stdoutStream, false);
+ stderrPumper = createPump(process.getErrorStream(), stderrStream, false);
+
+ if (stdinStream != null)
+ stdinPumper = createPump(stdinStream, process.getOutputStream(), true);
+ }
+
+ public void join() {
+ while (!stdoutPumper.isDone() || !stderrPumper.isDone() ||
+ (stdinPumper != null && !stdinPumper.isDone())) {
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ }
+ }
+ try {
+ if (stderrStream != null)
+ stderrStream.flush();
+ } catch (IOException e) {
+ }
+ try {
+ if (stdoutStream != null)
+ stdoutStream.flush();
+ } catch (IOException e) {
+ }
+ }
+
+ private Future<?> createPump(final InputStream input, final OutputStream output,
+ final boolean closeWhenExhausted) {
+
+ return Commandline.EXECUTOR_SERVICE.submit(new Runnable() {
+
+ public void run() {
+ byte[] buf = new byte[BUFFER_SIZE];
+
+ try {
+ int length;
+ while ((length = input.read(buf)) > 0) {
+ if (output != null)
+ output.write(buf, 0, length);
+ }
+ } catch (Exception e) {
+ logger.error("Error pumping stream.", e);
+ process.destroy();
+ } finally {
+ if (closeWhenExhausted && output!=null) {
+ try {
+ output.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ }
+
+ });
+ }
+
+}
diff --git a/gitop/src/main/java/com/pmease/gitop/Product.java b/gitop/src/main/java/com/pmease/gitop/Product.java
index 6018950dbf..979bad8e79 100644
--- a/gitop/src/main/java/com/pmease/gitop/Product.java
+++ b/gitop/src/main/java/com/pmease/gitop/Product.java
@@ -27,6 +27,8 @@ import com.pmease.commons.loader.AbstractPlugin;
import com.pmease.commons.persistence.dao.GeneralDao;
import com.pmease.commons.util.FileUtils;
import com.pmease.commons.util.StringUtils;
+import com.pmease.commons.util.execution.Commandline;
+import com.pmease.commons.util.execution.LineConsumer;
import com.pmease.gitop.core.model.User;
public class Product extends AbstractPlugin {
@@ -126,6 +128,7 @@ public class Product extends AbstractPlugin {
generalDaoProvider.get().save(user);
}
+ new Commandline("cmd /c dir w:\\").execute(new LineConsumer.InfoLogger(), new LineConsumer.ErrorLogger());
logger.info(PRODUCT_NAME + " has been started successfully.");
}
diff --git a/parent.general/pom.xml b/parent.general/pom.xml
index 7d70ff8d1a..e4861b5ab7 100644
--- a/parent.general/pom.xml
+++ b/parent.general/pom.xml
@@ -312,6 +312,11 @@
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.ant</groupId>
+ <artifactId>ant</artifactId>
+ <version>1.9.1</version>
+ </dependency>
</dependencies>
</dependencyManagement>
<dependencies>