常量类
帮助类
日志输出类
package common;
import com.chinacreator.config.ConfigManager;
/**
* <p>类的描述:定义常量类</p>
* @author gao.tang
* @version 1.0
* @date 2011-5-31 下午05:00:10
*/
public class Const {
public static final String ETL_PATH = ConfigManager.getInstance().getConfigValue("etlPath", "D:\\workspace\\kettle-3.2.0\\distrib");
public static final String TRAN_BAT_FILENAME = "Pan.bat";
public static final String JOB_BAT_FILENAME = "Kitchen.bat";
public static final String AUTH_PARAM = " -rep=zwres -user=admin -pass=admin ";
public static final String getOS(){
return System.getProperty("os.name");
}
}
帮助类
package common.shell;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import common.Const;
/**
* <p>
* 类的描述:脚本执行帮助类
* </p>
*
* @author gao.tang
* @version 1.0
* @date 2011-6-1 下午02:55:06
*/
public class ShellUtil {
static StringBuffer outInfo;
public static ShellUtil getInstance() {
return new ShellUtil();
}
/**
* 执行作业
*
* @param dir
* 作业所在目录
* @param jobName
* 作业名称
* @param params
* 作业扩展定义参数
* @return 返回0表示没有出现异常
*/
public int executeJob(String dir, String jobName, Map<String, String> params) {
return execute(dir, jobName, params, true);
}
/**
* 执行转换
*
* @param dir
* @param tranName
* @param params
* @return 返回0表示没有出现异常
*/
public int executeTran(String dir, String tranName,
Map<String, String> params) {
return execute(dir, tranName, params, false);
}
private int execute(String dir, String name, Map<String, String> params,
boolean isJob) {
String base[] = null;
if (Const.getOS().equals("Windows 95")) {
base = new String[] { "command.com", "/C" };
} else if (Const.getOS().startsWith("Windows")) {
base = new String[] { "cmd.exe", "/C" };
}
List<String> cmds = new ArrayList<String>();
for (int i = 0; i < base.length; i++)
cmds.add(base[i]);
if (Const.getOS().equals("Windows 95")
|| Const.getOS().startsWith("Windows")) {
StringBuffer cmdline = new StringBuffer(300);
cmdline.append("\"");
if (isJob) {
cmdline.append(Const.JOB_BAT_FILENAME).append(" ");
} else {
cmdline.append(Const.TRAN_BAT_FILENAME).append(" ");
}
cmdline.append(Const.AUTH_PARAM).append(" -dir=").append(dir).append(" ");
if (isJob) {
cmdline.append("-job=").append(name).append(" ");
} else {
cmdline.append("-trans=").append(name).append(" ");
}
if (params != null && !params.isEmpty()) {
// 拼接扩展参数
Iterator<Entry<String,String>> iterator = params.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> entry = iterator.next();
cmdline.append(" -param=\"").append(entry.getKey()).append("=")
.append(entry.getValue()).append("\" ");
}
}
cmdline.append("\"");
cmds.add(cmdline.toString());
}
ProcessBuilder procBuilder = new ProcessBuilder(cmds);
String filePath = Const.ETL_PATH;
File file = new File(filePath);
procBuilder.directory(file);
Process proc;
try {
outInfo = new StringBuffer();
proc = procBuilder.start();
// any error message?
StreamLogger errorLogger = new StreamLogger(proc.getErrorStream(),
" (stderr)",outInfo);
// any output?
StreamLogger outputLogger = new StreamLogger(proc.getInputStream(),
" (stdout)",outInfo);
// kick them off
new Thread(errorLogger).start();
new Thread(outputLogger).start();
proc.waitFor();
int errorCount = proc.exitValue();
proc.getErrorStream().close();
proc.getOutputStream().close();
return errorCount;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return -1;
}
}
日志输出类
package common.shell;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* <p>类的描述:bat命令行执行脚本日志</p>
* @author gao.tang
* @version 1.0
* @date 2011-6-1 下午02:49:49
*/
public class StreamLogger implements Runnable
{
private InputStream is;
private String type;
private StringBuffer outInfo;//add by jun.feng 记录工资自动生成所运行的etl日志
public StreamLogger(InputStream is, String type,StringBuffer outInfo)
{
this.is = is;
this.type = type;
this.outInfo = outInfo;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
outInfo.append(type + " - " + line);
}
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
package common.shell;
import java.util.HashMap;
import java.util.Map;
/**
* <p>类的描述:</p>
* @author gao.tang
* @version 1.0
* @date 2011-6-2 上午10:13:52
*/
public class ShellBusinessOp {
/**
* 删除核销记录数据
* @param annual
* @param payId
* @return
*/
public synchronized static boolean executeDelHxjl(String annual,String[] pay_id){
int state = -1;
if(pay_id != null && pay_id.length > 0){
StringBuilder payId = new StringBuilder();
for(String payid : pay_id){
payId.append("'").append(payid).append("',");
}
Map<String,String> params = new HashMap<String,String>();
params.put("annual", annual);
params.put("pay_id", payId.substring(0, payId.length() - 1));
state = ShellUtil.getInstance().executeTran("/工资自动生成支付", "删除核销记录", params);
}
return state == 0;
}
/**
* 自动生成工资支付数据
* @return
*/
public synchronized static boolean executeWageAutoPay(){
int state = -1;
state = ShellUtil.getInstance().executeJob("/工资自动生成支付", "工资支付接口", null);
return state == 0;
}
/**
* 未核工资数据更新
* @return
*/
public synchronized static boolean executeUpdateWage(){
int state = -1;
state = ShellUtil.getInstance().executeJob("/工资自动生成支付", "工资视图删除更新", null);
return state == 0;
}
/**
* 同步单位人员数据到表TB_DEPT_PEOPLENUM
* @return
*/
public synchronized static boolean executePeopleNum(){
int state = -1;
state = ShellUtil.getInstance().executeTran("/工资自动生成支付", "单位人口数据抽取", null);
return state == 0;
}
/**
* 同步更新工资统发部门工资统计表
* @return
*/
public synchronized static boolean executeStatictsData(){
int state = -1;
state = ShellUtil.getInstance().executeTran("/工资自动生成支付", "获取工资统计数据接口", null);
return state == 0;
}
}