update:优化代码

This commit is contained in:
water 2020-03-26 11:50:41 +08:00
parent debd4c0425
commit 60ced18885
8 changed files with 84 additions and 134 deletions

View File

@ -102,7 +102,7 @@ public class JobInfoController {
}
}
} catch (ParseException e) {
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
}
return new ReturnT<>(result);
}

View File

@ -74,7 +74,7 @@ public class JobTemplateController {
}
}
} catch (ParseException e) {
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
}
return new ReturnT<>(result);
}

View File

@ -49,7 +49,7 @@ public class JobTrigger {
}
if (GlueTypeEnum.BEAN.getDesc().equals(jobInfo.getGlueType())) {
//解密账密
String json = JSONUtils.decryptJson(jobInfo.getJobJson());
String json = JSONUtils.changeJson(jobInfo.getJobJson(), JSONUtils.decrypt);
jobInfo.setJobJson(json);
}
if (StringUtils.isNotBlank(executorParam)) {

View File

@ -15,8 +15,11 @@ import com.wugui.datax.admin.entity.JobLogReport;
import com.wugui.datax.admin.mapper.*;
import com.wugui.datax.admin.service.JobService;
import com.wugui.datax.admin.util.CronUtil;
import com.wugui.datax.admin.util.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -52,7 +55,7 @@ public class JobServiceImpl implements JobService {
int list_count = jobInfoMapper.pageListCount(start, length, jobGroup, triggerStatus, jobDesc, glueType, author);
// package result
Map<String, Object> maps = new HashMap<String, Object>();
Map<String, Object> maps = new HashMap<>();
maps.put("recordsTotal", list_count); // 总记录数
maps.put("recordsFiltered", list_count); // 过滤后的总记录数
maps.put("data", list); // 分页列表
@ -64,31 +67,31 @@ public class JobServiceImpl implements JobService {
// valid
JobGroup group = jobGroupMapper.load(jobInfo.getJobGroup());
if (group == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_choose") + I18nUtil.getString("jobinfo_field_jobgroup")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_choose") + I18nUtil.getString("jobinfo_field_jobgroup")));
}
if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
}
if (jobInfo.getGlueType().equals(GlueTypeEnum.BEAN.getDesc()) && jobInfo.getJobJson().trim().length() <= 2) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobjson")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobjson")));
}
if (jobInfo.getJobDesc() == null || jobInfo.getJobDesc().trim().length() == 0) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobdesc")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobdesc")));
}
if (jobInfo.getAuthor() == null || jobInfo.getAuthor().trim().length() == 0) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_author")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_author")));
}
if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy") + I18nUtil.getString("system_unvalid")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy") + I18nUtil.getString("system_unvalid")));
}
if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy") + I18nUtil.getString("system_unvalid")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy") + I18nUtil.getString("system_unvalid")));
}
if (GlueTypeEnum.match(jobInfo.getGlueType()) == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_gluetype") + I18nUtil.getString("system_unvalid")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_gluetype") + I18nUtil.getString("system_unvalid")));
}
if (GlueTypeEnum.BEAN == GlueTypeEnum.match(jobInfo.getGlueType()) && (jobInfo.getExecutorHandler() == null || jobInfo.getExecutorHandler().trim().length() == 0)) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + "JobHandler"));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + "JobHandler"));
}
// fix "\r" in shell
@ -124,14 +127,15 @@ public class JobServiceImpl implements JobService {
// add in db
jobInfo.setAddTime(new Date());
jobInfo.setJobJson(JSONUtils.changeJson(jobInfo.getJobJson(), JSONUtils.encrypt));
jobInfo.setUpdateTime(new Date());
jobInfo.setGlueUpdatetime(new Date());
jobInfoMapper.save(jobInfo);
if (jobInfo.getId() < 1) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_add") + I18nUtil.getString("system_fail")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_add") + I18nUtil.getString("system_fail")));
}
return new ReturnT<String>(String.valueOf(jobInfo.getId()));
return new ReturnT<>(String.valueOf(jobInfo.getId()));
}
@Override
@ -153,22 +157,22 @@ public class JobServiceImpl implements JobService {
// valid
if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
}
if (jobInfo.getGlueType().equals(GlueTypeEnum.BEAN.getDesc()) && jobInfo.getJobJson().trim().length() <= 2) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobjson")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobjson")));
}
if (jobInfo.getJobDesc() == null || jobInfo.getJobDesc().trim().length() == 0) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobdesc")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobdesc")));
}
if (jobInfo.getAuthor() == null || jobInfo.getAuthor().trim().length() == 0) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_author")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_author")));
}
if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy") + I18nUtil.getString("system_unvalid")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy") + I18nUtil.getString("system_unvalid")));
}
if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy") + I18nUtil.getString("system_unvalid")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy") + I18nUtil.getString("system_unvalid")));
}
// ChildJobId valid
@ -200,7 +204,7 @@ public class JobServiceImpl implements JobService {
// group valid
JobGroup jobGroup = jobGroupMapper.load(jobInfo.getJobGroup());
if (jobGroup == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_jobgroup") + I18nUtil.getString("system_unvalid")));
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_jobgroup") + I18nUtil.getString("system_unvalid")));
}
// stage job info
@ -220,26 +224,15 @@ public class JobServiceImpl implements JobService {
nextTriggerTime = nextValidTime.getTime();
} catch (ParseException e) {
logger.error(e.getMessage(), e);
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") + " | " + e.getMessage());
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid") + " | " + e.getMessage());
}
}
exists_jobInfo.setJobGroup(jobInfo.getJobGroup());
exists_jobInfo.setJobCron(jobInfo.getJobCron());
exists_jobInfo.setJobDesc(jobInfo.getJobDesc());
exists_jobInfo.setAuthor(jobInfo.getAuthor());
exists_jobInfo.setAlarmEmail(jobInfo.getAlarmEmail());
exists_jobInfo.setExecutorRouteStrategy(jobInfo.getExecutorRouteStrategy());
exists_jobInfo.setExecutorHandler(jobInfo.getExecutorHandler());
exists_jobInfo.setExecutorParam(jobInfo.getExecutorParam());
exists_jobInfo.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
exists_jobInfo.setExecutorTimeout(jobInfo.getExecutorTimeout());
exists_jobInfo.setExecutorFailRetryCount(jobInfo.getExecutorFailRetryCount());
exists_jobInfo.setChildJobId(jobInfo.getChildJobId());
exists_jobInfo.setTriggerNextTime(nextTriggerTime);
BeanUtils.copyProperties(jobInfo,exists_jobInfo);
exists_jobInfo.setReplaceParam(jobInfo.getReplaceParam());
exists_jobInfo.setJvmParam(jobInfo.getJvmParam());
exists_jobInfo.setIncStartTime(jobInfo.getIncStartTime());
exists_jobInfo.setTriggerNextTime(nextTriggerTime);
exists_jobInfo.setUpdateTime(new Date());
exists_jobInfo.setGlueType(jobInfo.getGlueType());
exists_jobInfo.setPartitionInfo(jobInfo.getPartitionInfo());
@ -285,7 +278,7 @@ public class JobServiceImpl implements JobService {
nextTriggerTime = nextValidTime.getTime();
} catch (ParseException e) {
logger.error(e.getMessage(), e);
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") + " | " + e.getMessage());
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid") + " | " + e.getMessage());
}
xxlJobInfo.setTriggerStatus(1);

