Search in sources :

Example 1 with MockConfig

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;
}
Also used : IClassMatchBuilder(com.shulie.instrument.simulator.api.listener.ext.IClassMatchBuilder) MockAdviceListener(com.pamirs.attach.plugin.mock.interceptor.MockAdviceListener) EventWatcher(com.shulie.instrument.simulator.api.listener.ext.EventWatcher) IBehaviorMatchBuilder(com.shulie.instrument.simulator.api.listener.ext.IBehaviorMatchBuilder) MockConfig(com.pamirs.pradar.internal.config.MockConfig) EventWatchBuilder(com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder)

Example 2 with MockConfig

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;
}
Also used : MockConfig(com.pamirs.pradar.internal.config.MockConfig)

Example 3 with MockConfig

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;
}
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 4 with MockConfig

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;
}
Also used : MockConfig(com.pamirs.pradar.internal.config.MockConfig) HashSet(java.util.HashSet)

Example 5 with MockConfig

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;
}
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)5 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 EventWatcher (com.shulie.instrument.simulator.api.listener.ext.EventWatcher)2 MockAdviceListener (com.pamirs.attach.plugin.mock.interceptor.MockAdviceListener)1 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 EventWatchBuilder (com.shulie.instrument.simulator.api.listener.ext.EventWatchBuilder)1 IBehaviorMatchBuilder (com.shulie.instrument.simulator.api.listener.ext.IBehaviorMatchBuilder)1 IClassMatchBuilder (com.shulie.instrument.simulator.api.listener.ext.IClassMatchBuilder)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1