ProcedureCollThread.java
5.63 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package com.sitech.ismp.coll.busi;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.busi.util.FilesOperation;
import com.sitech.ismp.coll.busi.util.JDBCPoolManager;
import com.sitech.ismp.coll.busi.util.TblDBLinkInfo;
import com.sitech.util.DES3;
import com.sitech.util.SameGUID;
public class ProcedureCollThread implements Runnable {
private Logger logger = Logger.getLogger(ProcedureCollThread.class);
private String ip;
private String port;
private String ssid;
private String url;
private String className;
private String username;
private String password;
private Connection conn = null;
private String localDir;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public ProcedureCollThread(String ip, String port, String ssid,
String className, String username, String password, String localDir) {
this.ip = ip;
this.port = port;
this.ssid = ssid;
this.className = className;
this.url = "jdbc:oracle:thin:@" + ip + ":" + port + ":" + ssid;
this.username = username;
this.password = DES3.decrypt(password);
this.localDir = localDir;
}
public void run() {
localDir += "/" + ip.replace(".", "_");
File file = new File(localDir);
if (!file.exists()) {
file.mkdir();
}
try {
conn = getJdbcConnection();
} catch (Exception e) {
logger.error("Exception while getJdbcConnection:" + url, e);
return;
}
String sqlGetProcedureObj = "select * from DBA_Objects where object_type='PROCEDURE'";
String sqlGetProcedureSource = "select * from DBA_SOURCE where type='PROCEDURE'";
Statement stmt = null;
ResultSet rs = null;
Map<String, List<String>> maps = new HashMap<String, List<String>>();
try {
stmt = conn.createStatement();
logger.info(sqlGetProcedureObj);
rs = stmt.executeQuery(sqlGetProcedureObj);
while (rs.next()) {
String owner = rs.getString("OWNER");
String objectName = rs.getString("OBJECT_NAME");
String objectId = rs.getString("OBJECT_ID");
Date created = rs.getTimestamp("CREATED");
Date lastDDLTime = rs.getTimestamp("LAST_DDL_TIME");
String timestamp = rs.getString("TIMESTAMP");
String status = rs.getString("STATUS");
String fileName = localDir
+ "/PROC_"
+ SameGUID.getSameGUID(ip + "_" + port + "_" + ssid
+ "_" + objectName) + ".info";
String descr = "";
descr += "TYPE=PROC\n";
descr += "IP_ADDR=" + ip + "\n";
descr += "PORT=" + port + "\n";
descr += "SSID=" + ssid + "\n";
descr += "OWNER=" + owner + "\n";
descr += "OBJECT_NAME=" + objectName + "\n";
descr += "OBJECT_ID=" + objectId + "\n";
descr += "CREATED=" + dateFormat.format(created) + "\n";
descr += "LAST_DDL_TIME=" + dateFormat.format(lastDDLTime)
+ "\n";
descr += "TIMESTAMP=" + timestamp + "\n";
descr += "STATUS=" + status + "\n";
descr += "COLL_TIME=" + dateFormat.format(new Date()) + "\n";
logger.info("\n" + descr);
FilesOperation.createFiles(fileName, descr);
maps.put(objectName, new ArrayList<String>());
}
} catch (Exception e) {
logger.error("Exception while collProcudure.", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
for (String name : maps.keySet()) {
List<String> list = maps.get(name);
String sql = sqlGetProcedureSource + " and NAME='" + name
+ "' order by LINE asc";
stmt = conn.createStatement();
logger.info(sql);
rs = stmt.executeQuery(sql);
while (rs.next()) {
list.add(rs.getString("TEXT"));
}
}
} catch (Exception e) {
logger.error("Exception while collProcudure.", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
for (String name : maps.keySet()) {
String guid = SameGUID.getSameGUID(ip + "_" + port + "_" + ssid
+ "_" + name);
String fileName = localDir + "/PROC_" + guid + ".txt";
String content = "";
List<String> list = maps.get(name);
for (String line : list) {
content += line + "\n";
}
try {
FilesOperation.createFiles(fileName, content);
} catch (Exception e) {
logger.error("Exception while create file:" + fileName, e);
}
}
}
/**
* 初始化JDBC连接
*
* @throws
* @since Ver 1.1
*/
private Connection getJdbcConnection() throws Exception {
String linkName = SameGUID.getSameGUID(url + username);
TblDBLinkInfo dbLinkInfo = new TblDBLinkInfo();
dbLinkInfo.setLinkName(linkName);
dbLinkInfo.setClassName(className);
dbLinkInfo.setUrl(url);
dbLinkInfo.setUserid(username);
dbLinkInfo.setPasswd(password);
dbLinkInfo.setMinPoolSize(2);
dbLinkInfo.setMaxPoolSize(5);
dbLinkInfo.setMaxStatements(10);
dbLinkInfo.setMaxIdleTime(60);
try {
JDBCPoolManager.getInstance().createPool(dbLinkInfo);
conn = JDBCPoolManager.getInstance().getConnection(
dbLinkInfo.getLinkName());
return conn;
} catch (Exception e) {
logger.error("Exception while getJdbcConnection.", e);
return null;
}
}
public static void main(String[] args) {
ProcedureCollThread thread = new ProcedureCollThread("172.21.0.77",
"1527", "bnms", "oracle.jdbc.driver.OracleDriver", "bnms15",
DES3.encrypt("bnms15"), "F:\\");
thread.run();
}
}