View File

@ -49,7 +49,7 @@ public class JobTemplateServiceImpl implements JobTemplateService {
int list_count = jobTemplateMapper.pageListCount(start, length, jobGroup, jobDesc, executorHandler, author);
// package result
Map<String, Object> maps = new HashMap<String, Object>();
Map<String, Object> maps = new HashMap<>();
maps.put("recordsTotal", list_count); // 总记录数
maps.put("recordsFiltered", list_count); // 过滤后的总记录数
maps.put("data", list); // 分页列表
@ -64,7 +64,7 @@ public class JobTemplateServiceImpl implements JobTemplateService {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_choose")+I18nUtil.getString("jobinfo_field_jobgroup")) );
}
if (!CronExpression.isValidExpression(jobTemplate.getJobCron())) {
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid") );
}
if (jobTemplate.getJobDesc()==null || jobTemplate.getJobDesc().trim().length()==0) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_jobdesc")) );
@ -142,7 +142,7 @@ public class JobTemplateServiceImpl implements JobTemplateService {
// valid
if (!CronExpression.isValidExpression(jobTemplate.getJobCron())) {
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
return new ReturnT<>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_invalid"));
}
if (jobTemplate.getJobDesc()==null || jobTemplate.getJobDesc().trim().length()==0) {
return new ReturnT<>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobdesc")));

View File

@ -1,6 +1,5 @@
package com.wugui.datax.admin.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@ -15,105 +14,63 @@ import com.alibaba.fastjson.JSONObject;
public class JSONUtils {
/**
* 返回格式化的json
*
* @param object
* decrypt 解密
*/
public static Integer decrypt = 0;
/**
* decrypt 加密
*/
public static Integer encrypt = 1;
/**
* @param content
* @param key
* @param changeType 0加密 or 1解密
* @return
*/
public static String formatJson(Object object) {
return JSON.toJSONString(object, true);
public static JSONObject change(String content, String key, Integer changeType) {
JSONObject keyObj = JSONObject.parseObject(JSONObject.parseObject(content).getString(key));
JSONObject params = JSONObject.parseObject(keyObj.getString("parameter"));
String dUsername = null, dPassword = null;
if (decrypt.equals(changeType)) { //解密
dUsername = AESUtil.decrypt(params.getString("username"));
dPassword = AESUtil.decrypt(params.getString("password"));
} else if (encrypt.equals(changeType)) {//加密
dUsername = AESUtil.encrypt(params.getString("username"));
dPassword = AESUtil.encrypt(params.getString("password"));
}
String username = dUsername == null ? params.getString("username") : dUsername;
String password = dPassword == null ? params.getString("password") : dPassword;
params.put("username", username);
params.put("password", password);
keyObj.put("parameter", params);
return keyObj;
}
public static String decryptJson(String jsonStr) {
/**
* @param jsonStr
* @param changeType 0加密 or 1解密
* @return jsonStr
*/
public static String changeJson(String jsonStr, Integer changeType) {
JSONObject json = JSONObject.parseObject(jsonStr);
JSONObject job = json.getJSONObject("job");
JSONArray content = job.getJSONArray("content");
for (int i = 0; i < content.size(); i++) {
((JSONObject) content.get(i)).put("reader", decrypt(content.getString(i), "reader"));
((JSONObject) content.get(i)).put("writer", decrypt(content.getString(i), "writer"));
JSONArray contents = job.getJSONArray("content");
for (int i = 0; i < contents.size(); i++) {
String contentStr = contents.getString(i);
Object obj = contents.get(i);
if (decrypt.equals(changeType)) { //解密
((JSONObject) obj).put("reader", change(contentStr, "reader", decrypt));
((JSONObject) obj).put("writer", change(contentStr, "writer", decrypt));
} else if (encrypt.equals(changeType)) {//加密
((JSONObject) obj).put("reader", change(contentStr, "reader", encrypt));
((JSONObject) obj).put("writer", change(contentStr, "writer", encrypt));
}
}
job.put("content", content);
job.put("content", contents);
json.put("job", job);
return json.toJSONString();
}
public static JSONObject decrypt(String content, String key) {
JSONObject writer = JSONObject.parseObject(JSONObject.parseObject(content).getString(key));
JSONObject writerParams = JSONObject.parseObject(writer.getString("parameter"));
String dUsername = AESUtil.decrypt(writerParams.getString("username"));
String username = dUsername == null ? writerParams.getString("username") : dUsername;
writerParams.put("username", username);
String dPassword = AESUtil.decrypt(writerParams.getString("password"));
String password = dPassword == null ? writerParams.getString("password") : dPassword;
writerParams.put("password", password);
writer.put("parameter", writerParams);
return writer;
}
public static void main(String[] args) {
String s = "{\n" +
" \"job\": {\n" +
" \"setting\": {\n" +
" \"speed\": {\n" +
" \"channel\": 3\n" +
" },\n" +
" \"errorLimit\": {\n" +
" \"record\": 0,\n" +
" \"percentage\": 0.02\n" +
" }\n" +
" },\n" +
" \"content\": [\n" +
" {\n" +
" \"reader\": {\n" +
" \"name\": \"mysqlreader\",\n" +
" \"parameter\": {\n" +
" \"username\": \"8ivlZcKGclwi5NwpuzkXzg==\",\n" +
" \"password\": \"fZfGM5387/mzCDxqjb2thQ==\",\n" +
" \"column\": [\n" +
" \"`id`\"\n" +
" ],\n" +
" \"splitPk\": \"\",\n" +
" \"connection\": [\n" +
" {\n" +
" \"table\": [\n" +
" \"datax_plugin\"\n" +
" ],\n" +
" \"jdbcUrl\": [\n" +
" \"jdbc:mysql://localhost:3306/datax_web\"\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" \"writer\": {\n" +
" \"name\": \"mysqlwriter\",\n" +
" \"parameter\": {\n" +
" \"username\": \"8ivlZcKGclwi5NwpuzkXzg==\",\n" +
" \"password\": \"fZfGM5387/mzCDxqjb2thQ==\",\n" +
" \"column\": [\n" +
" \"`id`\"\n" +
" ],\n" +
" \"preSql\": [\n" +
" \"\"\n" +
" ],\n" +
" \"connection\": [\n" +
" {\n" +
" \"table\": [\n" +
" \"job_log\"\n" +
" ],\n" +
" \"jdbcUrl\": \"jdbc:mysql://localhost:3306/datax_web\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
System.out.println(decryptJson(s));
}
}

View File

@ -122,7 +122,7 @@ jobinfo_field_readerColumns=Reader Column
jobinfo_field_writerColumns=Writer Column
jobinfo_field_gluetype=运行模式
jobinfo_field_executorparam=任务参数
jobinfo_field_cron_unvalid=Cron格式非法
jobinfo_field_cron_invalid=Cron格式非法
jobinfo_field_cron_never_fire=Cron非法永远不会触发
jobinfo_field_author=负责人
jobinfo_field_timeout=任务超时时间

View File

@ -117,7 +117,7 @@ jobinfo_field_jobJSON=Job JSON
jobinfo_field_timeout=Job timeout period
jobinfo_field_gluetype=GLUE Type
jobinfo_field_executorparam=Param
jobinfo_field_cron_unvalid=The Cron is illegal
jobinfo_field_cron_invalid=The Cron is illegal
jobinfo_field_cron_never_fire=The Cron will never fire
jobinfo_field_author=Author
jobinfo_field_alarmemail=Alarm email