RPCTarget.java 2.99 KB
package com.sitech.ismp.app.coll;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import org.apache.log4j.Logger;

public class RPCTarget {
	private Logger logger = Logger.getLogger("COLL");
	private Logger error =Logger.getLogger("ERROR");
	
	private String RPCCMD = "";

	Vector<String> recordSet = new Vector<String>();

	public Vector<String> getKPISet(String cmdstr) {
		String cmd = cmdstr;
		return execute(cmd);
	}

	public String getKPIValue(String cmdstr) {
		String cmd = cmdstr;
		Vector<String> aa = execute(cmd);
		String value = "";
		try {
			if (aa.size() > 0)
				value = aa.elementAt(0);
			for (int i = 1; i < aa.size(); i++) {
				value = value + "\n" + (String) aa.elementAt(i);
			}

		} catch (Exception e) {
			throw new RuntimeException("Exception getKPIValue:" + cmdstr, e);
		}
		return value.trim();
	}

	public Vector<String> execute(String shellCommand) {
		logger.info("1.do command: " + shellCommand);
		
		StringBuffer sb = new StringBuffer();
		
		Vector<String> result = new Vector<String>();		
		
		Process process = null;
		InputStream inputStream = null;
		InputStream errorInputStream = null;
		
		try {
			// 1. 执行命令
			process = start(shellCommand);
			if(process == null){
				return null;
			}

			// 2. 先读错误流
			String errLine;
			errorInputStream = process.getErrorStream();
			BufferedReader errorReader = new BufferedReader(
					new InputStreamReader(errorInputStream));

			while ((errLine = errorReader.readLine()) != null) {
				sb.append("ERR:" + errLine + "\n");
			}

			// 3. 再读标准输出流
			String line;
			inputStream = process.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inputStream));
			while ((line = reader.readLine()) != null) {
				result.addElement(line);
				sb.append(line + "\n");
			}
			
			logger.info("2.do command: " + shellCommand + ", result=\n"
					+ sb.toString());

			logger.info("3.begin: process.waitFor();");
			// 4. wartFor()
			process.waitFor();
			logger.info("4.end: process.waitFor();");
			
			return result;
		} catch (Exception e) {
			error.error("Exception while exec command:" + shellCommand, e);
			return null;
		}finally{
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
				}
			}
			if (errorInputStream != null) {
				try {
					errorInputStream.close();
				} catch (IOException e) {
				}
			}
			if (process != null) {
				process.destroy();
			}
		}
	}


	public Process start(String command) {
		Process process = null;
		try {
			process = Runtime.getRuntime().exec(this.RPCCMD + " " + command);
			return process;
		} catch (Exception e) {
			error.error("Exception while start command: " + command, e);
			if(process != null){
				process.destroy();
				process = null;
			}
			return null;
		}
	}

	public void setRpcCmd(String rpccmd) {
		RPCCMD = rpccmd;
	}

	public String getKPIValue1(String string) {
		return null;
	}
}