Java Code Examples for javax.enterprise.inject.spi.Bean#getScope()

The following examples show how to use javax.enterprise.inject.spi.Bean#getScope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BeanLocator.java    From AngularBeans with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);

		// if(reference==null && scopeAnnotationClass.equals(RequestScoped.class)){
		// reference= bean.create(beanManager.createCreationalContext(bean));
		// }

		return reference;
	}
 
Example 2
Source File: JAXRSCdiResourceExtension.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * JAX-RS application has defined singletons as being classes of any providers, resources and features.
 * In the JAXRSServerFactoryBean, those should be split around several method calls depending on instance
 * type. At the moment, only the Feature is CXF-specific and should be replaced by JAX-RS Feature implementation.
 * @param application the application instance
 * @return classified instances of classes by instance types
 */
private ClassifiedClasses classes2singletons(final Application application, final BeanManager beanManager) {
    final ClassifiedClasses classified = new ClassifiedClasses();

    // now loop through the classes
    Set<Class<?>> classes = application.getClasses();
    if (!classes.isEmpty()) {
        classified.addProviders(loadProviders(beanManager, classes));
        classified.addFeatures(loadFeatures(beanManager, classes));

        for (final Bean< ? > bean: serviceBeans) {
            if (classes.contains(bean.getBeanClass())) {
                // normal scoped beans will return us a proxy in getInstance so it is singletons for us,
                // @Singleton is indeed a singleton
                // @Dependent should be a request scoped instance but for backward compat we kept it a singleton
                //
                // other scopes are considered request scoped (for jaxrs)
                // and are created per request (getInstance/releaseInstance)
                final ResourceProvider resourceProvider;
                if (isCxfSingleton(beanManager, bean)) {
                    final Lifecycle lifecycle = new Lifecycle(beanManager, bean);
                    resourceProvider = new SingletonResourceProvider(lifecycle, bean.getBeanClass());

                    // if not a singleton we manage it per request
                    // if @Singleton the container handles it
                    // so we only need this case here
                    if (Dependent.class == bean.getScope()) {
                        disposableLifecycles.add(lifecycle);
                    }
                } else {
                    resourceProvider = new PerRequestResourceProvider(
                    () -> new Lifecycle(beanManager, bean), bean.getBeanClass());
                }
                classified.addResourceProvider(resourceProvider);
            }
        }
    }

    return classified;
}
 
Example 3
Source File: CDIJCacheHelper.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
private <T> T instance(final Class<T> type)
{
    final Set<Bean<?>> beans = beanManager.getBeans(type);
    if (beans.isEmpty())
    {
        if (CacheKeyGenerator.class == type) {
            return (T) defaultCacheKeyGenerator;
        }
        if (CacheResolverFactory.class == type) {
            return (T) defaultCacheResolverFactory();
        }
        return null;
    }
    final Bean<?> bean = beanManager.resolve(beans);
    final CreationalContext<?> context = beanManager.createCreationalContext(bean);
    final Class<? extends Annotation> scope = bean.getScope();
    final boolean normalScope = beanManager.isNormalScope(scope);
    try
    {
        final Object reference = beanManager.getReference(bean, bean.getBeanClass(), context);
        if (!normalScope)
        {
            toRelease.add(context);
        }
        return (T) reference;
    }
    finally
    {
        if (normalScope)
        { // TODO: release at the right moment, @PreDestroy? question is: do we assume it is thread safe?
            context.release();
        }
    }
}
 
Example 4
Source File: BeanClassRefreshAgent.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isReinjectingContext(Bean<?> bean) {
    return bean.getScope() != RequestScoped.class && bean.getScope() != Dependent.class;
}
 
Example 5
Source File: BeanReloadExecutor.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isReinjectingContext(Bean<?> bean) {
    return bean.getScope() != RequestScoped.class && bean.getScope() != Dependent.class;
}
 
Example 6
Source File: CdiPlugin.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public Object getSessionBeanProxy(final Bean<?> inBean, final Class<?> interfce, final CreationalContext<?> creationalContext) {
    Object instance = cacheProxies.get(inBean);
    if (instance != null) {
        return instance;
    }

    synchronized (inBean) { // singleton for the app so safe to sync on it
        instance = cacheProxies.get(inBean);
        if (instance != null) {
            return instance;
        }

        final Class<? extends Annotation> scopeClass = inBean.getScope();
        final CdiEjbBean<Object> cdiEjbBean = (CdiEjbBean<Object>) inBean;
        final CreationalContext<Object> cc = (CreationalContext<Object>) creationalContext;

        if (scopeClass == null || Dependent.class == scopeClass) { // no need to add any layer, null = @New
            return cdiEjbBean.createEjb(cc);
        }

        // only stateful normally
        final InstanceBean<Object> bean = new InstanceBean<>(cdiEjbBean);
        if (webBeansContext.getBeanManagerImpl().isNormalScope(scopeClass)) {
            final BeanContext beanContext = cdiEjbBean.getBeanContext();
            final Provider provider = webBeansContext.getNormalScopeProxyFactory().getInstanceProvider(beanContext.getClassLoader(), cdiEjbBean);

            if (!beanContext.isLocalbean()) {
                final List<Class> interfaces = new ArrayList<>();
                final InterfaceType type = beanContext.getInterfaceType(interfce);
                if (type != null) {
                    interfaces.addAll(beanContext.getInterfaces(type));
                } else { // can happen when looked up from impl instead of API in OWB -> default to business local
                    interfaces.addAll(beanContext.getInterfaces(InterfaceType.BUSINESS_LOCAL));
                }
                interfaces.add(Serializable.class);
                interfaces.add(IntraVmProxy.class);
                if (BeanType.STATEFUL.equals(beanContext.getComponentType()) || BeanType.MANAGED.equals(beanContext.getComponentType())) {
                    interfaces.add(BeanContext.Removable.class);
                }

                try {
                    instance = ProxyManager.newProxyInstance(interfaces.toArray(new Class<?>[interfaces.size()]), new InvocationHandler() {
                        @Override
                        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                            try {
                                return method.invoke(provider.get(), args);
                            } catch (final InvocationTargetException ite) {
                                throw ite.getCause();
                            }
                        }
                    });
                } catch (final IllegalAccessException e) {
                    throw new OpenEJBRuntimeException(e);
                }

            } else {
                final NormalScopeProxyFactory normalScopeProxyFactory = webBeansContext.getNormalScopeProxyFactory();
                final Class<?> proxyClass = normalScopeProxyFactory.createProxyClass(beanContext.getClassLoader(), beanContext.getBeanClass());
                instance = normalScopeProxyFactory.createProxyInstance(proxyClass, provider);
            }

            cacheProxies.put(inBean, instance);
        } else {
            final Context context = webBeansContext.getBeanManagerImpl().getContext(scopeClass);
            instance = context.get(bean, cc);
        }
        bean.setOwbProxy(instance);
        return instance;
    }
}
 
Example 7
Source File: ConverterAndValidatorProxyExtension.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
protected <X> boolean hasNormalScopeAnnotation(Bean<X> bean, BeanManager beanManager)
{
    Class<? extends Annotation> scopeAnnotationClass = bean.getScope();
    return scopeAnnotationClass != null && beanManager.isNormalScope(scopeAnnotationClass);
}