添加新的插件管理类,解决bean不能注入问题,并添加单元测试pluginListV2

This commit is contained in:
Jonathan 2022-01-27 11:55:03 +08:00
parent 5a2d3280d5
commit b6d2a28773
18 changed files with 138 additions and 85 deletions

View File

@ -18,7 +18,7 @@ import com.pgmmers.radar.service.common.CommonResult;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.service.engine.vo.DataColumnInfo;
import com.pgmmers.radar.service.enums.DataType;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManagerV2;
import com.pgmmers.radar.service.model.AbstractionService;
import com.pgmmers.radar.service.model.FieldService;
import com.pgmmers.radar.service.model.PreItemService;
@ -26,18 +26,12 @@ import com.pgmmers.radar.vo.model.AbstractionVO;
import com.pgmmers.radar.vo.model.FieldVO;
import com.pgmmers.radar.vo.model.PreItemVO;
import io.swagger.annotations.Api;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/services/v1/abstraction")
@Api(value = "AbstractionApi", description = "特征管理相关操作", tags = {"特征API"})
@ -51,6 +45,9 @@ public class AbstractionApiController {
@Autowired
private PreItemService preItemService;
@Autowired
private PluginManagerV2 pluginManagerV2;
@GetMapping("/{id}")
public CommonResult get(@PathVariable Long id) {
CommonResult result = new CommonResult();
@ -90,7 +87,7 @@ public class AbstractionApiController {
List<PreItemVO> listPreItem = preItemService.listPreItem(modelId);
if(listPreItem != null && listPreItem.size()!= 0){
for (PreItemVO preItem : listPreItem) {
PluginServiceV2 pt= PluginManager.pluginServiceMap().get(preItem.getPlugin());
PluginServiceV2 pt= pluginManagerV2.pluginServiceMap(preItem.getPlugin());
if (StringUtils.isNotEmpty(pt.getType()) && pt.getType().equals("JSON")) {
//load http request data
JsonNode json = preItem.getConfigJson();

View File

@ -19,31 +19,16 @@ import com.pgmmers.radar.service.common.CommonResult;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.service.engine.vo.DataColumnInfo;
import com.pgmmers.radar.service.enums.DataType;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.model.AbstractionService;
import com.pgmmers.radar.service.model.ActivationService;
import com.pgmmers.radar.service.model.FieldService;
import com.pgmmers.radar.service.model.PreItemService;
import com.pgmmers.radar.service.model.RuleService;
import com.pgmmers.radar.vo.model.AbstractionVO;
import com.pgmmers.radar.vo.model.ActivationVO;
import com.pgmmers.radar.vo.model.FieldVO;
import com.pgmmers.radar.vo.model.PreItemVO;
import com.pgmmers.radar.vo.model.RuleVO;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManagerV2;
import com.pgmmers.radar.service.model.*;
import com.pgmmers.radar.vo.model.*;
import io.swagger.annotations.Api;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/services/v1/activation")
@Api(value = "ActivationApi", description = "策略集管理相关操作", tags = {"策略集API"})
@ -60,6 +45,8 @@ public class ActivationApiController {
private PreItemService preItemService;
@Autowired
private RuleService ruleService;
@Autowired
private PluginManagerV2 pluginManagerV2;
@GetMapping("/{id}")
public CommonResult get(@PathVariable Long id) {
@ -92,7 +79,7 @@ public class ActivationApiController {
ds = new DataColumnInfo(DataType.PREITEMS.getDesc(), DataType.PREITEMS.getName());
List<PreItemVO> listPreItem = preItemService.listPreItem(modelId);
for (PreItemVO preItem : listPreItem) {
PluginServiceV2 pt= PluginManager.pluginServiceMap().get(preItem.getPlugin());
PluginServiceV2 pt = pluginManagerV2.pluginServiceMap(preItem.getPlugin());
if (StringUtils.isNotEmpty(pt.getType()) && pt.getType().equals("JSON")) {
//load http request data
JsonNode json = preItem.getConfigJson();

View File

@ -15,15 +15,12 @@ import com.alibaba.excel.util.IoUtils;
import com.pgmmers.radar.enums.FieldType;
import com.pgmmers.radar.service.cache.CacheService;
import com.pgmmers.radar.service.common.CommonResult;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManagerV2;
import com.pgmmers.radar.util.CaptchaUtil;
import com.pgmmers.radar.util.ZipUtils;
import com.pgmmers.radar.vo.common.PluginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -41,6 +38,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/services/v1/common")
@ -54,12 +52,14 @@ public class CommonApiController {
@Autowired
private CacheService cacheService;
@Autowired
private PluginManagerV2 pluginManagerV2;
@GetMapping("/plugins")
public CommonResult plugins() {
CommonResult result = new CommonResult();
List<PluginVO> plugins=PluginManager.pluginServiceMap()
List<PluginVO> plugins = pluginManagerV2.getPluginServiceMap()
.values()
.stream()
.map(t-> new PluginVO(t.key(),t.pluginName(),t.desc()))

View File

@ -20,7 +20,7 @@ import com.pgmmers.radar.service.common.CommonResult;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.service.engine.vo.DataColumnInfo;
import com.pgmmers.radar.service.enums.DataType;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManagerV2;
import com.pgmmers.radar.service.logs.EventService;
import com.pgmmers.radar.service.model.ActivationService;
import com.pgmmers.radar.service.model.FieldService;
@ -32,6 +32,13 @@ import com.pgmmers.radar.vo.model.FieldVO;
import com.pgmmers.radar.vo.model.PreItemVO;
import com.pgmmers.radar.vo.model.RuleVO;
import io.swagger.annotations.Api;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@ -39,17 +46,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 以后会独立拆分到分析子项目里面去
@ -72,6 +68,8 @@ public class EventApiController {
@Autowired
private RuleService ruleService;
@Autowired
private PluginManagerV2 pluginManagerV2;
@PostMapping("/query")
@ -192,7 +190,7 @@ public class EventApiController {
if (!itemsIdMap.containsKey(item.getDestField())) {
continue;
}
PluginServiceV2 plugin= PluginManager.pluginServiceMap().get(item.getPlugin());
PluginServiceV2 plugin = pluginManagerV2.pluginServiceMap(item.getPlugin());
String type = plugin.getType();
String meta = plugin.getMeta();

View File

@ -5,20 +5,23 @@ import com.pgmmers.radar.EngineApplication;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.service.engine.vo.Location;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManagerV2;
import com.pgmmers.radar.vo.model.PreItemVO;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("SpellCheckingInspection")
@RunWith(SpringRunner.class)
@ -27,7 +30,19 @@ public class PluginTest {
private static final Logger logger = LoggerFactory.getLogger(PluginTest.class);
@Autowired
private PluginManagerV2 pluginManagerV2;
@Test
public void pluginListV2() {
pluginManagerV2.getPluginServiceMap().values()
.stream()
.sorted(Comparator.comparing(PluginServiceV2::key))
.forEach(t -> logger.info(t.info()));
}
@Test
@Deprecated
public void pluginList() {
PluginManager.pluginServiceMap().values()
.stream()
@ -35,6 +50,10 @@ public class PluginTest {
.forEach(t -> logger.info(t.info()));
}
private Map<String, Object> jsonInfo;
@Before

View File

@ -10,7 +10,7 @@ import com.pgmmers.radar.service.engine.vo.AbstractionResult;
import com.pgmmers.radar.service.engine.vo.ActivationResult;
import com.pgmmers.radar.service.engine.vo.AdaptationResult;
import com.pgmmers.radar.service.engine.vo.AntiFraudProcessResult;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManagerV2;
import com.pgmmers.radar.service.model.ModelService;
import com.pgmmers.radar.service.model.PreItemService;
import com.pgmmers.radar.vo.model.PreItemVO;
@ -46,6 +46,9 @@ public class AntiFraudServiceImpl implements AntiFraudService {
@Autowired
private ModelService modelService;
@Autowired
private PluginManagerV2 pluginManagerV2;
@Override
public CommonResult process(Long modelId, Map<String, Map<String, ?>> context) {
AntiFraudProcessResult analysisResult = new AntiFraudProcessResult();
@ -110,7 +113,7 @@ public class AntiFraudServiceImpl implements AntiFraudService {
continue;
}
String[] sourceField = item.getSourceField().split(",");
Object transfer = PluginManager.pluginServiceMap().get(item.getPlugin()).handle(item,jsonInfo,sourceField);
Object transfer = pluginManagerV2.pluginServiceMap(item.getPlugin()).handle(item,jsonInfo,sourceField);
result.put(item.getDestField(), transfer);
}
return result;

View File

@ -2,6 +2,7 @@ package com.pgmmers.radar.service.impl.engine.plugin;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.vo.model.PreItemVO;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Map;
@ -10,6 +11,7 @@ import java.util.stream.Collectors;
/**
* author: wangcheng Date: 2020/5/19 Time: 上午11:45 Description:
*/
@Component
public class ALLINONE implements PluginServiceV2 {
@Override

View File

@ -4,8 +4,9 @@ import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.vo.model.PreItemVO;
import java.util.Map;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;
@Component
public class DATEFORMAT implements PluginServiceV2 {
@Override

View File

@ -13,6 +13,7 @@ import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import java.util.Map;
@ -20,6 +21,7 @@ import java.util.Map;
/**
* author: wangcheng Date: 2020/5/19 Time: 上午11:44 Description:
*/
@Component
public class GPS2LOCATION implements PluginServiceV2 {
private static final Logger logger = LoggerFactory.getLogger(GPS2LOCATION.class);

View File

@ -12,11 +12,13 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* author: wangcheng Date: 2020/5/19 Time: 上午11:47 Description:
*/
@Component
public class HTTP_UTIL implements PluginServiceV2 {
private static final Logger logger = LoggerFactory.getLogger(HTTP_UTIL.class);

View File

@ -4,19 +4,21 @@ import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.service.engine.vo.Location;
import com.pgmmers.radar.service.impl.util.BeanUtils;
import com.pgmmers.radar.vo.model.PreItemVO;
import java.util.Map;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Objects;
@Component
public class IP2LOCATION implements PluginServiceV2 {
private static final Logger logger = LoggerFactory.getLogger(IP2LOCATION.class);
private DbSearcher ipSearcher;
@Override
public Integer key() {
@ -38,16 +40,20 @@ public class IP2LOCATION implements PluginServiceV2 {
return "[{\"column\":\"country\", \"title\":\"国家\", \"type\":\"STRING\"},{\"column\":\"province\", \"title\":\"省份\", \"type\":\"STRING\"},{\"column\":\"city\", \"title\":\"城市\", \"type\":\"STRING\"}]";
}
public IP2LOCATION() {
static DbSearcher getDbSearcher() {
final DbSearcher ipSearcher;
try {
String ipFilePath = BeanUtils.getApplicationContext().getEnvironment()
.getProperty("ip2region.db.path");
DbConfig conf = new DbConfig();
ipSearcher = new DbSearcher(conf, ipFilePath);
logger.info("IP2LOCATION Plugin load success");
return ipSearcher;
} catch (Exception e) {
logger.error("ip2region init failed", e);
}
return null;
}
@Override
@ -59,7 +65,7 @@ public class IP2LOCATION implements PluginServiceV2 {
return null;
}
try {
DataBlock block = ipSearcher.memorySearch(ip);
DataBlock block = Objects.requireNonNull(IP2LOCATION.getDbSearcher()).memorySearch(ip);
String[] detail = block.getRegion().split("\\|");
location = new Location();
location.setCountry(detail[0]);

View File

@ -8,10 +8,12 @@ import com.pgmmers.radar.vo.data.MobileInfoVO;
import com.pgmmers.radar.vo.model.PreItemVO;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
/**
* author: wangcheng Date: 2020/5/19 Time: 上午11:46 Description:
*/
@Component
public class MOBILE2LOCATION implements PluginServiceV2 {
@Override

View File

@ -1,12 +1,14 @@
package com.pgmmers.radar.service.impl.engine.plugin;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import org.springframework.core.io.support.SpringFactoriesLoader;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.core.io.support.SpringFactoriesLoader;
@Deprecated
public class PluginManager {
private PluginManager() {

View File

@ -0,0 +1,35 @@
package com.pgmmers.radar.service.impl.engine.plugin;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* @author Model
* @since 2022/1/27 11:29
*/
@Service
public class PluginManagerV2 implements ApplicationContextAware {
private final Map<String, PluginServiceV2> pluginServiceMap = new ConcurrentHashMap<>();
public PluginServiceV2 pluginServiceMap(String key){
return pluginServiceMap.get(key);
}
public Map<String, PluginServiceV2> getPluginServiceMap(){
return pluginServiceMap;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, PluginServiceV2> beansOfType = applicationContext.getBeansOfType(PluginServiceV2.class);
pluginServiceMap.putAll(beansOfType);
}
}

View File

@ -2,12 +2,15 @@ package com.pgmmers.radar.service.impl.engine.plugin;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.vo.model.PreItemVO;
import org.springframework.stereotype.Component;
import java.util.Calendar;
import java.util.Map;
/**
* author: wangcheng Date: 2020/5/19 Time: 上午11:46 Description:
*/
@Component
public class SENSITIVE_TIME implements PluginServiceV2 {
@Override

View File

@ -2,11 +2,14 @@ package com.pgmmers.radar.service.impl.engine.plugin;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.vo.model.PreItemVO;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* author: wangcheng Date: 2020/5/19 Time: 上午11:45 Description:
*/
@Component
public class SUBSTRING implements PluginServiceV2 {
@Override

View File

@ -14,22 +14,13 @@ import com.pgmmers.radar.service.cache.SubscribeHandle;
import com.pgmmers.radar.service.common.CommonResult;
import com.pgmmers.radar.service.data.MongoService;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManagerV2;
import com.pgmmers.radar.service.model.ModelService;
import com.pgmmers.radar.service.search.SearchEngineService;
import com.pgmmers.radar.util.JsonUtils;
import com.pgmmers.radar.vo.model.FieldVO;
import com.pgmmers.radar.vo.model.ModelVO;
import com.pgmmers.radar.vo.model.PreItemVO;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.slf4j.Logger;
@ -38,6 +29,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service
public class ModelServiceImpl extends BaseLocalCacheService implements ModelService,
@ -62,6 +59,9 @@ public class ModelServiceImpl extends BaseLocalCacheService implements ModelServ
@Autowired
private MongoService mongoService;
@Autowired
private PluginManagerV2 pluginManagerV2;
// 维护GUID到modelId的映射
private Map<String, Long> guidMap;
@ -252,8 +252,7 @@ public class ModelServiceImpl extends BaseLocalCacheService implements ModelServ
// pre item mapping
JSONObject preItemJson = new JSONObject();
for (PreItemVO item : items) {
String pluginType = item.getPlugin();
PluginServiceV2 plugin= PluginManager.pluginServiceMap().get(pluginType);
PluginServiceV2 plugin = pluginManagerV2.pluginServiceMap(item.getPlugin());
String columns = plugin.getMeta();
if (columns == null) {
String fieldType = plugin.getType();

View File

@ -1,12 +1,4 @@
com.pgmmers.radar.service.engine.PluginServiceV2=\
com.pgmmers.radar.service.impl.engine.plugin.DATEFORMAT,\
com.pgmmers.radar.service.impl.engine.plugin.ALLINONE,\
com.pgmmers.radar.service.impl.engine.plugin.GPS2LOCATION,\
com.pgmmers.radar.service.impl.engine.plugin.HTTP_UTIL,\
com.pgmmers.radar.service.impl.engine.plugin.IP2LOCATION,\
com.pgmmers.radar.service.impl.engine.plugin.MOBILE2LOCATION,\
com.pgmmers.radar.service.impl.engine.plugin.SENSITIVE_TIME,\
com.pgmmers.radar.service.impl.engine.plugin.SUBSTRING