RPCTarget.java
2.99 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
}
}