forked from xtender/xt_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xt_shell.jsp
143 lines (133 loc) · 4.22 KB
/
xt_shell.jsp
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
134
135
136
137
138
139
140
141
142
143
create or replace and compile java source named xt_shell as
package com.xt_r;
/* Imports */
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.driver.OracleDriver;
import System.Threading.Thread.*;
import java.lang.System.*;
/* Main class */
public class XT_SHELL
{
// uncomment for utf or change to your locale
// public static final String ENCODING="UTF-8";
// for windows
// public static final String ENCODING="CP1252";
// for russian windows
public static final String ENCODING="CP1251";
/**
* Function shellExec
* @param String shell command
* @return String
*/
public static java.lang.String shellExec (String command,int TIMEOUT)
throws SQLException
{
Worker worker = new Worker(command);
try{
worker.start();
try {
worker.join(TIMEOUT);
if (worker.isAlive())
throw new InterruptedException("IntrException: Timeout exceed("+TIMEOUT+"ms)!");
return worker.result.toString();
} catch(InterruptedException ex) {
worker.interrupt();
Thread.currentThread().interrupt();
return "Timeout exceed!("+TIMEOUT+"c.)";
} finally {
worker.process.destroy();
}
}catch(Exception e){
worker.result.append("\nException(")
.append(e.toString())
.append("):")
.append(e.getCause())
.append(",")
.append(e.getMessage())
.append("\n\n");
return worker.result.toString();
}
}
/**
* Function shellExec
* @param String shell command
* @return String
*/
public static oracle.sql.ARRAY SQLshellExec (String command,int TIMEOUT)
throws SQLException
{
String shellResult = shellExec(command,TIMEOUT);
Connection conn = new OracleDriver().defaultConnection();
ArrayDescriptor descriptor =
ArrayDescriptor.createDescriptor("VARCHAR2_TABLE", conn );
oracle.sql.ARRAY outArray = new oracle.sql.ARRAY(descriptor,conn,shellResult.split("\n"));
return outArray;
}
/**
* Worker
*/
private static class Worker extends Thread {
public Process process;
public int exit;
public StringBuffer result;
private final String command;
private OutputStream stdin = null;
private InputStream stderr = null;
private InputStream stdout = null;
/**
* Constructor
*/
private Worker(String command) {
this.command = command;
this.result = new StringBuffer();
this.exit = -1;
}
/**
* main method - run
*/
public void run() {
try {
process = Runtime.getRuntime().exec(command);
stdin = process.getOutputStream ();
stderr = process.getErrorStream ();
stdout = process.getInputStream ();
BufferedReader brCleanUp =
new BufferedReader (
new InputStreamReader (stdout,ENCODING));
String line;
while ((line = brCleanUp.readLine ()) != null) {
result.append(line).append("\n");
}
brCleanUp.close();
brCleanUp =
new BufferedReader (new InputStreamReader (stderr,ENCODING));
while ((line = brCleanUp.readLine ()) != null) {
result.append("STDERR:\t").append(line).append("\n");
}
brCleanUp.close();
exit = process.waitFor();
} catch (InterruptedException e) {
exit = -1;
result.append("InterruptedException:").append(e.getMessage());
return;
} catch (java.io.IOException e) {
exit = 0;
result.append("IOException:").append(e.getMessage());
return;
} catch (Exception e) {
exit = 0;
result.append("Exception:").append(e.getMessage());
return;
}
}
}
}
/