修改插件管理类PluginManager,解决bean不能注入问题

This commit is contained in:
Jonathan 2022-02-12 11:48:31 +08:00
parent b6d2a28773
commit eefa7eedc1
10 changed files with 79 additions and 81 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.PluginManagerV2;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.model.AbstractionService;
import com.pgmmers.radar.service.model.FieldService;
import com.pgmmers.radar.service.model.PreItemService;
@ -46,7 +46,7 @@ public class AbstractionApiController {
private PreItemService preItemService;
@Autowired
private PluginManagerV2 pluginManagerV2;
private PluginManager pluginManager;
@GetMapping("/{id}")
public CommonResult get(@PathVariable Long id) {
@ -87,7 +87,7 @@ public class AbstractionApiController {
List<PreItemVO> listPreItem = preItemService.listPreItem(modelId);
if(listPreItem != null && listPreItem.size()!= 0){
for (PreItemVO preItem : listPreItem) {
PluginServiceV2 pt= pluginManagerV2.pluginServiceMap(preItem.getPlugin());
PluginServiceV2 pt = pluginManager.pluginServiceMap(preItem.getPlugin());
if (StringUtils.isNotEmpty(pt.getType()) && pt.getType().equals("JSON")) {
//load http request data
JsonNode json = preItem.getConfigJson();

View File

@ -19,7 +19,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.PluginManagerV2;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.model.*;
import com.pgmmers.radar.vo.model.*;
import io.swagger.annotations.Api;
@ -46,7 +46,7 @@ public class ActivationApiController {
@Autowired
private RuleService ruleService;
@Autowired
private PluginManagerV2 pluginManagerV2;
private PluginManager pluginManager;
@GetMapping("/{id}")
public CommonResult get(@PathVariable Long id) {
@ -79,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 = pluginManagerV2.pluginServiceMap(preItem.getPlugin());
PluginServiceV2 pt = pluginManager.pluginServiceMap(preItem.getPlugin());
if (StringUtils.isNotEmpty(pt.getType()) && pt.getType().equals("JSON")) {
//load http request data
JsonNode json = preItem.getConfigJson();

View File

@ -15,7 +15,7 @@ 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.PluginManagerV2;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.util.CaptchaUtil;
import com.pgmmers.radar.util.ZipUtils;
import com.pgmmers.radar.vo.common.PluginVO;
@ -53,13 +53,13 @@ public class CommonApiController {
@Autowired
private CacheService cacheService;
@Autowired
private PluginManagerV2 pluginManagerV2;
private PluginManager pluginManager;
@GetMapping("/plugins")
public CommonResult plugins() {
CommonResult result = new CommonResult();
List<PluginVO> plugins = pluginManagerV2.getPluginServiceMap()
List<PluginVO> plugins = pluginManager.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.PluginManagerV2;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.logs.EventService;
import com.pgmmers.radar.service.model.ActivationService;
import com.pgmmers.radar.service.model.FieldService;
@ -69,7 +69,7 @@ public class EventApiController {
@Autowired
private RuleService ruleService;
@Autowired
private PluginManagerV2 pluginManagerV2;
private PluginManager pluginManager;
@PostMapping("/query")
@ -190,7 +190,7 @@ public class EventApiController {
if (!itemsIdMap.containsKey(item.getDestField())) {
continue;
}
PluginServiceV2 plugin = pluginManagerV2.pluginServiceMap(item.getPlugin());
PluginServiceV2 plugin = pluginManager.pluginServiceMap(item.getPlugin());
String type = plugin.getType();
String meta = plugin.getMeta();

View File

@ -5,7 +5,6 @@ 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 org.junit.Assert;
import org.junit.Before;
@ -31,20 +30,17 @@ public class PluginTest {
private static final Logger logger = LoggerFactory.getLogger(PluginTest.class);
@Autowired
private PluginManagerV2 pluginManagerV2;
private PluginManager pluginManager;
@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()
// PluginManager.pluginServiceMap().values()
// .stream()
// .sorted(Comparator.comparing(PluginServiceV2::key))
// .forEach(t -> logger.info(t.info()));
pluginManager.getPluginServiceMap().values()
.stream()
.sorted(Comparator.comparing(PluginServiceV2::key))
.forEach(t -> logger.info(t.info()));
@ -68,7 +64,8 @@ public class PluginTest {
jsonInfo.put("firsName", "aaa");
jsonInfo.put("lastName", "ccc");
String[] sourceField = item.getSourceField().split(",");
PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("ALLINONE");
// PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("ALLINONE");
PluginServiceV2 pluginServiceV2 = pluginManager.getPluginServiceMap().get("ALLINONE");
String result = (String) pluginServiceV2
.handle(item, jsonInfo, sourceField);
Assert.assertNotNull(result);
@ -82,7 +79,8 @@ public class PluginTest {
item.setSourceField("time");
jsonInfo.put("time", System.currentTimeMillis());
String[] sourceField = item.getSourceField().split(",");
PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("DATEFORMAT");
// PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("DATEFORMAT");
PluginServiceV2 pluginServiceV2 = pluginManager.getPluginServiceMap().get("DATEFORMAT");
String result = (String) pluginServiceV2
.handle(item, jsonInfo, sourceField);
Assert.assertNotNull(result);
@ -102,7 +100,8 @@ public class PluginTest {
item.setSourceField("json");
jsonInfo.put("json","");
String[] sourceField = item.getSourceField().split(",");
PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("HTTP_UTIL");
// PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("HTTP_UTIL");
PluginServiceV2 pluginServiceV2 = pluginManager.getPluginServiceMap().get("HTTP_UTIL");
JSONObject result = (JSONObject) pluginServiceV2
.handle(item, jsonInfo, sourceField);
Assert.assertNotNull(result);
@ -115,7 +114,8 @@ public class PluginTest {
item.setSourceField("ip");
jsonInfo.put("ip", "1.1.1.1");
String[] sourceField = item.getSourceField().split(",");
PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("IP2LOCATION");
// PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("IP2LOCATION");
PluginServiceV2 pluginServiceV2 = pluginManager.getPluginServiceMap().get("IP2LOCATION");
Location location = (Location) pluginServiceV2
.handle(item, jsonInfo, sourceField);
Assert.assertNotNull(location);
@ -128,7 +128,8 @@ public class PluginTest {
item.setSourceField("phone");
jsonInfo.put("phone", "18657150000");
String[] sourceField = item.getSourceField().split(",");
PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("MOBILE2LOCATION");
// PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("MOBILE2LOCATION");
PluginServiceV2 pluginServiceV2 = pluginManager.getPluginServiceMap().get("MOBILE2LOCATION");
Location location = (Location) pluginServiceV2
.handle(item, jsonInfo, sourceField);
Assert.assertNotNull(location);
@ -141,7 +142,8 @@ public class PluginTest {
item.setSourceField("time");
jsonInfo.put("time",System.currentTimeMillis());
String[] sourceField = item.getSourceField().split(",");
PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("SENSITIVE_TIME");
// PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("SENSITIVE_TIME");
PluginServiceV2 pluginServiceV2 = pluginManager.getPluginServiceMap().get("SENSITIVE_TIME");
String location = (String) pluginServiceV2
.handle(item, jsonInfo, sourceField);
Assert.assertNotNull(location);
@ -156,7 +158,8 @@ public class PluginTest {
item.setSourceField("phone");
jsonInfo.put("phone", "18657150000");
String[] sourceField = item.getSourceField().split(",");
PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("SUBSTRING");
// PluginServiceV2 pluginServiceV2 = PluginManager.pluginServiceMap().get("SUBSTRING");
PluginServiceV2 pluginServiceV2 = pluginManager.getPluginServiceMap().get("SUBSTRING");
String location = (String) pluginServiceV2
.handle(item, jsonInfo, sourceField);
Assert.assertNotNull(location);

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.PluginManagerV2;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.model.ModelService;
import com.pgmmers.radar.service.model.PreItemService;
import com.pgmmers.radar.vo.model.PreItemVO;
@ -47,7 +47,7 @@ public class AntiFraudServiceImpl implements AntiFraudService {
private ModelService modelService;
@Autowired
private PluginManagerV2 pluginManagerV2;
private PluginManager pluginManager;
@Override
public CommonResult process(Long modelId, Map<String, Map<String, ?>> context) {
@ -113,7 +113,7 @@ public class AntiFraudServiceImpl implements AntiFraudService {
continue;
}
String[] sourceField = item.getSourceField().split(",");
Object transfer = pluginManagerV2.pluginServiceMap(item.getPlugin()).handle(item,jsonInfo,sourceField);
Object transfer = pluginManager.pluginServiceMap(item.getPlugin()).handle(item,jsonInfo,sourceField);
result.put(item.getDestField(), transfer);
}
return result;

View File

@ -1,28 +1,49 @@
package com.pgmmers.radar.service.impl.engine.plugin;
import com.pgmmers.radar.service.engine.PluginServiceV2;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.concurrent.ConcurrentHashMap;
@Deprecated
public class PluginManager {
@Service
public class PluginManager implements ApplicationContextAware {
private PluginManager() {
// private PluginManager() {
// }
// public static Map<String, PluginServiceV2> pluginServiceMap() {
// return SingletonHolder.pluginServiceMap;
// }
private final Map<String, PluginServiceV2> pluginServiceMap = new ConcurrentHashMap<>();
public PluginServiceV2 pluginServiceMap(String key){
return pluginServiceMap.get(key);
}
public static Map<String, PluginServiceV2> pluginServiceMap() {
return SingletonHolder.pluginServiceMap;
public Map<String, PluginServiceV2> getPluginServiceMap(){
return pluginServiceMap;
}
private static class SingletonHolder {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, PluginServiceV2> beansOfType = applicationContext.getBeansOfType(PluginServiceV2.class);
pluginServiceMap.putAll(beansOfType);
}
// 工厂模式实现单例存在不能注入bean问题
/* private static class SingletonHolder {
private static final Map<String, PluginServiceV2> pluginServiceMap = SpringFactoriesLoader
.loadFactories(PluginServiceV2.class, null).stream()
.sorted(Comparator.comparing(PluginServiceV2::key))
.collect(Collectors.toMap(PluginServiceV2::pluginName, e -> e,
(oldValue, newValue) -> newValue));
;
}
}*/
}

View File

@ -1,35 +0,0 @@
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

@ -14,7 +14,7 @@ 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.PluginManagerV2;
import com.pgmmers.radar.service.impl.engine.plugin.PluginManager;
import com.pgmmers.radar.service.model.ModelService;
import com.pgmmers.radar.service.search.SearchEngineService;
import com.pgmmers.radar.util.JsonUtils;
@ -60,7 +60,7 @@ public class ModelServiceImpl extends BaseLocalCacheService implements ModelServ
private MongoService mongoService;
@Autowired
private PluginManagerV2 pluginManagerV2;
private PluginManager pluginManager;
// 维护GUID到modelId的映射
private Map<String, Long> guidMap;
@ -252,7 +252,7 @@ public class ModelServiceImpl extends BaseLocalCacheService implements ModelServ
// pre item mapping
JSONObject preItemJson = new JSONObject();
for (PreItemVO item : items) {
PluginServiceV2 plugin = pluginManagerV2.pluginServiceMap(item.getPlugin());
PluginServiceV2 plugin = pluginManager.pluginServiceMap(item.getPlugin());
String columns = plugin.getMeta();
if (columns == null) {
String fieldType = plugin.getType();

View File

@ -1,4 +1,13 @@
# PluginManager reset
#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