아키텍처와 함께

블로그 이미지
by gregorio
  • Total hit
  • Today hit
  • Yesterday hit

Java에서 Shell 호출 시 apache commons-exec를 사용하면 쉽게 shell을 호출하고 shell에 파라미터 전달도 가능하다.


■ 소스

import java.io.ByteArrayOutputStream;

import java.util.HashMap;

import java.util.Map;


import org.apache.commons.exec.CommandLine;

import org.apache.commons.exec.DefaultExecuteResultHandler;

import org.apache.commons.exec.DefaultExecutor;

import org.apache.commons.exec.ExecuteStreamHandler;

import org.apache.commons.exec.ExecuteWatchdog;

import org.apache.commons.exec.PumpStreamHandler;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;


import ncd.spring.common.util.PropertyUtil;


@Controller

public class DeployController {


private static final Logger LOGGER = LoggerFactory.getLogger(DeployController.class);

@RequestMapping(value = "/admin/deploy.do")

public ModelAndView sourceDeploy(@RequestParam("type") String type) throws Exception {

LOGGER.info("Start Source Deploy....");

final Map<String, Object> resultMap = new HashMap<String, Object>();


String scriptFile  = PropertyUtil.getString("deploy.script.file");

String confFile = null;

if ("oz".equals(type)) {

confFile = PropertyUtil.getString("oz.deploy.config.file");

}

else {

confFile = PropertyUtil.getString("was.deploy.config.file");

}

if (scriptFile == null || confFile == null) {

resultMap.put("error", "script file or config file is null");

return new ModelAndView("ajaxMultiDataView", resultMap);

}

LOGGER.info("{},{}", scriptFile, confFile);

/** Initiate Executor **/

DefaultExecutor executor = new DefaultExecutor();

ExecuteWatchdog watchDog = new ExecuteWatchdog(60*1000);

executor.setWatchdog(watchDog);

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ExecuteStreamHandler stream = new PumpStreamHandler(bos, bos, null);

executor.setStreamHandler(stream);

/** Initiate Command Line **/

CommandLine cmdLine = new CommandLine(PropertyUtil.getString("deploy.script.command"));

/** Set parameter to command line **/

cmdLine.addArgument(scriptFile);

cmdLine.addArgument(confFile);

cmdLine.addArgument(type);

LOGGER.info("Execute Command :: ", cmdLine.toString());

/** Execute shell **/

try {

executor.execute(cmdLine, resultHandler);

resultHandler.waitFor();

int exitCode = resultHandler.getExitValue();

String result = bos.toString();

LOGGER.info("Result :: ", result);

resultMap.put("result", result);

resultMap.put("exitCode", exitCode);

}

catch(Exception ex) {

LOGGER.error(ex.getMessage());

}

finally {

bos.close();

}

ModelAndView mav = new ModelAndView("ajaxMultiDataView", resultMap);

return mav;

}



Shell의 수행결과를 받기 위해서는 PumpStreamHandeler를 생성하고, executer에 StreamHanlder에 Set한다.



'Java' 카테고리의 다른 글

Java Convert Map to XML 예제  (0) 2018.07.26
JVM GC 설정  (0) 2018.06.07
Java Reflection 사용법  (0) 2018.04.12
JSP의 img, script에 jsessionid가 추가되는 경우  (0) 2018.02.22
Html a tag function call  (0) 2018.02.20
AND

ARTICLE CATEGORY

분류 전체보기 (56)
Spring Framrwork (33)
Linux (1)
APM (1)
Java (8)
python (0)
ant (1)
chart (1)
OS (1)
tomcat (1)
apache (1)
database (0)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2024/10   »
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

ARCHIVE

LINK