refactor(xxl-job-admin):优化代码结构和逻辑

-替换 StringUtils 为 StringTool- 替换手动空值检查为 StringTool.isBlank()
- 优化集合操作,使用 CollectionTool
- 移除未使用的代码和冗余逻辑
- 统一代码风格和命名规范
3.2.0-release
xuxueli 4 weeks ago
parent 6fa72373f2
commit c5d514bc48

@ -10,6 +10,8 @@ import com.xxl.job.admin.mapper.XxlJobRegistryMapper;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.enums.RegistryConfig; import com.xxl.job.core.enums.RegistryConfig;
import com.xxl.sso.core.annotation.XxlSso; import com.xxl.sso.core.annotation.XxlSso;
import com.xxl.tool.core.CollectionTool;
import com.xxl.tool.core.StringTool;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@ -68,7 +70,7 @@ public class JobGroupController {
public ReturnT<String> save(XxlJobGroup xxlJobGroup){ public ReturnT<String> save(XxlJobGroup xxlJobGroup){
// valid // valid
if (xxlJobGroup.getAppname()==null || xxlJobGroup.getAppname().trim().length()==0) { if (StringTool.isBlank(xxlJobGroup.getAppname())) {
return ReturnT.ofFail((I18nUtil.getString("system_please_input")+"AppName") ); return ReturnT.ofFail((I18nUtil.getString("system_please_input")+"AppName") );
} }
if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) { if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) {
@ -77,14 +79,14 @@ public class JobGroupController {
if (xxlJobGroup.getAppname().contains(">") || xxlJobGroup.getAppname().contains("<")) { if (xxlJobGroup.getAppname().contains(">") || xxlJobGroup.getAppname().contains("<")) {
return ReturnT.ofFail( "AppName"+I18nUtil.getString("system_unvalid") ); return ReturnT.ofFail( "AppName"+I18nUtil.getString("system_unvalid") );
} }
if (xxlJobGroup.getTitle()==null || xxlJobGroup.getTitle().trim().length()==0) { if (StringTool.isBlank(xxlJobGroup.getTitle())) {
return ReturnT.ofFail((I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) ); return ReturnT.ofFail((I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
} }
if (xxlJobGroup.getTitle().contains(">") || xxlJobGroup.getTitle().contains("<")) { if (xxlJobGroup.getTitle().contains(">") || xxlJobGroup.getTitle().contains("<")) {
return ReturnT.ofFail(I18nUtil.getString("jobgroup_field_title")+I18nUtil.getString("system_unvalid") ); return ReturnT.ofFail(I18nUtil.getString("jobgroup_field_title")+I18nUtil.getString("system_unvalid") );
} }
if (xxlJobGroup.getAddressType()!=0) { if (xxlJobGroup.getAddressType()!=0) {
if (xxlJobGroup.getAddressList()==null || xxlJobGroup.getAddressList().trim().length()==0) { if (StringTool.isBlank(xxlJobGroup.getAddressList())) {
return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_addressType_limit") ); return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_addressType_limit") );
} }
if (xxlJobGroup.getAddressList().contains(">") || xxlJobGroup.getAddressList().contains("<")) { if (xxlJobGroup.getAddressList().contains(">") || xxlJobGroup.getAddressList().contains("<")) {
@ -93,7 +95,7 @@ public class JobGroupController {
String[] addresss = xxlJobGroup.getAddressList().split(","); String[] addresss = xxlJobGroup.getAddressList().split(",");
for (String item: addresss) { for (String item: addresss) {
if (item==null || item.trim().length()==0) { if (StringTool.isBlank(item)) {
return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_registryList_unvalid") ); return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_registryList_unvalid") );
} }
} }
@ -111,36 +113,32 @@ public class JobGroupController {
@XxlSso(role = Consts.ADMIN_ROLE) @XxlSso(role = Consts.ADMIN_ROLE)
public ReturnT<String> update(XxlJobGroup xxlJobGroup){ public ReturnT<String> update(XxlJobGroup xxlJobGroup){
// valid // valid
if (xxlJobGroup.getAppname()==null || xxlJobGroup.getAppname().trim().length()==0) { if (StringTool.isBlank(xxlJobGroup.getAppname())) {
return ReturnT.ofFail((I18nUtil.getString("system_please_input")+"AppName") ); return ReturnT.ofFail((I18nUtil.getString("system_please_input")+"AppName") );
} }
if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) { if (xxlJobGroup.getAppname().length()<4 || xxlJobGroup.getAppname().length()>64) {
return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_appname_length") ); return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_appname_length") );
} }
if (xxlJobGroup.getTitle()==null || xxlJobGroup.getTitle().trim().length()==0) { if (StringTool.isBlank(xxlJobGroup.getTitle())) {
return ReturnT.ofFail( (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) ); return ReturnT.ofFail( (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobgroup_field_title")) );
} }
if (xxlJobGroup.getAddressType() == 0) { if (xxlJobGroup.getAddressType() == 0) {
// 0=自动注册 // 0=自动注册
List<String> registryList = findRegistryByAppName(xxlJobGroup.getAppname()); List<String> registryList = findRegistryByAppName(xxlJobGroup.getAppname());
String addressListStr = null; String addressListStr = null;
if (registryList!=null && !registryList.isEmpty()) { if (CollectionTool.isNotEmpty(registryList)) {
Collections.sort(registryList); Collections.sort(registryList);
addressListStr = ""; addressListStr = String.join(",", registryList);
for (String item:registryList) {
addressListStr += item + ",";
}
addressListStr = addressListStr.substring(0, addressListStr.length()-1);
} }
xxlJobGroup.setAddressList(addressListStr); xxlJobGroup.setAddressList(addressListStr);
} else { } else {
// 1=手动录入 // 1=手动录入
if (xxlJobGroup.getAddressList()==null || xxlJobGroup.getAddressList().trim().length()==0) { if (StringTool.isBlank(xxlJobGroup.getAddressList())) {
return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_addressType_limit") ); return ReturnT.ofFail( I18nUtil.getString("jobgroup_field_addressType_limit") );
} }
String[] addresss = xxlJobGroup.getAddressList().split(","); String[] addresss = xxlJobGroup.getAddressList().split(",");
for (String item: addresss) { for (String item: addresss) {
if (item==null || item.trim().length()==0) { if (StringTool.isBlank(item)) {
return ReturnT.ofFail(I18nUtil.getString("jobgroup_field_registryList_unvalid") ); return ReturnT.ofFail(I18nUtil.getString("jobgroup_field_registryList_unvalid") );
} }
} }
@ -154,22 +152,20 @@ public class JobGroupController {
} }
private List<String> findRegistryByAppName(String appnameParam){ private List<String> findRegistryByAppName(String appnameParam){
HashMap<String, List<String>> appAddressMap = new HashMap<String, List<String>>(); HashMap<String, List<String>> appAddressMap = new HashMap<>();
List<XxlJobRegistry> list = xxlJobRegistryMapper.findAll(RegistryConfig.DEAD_TIMEOUT, new Date()); List<XxlJobRegistry> list = xxlJobRegistryMapper.findAll(RegistryConfig.DEAD_TIMEOUT, new Date());
if (list != null) { if (CollectionTool.isNotEmpty(list)) {
for (XxlJobRegistry item: list) { for (XxlJobRegistry item: list) {
if (RegistryConfig.RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) { if (!RegistryConfig.RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
String appname = item.getRegistryKey(); continue;
List<String> registryList = appAddressMap.get(appname);
if (registryList == null) {
registryList = new ArrayList<String>();
} }
String appname = item.getRegistryKey();
List<String> registryList = appAddressMap.computeIfAbsent(appname, k -> new ArrayList<>());
if (!registryList.contains(item.getRegistryValue())) { if (!registryList.contains(item.getRegistryValue())) {
registryList.add(item.getRegistryValue()); registryList.add(item.getRegistryValue());
} }
appAddressMap.put(appname, registryList);
}
} }
} }
return appAddressMap.get(appnameParam); return appAddressMap.get(appnameParam);

@ -17,7 +17,7 @@ import com.xxl.job.core.glue.GlueTypeEnum;
import com.xxl.job.core.util.DateUtil; import com.xxl.job.core.util.DateUtil;
import com.xxl.sso.core.helper.XxlSsoHelper; import com.xxl.sso.core.helper.XxlSsoHelper;
import com.xxl.sso.core.model.LoginInfo; import com.xxl.sso.core.model.LoginInfo;
import com.xxl.tool.core.StringTool; import com.xxl.tool.core.CollectionTool;
import com.xxl.tool.response.Response; import com.xxl.tool.response.Response;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
@ -63,7 +63,7 @@ public class JobInfoController {
// filter group // filter group
List<XxlJobGroup> jobGroupList = JobGroupPermissionUtil.filterJobGroupByPermission(request, jobGroupListTotal); List<XxlJobGroup> jobGroupList = JobGroupPermissionUtil.filterJobGroupByPermission(request, jobGroupListTotal);
if (jobGroupList==null || jobGroupList.isEmpty()) { if (CollectionTool.isEmpty(jobGroupList)) {
throw new XxlJobException(I18nUtil.getString("jobgroup_empty")); throw new XxlJobException(I18nUtil.getString("jobgroup_empty"));
} }

@ -17,13 +17,14 @@ import com.xxl.job.core.biz.model.LogParam;
import com.xxl.job.core.biz.model.LogResult; import com.xxl.job.core.biz.model.LogResult;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.util.DateUtil; import com.xxl.job.core.util.DateUtil;
import com.xxl.tool.core.CollectionTool;
import com.xxl.tool.core.StringTool;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -59,7 +60,7 @@ public class JobLogController {
List<XxlJobGroup> jobGroupListTotal = xxlJobGroupMapper.findAll(); List<XxlJobGroup> jobGroupListTotal = xxlJobGroupMapper.findAll();
// filter jobGroup // filter jobGroup
List<XxlJobGroup> jobGroupList = JobGroupPermissionUtil.filterJobGroupByPermission(request, jobGroupListTotal); List<XxlJobGroup> jobGroupList = JobGroupPermissionUtil.filterJobGroupByPermission(request, jobGroupListTotal);
if (jobGroupList==null || jobGroupList.isEmpty()) { if (CollectionTool.isEmpty(jobGroupList)) {
throw new XxlJobException(I18nUtil.getString("jobgroup_empty")); throw new XxlJobException(I18nUtil.getString("jobgroup_empty"));
} }
// write jobGroup // write jobGroup
@ -118,7 +119,7 @@ public class JobLogController {
// parse param // parse param
Date triggerTimeStart = null; Date triggerTimeStart = null;
Date triggerTimeEnd = null; Date triggerTimeEnd = null;
if (filterTime!=null && filterTime.trim().length()>0) { if (StringTool.isNotBlank(filterTime)) {
String[] temp = filterTime.split(" - "); String[] temp = filterTime.split(" - ");
if (temp.length == 2) { if (temp.length == 2) {
triggerTimeStart = DateUtil.parseDateTime(temp[0]); triggerTimeStart = DateUtil.parseDateTime(temp[0]);
@ -179,7 +180,7 @@ public class JobLogController {
} }
// fix xss // fix xss
if (logResult.getContent()!=null && StringUtils.hasText(logResult.getContent().getLogContent())) { if (logResult.getContent()!=null && StringTool.isNotBlank(logResult.getContent().getLogContent())) {
String newLogContent = filter(logResult.getContent().getLogContent()); String newLogContent = filter(logResult.getContent().getLogContent());
logResult.getContent().setLogContent(newLogContent); logResult.getContent().setLogContent(newLogContent);
} }

@ -10,13 +10,13 @@ import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.sso.core.annotation.XxlSso; import com.xxl.sso.core.annotation.XxlSso;
import com.xxl.sso.core.helper.XxlSsoHelper; import com.xxl.sso.core.helper.XxlSsoHelper;
import com.xxl.sso.core.model.LoginInfo; import com.xxl.sso.core.model.LoginInfo;
import com.xxl.tool.core.StringTool;
import com.xxl.tool.encrypt.SHA256Tool; import com.xxl.tool.encrypt.SHA256Tool;
import com.xxl.tool.response.Response; import com.xxl.tool.response.Response;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@ -81,7 +81,7 @@ public class JobUserController {
public ReturnT<String> add(XxlJobUser xxlJobUser) { public ReturnT<String> add(XxlJobUser xxlJobUser) {
// valid username // valid username
if (!StringUtils.hasText(xxlJobUser.getUsername())) { if (StringTool.isBlank(xxlJobUser.getUsername())) {
return ReturnT.ofFail(I18nUtil.getString("system_please_input")+I18nUtil.getString("user_username") ); return ReturnT.ofFail(I18nUtil.getString("system_please_input")+I18nUtil.getString("user_username") );
} }
xxlJobUser.setUsername(xxlJobUser.getUsername().trim()); xxlJobUser.setUsername(xxlJobUser.getUsername().trim());
@ -89,7 +89,7 @@ public class JobUserController {
return ReturnT.ofFail(I18nUtil.getString("system_lengh_limit")+"[4-20]" ); return ReturnT.ofFail(I18nUtil.getString("system_lengh_limit")+"[4-20]" );
} }
// valid password // valid password
if (!StringUtils.hasText(xxlJobUser.getPassword())) { if (StringTool.isBlank(xxlJobUser.getPassword())) {
return ReturnT.ofFail(I18nUtil.getString("system_please_input")+I18nUtil.getString("user_password") ); return ReturnT.ofFail(I18nUtil.getString("system_please_input")+I18nUtil.getString("user_password") );
} }
xxlJobUser.setPassword(xxlJobUser.getPassword().trim()); xxlJobUser.setPassword(xxlJobUser.getPassword().trim());
@ -123,7 +123,7 @@ public class JobUserController {
} }
// valid password // valid password
if (StringUtils.hasText(xxlJobUser.getPassword())) { if (StringTool.isNotBlank(xxlJobUser.getPassword())) {
xxlJobUser.setPassword(xxlJobUser.getPassword().trim()); xxlJobUser.setPassword(xxlJobUser.getPassword().trim());
if (!(xxlJobUser.getPassword().length()>=4 && xxlJobUser.getPassword().length()<=20)) { if (!(xxlJobUser.getPassword().length()>=4 && xxlJobUser.getPassword().length()<=20)) {
return ReturnT.ofFail(I18nUtil.getString("system_lengh_limit")+"[4-20]" ); return ReturnT.ofFail(I18nUtil.getString("system_lengh_limit")+"[4-20]" );

@ -1,5 +1,7 @@
package com.xxl.job.admin.model; package com.xxl.job.admin.model;
import com.xxl.tool.core.StringTool;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -20,8 +22,8 @@ public class XxlJobGroup {
// registry list // registry list
private List<String> registryList; // 执行器地址列表(系统注册) private List<String> registryList; // 执行器地址列表(系统注册)
public List<String> getRegistryList() { public List<String> getRegistryList() {
if (addressList!=null && addressList.trim().length()>0) { if (StringTool.isNotBlank(addressList)) {
registryList = new ArrayList<String>(Arrays.asList(addressList.split(","))); registryList = new ArrayList<>(Arrays.asList(addressList.split(",")));
} }
return registryList; return registryList;
} }

@ -1,7 +1,5 @@
package com.xxl.job.admin.model; package com.xxl.job.admin.model;
import org.springframework.util.StringUtils;
/** /**
* @author xuxueli 2019-05-04 16:43:12 * @author xuxueli 2019-05-04 16:43:12
*/ */
@ -62,21 +60,4 @@ public class XxlJobUser {
this.permission = permission; this.permission = permission;
} }
// plugin
public boolean validPermission(int jobGroup){
if (this.role == 1) {
return true;
} else {
if (StringUtils.hasText(this.permission)) {
for (String permissionItem : this.permission.split(",")) {
if (String.valueOf(jobGroup).equals(permissionItem)) {
return true;
}
}
}
return false;
}
}
} }

@ -1,14 +1,14 @@
package com.xxl.job.admin.scheduler.thread; package com.xxl.job.admin.scheduler.thread;
import com.xxl.job.admin.scheduler.conf.XxlJobAdminConfig;
import com.xxl.job.admin.model.XxlJobGroup; import com.xxl.job.admin.model.XxlJobGroup;
import com.xxl.job.admin.model.XxlJobRegistry; import com.xxl.job.admin.model.XxlJobRegistry;
import com.xxl.job.admin.scheduler.conf.XxlJobAdminConfig;
import com.xxl.job.core.biz.model.RegistryParam; import com.xxl.job.core.biz.model.RegistryParam;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.enums.RegistryConfig; import com.xxl.job.core.enums.RegistryConfig;
import com.xxl.tool.core.StringTool;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
@ -149,9 +149,9 @@ public class JobRegistryHelper {
public ReturnT<String> registry(RegistryParam registryParam) { public ReturnT<String> registry(RegistryParam registryParam) {
// valid // valid
if (!StringUtils.hasText(registryParam.getRegistryGroup()) if (StringTool.isBlank(registryParam.getRegistryGroup())
|| !StringUtils.hasText(registryParam.getRegistryKey()) || StringTool.isBlank(registryParam.getRegistryKey())
|| !StringUtils.hasText(registryParam.getRegistryValue())) { || StringTool.isBlank(registryParam.getRegistryValue())) {
return ReturnT.ofFail("Illegal Argument."); return ReturnT.ofFail("Illegal Argument.");
} }
@ -181,9 +181,9 @@ public class JobRegistryHelper {
public ReturnT<String> registryRemove(RegistryParam registryParam) { public ReturnT<String> registryRemove(RegistryParam registryParam) {
// valid // valid
if (!StringUtils.hasText(registryParam.getRegistryGroup()) if (StringTool.isBlank(registryParam.getRegistryGroup())
|| !StringUtils.hasText(registryParam.getRegistryKey()) || StringTool.isBlank(registryParam.getRegistryKey())
|| !StringUtils.hasText(registryParam.getRegistryValue())) { || StringTool.isBlank(registryParam.getRegistryValue())) {
return ReturnT.ofFail("Illegal Argument."); return ReturnT.ofFail("Illegal Argument.");
} }

Loading…
Cancel
Save