use of com.pamirs.pradar.internal.config.MockConfig in project LinkAgent by shulieTech.
the class MockPlugin method enhanceClassMethod.
public Map<String, EventWatcher> enhanceClassMethod(Map<String, Set<MockConfig>> configs) {
if (configs == null || configs.isEmpty()) {
return Collections.EMPTY_MAP;
}
Map<String, EventWatcher> watchers = new HashMap<String, EventWatcher>();
for (Map.Entry<String, Set<MockConfig>> entry : configs.entrySet()) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("pre enhance class:{} ,configs:{}", entry.getKey(), entry.getValue());
}
for (MockConfig mockConfig : entry.getValue()) {
IClassMatchBuilder buildingForClass = new EventWatchBuilder(moduleEventWatcher).onClass(entry.getKey());
IBehaviorMatchBuilder buildingForBehavior = buildingForClass.onAnyBehavior(mockConfig.getMethodName());
if (mockConfig.getMethodArgClasses() != null && !mockConfig.getMethodArgClasses().isEmpty()) {
buildingForBehavior.withParameterTypes(mockConfig.getMethodArgClasses().toArray(new String[mockConfig.getMethodArgClasses().size()]));
}
buildingForBehavior.onListener(Listeners.of(MockAdviceListener.class, new Object[] { mockConfig.getCodeScript() }));
watchers.put(mockConfig.getKey(), buildingForClass.onWatch());
}
}
return watchers;
}
use of com.pamirs.pradar.internal.config.MockConfig in project LinkAgent by shulieTech.
the class MockPlugin method groupByClass.
private Map<String, Set<MockConfig>> groupByClass(Set<MockConfig> mockConfigs) {
Map<String, Set<MockConfig>> map = new HashMap<String, Set<MockConfig>>();
for (MockConfig mockConfig : mockConfigs) {
Set<MockConfig> configs = map.get(mockConfig.getClassName());
if (configs == null) {
configs = new HashSet<MockConfig>();
map.put(mockConfig.getClassName(), configs);
}
configs.add(mockConfig);
}
return map;
}
use of com.pamirs.pradar.internal.config.MockConfig in project LinkAgent by shulieTech.
the class MockPlugin method onActive.
@Override
public boolean onActive() throws Throwable {
try {
this.watchers = new HashMap<String, EventWatcher>();
EventRouter.router().addListener(new PradarEventListener() {
@Override
public EventResult onEvent(IEvent event) {
if (event instanceof MockConfigAddEvent) {
Set<MockConfig> mockConfigs = ((MockConfigAddEvent) event).getTarget();
Set<MockConfig> newConfigs = new HashSet<MockConfig>();
for (MockConfig mockConfig : mockConfigs) {
if (watchers.containsKey(mockConfig.getKey())) {
continue;
}
newConfigs.add(mockConfig);
}
Map<String, Set<MockConfig>> map = groupByClass(newConfigs);
watchers.putAll(enhanceClassMethod(map));
} else if (event instanceof MockConfigRemoveEvent) {
Set<MockConfig> mockConfigs = ((MockConfigRemoveEvent) event).getTarget();
for (MockConfig mockConfig : mockConfigs) {
EventWatcher watcher = watchers.remove(mockConfig.getKey());
if (watcher != null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Remove mock config interceptor {}", mockConfig.getKey());
}
watcher.onUnWatched();
}
}
} else if (event instanceof MockConfigModifyEvent) {
Set<MockConfig> mockConfigs = ((MockConfigModifyEvent) event).getTarget();
for (MockConfig mockConfig : mockConfigs) {
EventWatcher watcher = watchers.remove(mockConfig.getKey());
if (watcher != null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Modify mock config pre remove interceptor {}", mockConfig.getKey());
}
watcher.onUnWatched();
}
}
Map<String, Set<MockConfig>> map = groupByClass(mockConfigs);
watchers.putAll(enhanceClassMethod(map));
} else {
return EventResult.IGNORE;
}
return EventResult.success("Mock config update successful.");
}
@Override
public int order() {
return -1;
}
});
Set<MockConfig> mockConfigs = new HashSet<MockConfig>();
for (MockConfig mockConfig : GlobalConfig.getInstance().getMockConfigs()) {
if (this.watchers.containsKey(mockConfig.getKey())) {
continue;
}
mockConfigs.add(mockConfig);
}
if (CollectionUtils.isNotEmpty(mockConfigs)) {
Map<String, Set<MockConfig>> map = groupByClass(mockConfigs);
watchers.putAll(enhanceClassMethod(map));
}
} catch (Throwable e) {
LOGGER.warn("挡板增强失败", e);
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.LinkGuardEnhance).setErrorCode("mock-enhance-0001").setMessage("挡板增强失败!").setDetail("挡板增强失败:" + e.getMessage()).closePradar(ConfigNames.LINK_GUARD_CONFIG).report();
throw e;
}
return true;
}
use of com.pamirs.pradar.internal.config.MockConfig in project LinkAgent by shulieTech.
the class MockConfigChanger method calculateModifyMocks.
/**
* 计算所有变更的mock配置
*
* @param newMockConfigs
* @param oldMockConfigs
* @return
*/
private Set<MockConfig> calculateModifyMocks(Map<String, MockConfig> newMockConfigs, Map<String, MockConfig> oldMockConfigs) {
Set<String> retains = calculateRetain(newMockConfigs.keySet(), oldMockConfigs.keySet());
Set<MockConfig> mockConfigs = new HashSet<MockConfig>();
for (String retain : retains) {
MockConfig newMockConfig = newMockConfigs.get(retain);
MockConfig oldMockConfig = oldMockConfigs.get(retain);
if (!StringUtils.equals(newMockConfig.getCodeScript(), oldMockConfig.getCodeScript())) {
mockConfigs.add(newMockConfig);
}
}
return mockConfigs;
}
use of com.pamirs.pradar.internal.config.MockConfig in project LinkAgent by shulieTech.
the class MockConfigChanger method compareIsChangeAndSet.
@Override
public Boolean compareIsChangeAndSet(ApplicationConfig applicationConfig, Set<MockConfig> newValue) {
Set<MockConfig> mockConfigs = GlobalConfig.getInstance().getMockConfigs();
if (ObjectUtils.equals(mockConfigs.size(), newValue.size()) && CollectionUtils.equals(mockConfigs, newValue)) {
return Boolean.FALSE;
}
applicationConfig.setMockConfigs(newValue);
Map<String, MockConfig> newConfigs = new HashMap<String, MockConfig>();
Map<String, MockConfig> oldConfigs = new HashMap<String, MockConfig>();
for (MockConfig mockConfig : newValue) {
newConfigs.put(mockConfig.getKey(), mockConfig);
}
for (MockConfig mockConfig : mockConfigs) {
oldConfigs.put(mockConfig.getKey(), mockConfig);
}
Set<MockConfig> addMockConfigs = calculateAddMocks(newConfigs, oldConfigs);
Set<MockConfig> removeMockConfigs = calculateRemoveMocks(newConfigs, oldConfigs);
Set<MockConfig> modifyMockConfigs = calculateModifyMocks(newConfigs, oldConfigs);
if (CollectionUtils.isNotEmpty(addMockConfigs)) {
MockConfigAddEvent addEvent = new MockConfigAddEvent(addMockConfigs);
EventRouter.router().publish(addEvent);
}
if (CollectionUtils.isNotEmpty(removeMockConfigs)) {
MockConfigRemoveEvent removeEvent = new MockConfigRemoveEvent(removeMockConfigs);
EventRouter.router().publish(removeEvent);
}
if (CollectionUtils.isNotEmpty(modifyMockConfigs)) {
MockConfigModifyEvent modifyEvent = new MockConfigModifyEvent(modifyMockConfigs);
EventRouter.router().publish(modifyEvent);
}
GlobalConfig.getInstance().setMockConfigs(newValue);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("publish mock config successful. config={}", newValue);
}
return Boolean.TRUE;
}
Aggregations