package utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import model.ProcessModel;
/**
* 工具类:调用linux命令、脚本等
* @author penggj
*
*/
public class ProcessUtil {
private final Logger log = Logger.getLogger(ProcessUtil.class);
/**
* 采用静态内部类实现单例模式,线程安全
* @author penggj
*
*/
private static class SingletonHolder {
private static final ProcessUtil INSTANCE = new ProcessUtil();
}
/**
* 获取类的实例(通过静态内部类)
* @return
*/
public static final ProcessUtil getInstance() {
return SingletonHolder.INSTANCE;
}
/**
* 调用linux命令、脚本等
* @param command
* @return
*/
public ProcessModel excuteCmd(String[] command) {
ProcessModel model = new ProcessModel();
model.setWaitFor(1);
model.setExitValue(1);
try {
Process process = Runtime.getRuntime().exec(command);
model.setWaitFor(process.waitFor());
log.info("<<<<<< waitFor <<<<<" + model.getWaitFor());
// System.out.println("waitFor: " +
// String.valueOf(model.getWaitFor()));
model.setExitValue(process.exitValue());
// System.out.println("exitValue: " +
// String.valueOf(model.getExitValue()));
byte[] b = new byte[1024];
int readbytes = -1;
StringBuilder sb = new StringBuilder();
StringBuilder eb = new StringBuilder();
// 读取进程输出值
// 在JAVA IO中,输入输出是针对JVM而言,读写是针对外部数据源而言
InputStream in = process.getInputStream();
try {
while ((readbytes = in.read(b)) != -1) {
sb.append(new String(b, 0, readbytes));
}
// System.out.println("sb: " + sb.toString());
} catch (IOException e1) {
log.error("读取输出流错误", e1);
} finally {
try {
if (in != null)
in.close();
} catch (IOException e2) {
log.error("关闭输出流错误", e2);
}
}
model.setInputString(sb.toString());
// 读取错误输出流
InputStream err = process.getErrorStream();
try {
while ((readbytes = err.read(b)) != -1) {
eb.append(new String(b, 0, readbytes));
}
// System.out.println("eb: " + eb.toString());
} catch (IOException e1) {
log.error("读取错误输出流错误", e1);
} finally {
try {
if (err != null)
err.close();
} catch (IOException e2) {
log.error("关闭错误输出流错误", e2);
}
}
model.setErrorString(eb.toString());
} catch (Exception e) {
log.error("执行命令错误", e);
model.setErrorString(e.getMessage());
}
return model;
}
/**
* 调用linux命令、脚本等
* @param command
* @return
*/
public ProcessModel excuteCmd(String command) {
ProcessModel model = new ProcessModel();
model.setWaitFor(1);
model.setExitValue(1);
try {
Process process = Runtime.getRuntime().exec(command);
model.setWaitFor(process.waitFor());
// System.out.println("waitFor: " +
// String.valueOf(model.getWaitFor()));
model.setExitValue(process.exitValue());
// System.out.println("exitValue: " +
// String.valueOf(model.getExitValue()));
byte[] b = new byte[1024];
int readbytes = -1;
StringBuilder sb = new StringBuilder();
StringBuilder eb = new StringBuilder();
// 读取进程输出值
// 在JAVA IO中,输入输出是针对JVM而言,读写是针对外部数据源而言
InputStream in = process.getInputStream();
try {
while ((readbytes = in.read(b)) != -1) {
sb.append(new String(b, 0, readbytes));
}
// System.out.println("sb: " + sb.toString());
} catch (IOException e1) {
log.error("读取输出流错误", e1);
} finally {
try {
if (in != null)
in.close();
} catch (IOException e2) {
log.error("关闭输出流错误", e2);
}
}
model.setInputString(sb.toString());
// 读取错误输出流
InputStream err = process.getErrorStream();
try {
while ((readbytes = err.read(b)) != -1) {
eb.append(new String(b, 0, readbytes));
}
// System.out.println("eb: " + eb.toString());
} catch (IOException e1) {
log.error("读取错误输出流错误", e1);
} finally {
try {
if (err != null)
err.close();
} catch (IOException e2) {
log.error("关闭错误输出流错误", e2);
}
}
model.setErrorString(eb.toString());
} catch (Exception e) {
log.error("执行命令错误", e);
model.setErrorString(e.getMessage());
}
return model;
}
public Map<String,Object> getFileLineNumByCmd(String filePath) {
Map<String,Object> retMap = new HashMap<String,Object>();
String wcCmd = "wc -l " + filePath;
// String cmd = "awk '{print NR}' "+filePath+" | tail -n1";
ProcessModel result = ProcessUtil.getInstance().excuteCmd(wcCmd);
if (result.getWaitFor() == 0) {
retMap.put("success", 1);
String fileNumStr = result.getInputString().replace(filePath, "");
log.info("inputString:" + fileNumStr);
fileNumStr = fileNumStr.replaceAll("\r|\n", "");
fileNumStr = fileNumStr.replaceAll(" ", "");
log.info("inputString:" + fileNumStr);
int fileNum = Integer.parseInt(fileNumStr);
retMap.put("file_num", fileNum);
retMap.put("msg", "查询文件行数成功");
} else {
retMap.put("success", 0);
retMap.put("msg", result.getErrorString());
}
return retMap;
}
}