Search in sources :

Example 1 with AopPlugin

use of io.vertigo.core.component.AopPlugin in project vertigo by KleeGroup.

the class WebServiceManagerImpl method scanComponents.

/**
 * Scan WebServices as WebServiceDefinitions on all the components.
 * @param componentSpace ComponentSpace
 * @return Scanned webServiceDefinitions
 */
List<WebServiceDefinition> scanComponents(final ComponentSpace componentSpace) {
    final AopPlugin aopPlugin = Home.getApp().getConfig().getBootConfig().getAopPlugin();
    final ListBuilder<WebServiceDefinition> allWebServiceDefinitionListBuilder = new ListBuilder<>();
    // 1- We introspect all RestfulService class
    for (final String componentId : componentSpace.keySet()) {
        final Object component = componentSpace.resolve(componentId, Object.class);
        if (component instanceof WebServices) {
            final WebServices webServices = aopPlugin.unwrap(WebServices.class.cast(component));
            final List<WebServiceDefinition> webServiceDefinitions = webServiceScannerPlugin.scanWebService(webServices.getClass());
            allWebServiceDefinitionListBuilder.addAll(webServiceDefinitions);
        }
    }
    // 2- We sort by path, parameterized path should be after strict path
    return allWebServiceDefinitionListBuilder.sort(Comparator.comparing(WebServiceDefinition::getName)).unmodifiable().build();
}
Also used : WebServiceDefinition(io.vertigo.vega.webservice.metamodel.WebServiceDefinition) WebServices(io.vertigo.vega.webservice.WebServices) ListBuilder(io.vertigo.util.ListBuilder) AopPlugin(io.vertigo.core.component.AopPlugin)

Example 2 with AopPlugin

use of io.vertigo.core.component.AopPlugin in project vertigo by KleeGroup.

the class MetricAnalyticsUtil method createMetricDefinitions.

/**
 * Registers all methods annotated with @Metrics
 */
public static List<MetricDefinition> createMetricDefinitions(final String componentId, final Component component, final AopPlugin aopPlugin) {
    Assertion.checkNotNull(component);
    // -- we construct a map of feature by componentId
    final Map<String, String> featureByComponentId = new HashMap<>();
    Home.getApp().getConfig().getModuleConfigs().forEach(moduleConfig -> moduleConfig.getComponentConfigs().forEach(componentConfig -> featureByComponentId.put(componentConfig.getId(), moduleConfig.getName())));
    // 1. search all methods
    return Stream.of(aopPlugin.unwrap(component).getClass().getMethods()).filter(method -> method.isAnnotationPresent(Metrics.class)).map(method -> {
        Assertion.checkArgument(List.class.isAssignableFrom(method.getReturnType()), "metrics supplier methods of class {0} must return a List of Metric instead of {1}", component.getClass(), method.getReturnType());
        Assertion.checkArgument(method.getParameterTypes().length == 0, "metrics supplier methods of class {0} must not have any parameter", component.getClass());
        // -----
        // 2. For each method register a listener
        // we remove # because it doesn't comply with definition naming rule
        final String metricDefinitionName = "MET_" + StringUtil.camelToConstCase(componentId.replaceAll("#", "")) + "$" + StringUtil.camelToConstCase(method.getName());
        return new MetricDefinition(metricDefinitionName, () -> (List<Metric>) ClassUtil.invoke(component, method));
    }).collect(Collectors.toList());
}
Also used : HashMap(java.util.HashMap) ClassUtil(io.vertigo.util.ClassUtil) Collectors(java.util.stream.Collectors) Home(io.vertigo.app.Home) List(java.util.List) Stream(java.util.stream.Stream) StringUtil(io.vertigo.util.StringUtil) Map(java.util.Map) Assertion(io.vertigo.lang.Assertion) Component(io.vertigo.core.component.Component) Metrics(io.vertigo.commons.analytics.metric.Metrics) AopPlugin(io.vertigo.core.component.AopPlugin) MetricDefinition(io.vertigo.commons.analytics.metric.MetricDefinition) Metric(io.vertigo.commons.analytics.metric.Metric) Metrics(io.vertigo.commons.analytics.metric.Metrics) MetricDefinition(io.vertigo.commons.analytics.metric.MetricDefinition) HashMap(java.util.HashMap) List(java.util.List)

Example 3 with AopPlugin

use of io.vertigo.core.component.AopPlugin in project vertigo by KleeGroup.

the class HealthAnalyticsUtil method createHealthCheckDefinitions.

/**
 * Registers all methods annotated with @Suscriber on the object
 * @param componentId componentId to check
 * @param component Component to check
 * @param aopPlugin Aop plugin use for unwrap
 * @return List of HealthCheckDefinition
 */
