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());
}
}
}
}
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();
}
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));
}
}
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
}
}
}
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");
}
}
Aggregations