Search in sources :

Example 1 with MockConfigAddEvent

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;
}
Also used : IEvent(com.pamirs.pradar.pressurement.agent.event.IEvent) MockConfigAddEvent(com.pamirs.pradar.pressurement.agent.event.impl.MockConfigAddEvent) EventWatcher(com.shulie.instrument.simulator.api.listener.ext.EventWatcher) PradarEventListener(com.pamirs.pradar.pressurement.agent.listener.PradarEventListener) EventResult(com.pamirs.pradar.pressurement.agent.listener.EventResult) MockConfigRemoveEvent(com.pamirs.pradar.pressurement.agent.event.impl.MockConfigRemoveEvent) MockConfig(com.pamirs.pradar.internal.config.MockConfig) MockConfigModifyEvent(com.pamirs.pradar.pressurement.agent.event.impl.MockConfigModifyEvent)

Example 2 with MockConfigAddEvent

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;
}
Also used : MockConfigRemoveEvent(com.pamirs.pradar.pressurement.agent.event.impl.MockConfigRemoveEvent) HashMap(java.util.HashMap) MockConfigAddEvent(com.pamirs.pradar.pressurement.agent.event.impl.MockConfigAddEvent) MockConfig(com.pamirs.pradar.internal.config.MockConfig) MockConfigModifyEvent(com.pamirs.pradar.pressurement.agent.event.impl.MockConfigModifyEvent)

Aggregations

MockConfig (com.pamirs.pradar.internal.config.MockConfig)2 MockConfigAddEvent (com.pamirs.pradar.pressurement.agent.event.impl.MockConfigAddEvent)2 MockConfigModifyEvent (com.pamirs.pradar.pressurement.agent.event.impl.MockConfigModifyEvent)2 MockConfigRemoveEvent (com.pamirs.pradar.pressurement.agent.event.impl.MockConfigRemoveEvent)2 IEvent (com.pamirs.pradar.pressurement.agent.event.IEvent)1 EventResult (com.pamirs.pradar.pressurement.agent.listener.EventResult)1 PradarEventListener (com.pamirs.pradar.pressurement.agent.listener.PradarEventListener)1 EventWatcher (com.shulie.instrument.simulator.api.listener.ext.EventWatcher)1 HashMap (java.util.HashMap)1