use of jakarta.enterprise.inject.spi.BeanManager in project cdi-tck by eclipse-ee4j.
the class ActivateRequestContextProgrammaticallyTest method programmaticRequestContextActivation.
@Test
@SpecAssertions({ @SpecAssertion(section = ACTIVATING_REQUEST_CONTEXT, id = "a"), @SpecAssertion(section = ACTIVATING_REQUEST_CONTEXT, id = "c"), @SpecAssertion(section = ACTIVATING_REQUEST_CONTEXT, id = "da"), @SpecAssertion(section = ACTIVATING_REQUEST_CONTEXT, id = "e") })
public void programmaticRequestContextActivation() {
SeContainerInitializer seContainerInitializer = SeContainerInitializer.newInstance();
try (SeContainer container = seContainerInitializer.initialize()) {
TestContextActivator contextActivator = container.select(TestContextActivator.class).get();
boolean activated = contextActivator.activate();
Assert.assertTrue(activated);
Assert.assertFalse(contextActivator.activate());
contextActivator.callRequestScopeBean();
RequestScopeCounter counter = container.select(RequestScopeCounter.class).get();
Assert.assertEquals(contextActivator.callRequestScopeBean(), 2);
contextActivator.deactivate();
contextActivator.activate();
BeanManager beanManager = container.getBeanManager();
Assert.assertTrue(beanManager.getContext(RequestScoped.class).isActive());
Assert.assertEquals(contextActivator.callRequestScopeBean(), 1);
contextActivator.deactivate();
try {
beanManager.getContext(RequestScoped.class).isActive();
Assert.fail(ContextNotActiveException.class.getSimpleName() + " was not thrown !");
} catch (ContextNotActiveException e) {
}
}
}
use of jakarta.enterprise.inject.spi.BeanManager in project cdi-tck by eclipse-ee4j.
the class InjectionOfResourceTest method testProduceResourceProxy.
@Test
@SpecAssertions({ @SpecAssertion(section = RESOURCE_LIFECYCLE, id = "la"), @SpecAssertion(section = RESOURCE_LIFECYCLE, id = "ma"), @SpecAssertion(section = RESOURCE_LIFECYCLE, id = "o") })
public void testProduceResourceProxy() {
Bean<BeanManager> beanManagerBean = getBeans(BeanManager.class, new AnnotationLiteral<Another>() {
}).iterator().next();
CreationalContext<BeanManager> beanManagerCc = getCurrentManager().createCreationalContext(beanManagerBean);
BeanManager beanManager = beanManagerBean.create(beanManagerCc);
assert beanManager != null;
}
use of jakarta.enterprise.inject.spi.BeanManager in project quarkus by quarkusio.
the class QuarkusRestClientBuilder method build.
public <T> T build(Class<T> aClass, ClientHttpEngine httpEngine) throws IllegalStateException, RestClientDefinitionException {
RestClientListeners.get().forEach(listener -> listener.onNewClient(aClass, this));
// Interface validity
verifyInterface(aClass);
if (baseURI == null) {
throw new IllegalStateException("Neither baseUri nor baseUrl was specified");
}
// Provider annotations
RegisterProvider[] providers = aClass.getAnnotationsByType(RegisterProvider.class);
for (RegisterProvider provider : providers) {
register(provider.value(), provider.priority());
}
// Default exception mapper
if (!isMapperDisabled()) {
register(DefaultResponseExceptionMapper.class);
}
builderDelegate.register(new ExceptionMapping(localProviderInstances), 1);
ClassLoader classLoader = aClass.getClassLoader();
T actualClient;
ResteasyClient client;
ResteasyClientBuilder resteasyClientBuilder = builderDelegate;
if (this.proxyHost != null) {
resteasyClientBuilder.defaultProxy(proxyHost, this.proxyPort);
} else {
String userProxyHost = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_HOST)).filter(String.class::isInstance).map(String.class::cast).orElse(null);
Integer userProxyPort = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_PORT)).filter(Integer.class::isInstance).map(Integer.class::cast).orElse(null);
// configuration of the builder has precedence over environment variables
if (userProxyHost != null && userProxyPort != null) {
String userProxyScheme = Optional.ofNullable(getConfiguration().getProperty(PROPERTY_PROXY_SCHEME)).filter(String.class::isInstance).map(String.class::cast).orElse(null);
resteasyClientBuilder.defaultProxy(userProxyHost, userProxyPort, userProxyScheme);
} else {
selectHttpProxy().ifPresent(proxyAddress -> resteasyClientBuilder.defaultProxy(proxyAddress.getHostString(), proxyAddress.getPort()));
}
}
if (this.executorService != null) {
resteasyClientBuilder.executorService(this.executorService);
} else {
this.executorService = ContextualExecutors.threadPool();
resteasyClientBuilder.executorService(executorService, !executorService.isManaged());
}
resteasyClientBuilder.register(DEFAULT_MEDIA_TYPE_FILTER);
resteasyClientBuilder.register(METHOD_INJECTION_FILTER);
resteasyClientBuilder.register(HEADERS_REQUEST_FILTER);
register(new MpPublisherMessageBodyReader(executorService));
resteasyClientBuilder.sslContext(sslContext);
resteasyClientBuilder.trustStore(trustStore);
resteasyClientBuilder.keyStore(keyStore, keystorePassword);
resteasyClientBuilder.hostnameVerifier(hostnameVerifier);
resteasyClientBuilder.setIsTrustSelfSignedCertificates(false);
checkQueryParamStyleProperty(aClass);
checkFollowRedirectProperty(aClass);
resteasyClientBuilder.setFollowRedirects(followRedirect);
if (readTimeout != null) {
resteasyClientBuilder.readTimeout(readTimeout, readTimeoutUnit);
}
if (connectTimeout != null) {
resteasyClientBuilder.connectTimeout(connectTimeout, connectTimeoutUnit);
}
if (httpEngine != null) {
resteasyClientBuilder.httpEngine(httpEngine);
} else {
boolean registerEngine = false;
for (Object p : getBuilderDelegate().getProviderFactory().getProviderInstances()) {
if (p instanceof ClientHttpEngine) {
resteasyClientBuilder.httpEngine((ClientHttpEngine) p);
registerEngine = true;
break;
}
}
if (!registerEngine && useURLConnection()) {
resteasyClientBuilder.httpEngine(new URLConnectionClientEngineBuilder().resteasyClientBuilder(resteasyClientBuilder).build());
resteasyClientBuilder.sslContext(null);
resteasyClientBuilder.trustStore(null);
resteasyClientBuilder.keyStore(null, "");
}
}
configureTrustAll(resteasyClientBuilder);
// because otherwise Apache HTTP Client will try to initialize one and will fail
if (ImageInfo.inImageRuntimeCode() && !SslContextConfiguration.isSslNativeEnabled()) {
resteasyClientBuilder.sslContext(new DisabledSSLContext());
}
if (!invocationInterceptorFactories.isEmpty()) {
resteasyClientBuilder.register(new AsyncInvocationInterceptorThreadContext(invocationInterceptorFactories));
}
client = resteasyClientBuilder.build();
((MpClient) client).setQueryParamStyle(queryParamStyle);
client.register(AsyncInterceptorRxInvokerProvider.class);
actualClient = client.target(baseURI).proxyBuilder(aClass).classloader(classLoader).defaultConsumes(MediaType.APPLICATION_JSON).defaultProduces(MediaType.APPLICATION_JSON).build();
Class<?>[] interfaces = new Class<?>[3];
interfaces[0] = aClass;
interfaces[1] = RestClientProxy.class;
interfaces[2] = Closeable.class;
final BeanManager beanManager = getBeanManager();
T proxy = (T) Proxy.newProxyInstance(classLoader, interfaces, new ProxyInvocationHandler(aClass, actualClient, getLocalProviderInstances(), client, beanManager));
ClientHeaderProviders.registerForClass(aClass, proxy, beanManager);
return proxy;
}
use of jakarta.enterprise.inject.spi.BeanManager 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.spi.BeanManager in project helidon by oracle.
the class MainTest method testEvents.
@Test
void testEvents() {
// build time
HelidonContainer instance = HelidonContainer.instance();
BeanManager beanManager = CDI.current().getBeanManager();
TestExtension extension = beanManager.getExtension(TestExtension.class);
assertThat(extension.runtimeConfig(), nullValue());
assertThat(extension.events(), contains(TestExtension.BUILD_TIME_START, TestExtension.BUILD_TIME_END));
Config config = ConfigProviderResolver.instance().getBuilder().withSources(MpConfigSources.create(Map.of("key", "value"))).build();
ConfigProviderResolver.instance().registerConfig(config, Thread.currentThread().getContextClassLoader());
instance.start();
Object runtimeConfig = extension.runtimeConfig();
assertThat(runtimeConfig, instanceOf(Config.class));
Config mpConfig = (Config) runtimeConfig;
try {
mpConfig = ((Config) runtimeConfig).unwrap(Config.class);
} catch (Exception ignored) {
}
assertThat(mpConfig, sameInstance(config));
instance.shutdown();
assertThat(extension.events(), is(List.of(TestExtension.BUILD_TIME_START, TestExtension.BUILD_TIME_END, TestExtension.RUNTIME_INIT, TestExtension.APPLICATION_INIT, TestExtension.APPLICATION_BEFORE_DESTROYED, TestExtension.APPLICATION_DESTROYED)));
}
Aggregations