Search in sources :

Example 11 with SimpleKey

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

the class MapConfigNodeFactory method load.

protected static void load(ConfigNode config, List<?> input) {
    int num = input.size();
    for (int i = 0; i < num; ++i) {
        Object value = input.get(i);
        ConfigNode child = load(value);
        config.child(new SimpleKey("" + i), child);
    }
}
Also used : SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey)

Example 12 with SimpleKey

use of org.wildfly.swarm.spi.api.config.SimpleKey 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 13 with SimpleKey

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

the class SocketBindingGroupExtension method afterBeanDiscovery.

@SuppressWarnings("unused")
void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager beanManager) throws Exception {
    List<SimpleKey> configuredGroups = this.configView.simpleSubkeys(ROOT);
    for (SimpleKey groupName : configuredGroups) {
        Set<Bean<?>> groups = beanManager.getBeans(SocketBindingGroup.class, AnyLiteral.INSTANCE);
        AtomicBoolean producerRequired = new AtomicBoolean(false);
        if (groups.stream().noneMatch(e -> e.getQualifiers().stream().anyMatch(anno -> anno instanceof Named && ((Named) anno).value().equals(groupName)))) {
            SocketBindingGroup group = new SocketBindingGroup(groupName.name(), null, "0");
            applyConfiguration(group);
            if (producerRequired.get()) {
                CommonBean<SocketBindingGroup> interfaceBean = CommonBeanBuilder.newBuilder(SocketBindingGroup.class).beanClass(SocketBindingGroupExtension.class).scope(ApplicationScoped.class).addQualifier(AnyLiteral.INSTANCE).addQualifier(new NamedLiteral(group.name())).createSupplier(() -> group).addType(SocketBindingGroup.class).addType(Object.class).build();
                abd.addBean(interfaceBean);
            }
        }
    }
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AnyLiteral(org.jboss.weld.literal.AnyLiteral) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) List(java.util.List) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) SocketBindingGroup(org.wildfly.swarm.spi.api.SocketBindingGroup) Observes(javax.enterprise.event.Observes) NamedLiteral(org.jboss.weld.literal.NamedLiteral) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Named(javax.inject.Named) ConfigView(org.wildfly.swarm.spi.api.config.ConfigView) Bean(javax.enterprise.inject.spi.Bean) BeanManager(javax.enterprise.inject.spi.BeanManager) CommonBeanBuilder(org.wildfly.swarm.spi.api.cdi.CommonBeanBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Named(javax.inject.Named) SocketBindingGroup(org.wildfly.swarm.spi.api.SocketBindingGroup) NamedLiteral(org.jboss.weld.literal.NamedLiteral) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) Bean(javax.enterprise.inject.spi.Bean)

Example 14 with SimpleKey

use of org.wildfly.swarm.spi.api.config.SimpleKey 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)

Example 15 with SimpleKey

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

the class CompositeKeyTest method testSubkey.

@Test
public void testSubkey() {
    CompositeKey key = new CompositeKey("foo", "bar", "baz");
    assertThat(key.subkey(1).head()).isEqualTo(new SimpleKey("bar"));
    assertThat(key.subkey(2).head()).isEqualTo(new SimpleKey("baz"));
    assertThat(key.subkey(3).head()).isEqualTo(ConfigKey.EMPTY);
}
Also used : CompositeKey(org.wildfly.swarm.spi.api.config.CompositeKey) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) Test(org.junit.Test)

Aggregations

SimpleKey (org.wildfly.swarm.spi.api.config.SimpleKey)18 ConfigKey (org.wildfly.swarm.spi.api.config.ConfigKey)10 List (java.util.List)6 Test (org.junit.Test)6 ConfigView (org.wildfly.swarm.spi.api.config.ConfigView)5 ApplicationScoped (javax.enterprise.context.ApplicationScoped)4 AfterBeanDiscovery (javax.enterprise.inject.spi.AfterBeanDiscovery)3 BeanManager (javax.enterprise.inject.spi.BeanManager)3 Interface (org.wildfly.swarm.container.Interface)3 SocketBindingGroup (org.wildfly.swarm.spi.api.SocketBindingGroup)3 CommonBean (org.wildfly.swarm.spi.api.cdi.CommonBean)3 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Observes (javax.enterprise.event.Observes)2 Any (javax.enterprise.inject.Any)2 Instance (javax.enterprise.inject.Instance)2 Bean (javax.enterprise.inject.spi.Bean)2