Search in sources :

Example 1 with SimpleKey

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

the class SimpleKeyTest method testHead.

@Test
public void testHead() {
    SimpleKey key = new SimpleKey("hi");
    assertThat(key.head()).isEqualTo(key);
}
Also used : SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) Test(org.junit.Test)

Example 2 with SimpleKey

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

the class InterfaceExtensionTest method testAfterBeanDiscovery.

@Test
public void testAfterBeanDiscovery() throws Exception {
    ConfigView configView = mock(ConfigView.class);
    List<SimpleKey> interfaces = Arrays.asList(new SimpleKey("test"));
    when(configView.simpleSubkeys(any(ConfigKey.class))).thenReturn(interfaces);
    when(configView.valueOf(any(ConfigKey.class))).thenReturn("192.168.1.1");
    ext = new InterfaceExtension(configView);
    BeanManager beanManager = mock(BeanManager.class);
    AfterBeanDiscovery abd = mock(AfterBeanDiscovery.class);
    @SuppressWarnings("unchecked") ArgumentCaptor<CommonBean<Interface>> captor = ArgumentCaptor.forClass(CommonBean.class);
    ext.afterBeanDiscovery(abd, beanManager);
    verify(abd, times(1)).addBean(captor.capture());
    assertThat(captor.getValue().create(null).getName()).isEqualTo("test");
    assertThat(captor.getValue().create(null).getExpression()).isEqualTo("192.168.1.1");
}
Also used : ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) ConfigView(org.wildfly.swarm.spi.api.config.ConfigView) AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) CommonBean(org.wildfly.swarm.spi.api.cdi.CommonBean) BeanManager(javax.enterprise.inject.spi.BeanManager) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) Test(org.junit.Test)

Example 3 with SimpleKey

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

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

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

the class NetworkConfigurer method fixSocketBindings.

protected void fixSocketBindings(SocketBindingGroup group) {
    ConfigKey key = ROOT.append(group.name()).append("socket-bindings");
    List<SimpleKey> names = this.configView.simpleSubkeys(key);
    names.stream().map(e -> e.name()).map(name -> group.socketBindings().stream().filter(e -> e.name().equals(name)).findFirst().orElseGet(() -> {
        SocketBinding binding = new SocketBinding(name);
        group.socketBinding(binding);
        return binding;
    })).forEach(e -> {
        applyConfiguration(key, e);
    });
}
Also used : SocketBinding(org.wildfly.swarm.spi.api.SocketBinding) OutboundSocketBinding(org.wildfly.swarm.spi.api.OutboundSocketBinding) Consumer(java.util.function.Consumer) Inject(javax.inject.Inject) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey) List(java.util.List) Interface(org.wildfly.swarm.container.Interface) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) SocketBindingGroup(org.wildfly.swarm.spi.api.SocketBindingGroup) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Any(javax.enterprise.inject.Any) ConfigView(org.wildfly.swarm.spi.api.config.ConfigView) Instance(javax.enterprise.inject.Instance) SocketBinding(org.wildfly.swarm.spi.api.SocketBinding) OutboundSocketBinding(org.wildfly.swarm.spi.api.OutboundSocketBinding) ConfigKey(org.wildfly.swarm.spi.api.config.ConfigKey) SimpleKey(org.wildfly.swarm.spi.api.config.SimpleKey)

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