use of com.dtstack.taier.common.exception.DtCenterDefException in project Taier by DTStack.
the class TimeParamOperator method dealCustomizeTimeOperator.
public static String dealCustomizeTimeOperator(String command, String cycTime) {
String result = "";
String split = null;
String timeFmtStr = "";
if (command.startsWith("$[") && command.endsWith("]")) {
// 需要计算的变量
String line = command.substring(2, command.indexOf("]")).trim();
if (line.startsWith("format")) {
return doFormatFunction(line, cycTime);
} else if (line.startsWith("add_months")) {
String params = line.substring(line.indexOf("(") + 1, line.indexOf(")"));
String[] paramsArrays = params.split(",");
timeFmtStr = paramsArrays[0].trim();
if (paramsArrays.length > 2) {
// 第三个参数为连接符
split = paramsArrays[2].replaceAll("'", "");
}
if (YEAR_FMT.equals(timeFmtStr)) {
String year = StringUtils.deleteWhitespace(paramsArrays[1]);
if (!year.contains("*")) {
result = plusYear(MathUtil.getIntegerVal(year), cycTime, timeFmtStr);
} else {
int m = MathUtil.getIntegerVal(year.split("\\*")[0]);
result = plusYear(m, cycTime, timeFmtStr);
}
} else if (DAY_FMT.equals(timeFmtStr) || MONTH_FMT.equals(timeFmtStr)) {
String months = StringUtils.deleteWhitespace(paramsArrays[1]);
if (!months.contains("*")) {
result = plusMonth(MathUtil.getIntegerVal(months), cycTime, timeFmtStr);
} else {
int m = MathUtil.getIntegerVal(months.split("\\*")[0]);
int n = MathUtil.getIntegerVal(months.split("\\*")[1]);
if (Math.abs(m) != 12 && Math.abs(n) != 12) {
throw new DtCenterDefException("illegal command " + command);
}
result = plusMonth(m * n, cycTime, timeFmtStr);
}
}
} else {
// 没有函数
if (line.contains(",")) {
String[] paramsArrays = line.split(",");
line = paramsArrays[0];
split = String.valueOf(paramsArrays[1]).replaceAll("'", "");
}
Matcher matcher = customizePattern.matcher(line);
if (matcher.find() && matcher.groupCount() == 3) {
timeFmtStr = matcher.group(1).trim();
String operatorStr = matcher.group(2).trim();
String operatorNumStr = StringUtils.deleteWhitespace(matcher.group(3));
if (DAY_FMT.equals(timeFmtStr)) {
int days = 0;
if (!operatorNumStr.contains("*")) {
days = MathUtil.getIntegerVal(operatorNumStr);
} else if (operatorNumStr.split("\\*").length == 2) {
int m = Integer.parseInt(operatorNumStr.split("\\*")[0]);
int n = Integer.parseInt(operatorNumStr.split("\\*")[1]);
if (m != 7 && n != 7) {
throw new DtCenterDefException("illegal command " + command);
}
days = m * n;
}
if ("-".equals(operatorStr)) {
result = minusDay(days, cycTime, timeFmtStr);
} else if ("+".equals(operatorStr)) {
result = plusDay(days, cycTime, timeFmtStr);
}
} else if ("hh24miss".equals(timeFmtStr)) {
int time;
if (operatorNumStr.split("/").length == 2) {
// 小时
time = MathUtil.getIntegerVal(operatorNumStr.split("/")[0]);
if ("-".equals(operatorStr)) {
result = minusHour(time, cycTime, STD_FMT);
}
if ("+".equals(operatorStr)) {
result = plusHour(time, cycTime, STD_FMT);
}
} else if (operatorNumStr.split("/").length == 3) {
// 分钟
time = MathUtil.getIntegerVal(operatorNumStr.split("/")[0]);
if ("-".equals(operatorStr)) {
result = minusMinute(time, cycTime, STD_FMT);
}
if ("+".equals(operatorStr)) {
result = plusMinute(time, cycTime, STD_FMT);
}
}
}
} else if (formattedPattern.matcher(line).matches()) {
// 时间格式化
try {
FastDateFormat cycDate = FastDateFormat.getInstance(STD_FMT, TimeZone.getTimeZone("GMT+8"));
Date cycd = cycDate.parse(cycTime);
if (line.contains("hh24")) {
line = line.replaceAll("hh24", "HH");
}
result = FastDateFormat.getInstance(line, TimeZone.getTimeZone("GMT+8")).format(cycd);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
if (StringUtils.isBlank(result)) {
throw new DtCenterDefException("illegal command " + command);
}
if (StringUtils.isNotEmpty(split) && StringUtils.isNotEmpty(result)) {
if (StringUtils.isBlank(timeFmtStr)) {
timeFmtStr = line;
}
return convertResultWithSplit(result, timeFmtStr, split.trim());
}
return result;
} else if (command.startsWith("${") && command.endsWith("}")) {
// 特殊处理 ${bdp.system.currenttime}
if (SYSTEM_CURRENTTIME.equals(command.substring(2, command.length() - 1).trim())) {
return new SimpleDateFormat(STD_FMT).format(new Date());
}
// 支持基于业务日期作为基准取值的格式 时间减一天,其余照原逻辑处理 不多做任何校验
String yesterdayCycTime = minusDay(1, cycTime, STD_FMT);
String normalCommand = command.replaceFirst("\\{", "[").replaceFirst("}", "]");
return dealCustomizeTimeOperator(normalCommand, yesterdayCycTime);
} else if (command.startsWith("$(") && command.endsWith(")")) {
// 支持基于当前时间作为基准取值的格式,其余照原逻辑处理 不多做任何校验
String currentTime = new SimpleDateFormat(STD_FMT).format(new Date());
String normalCommand = command.replaceFirst("\\(", "[");
normalCommand = normalCommand.substring(0, normalCommand.length() - 1) + "]";
return dealCustomizeTimeOperator(normalCommand, currentTime);
} else {
// 直接返回
return command;
}
}
use of com.dtstack.taier.common.exception.DtCenterDefException in project Taier by DTStack.
the class TimeParamOperator method dealTimeOperator.
public static String dealTimeOperator(String command, String cycTime) {
// 增加一种系统参数。格式:yyyy-MM-dd,-1 逗号左侧为format,右边为时间运算表达式
String[] split = command.split(",");
String realCommand = null;
String realOperator = null;
String realOperatorNum = null;
if (split.length == 2) {
realCommand = split[0];
realOperator = split[1].substring(0, 1);
realOperatorNum = split[1].substring(1, split[1].length());
} else {
Matcher matcher = pattern.matcher(command);
if (!(matcher.find() && matcher.groupCount() == 3)) {
throw new DtCenterDefException("illegal command " + command);
}
String timeFmtStr = matcher.group(1).trim();
realCommand = timeFmtStr;
String operatorStr = matcher.group(2).trim();
realOperator = operatorStr;
String operatorNumStr = matcher.group(3).trim();
realOperatorNum = operatorNumStr;
}
int operatorNum = MathUtil.getIntegerVal(realOperatorNum);
if ("-".equals(realOperator)) {
if (realCommand.length() == 10) {
return minusDay(operatorNum, cycTime, realCommand);
} else if (realCommand.length() == 8) {
return minusDay(operatorNum, cycTime, realCommand);
} else if (realCommand.length() == 6) {
return minusMonth(operatorNum, cycTime, realCommand);
} else if (realCommand.length() == 4) {
return minusYear(operatorNum, cycTime, realCommand);
} else {
throw new DtCenterDefException("illegal command " + command);
}
} else if ("+".equals(realOperator)) {
if (realCommand.length() == 10) {
return minusDay(operatorNum, cycTime, realCommand);
} else if (realCommand.length() == 8) {
return plusDay(operatorNum, cycTime, realCommand);
} else if (realCommand.length() == 6) {
return plusMonth(operatorNum, cycTime, realCommand);
} else if (realCommand.length() == 4) {
return plusYear(operatorNum, cycTime, realCommand);
} else {
throw new DtCenterDefException("illegal command " + command);
}
} else {
throw new DtCenterDefException("illegal command " + command);
}
}
use of com.dtstack.taier.common.exception.DtCenterDefException in project Taier by DTStack.
the class SFTPHandler method downloadFile.
public void downloadFile(String ftpPath, String localPath) {
if (!isFileExist(ftpPath)) {
throw new DtCenterDefException("File not exist on sftp:" + ftpPath);
}
OutputStream os = null;
try {
os = new FileOutputStream(new File(localPath));
channelSftp.get(ftpPath, os);
} catch (Exception e) {
throw new DtCenterDefException("download file from sftp error");
} finally {
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
}
use of com.dtstack.taier.common.exception.DtCenterDefException in project Taier by DTStack.
the class DataSourceUtils method downloadFileFromSftp.
/**
* 从 SFTP 上下载 kerberos 配置文件到本地。
* 先比较传入时间戳与 本地 lock 文件的时间戳,如果传入时间戳大于本地文件时间戳则重新下载
* 如果传入时间戳为 null,则比较本地 kerberos 文件路径下的 lock 时间戳与 sftp lock 文件时间戳判断是否需要重新下载
*
* @param sftpDir SFTP 上 kerberos 配置文件相对路径
* @param localPath 本地 kerberos 目录
* @param sftpMap sftp 配置getLocalTimeLock
* @param fileTimestamp 本地时间戳
*/
public static void downloadFileFromSftp(String sftpDir, String localPath, Map<String, String> sftpMap, Timestamp fileTimestamp) {
// 需要读取配置文件
// 本地kerberos文件
Long localTimeLock = getLocalTimeLock(localPath);
if (fileTimestamp != null && localTimeLock >= fileTimestamp.getTime()) {
return;
}
String sftpPath = sftpMap.get("path") + SEPARATE + sftpDir;
SFTPHandler handler = null;
try {
handler = SFTPHandler.getInstance(sftpMap);
// sftp服务器文件
Long timeLock = getSftpTimeLock(handler, sftpPath);
// 如果 timeLock 为空,则说明不存在 .lock 文件,则
if (timeLock == 0L || localTimeLock < timeLock) {
// 对文件本地删除和sftp下载进行加锁
synchronized (sftpDir.intern()) {
// 需要下载替换当时的配置
delFile(new File(localPath));
handler.downloadDir(sftpPath, localPath);
// 如果 SFTP 不存在 .lock 文件,则手动创建一个
createIfNotExistLockFile(localPath);
}
}
} catch (Exception e) {
throw new DtCenterDefException(String.format("从 SFTP 下载配置文件异常: %s", e.getMessage()), e);
} finally {
if (handler != null) {
handler.close();
}
}
}
use of com.dtstack.taier.common.exception.DtCenterDefException in project Taier by DTStack.
the class PoolHttpClient method execute.
private static String execute(HttpPost httpPost) {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseBody = null;
// 请求数据
int status = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
// FIXME 暂时不从header读取
String result = EntityUtils.toString(entity, code);
if (status == HttpStatus.SC_OK) {
responseBody = result;
} else if (status == HttpStatus.SC_UNAUTHORIZED) {
throw new DtCenterDefException("登陆状态失效");
} else {
logger.error("request url:{} fail:{}", httpPost.getURI().toString(), result);
return null;
}
return responseBody;
} catch (Exception e) {
logger.error("url:" + httpPost.getURI().toString() + "--->http request error:", e);
}
return null;
}
Aggregations