SqlldrHandler.java
5.26 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
package com.sitech.ismp.coll.busi.e2e.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.util.Date;
import org.apache.log4j.Logger;
import org.apache.velocity.VelocityContext;
import com.sitech.base.AgentProperties;
import com.sitech.ismp.coll.busi.e2e.E2EConstant;
import com.sitech.util.DES3;
import com.sitech.util.Formater;
/**
* SqlldrHandler
*
* @author linxc
* @version
* @since Ver 1.1
* @Date 2012 May 18, 2012 10:08:01 AM
*/
public class SqlldrHandler {
private Logger logger = Logger.getLogger("BUSI_COLL");
public static String templatePath = AgentProperties.AGENT_HOME + "/config/velocity/";
public static String sqlldrCtlPath = AgentProperties.AGENT_HOME + "/data/";
public static String logPath = AgentProperties.AGENT_HOME + "/logs/";
public static String shellPath = AgentProperties.AGENT_HOME + "/script/busi/";
public static String noticePath = AgentProperties.AGENT_HOME + "/notice/busi/";
private int dataFileType;
private String dataFileTime;
private String dataFileName;
private String badFileName;
private String logFileName;
private String sqlldrCtlFileName;
private String sqlldrExecShellName;
private String noticeFileName;
private String username;
private String password;
public SqlldrHandler(String sFileType, String dataFileName,String username,String password) {
try{
this.username = username;
this.password = DES3.decrypt(password);
this.dataFileType = Integer.parseInt(sFileType);
this.dataFileTime = getDataFileTime(dataFileName);
this.dataFileName = sqlldrCtlPath + dataFileName;
this.badFileName = sqlldrCtlPath + getBadFileName(dataFileName);
this.logFileName = logPath + getLogFileName(dataFileName);
this.sqlldrCtlFileName = sqlldrCtlPath + getSqlldrCtlFileName(dataFileName);
this.sqlldrExecShellName = shellPath + getqlldrExecShellName(dataFileName);
this.noticeFileName = noticePath + getqlldrExecShellName(dataFileName);
}catch (Exception e) {
logger.error("Exception while create SqlldrHandler.", e);
}
}
/**
* 创建文件入库的控制文件
*/
public void createDataCtl() throws Exception{
String templateName = "";
switch (dataFileType) {
case E2EConstant.FILE_TYPE_CRM_ORDER:
templateName = "crm_order_ctl.vm";
break;
case E2EConstant.FILE_TYPE_CRM_ORDER_PROP:
templateName = "crm_prop_ctl.vm";
break;
case E2EConstant.FILE_TYPE_IBP_FINISH_ORDER:
templateName = "ibp_finish_ctl.vm";
break;
case E2EConstant.FILE_TYPE_IBP_ORDER:
templateName = "ibp_order_ctl.vm";
break;
case E2EConstant.FILE_TYPE_IBP_RECEIVE_ORDER:
templateName = "ibp_receive_ctl.vm";
break;
case E2EConstant.FILE_TYPE_CA_OUTSIDE_ORDER:
templateName = "ca_order_ctl.vm";
break;
case E2EConstant.FILE_TYPE_KX_ORDER:
templateName = "onestop_kx_order_ctl.vm";
break;
case E2EConstant.FILE_TYPE_ONESTOP_CRM_ORDER:
templateName = "onestop_crm_order_ctl.vm";
break;
default:
break;
}
VelocityContext vc = new VelocityContext();
String context = null;
vc.put("INFILE", dataFileName);
vc.put("BADFILE", badFileName);
vc.put("FILE_TIME", dataFileTime);
StringWriter writer = new StringWriter();
FileTemplate.getRuleTemplate(templatePath, templateName).merge(vc, writer);
context = writer.toString();
createFiles(sqlldrCtlFileName, context);
}
/**
* 创建执行SQL Loader的shell脚本
*/
public void createSqlldrExecShell() throws Exception {
VelocityContext vc = new VelocityContext();
String context = null;
vc.put("USERNAME", username);
vc.put("PASSWORD", password);
vc.put("DATA_CTL", sqlldrCtlFileName);
vc.put("LOG_FILE", logFileName);
vc.put("DATA_FILE", dataFileName);
StringWriter writer = new StringWriter();
FileTemplate.getRuleTemplate(templatePath, "sqlldrExecShell.vm").merge(vc, writer);
context = writer.toString();
createFiles(sqlldrExecShellName, context);
createFiles(noticeFileName, noticeFileName);
}
private String getqlldrExecShellName(String fileName) {
return fileName.substring(0, fileName.lastIndexOf(".")) + ".sh";
}
private String getSqlldrCtlFileName(String fileName) {
return fileName.substring(0, fileName.lastIndexOf(".")) + ".ctl";
}
private String getBadFileName(String fileName) {
return fileName.substring(0, fileName.lastIndexOf(".")) + ".bad";
}
private String getLogFileName(String fileName) {
return fileName.substring(0, fileName.lastIndexOf(".")) + ".log";
}
private String getDataFileTime(String fileName) throws Exception {
String subFileName = fileName.substring(fileName.lastIndexOf("_") + 1,
fileName.lastIndexOf("."));
Date fileTime = Formater.stringToDate(subFileName, "yyyyMMddHH");
return Formater.datetimeToString(fileTime);
}
private File createFiles(String filename, String context)
throws Exception {
File redirect = null;
if (filename.lastIndexOf("/") != -1) {
String dir = filename.substring(0, filename.lastIndexOf("/"));
redirect = new File(dir);
if (!redirect.exists()) {
redirect.mkdirs();
}
}
File file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(context);
bw.flush();
bw.close();
return file;
}
}