Search in sources :

Example 1 with Instance

use of jakarta.enterprise.inject.Instance in project helidon by oracle.

the class JedisExtension method addBeans.

private void addBeans(@Observes final AfterBeanDiscovery event, final BeanManager beanManager) throws IntrospectionException {
    if (event != null && beanManager != null) {
        for (final String instanceName : this.instanceNames) {
            if (instanceName != null) {
                final Set<Annotation> qualifiers;
                if (instanceName.equals("default")) {
                    qualifiers = Collections.singleton(Default.Literal.INSTANCE);
                } else {
                    qualifiers = Collections.singleton(NamedLiteral.of(instanceName));
                }
                event.<JedisPoolConfig>addBean().addTransitiveTypeClosure(JedisPoolConfig.class).scope(ApplicationScoped.class).addQualifiers(qualifiers).produceWith((instance) -> {
                    final JedisPoolConfig returnValue = new JedisPoolConfig();
                    try {
                        this.configure(instance.select(Config.class).get(), returnValue, JedisPoolConfig.class, instanceName);
                    } catch (final IntrospectionException | ReflectiveOperationException e) {
                        throw new CreationException(e.getMessage(), e);
                    }
                    return returnValue;
                });
                final Annotation[] qualifiersArray = qualifiers.toArray(new Annotation[qualifiers.size()]);
                event.<JedisPool>addBean().addTransitiveTypeClosure(JedisPool.class).scope(ApplicationScoped.class).addQualifiers(qualifiers).produceWith(instance -> {
                    return produceJedisPool(instance, instanceName, qualifiersArray);
                }).disposeWith((p, instance) -> p.destroy());
                event.<Jedis>addBean().addTransitiveTypeClosure(Jedis.class).scope(Dependent.class).addQualifiers(qualifiers).produceWith(instance -> instance.select(JedisPool.class, qualifiersArray).get().getResource()).disposeWith((j, instance) -> j.close());
            }
        }
    }
}
Also used : Dependent(jakarta.enterprise.context.Dependent) Observes(jakarta.enterprise.event.Observes) SSLParameters(javax.net.ssl.SSLParameters) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) HashMap(java.util.HashMap) ProcessInjectionPoint(jakarta.enterprise.inject.spi.ProcessInjectionPoint) Instance(jakarta.enterprise.inject.Instance) Default(jakarta.enterprise.inject.Default) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Introspector(java.beans.Introspector) BeanInfo(java.beans.BeanInfo) Map(java.util.Map) JedisPool(redis.clients.jedis.JedisPool) HostnameVerifier(javax.net.ssl.HostnameVerifier) Method(java.lang.reflect.Method) BeanManager(jakarta.enterprise.inject.spi.BeanManager) CreationException(jakarta.enterprise.inject.CreationException) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) NamedLiteral(jakarta.enterprise.inject.literal.NamedLiteral) Set(java.util.Set) Config(org.eclipse.microprofile.config.Config) IntrospectionException(java.beans.IntrospectionException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Objects(java.util.Objects) Protocol(redis.clients.jedis.Protocol) ParameterizedType(java.lang.reflect.ParameterizedType) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Type(java.lang.reflect.Type) Provider(jakarta.inject.Provider) Jedis(redis.clients.jedis.Jedis) PropertyDescriptor(java.beans.PropertyDescriptor) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Collections(java.util.Collections) Named(jakarta.inject.Named) IntrospectionException(java.beans.IntrospectionException) CreationException(jakarta.enterprise.inject.CreationException) Dependent(jakarta.enterprise.context.Dependent) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Annotation(java.lang.annotation.Annotation)

Example 2 with Instance

use of jakarta.enterprise.inject.Instance in project helidon by oracle.

the class AnnotatedServiceTest method descriptor.

private static ServiceDescriptor descriptor(Class<?> cls) {
    BeanManager beanManager = mock(BeanManager.class);
    Instance instance = mock(Instance.class);
    when(beanManager.createInstance()).thenReturn(instance);
    GrpcServiceBuilder builder = GrpcServiceBuilder.create(cls, beanManager);
    return builder.build();
}
Also used : Instance(jakarta.enterprise.inject.Instance) BeanManager(jakarta.enterprise.inject.spi.BeanManager)

Example 3 with Instance

use of jakarta.enterprise.inject.Instance in project core by weld.

the class InstanceHandleTest method testGetHandle.

@Test
public void testGetHandle() {
    ActionSequence.reset();
    try (WeldContainer container = new Weld().initialize()) {
        Bean<?> alphaBean = container.getBeanManager().resolve(container.getBeanManager().getBeans(Alpha.class));
        Instance<Alpha> instance = container.select(Alpha.class);
        Instance.Handle<Alpha> alpha1 = instance.getHandle();
        assertEquals(alphaBean, alpha1.getBean());
        assertEquals(Dependent.class, alpha1.getBean().getScope());
        // Contextual reference is obtained lazily
        assertNull(ActionSequence.getSequenceData());
        String alpha2Id;
        // Test try-with-resource
        try (Instance.Handle<Alpha> alpha2 = instance.getHandle()) {
            assertNull(ActionSequence.getSequenceData());
            alpha2Id = alpha2.get().getId();
            assertFalse(alpha1.get().getId().equals(alpha2Id));
        }
        List<String> sequence = ActionSequence.getSequenceData();
        assertEquals(3, sequence.size());
        assertEquals("c" + alpha2Id, sequence.get(0));
        assertEquals("c" + alpha1.get().getId(), sequence.get(1));
        assertEquals("d" + alpha2Id, sequence.get(2));
        alpha1.destroy();
        // Alpha1 destroyed
        sequence = ActionSequence.getSequenceData();
        assertEquals(4, sequence.size());
        // Subsequent invocations are no-op
        alpha1.destroy();
        // Test normal scoped bean is also destroyed
        Instance<Bravo> bravoInstance = container.select(Bravo.class);
        String bravoId = bravoInstance.get().getId();
        try (Instance.Handle<Bravo> bravo = bravoInstance.getHandle()) {
            assertEquals(bravoId, bravo.get().getId());
            ActionSequence.reset();
        }
        sequence = ActionSequence.getSequenceData();
        assertEquals(1, sequence.size());
        assertEquals("d" + bravoId, sequence.get(0));
    }
}
Also used : Instance(jakarta.enterprise.inject.Instance) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 4 with Instance

use of jakarta.enterprise.inject.Instance in project core by weld.

the class InstanceHandleTest method testGetAfterDestroyingContextualInstance.

@Test
public void testGetAfterDestroyingContextualInstance() {
    ActionSequence.reset();
    try (WeldContainer container = new Weld().initialize()) {
        WeldClient client = container.select(WeldClient.class).get();
        assertNotNull(client);
        Instance.Handle<Alpha> alphaHandle = client.getAlphaInstance().getHandle();
        // trigger bean creation and assert
        alphaHandle.get();
        List<String> sequence = ActionSequence.getSequenceData();
        assertEquals(1, sequence.size());
        // trigger bean destruction
        alphaHandle.destroy();
        // verify that the destruction happened
        sequence = ActionSequence.getSequenceData();
        assertEquals(2, sequence.size());
        // try to invoke Handle.get() again; this should throw an exception
        try {
            alphaHandle.get();
            Assert.fail("Invoking Handle.get() after destroying contextual instance should throw an exception.");
        } catch (IllegalStateException e) {
        // expected
        }
    }
}
Also used : Instance(jakarta.enterprise.inject.Instance) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 5 with Instance

use of jakarta.enterprise.inject.Instance in project core by weld.

the class InstanceHandleTest method testHandles.

@Test
public void testHandles() {
    ActionSequence.reset();
    try (WeldContainer container = new Weld().initialize()) {
        Instance<Processor> instance = container.select(Processor.class);
        assertTrue(instance.isAmbiguous());
        for (Instance.Handle<Processor> handle : instance.handles()) {
            handle.get().ping();
            if (handle.getBean().getScope().equals(Dependent.class)) {
                handle.destroy();
            }
        }
        assertEquals(3, ActionSequence.getSequenceSize());
        ActionSequence.assertSequenceDataContainsAll("firstPing", "secondPing", "firstDestroy");
        ActionSequence.reset();
        assertTrue(instance.isAmbiguous());
        for (Iterator<? extends Instance.Handle<Processor>> iterator = instance.handles().iterator(); iterator.hasNext(); ) {
            try (Instance.Handle<Processor> handle = iterator.next()) {
                handle.get().ping();
            }
        }
        assertEquals(4, ActionSequence.getSequenceSize());
        ActionSequence.assertSequenceDataContainsAll("firstPing", "secondPing", "firstDestroy", "secondDestroy");
    }
}
Also used : Instance(jakarta.enterprise.inject.Instance) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Aggregations

Instance (jakarta.enterprise.inject.Instance)8 BeanManager (jakarta.enterprise.inject.spi.BeanManager)3 Weld (org.jboss.weld.environment.se.Weld)3 WeldContainer (org.jboss.weld.environment.se.WeldContainer)3 Test (org.junit.Test)3 FacesException (jakarta.faces.FacesException)2 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 Dependent (jakarta.enterprise.context.Dependent)1 Observes (jakarta.enterprise.event.Observes)1 CreationException (jakarta.enterprise.inject.CreationException)1 Default (jakarta.enterprise.inject.Default)1 NamedLiteral (jakarta.enterprise.inject.literal.NamedLiteral)1 AfterBeanDiscovery (jakarta.enterprise.inject.spi.AfterBeanDiscovery)1 InjectionPoint (jakarta.enterprise.inject.spi.InjectionPoint)1 ProcessInjectionPoint (jakarta.enterprise.inject.spi.ProcessInjectionPoint)1 UIViewRoot (jakarta.faces.component.UIViewRoot)1 Named (jakarta.inject.Named)1 Provider (jakarta.inject.Provider)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1