use of javax.enterprise.inject.spi.Bean in project AngularBeans by bessemHmidi.
the class BeanLocator method lookup.
public Object lookup(String beanName, String sessionID) {
NGSessionScopeContext.setCurrentContext(sessionID);
Set<Bean<?>> beans = beanManager.getBeans(beanName);
Class beanClass = CommonUtils.beanNamesHolder.get(beanName);
if (beans.isEmpty()) {
beans = beanManager.getBeans(beanClass, new //
AnnotationLiteral<Any>() {
});
}
Bean bean = beanManager.resolve(beans);
Class scopeAnnotationClass = bean.getScope();
Context context;
if (scopeAnnotationClass.equals(RequestScoped.class)) {
context = beanManager.getContext(scopeAnnotationClass);
if (context == null)
return bean.create(beanManager.createCreationalContext(bean));
} else {
if (scopeAnnotationClass.equals(NGSessionScopeContext.class)) {
context = NGSessionScopeContext.getINSTANCE();
} else {
context = beanManager.getContext(scopeAnnotationClass);
}
}
CreationalContext creationalContext = beanManager.createCreationalContext(bean);
Object reference = context.get(bean, creationalContext);
return reference;
}
use of javax.enterprise.inject.spi.Bean in project AngularBeans by bessemHmidi.
the class NGSessionScopeContext method get.
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
if (holder.get() == null)
return null;
Bean bean = (Bean) contextual;
if (holder.get().getBeans().containsKey(bean.getBeanClass())) {
return (T) holder.get().getBean(bean.getBeanClass()).instance;
} else {
T instance = (T) bean.create(creationalContext);
NGSessionScopeInstance customInstance = new NGSessionScopeInstance();
customInstance.bean = bean;
customInstance.ctx = creationalContext;
customInstance.instance = instance;
holder.get().putBean(customInstance);
return instance;
}
}
use of javax.enterprise.inject.spi.Bean in project cxf by apache.
the class JAXRSCdiResourceExtension method customize.
/**
* Look and apply the available JAXRSServerFactoryBean extensions to customize its
* creation (f.e. add features, providers, assign transport, ...)
* @param beanManager bean manager
* @param bean JAX-RS server factory bean about to be created
*/
private void customize(final BeanManager beanManager, final JAXRSServerFactoryBean bean) {
JAXRSServerFactoryCustomizationUtils.customize(bean);
final Collection<Bean<?>> extensionBeans = beanManager.getBeans(JAXRSServerFactoryCustomizationExtension.class);
for (final Bean<?> extensionBean : extensionBeans) {
final JAXRSServerFactoryCustomizationExtension extension = (JAXRSServerFactoryCustomizationExtension) beanManager.getReference(extensionBean, JAXRSServerFactoryCustomizationExtension.class, createCreationalContext(beanManager, extensionBean));
extension.customize(bean);
}
}
use of javax.enterprise.inject.spi.Bean in project Payara by payara.
the class HealthCheckServletContainerInitializer method onStartup.
@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
// Check if this context is the root one ("/")
if (ctx.getContextPath().isEmpty()) {
// Check if there is already a servlet for healthcheck
Map<String, ? extends ServletRegistration> registrations = ctx.getServletRegistrations();
for (ServletRegistration reg : registrations.values()) {
if (reg.getClass().equals(HealthCheckServlet.class)) {
return;
}
}
// Register servlet
ServletRegistration.Dynamic reg = ctx.addServlet("microprofile-healthcheck-servlet", HealthCheckServlet.class);
reg.addMapping("/health");
}
// Get the BeanManager
BeanManager beanManager = null;
try {
beanManager = CDI.current().getBeanManager();
} catch (Exception ex) {
Logger.getLogger(HealthCheckServletContainerInitializer.class.getName()).log(Level.FINE, "Exception getting BeanManager; this probably isn't a CDI application. " + "No HealthChecks will be registered", ex);
}
// Check for any Beans annotated with @Health
if (beanManager != null) {
HealthCheckService healthCheckService = Globals.getDefaultBaseServiceLocator().getService(HealthCheckService.class);
InvocationManager invocationManager = Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class);
// For each bean annotated with @Health and implementing the HealthCheck interface,
// register it to the HealthCheckService along with the application name
Set<Bean<?>> beans = beanManager.getBeans(HealthCheck.class, new AnnotationLiteral<Health>() {
});
for (Bean<?> bean : beans) {
HealthCheck healthCheck = (HealthCheck) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean));
healthCheckService.registerHealthCheck(invocationManager.getCurrentInvocation().getAppName(), healthCheck);
healthCheckService.registerClassLoader(invocationManager.getCurrentInvocation().getAppName(), healthCheck.getClass().getClassLoader());
Logger.getLogger(HealthCheckServletContainerInitializer.class.getName()).log(Level.INFO, "Registered {0} as a HealthCheck for app: {1}", new Object[] { bean.getBeanClass().getCanonicalName(), invocationManager.getCurrentInvocation().getAppName() });
}
}
}
use of javax.enterprise.inject.spi.Bean in project Payara by payara.
the class CdiInitEventHandler method installAuthenticationMechanism.
public static void installAuthenticationMechanism(AfterBeanDiscovery afterBeanDiscovery) {
afterBeanDiscovery.addBean(new CdiProducer<IdentityStore>().scope(ApplicationScoped.class).beanClass(IdentityStore.class).types(Object.class, IdentityStore.class, SignedJWTIdentityStore.class).addToId("store " + LoginConfig.class).create(e -> new SignedJWTIdentityStore()));
afterBeanDiscovery.addBean(new CdiProducer<HttpAuthenticationMechanism>().scope(ApplicationScoped.class).beanClass(HttpAuthenticationMechanism.class).types(Object.class, HttpAuthenticationMechanism.class, JWTAuthenticationMechanism.class).addToId("mechanism " + LoginConfig.class).create(e -> new JWTAuthenticationMechanism()));
// MP-JWT 1.0 7.1.1. Injection of JsonWebToken
afterBeanDiscovery.addBean(new CdiProducer<JsonWebToken>().scope(RequestScoped.class).beanClass(JsonWebToken.class).types(Object.class, JsonWebToken.class).addToId("token " + LoginConfig.class).create(e -> getJsonWebToken()));
// MP-JWT 1.0 7.1.2
for (JWTInjectableType injectableType : computeTypes()) {
// Add a new Bean<T>/Dynamic producer for each type that 7.1.2 asks
// us to support.
afterBeanDiscovery.addBean(new CdiProducer<Object>().scope(Dependent.class).beanClass(CdiInitEventHandler.class).types(injectableType.getFullType()).qualifiers(new ClaimAnnotationLiteral()).addToId("claim for " + injectableType.getFullType()).create(creationalContext -> {
// Get the qualifier from the injection point
Claim claim = getQualifier(getCurrentInjectionPoint(CdiUtils.getBeanManager(), creationalContext), Claim.class);
String claimName = getClaimName(claim);
// Obtain the raw named value from the request scoped JsonWebToken's embedded claims and convert
// it according to the target type for which this Bean<T> was created.
Object claimObj = injectableType.convert(getJsonWebToken().getClaims().get(claimName));
// into an Optional. I.e. Optional<Long> or ClaimValue<Optional<Long>>
if (injectableType.isOptional()) {
claimObj = Optional.ofNullable(claimObj);
}
// into a ClaimValue, e.g. ClaimValue<Long> or ClaimValue<Optional<Long>>
if (injectableType.isClaimValue()) {
claimObj = new ClaimValueImpl<Object>(claimName, claimObj);
}
return claimObj;
}));
}
}
Aggregations