public static List<HealthCheckDefinition> createHealthCheckDefinitions(final String componentId, final Component component, final AopPlugin aopPlugin) {
    Assertion.checkNotNull(component);
    // -- we construct a map of feature by componentId
    final Map<String, String> featureByComponentId = new HashMap<>();
    Home.getApp().getConfig().getModuleConfigs().forEach(moduleConfig -> moduleConfig.getComponentConfigs().forEach(componentConfig -> featureByComponentId.put(componentConfig.getId(), moduleConfig.getName())));
    // 1. search all methods
    return Stream.of(aopPlugin.unwrap(component).getClass().getMethods()).filter(method -> method.isAnnotationPresent(HealthChecked.class)).map(method -> {
        final HealthChecked healthChecked = method.getAnnotation(HealthChecked.class);
        Assertion.checkArgument(HealthMeasure.class.equals(method.getReturnType()), "health check methods of class {0} must return a HealthMeasure instead of {1}", component.getClass(), method.getReturnType());
        Assertion.checkArgument(method.getName().startsWith("check"), "health check methods of class {0} must start with check", component.getClass());
        Assertion.checkArgument(method.getParameterTypes().length == 0, "health check methods of class {0} must not have any parameter", component.getClass());
        // -----
        // 2. For each method register a listener
        // we remove # because it doesn't comply with definition naming rule
        final String healthCheckDefinitionName = "HCHK_" + StringUtil.camelToConstCase(componentId.replaceAll("#", "")) + "$" + StringUtil.camelToConstCase(method.getName());
        return new HealthCheckDefinition(healthCheckDefinitionName, healthChecked.name(), componentId, featureByComponentId.get(componentId), healthChecked.feature(), () -> (HealthMeasure) ClassUtil.invoke(component, method));
    }).collect(Collectors.toList());
}
Also used : HealthChecked(io.vertigo.commons.analytics.health.HealthChecked) HashMap(java.util.HashMap) Instant(java.time.Instant) ClassUtil(io.vertigo.util.ClassUtil) Collectors(java.util.stream.Collectors) HealthStatus(io.vertigo.commons.analytics.health.HealthStatus) Home(io.vertigo.app.Home) List(java.util.List) Stream(java.util.stream.Stream) StringUtil(io.vertigo.util.StringUtil) Map(java.util.Map) Assertion(io.vertigo.lang.Assertion) HealthCheck(io.vertigo.commons.analytics.health.HealthCheck) Component(io.vertigo.core.component.Component) HealthMeasure(io.vertigo.commons.analytics.health.HealthMeasure) AopPlugin(io.vertigo.core.component.AopPlugin) HealthCheckDefinition(io.vertigo.commons.analytics.health.HealthCheckDefinition) HealthMeasure(io.vertigo.commons.analytics.health.HealthMeasure) HashMap(java.util.HashMap) HealthChecked(io.vertigo.commons.analytics.health.HealthChecked) HealthCheckDefinition(io.vertigo.commons.analytics.health.HealthCheckDefinition)

Example 4 with AopPlugin

use of io.vertigo.core.component.AopPlugin in project vertigo by KleeGroup.

the class AspectTest method testUnwrapp.

@Test
public final void testUnwrapp() {
    final AopPlugin aopPlugin = getApp().getConfig().getBootConfig().getAopPlugin();
    final F f = getApp().getComponentSpace().resolve(F.class);
    // Il y a des aspects sur la classe donc elle doit ĂȘtre dewrappable
    assertNotEquals(F.class.getName(), f.getClass().getName());
    assertEquals(F.class.getName(), aopPlugin.unwrap(f).getClass().getName());
    // Il y a pas d'aspect
    final A myA = getApp().getComponentSpace().resolve(A.class);
    assertEquals(A.class.getName(), myA.getClass().getName());
    assertEquals(A.class.getName(), aopPlugin.unwrap(myA).getClass().getName());
}
Also used : A(io.vertigo.core.component.aop.data.components.A) F(io.vertigo.core.component.aop.data.components.F) AopPlugin(io.vertigo.core.component.AopPlugin) Test(org.junit.jupiter.api.Test)

Aggregations

AopPlugin (io.vertigo.core.component.AopPlugin)4 Home (io.vertigo.app.Home)2 Component (io.vertigo.core.component.Component)2 Assertion (io.vertigo.lang.Assertion)2 ClassUtil (io.vertigo.util.ClassUtil)2 StringUtil (io.vertigo.util.StringUtil)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 HealthCheck (io.vertigo.commons.analytics.health.HealthCheck)1 HealthCheckDefinition (io.vertigo.commons.analytics.health.HealthCheckDefinition)1 HealthChecked (io.vertigo.commons.analytics.health.HealthChecked)1 HealthMeasure (io.vertigo.commons.analytics.health.HealthMeasure)1 HealthStatus (io.vertigo.commons.analytics.health.HealthStatus)1 Metric (io.vertigo.commons.analytics.metric.Metric)1 MetricDefinition (io.vertigo.commons.analytics.metric.MetricDefinition)1 Metrics (io.vertigo.commons.analytics.metric.Metrics)1 A (io.vertigo.core.component.aop.data.components.A)1