Search in sources :

Example 1 with Component

use of io.vertigo.core.component.Component 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 2 with Component

use of io.vertigo.core.component.Component 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 3 with Component

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

the class ComponentLoader method registerComponents.

/**
 * registers all the components defined by their configs.
 * @param paramManagerOpt the optional manager of params
 * @param moduleName the name of the module
 * @param componentConfigs the configs of the components
 */
public void registerComponents(final Optional<ParamManager> paramManagerOpt, final String moduleName, final List<ComponentConfig> componentConfigs) {
    Assertion.checkNotNull(componentSpace);
    Assertion.checkNotNull(paramManagerOpt);
    Assertion.checkNotNull(moduleName);
    Assertion.checkNotNull(componentConfigs);
    // ---- Proxies----
    componentConfigs.stream().filter(ComponentConfig::isProxy).forEach(componentConfig -> {
        final Component component = createProxyWithOptions(/*paramManagerOpt,*/
        componentConfig);
        componentSpace.registerComponent(componentConfig.getId(), component);
    });
    // ---- No proxy----
    final DIReactor reactor = new DIReactor();
    // 0; On ajoute la liste des ids qui sont déjà résolus.
    for (final String id : componentSpace.keySet()) {
        reactor.addParent(id);
    }
    // Map des composants définis par leur id
    final Map<String, ComponentConfig> componentConfigById = componentConfigs.stream().filter(componentConfig -> !componentConfig.isProxy()).peek(componentConfig -> reactor.addComponent(componentConfig.getId(), componentConfig.getImplClass(), componentConfig.getParams().keySet())).collect(Collectors.toMap(ComponentConfig::getId, Function.identity()));
    // Comment trouver des plugins orphenlins ?
    final List<String> ids = reactor.proceed();
    // On a récupéré la liste ordonnée des ids.
    // On positionne un proxy pour compter les plugins non utilisés
    final ComponentUnusedKeysContainer componentProxyContainer = new ComponentUnusedKeysContainer(componentSpace);
    for (final String id : ids) {
        if (componentConfigById.containsKey(id)) {
            // Si il s'agit d'un composant (y compris plugin)
            final ComponentConfig componentConfig = componentConfigById.get(id);
            // 2.a On crée le composant avec AOP et autres options (elastic)
            final Component component = createComponentWithOptions(paramManagerOpt, componentProxyContainer, componentConfig);
            // 2.b. On enregistre le composant
            componentSpace.registerComponent(componentConfig.getId(), component);
        }
    }
    // --Search for unuseds plugins
    final List<String> unusedPluginIds = componentConfigs.stream().filter(componentConfig -> !componentConfig.isProxy()).filter(componentConfig -> Plugin.class.isAssignableFrom(componentConfig.getImplClass())).map(ComponentConfig::getId).filter(pluginId -> !componentProxyContainer.getUsedKeys().contains(pluginId)).collect(Collectors.toList());
    if (!unusedPluginIds.isEmpty()) {
        throw new VSystemException("plugins '{0}' in module'{1}' are not used by injection", unusedPluginIds, moduleName);
    }
}
Also used : ComponentSpaceWritable(io.vertigo.core.component.ComponentSpaceWritable) VSystemException(io.vertigo.lang.VSystemException) DIInjector(io.vertigo.core.component.di.injector.DIInjector) Aspect(io.vertigo.core.component.aop.Aspect) Container(io.vertigo.core.component.Container) DIReactor(io.vertigo.core.component.di.reactor.DIReactor) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ModuleConfig(io.vertigo.app.config.ModuleConfig) List(java.util.List) ProxyMethod(io.vertigo.core.component.proxy.ProxyMethod) ParamManager(io.vertigo.core.param.ParamManager) AspectConfig(io.vertigo.app.config.AspectConfig) Plugin(io.vertigo.core.component.Plugin) Map(java.util.Map) Assertion(io.vertigo.lang.Assertion) Optional(java.util.Optional) Component(io.vertigo.core.component.Component) AopPlugin(io.vertigo.core.component.AopPlugin) Method(java.lang.reflect.Method) ComponentConfig(io.vertigo.app.config.ComponentConfig) ProxyMethodConfig(io.vertigo.app.config.ProxyMethodConfig) ComponentConfig(io.vertigo.app.config.ComponentConfig) Component(io.vertigo.core.component.Component) DIReactor(io.vertigo.core.component.di.reactor.DIReactor) VSystemException(io.vertigo.lang.VSystemException)

Example 4 with Component

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

the class ComponentLoader method createComponentWithOptions.

// ici
private Component createComponentWithOptions(final Optional<ParamManager> paramManagerOpt, final ComponentUnusedKeysContainer componentContainer, final ComponentConfig componentConfig) {
    Assertion.checkArgument(!componentConfig.isProxy(), "a no-proxy component is expected");
    // ---
    // 1. An instance is created
    final Component instance = createInstance(componentContainer, paramManagerOpt, componentConfig);
    // 2. AOP , a new instance is created when aspects are injected in the previous instance
    return injectAspects(instance, componentConfig.getImplClass());
}
Also used : Component(io.vertigo.core.component.Component)

Aggregations

Component (io.vertigo.core.component.Component)4 AopPlugin (io.vertigo.core.component.AopPlugin)3 Assertion (io.vertigo.lang.Assertion)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Home (io.vertigo.app.Home)2 ClassUtil (io.vertigo.util.ClassUtil)2 StringUtil (io.vertigo.util.StringUtil)2 HashMap (java.util.HashMap)2 Stream (java.util.stream.Stream)2 AspectConfig (io.vertigo.app.config.AspectConfig)1 ComponentConfig (io.vertigo.app.config.ComponentConfig)1 ModuleConfig (io.vertigo.app.config.ModuleConfig)1 ProxyMethodConfig (io.vertigo.app.config.ProxyMethodConfig)1 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