Search in sources :

Example 16 with ConfigKey

use of org.wildfly.swarm.spi.api.config.ConfigKey in project wildfly-swarm by wildfly-swarm.

the class ConfigurableManager method scanSubresources.

protected void scanSubresources(ConfigKey prefix, Object instance) throws Exception {
    Method method = getSubresourcesMethod(instance);
    if (method == null) {
        return;
    }
    Object subresources = method.invoke(instance);
    Field[] fields = subresources.getClass().getDeclaredFields();
    for (Field field : fields) {
        if (field.getAnnotation(SubresourceInfo.class) == null && List.class.isAssignableFrom(field.getType())) {
            continue;
        }
        field.setAccessible(true);
        Object value = field.get(subresources);
        ConfigKey subPrefix = prefix.append(nameFor(field));
        if (seen(subPrefix)) {
            continue;
        }
        if (value != null && value instanceof List) {
            int index = 0;
            Set<SimpleKey> seenKeys = new HashSet<>();
            for (Object each : ((List) value)) {
                SimpleKey key = getKey(each);
                ConfigKey itemPrefix = null;
                if (key != null) {
                    seenKeys.add(key);
                    itemPrefix = subPrefix.append(key);
                } else {
                    itemPrefix = subPrefix.append("" + index);
                }
                scan(itemPrefix, each, true);
                ++index;
            }
            List<SimpleKey> keysWithConfiguration = this.configView.simpleSubkeys(subPrefix);
            keysWithConfiguration.removeAll(seenKeys);
            if (!keysWithConfiguration.isEmpty()) {
                Method factoryMethod = getKeyedFactoryMethod(instance, field);
                if (factoryMethod != null) {
                    for (SimpleKey key : keysWithConfiguration) {
                        ConfigKey itemPrefix = subPrefix.append(key);
                        Object lambda = createLambda(itemPrefix, factoryMethod);
                        if (lambda != null) {
                            factoryMethod.invoke(instance, key.name(), lambda);
                        }
                    }
                }
            }
        } else {
            // Singleton resources, without key
            if (value == null) {
                // configuration keys that imply we want it.
                if (this.configView.hasKeyOrSubkeys(subPrefix)) {
                    Method factoryMethod = getNonKeyedFactoryMethod(instance, field);
                    if (factoryMethod != null) {
                        Object lambda = createLambda(subPrefix, factoryMethod);
                        if (lambda != null) {
                            factoryMethod.invoke(instance, lambda);
                        }
                    }
                }
            } else {
                scan(subPrefix, value, true);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) HashSet(java.util.HashSet)

Example 17 with ConfigKey

use of org.wildfly.swarm.spi.api.config.ConfigKey in project wildfly-swarm by wildfly-swarm.

the class ConfigurableManager method scanFraction.

protected void scanFraction(Fraction fraction) throws Exception {
    ConfigKey prefix = nameFor(fraction);
    scan(prefix, fraction, true);
}
Also used : ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey)

Example 18 with ConfigKey

use of org.wildfly.swarm.spi.api.config.ConfigKey in project wildfly-swarm by wildfly-swarm.

the class ConfigurableManager method createLambda.

protected Object createLambda(ConfigKey itemPrefix, Method factoryMethod) {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    // The consumer is the last parameter
    Class<?> consumerType = factoryMethod.getParameterTypes()[factoryMethod.getParameterCount() - 1];
    try {
        Method acceptMethod = null;
        for (Method method : consumerType.getMethods()) {
            if (method.getName().equals(ACCEPT)) {
                acceptMethod = method;
            }
        }
        if (acceptMethod == null) {
            return null;
        }
        MethodHandle target = lookup.findVirtual(ConfigurableManager.class, "subresourceAdded", MethodType.methodType(void.class, ConfigKey.class, Object.class));
        MethodType samType = MethodType.methodType(void.class, acceptMethod.getParameterTypes()[0]);
        MethodHandle mh = LambdaMetafactory.metafactory(lookup, ACCEPT, MethodType.methodType(consumerType, ConfigurableManager.class, ConfigKey.class), samType, target, samType).getTarget();
        return mh.invoke(this, itemPrefix);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) MethodType(java.lang.invoke.MethodType) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) Method(java.lang.reflect.Method) MethodHandle(java.lang.invoke.MethodHandle)

Example 19 with ConfigKey

use of org.wildfly.swarm.spi.api.config.ConfigKey in project wildfly-swarm by wildfly-swarm.

the class NetworkConfigurer method applyConfiguration.

protected void applyConfiguration(ConfigKey root, SocketBinding binding) {
    ConfigKey key = root.append(binding.name());
    applyConfiguration(key.append("port"), (port) -> {
        binding.port(port.toString());
    });
    applyConfiguration(key.append("multicast-port"), (port) -> {
        binding.multicastPort(port.toString());
    });
    applyConfiguration(key.append("multicast-address"), (addr) -> {
        binding.multicastAddress(addr.toString());
    });
    applyConfiguration(key.append("interface"), (iface) -> {
        binding.iface(iface.toString());
    });
}
Also used : ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey)

Example 20 with ConfigKey

use of org.wildfly.swarm.spi.api.config.ConfigKey in project wildfly-swarm by wildfly-swarm.

the class ConfigNode method valueOf.

/**
 * Retrieve a value.
 *
 * @param key The possibly-complex key of the value to retrieve.
 * @return The value of {@code null} if none.
 */
public Object valueOf(ConfigKey key) {
    SimpleKey head = key.head();
    if (head == ConfigKey.EMPTY) {
        if (this.value == null && this.children != null) {
            return this;
        }
        return this.value;
    }
    ConfigNode child = child(head);
    if (child != null) {
        ConfigKey rest = key.subkey(1);
        return child.valueOf(rest);
    }
    return null;
}
Also used : ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey)

Aggregations

ConfigKey (org.wildfly.swarm.spi.api.config.ConfigKey)26 SimpleKey (org.wildfly.swarm.spi.api.config.SimpleKey)7 Test (org.junit.Test)6 List (java.util.List)4 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 ApplicationScoped (javax.enterprise.context.ApplicationScoped)3 Any (javax.enterprise.inject.Any)3 Instance (javax.enterprise.inject.Instance)3 Inject (javax.inject.Inject)3 Field (java.lang.reflect.Field)2 HashSet (java.util.HashSet)2 Consumer (java.util.function.Consumer)2 Interface (org.wildfly.swarm.container.Interface)2 OutboundSocketBinding (org.wildfly.swarm.spi.api.OutboundSocketBinding)2 SocketBinding (org.wildfly.swarm.spi.api.SocketBinding)2 SocketBindingGroup (org.wildfly.swarm.spi.api.SocketBindingGroup)2 Configurable (org.wildfly.swarm.spi.api.annotations.Configurable)2 ConfigView (org.wildfly.swarm.spi.api.config.ConfigView)2 ConfigurationManager (com.netflix.config.ConfigurationManager)1