/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

//      Contributors:      Xu Lijia 

package ci.xlj.libs.procinvoker;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import ci.xlj.libs.utils.StringUtils;

public class ProcInvoker {

	private Logger logger = Logger.getLogger(ProcInvoker.class);

	public ProcInvoker(String command) {
		this.command = command;
		logger.debug("command: " + command);
	}

	private String command;

	public String getCommand() {
		return command;
	}

	private StringBuilder outputBuilder;

	private Process process;

	private String defaultCharset = "utf-8";

	private String output;
	private String errorMessage;

	public int invoke(String charset) {
		int returnCode = Integer.MIN_VALUE;

		try {
			process = Runtime.getRuntime().exec(command);

			// retrieve output
			InputStreamReader inputStreamReader = new InputStreamReader(
					process.getInputStream(),
					StringUtils.isValid(charset) ? charset : defaultCharset);
			BufferedReader bufferedReader = new BufferedReader(
					inputStreamReader);

			outputBuilder = new StringBuilder();
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				outputBuilder.append(line + "\n");
			}

			bufferedReader.close();
			inputStreamReader.close();

			output = outputBuilder.toString();

			inputStreamReader = new InputStreamReader(process.getErrorStream(),
					StringUtils.isValid(charset) ? charset : defaultCharset);
			bufferedReader = new BufferedReader(inputStreamReader);

			outputBuilder = new StringBuilder();
			while ((line = bufferedReader.readLine()) != null) {
				outputBuilder.append(line + "\n");
			}

			bufferedReader.close();
			inputStreamReader.close();

			errorMessage = outputBuilder.toString();

			returnCode = process.waitFor();
			process.destroy();

		} catch (Exception e) {
			logger.error("Error in invoking command. Details:\n"
					+ StringUtils.getStrackTrace(e));
		}

		return returnCode;
	}

	public int invoke() {
		return invoke(defaultCharset);
	}

	public String getOutput() {
		return output;
	}

	public String getErrorMessage() {
		return errorMessage;
	}
}

@@ -1037,16 +1037,96 @@
 ils;%0D%0A%0D%0A
+/**%0D%0A * Invoke commands and give feedbacks%0D%0A * %0D%0A * @author kfzx-xulj%0D%0A *%0D%0A */%0D%0A
 public c
@@ -1474,24 +1474,66 @@
 process;%0D%0A%0D%0A
+%09/**%0D%0A%09 * Default charset is UTF-8%0D%0A%09 */%0D%0A
 %09private Str
@@ -1619,24 +1619,81 @@
 Message;%0D%0A%0D%0A
+%09/**%0D%0A%09 * @param charset%0D%0A%09 * @return return code%0D%0A%09 */%0D%0A
 %09public int 
@@ -3024,32 +3024,71 @@
 urnCode;%0D%0A%09%7D%0D%0A%0D%0A
+%09/**%0D%0A%09 * Invoke by using UTF-8%0D%0A%09 */%0D%0A
 %09public int invo

add comments