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; } }