ShellScriptHelper.java 3.27 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
package jp.agentec.sinaburocast.helper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import jp.agentec.sinaburocast.common.exception.SystemException;
import jp.agentec.sinaburocast.common.io.FileUtil;
import jp.agentec.sinaburocast.common.proc.ProcessUtil;
import jp.agentec.sinaburocast.common.util.PropertyUtil;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;

@Component(instance = InstanceType.SINGLETON)
public class ShellScriptHelper {
	private final Logger logger = Logger.getLogger(getClass());
	private boolean useCygwin = PropertyUtil.getBoolean("USE_CYGWIN");

	/**
	 * シェルを実行し、実行結果を返す。
	 * 標準・エラー出力はログに出力。
	 *
	 * @param scriptKey
	 * @param parameterArray
	 * @return
	 * @throws SystemException
	 */
	public int executeShellScript(String scriptKey, String[] parameterArray) throws SystemException {
		int timeout = PropertyUtil.getInt(scriptKey + ".timeout");
		String loggerName = PropertyUtil.getProperty(scriptKey + ".logger");
		return ProcessUtil.execute(getCommandList(scriptKey, parameterArray), timeout, loggerName);
	}

	/**
	 * シェルを実行し、標準・エラー出力内容を返す。
	 *
	 * @param scriptKey
	 * @param parameterArray
	 * @return
	 * @throws SystemException
	 */
	public String executeShellScriptGetOutput(String scriptKey, String[] parameterArray) throws SystemException {
		return ProcessUtil.execute(getCommandList(scriptKey, parameterArray));
	}

	/**
	 * シェルを実行し、エラー出力内容をStringBufferにセットし結果を返す。
	 *
	 * @param scriptKey
	 * @param parameterArray
	 * @param err
	 * @return
	 * @throws SystemException
	 */
	public int executeShellScriptGetError(String scriptKey, String[] parameterArray, StringBuffer err) throws SystemException {
		int timeout = PropertyUtil.getInt(scriptKey + ".timeout");
		return ProcessUtil.execute(getCommandList(scriptKey, parameterArray), err, timeout);
	}

	/**
	 * シェルを実行し、標準出力、エラー出力内容をStringBufferにセットし結果を返す。
	 *
	 * @param scriptKey
	 * @param parameterArray
	 * @param err
	 * @return
	 * @throws SystemException
	 */
	public int executeShellScriptGetOutput(String scriptKey, String[] parameterArray, StringBuffer std, StringBuffer err) throws SystemException {
		int timeout = PropertyUtil.getInt(scriptKey + ".timeout");
		return ProcessUtil.execute(getCommandList(scriptKey, parameterArray), std, err, timeout);
	}

	private List<String> getCommandList(String scriptKey, String[] parameterArray) throws SystemException {
		String scriptPath = PropertyUtil.getProperty(scriptKey);

		if (!FileUtil.exists(scriptPath)) {
			throw new SystemException("script is not found. scriptPath=" + scriptPath, "E000");
		}
		if (useCygwin) {
			scriptPath = "/cygdrive/c" + scriptPath;
		}

		List<String> commandList = new ArrayList<String>();
		if (useCygwin) {
			commandList.addAll(Arrays.asList("cmd.exe", "/c", PropertyUtil.getProperty("CYGWIN_BASH_PATH")));
		}

		commandList.add(scriptPath);
		CollectionUtils.addAll(commandList, parameterArray);

		return commandList;
	}


}