use of com.pamirs.pradar.pressurement.agent.event.impl.MockConfigAddEvent 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.pressurement.agent.event.impl.MockConfigAddEvent 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