Search in sources :

Example 66 with Bean

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;
}
Also used : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) CreationalContext(javax.enterprise.context.spi.CreationalContext) AnnotationLiteral(javax.enterprise.util.AnnotationLiteral) Bean(javax.enterprise.inject.spi.Bean)

Example 67 with Bean

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;
    }
}
Also used : NGSessionScopeInstance(angularBeans.context.NGSessionContextHolder.NGSessionScopeInstance) Bean(javax.enterprise.inject.spi.Bean)

Example 68 with Bean

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);
    }
}
Also used : JAXRSServerFactoryCustomizationExtension(org.apache.cxf.jaxrs.ext.JAXRSServerFactoryCustomizationExtension) ProcessBean(javax.enterprise.inject.spi.ProcessBean) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) Bean(javax.enterprise.inject.spi.Bean)

Example 69 with 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() });
        }
    }
}
Also used : HealthCheckService(fish.payara.microprofile.healthcheck.HealthCheckService) Health(org.eclipse.microprofile.health.Health) InvocationManager(org.glassfish.api.invocation.InvocationManager) HealthCheck(org.eclipse.microprofile.health.HealthCheck) ServletException(javax.servlet.ServletException) Bean(javax.enterprise.inject.spi.Bean) ServletRegistration(javax.servlet.ServletRegistration) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 70 with Bean

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;
        }));
    }
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) ClaimValue(org.eclipse.microprofile.jwt.ClaimValue) IdentityStore(javax.security.enterprise.identitystore.IdentityStore) ClaimValueImpl(fish.payara.microprofile.jwtauth.jwt.ClaimValueImpl) CdiProducer(org.glassfish.soteria.cdi.CdiProducer) LoginConfig(org.eclipse.microprofile.auth.LoginConfig) HashSet(java.util.HashSet) CreationalContext(javax.enterprise.context.spi.CreationalContext) JsonStructure(javax.json.JsonStructure) SecurityContext(javax.security.enterprise.SecurityContext) JsonNumber(javax.json.JsonNumber) SignedJWTIdentityStore(fish.payara.microprofile.jwtauth.eesecurity.SignedJWTIdentityStore) Arrays.asList(java.util.Arrays.asList) JWTAuthenticationMechanism(fish.payara.microprofile.jwtauth.eesecurity.JWTAuthenticationMechanism) ClaimAnnotationLiteral(fish.payara.microprofile.jwtauth.jwt.ClaimAnnotationLiteral) HttpAuthenticationMechanism(javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism) Collectors.toSet(java.util.stream.Collectors.toSet) Collections.emptyMap(java.util.Collections.emptyMap) JsonObject(javax.json.JsonObject) CdiUtils.getBeanReference(org.glassfish.soteria.cdi.CdiUtils.getBeanReference) JsonArray(javax.json.JsonArray) Set(java.util.Set) Claim(org.eclipse.microprofile.jwt.Claim) JsonWebTokenImpl(fish.payara.microprofile.jwtauth.jwt.JsonWebTokenImpl) JsonString(javax.json.JsonString) Dependent(javax.enterprise.context.Dependent) RequestScoped(javax.enterprise.context.RequestScoped) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) JWTInjectableType(fish.payara.microprofile.jwtauth.jwt.JWTInjectableType) Collections(java.util.Collections) Bean(javax.enterprise.inject.spi.Bean) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) CdiUtils(org.glassfish.soteria.cdi.CdiUtils) BeanManager(javax.enterprise.inject.spi.BeanManager) HttpAuthenticationMechanism(javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism) RequestScoped(javax.enterprise.context.RequestScoped) ClaimAnnotationLiteral(fish.payara.microprofile.jwtauth.jwt.ClaimAnnotationLiteral) SignedJWTIdentityStore(fish.payara.microprofile.jwtauth.eesecurity.SignedJWTIdentityStore) JsonString(javax.json.JsonString) ClaimValueImpl(fish.payara.microprofile.jwtauth.jwt.ClaimValueImpl) ApplicationScoped(javax.enterprise.context.ApplicationScoped) JWTInjectableType(fish.payara.microprofile.jwtauth.jwt.JWTInjectableType) JsonWebToken(org.eclipse.microprofile.jwt.JsonWebToken) CdiProducer(org.glassfish.soteria.cdi.CdiProducer) LoginConfig(org.eclipse.microprofile.auth.LoginConfig) JsonObject(javax.json.JsonObject) JWTAuthenticationMechanism(fish.payara.microprofile.jwtauth.eesecurity.JWTAuthenticationMechanism) IdentityStore(javax.security.enterprise.identitystore.IdentityStore) SignedJWTIdentityStore(fish.payara.microprofile.jwtauth.eesecurity.SignedJWTIdentityStore) Claim(org.eclipse.microprofile.jwt.Claim)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)74 BeanManager (javax.enterprise.inject.spi.BeanManager)33 Annotation (java.lang.annotation.Annotation)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)8 CreationalContext (javax.enterprise.context.spi.CreationalContext)7 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)7 Test (org.junit.Test)7 Set (java.util.Set)5 ProcessBean (javax.enterprise.inject.spi.ProcessBean)5 ScopedInstance (org.eclipse.jetty.cdi.core.ScopedInstance)5 IOException (java.io.IOException)4 Type (java.lang.reflect.Type)4 Map (java.util.Map)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)3 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)3 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)3