Search in sources :

Example 1 with Configurable

use of org.wildfly.swarm.spi.api.annotations.Configurable in project wildfly-swarm by wildfly-swarm.

the class ConfigurableManager method scan.

protected void scan(ConfigKey prefix, Object instance, Class<?> curClass, boolean implicit) throws Exception {
    if (curClass == null || curClass == Object.class || isBlacklisted(curClass)) {
        return;
    }
    Field[] fields = curClass.getDeclaredFields();
    for (Field field : fields) {
        if (!Modifier.isStatic(field.getModifiers())) {
            if (isBlacklisted(field)) {
                continue;
            }
            if (implicit || field.getAnnotation(Configurable.class) != null || field.getAnnotation(Configurables.class) != null) {
                if (isConfigurableType(field.getType())) {
                    List<ConfigKey> names = namesFor(prefix, field);
                    boolean configured = false;
                    for (ConfigKey name : names) {
                        if (!seen(name)) {
                            ConfigurableHandle configurable = new ObjectBackedConfigurableHandle(name, instance, field);
                            this.configurables.add(configurable);
                            configured = configure(configurable);
                        }
                        if (configured) {
                            break;
                        }
                    }
                }
            }
        }
    }
    if (!rescanning) {
        Method[] methods = curClass.getDeclaredMethods();
        for (Method method : methods) {
            if (method.isAnnotationPresent(Configurable.class)) {
                ConfigKey subPrefix = prefix.append(nameFor(method));
                if (method.getParameterCount() == 1) {
                    // configuration keys that imply we want it.
                    if (this.configView.hasKeyOrSubkeys(subPrefix)) {
                        Object lambda = createLambda(subPrefix, method);
                        if (lambda != null) {
                            method.invoke(instance, lambda);
                        }
                    }
                } else if (method.getParameterCount() == 2) {
                    List<SimpleKey> keysWithConfiguration = this.configView.simpleSubkeys(subPrefix);
                    if (!keysWithConfiguration.isEmpty()) {
                        for (SimpleKey key : keysWithConfiguration) {
                            ConfigKey itemPrefix = subPrefix.append(key);
                            Object lambda = createLambda(itemPrefix, method);
                            if (lambda != null) {
                                method.invoke(instance, key.name(), lambda);
                            }
                        }
                    }
                }
            }
        }
    }
    scan(prefix, instance, curClass.getSuperclass(), implicit);
}
Also used : ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) Configurable(org.wildfly.swarm.spi.api.annotations.Configurable) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey)

Example 2 with Configurable

use of org.wildfly.swarm.spi.api.annotations.Configurable in project wildfly-swarm by wildfly-swarm.

the class ConfigurableManager method nameFor.

protected ConfigKey nameFor(Fraction fraction) throws Exception {
    Configurable anno = fraction.getClass().getAnnotation(Configurable.class);
    if (anno != null) {
        return ConfigKey.parse(anno.value());
    }
    SimpleKey key = getKey(fraction);
    if (key == null) {
        key = new SimpleKey(fraction.getClass().getSimpleName().replace("Fraction", "").toLowerCase());
    }
    return ConfigKey.of("swarm").append(key);
}
Also used : Configurable(org.wildfly.swarm.spi.api.annotations.Configurable) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey)

Example 3 with Configurable

use of org.wildfly.swarm.spi.api.annotations.Configurable in project wildfly-swarm by wildfly-swarm.

the class ConfigurableManager method namesFor.

protected List<ConfigKey> namesFor(ConfigKey prefix, Field field) {
    List<ConfigKey> names = new ArrayList<>();
    Configurables plural = field.getAnnotation(Configurables.class);
    if (plural != null) {
        for (Configurable each : plural.value()) {
            ConfigKey key = nameFor(prefix, each);
            if (key != null) {
                names.add(key);
            }
        }
    } else {
        Configurable[] annos = field.getAnnotationsByType(Configurable.class);
        if (annos != null && annos.length > 0) {
            for (Configurable anno : annos) {
                ConfigKey key = nameFor(prefix, anno);
                if (key != null) {
                    names.add(key);
                }
            }
        } else {
            ConfigKey key = handleDeploymentConfiguration(prefix.append(nameFor(field)));
            names.add(key);
        }
    }
    return names;
}
Also used : Configurables(org.wildfly.swarm.spi.api.annotations.Configurables) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) ArrayList(java.util.ArrayList) Configurable(org.wildfly.swarm.spi.api.annotations.Configurable)

Aggregations

Configurable (org.wildfly.swarm.spi.api.annotations.Configurable)3 ArrayList (java.util.ArrayList)2 ConfigKey (org.wildfly.swarm.spi.api.config.ConfigKey)2 SimpleKey (org.wildfly.swarm.spi.api.config.SimpleKey)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Configurables (org.wildfly.swarm.spi.api.annotations.Configurables)1