Java Code Examples for javax.enterprise.inject.spi.BeanManager#isNormalScope()

The following examples show how to use javax.enterprise.inject.spi.BeanManager#isNormalScope() . 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: JpaExtension.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private PersistenceUnitInfo tryCreateDefaultPersistenceUnit(final String unitName, final BeanManager bm, final Map<String, String> props) {
    final Set<Bean<?>> beans = bm.getBeans(DataSource.class);
    final Bean<?> bean = bm.resolve(beans);
    if (bean == null || !bm.isNormalScope(bean.getScope())) {
        return null;
    }

    final DataSource ds = DataSource.class.cast(bm.getReference(bean, DataSource.class, bm.createCreationalContext(null)));

    final PersistenceUnitInfoBuilder builder = new PersistenceUnitInfoBuilder()
            .setManagedClassNames(jpaClasses)
            .setExcludeUnlistedClasses(true)
            .setUnitName(unitName)
            .setDataSource(ds);

    props.forEach(builder::addProperty);

    return builder.toInfo();
}
 
Example 2
Source File: CDIBatchArtifactFactory.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
@Override
public Instance load(final String batchId) {
    final BeanManager bm = getBeanManager();
    if (bm == null) {
        return super.load(batchId);
    }

    final Set<Bean<?>> beans = bm.getBeans(batchId);
    final Bean<?> bean = bm.resolve(beans);
    if (bean == null) { // fallback to try to instantiate it from TCCL as per the spec
        return super.load(batchId);
    }
    final Class<?> clazz = bean.getBeanClass();
    final CreationalContext creationalContext = bm.createCreationalContext(bean);
    final Object artifactInstance = bm.getReference(bean, clazz, creationalContext);
    if (Dependent.class.equals(bean.getScope()) || !bm.isNormalScope(bean.getScope())) { // need to be released
        return new Instance(artifactInstance, new Closeable() {
            @Override
            public void close() throws IOException {
                creationalContext.release();
            }
        });
    }
    return new Instance(artifactInstance, null);
}
 
Example 3
Source File: EntityManagerRefLookup.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
private synchronized void initGlobalEntityManager()
{
    // switch into paranoia mode
    if (this.globalEntityManagerInitialized == null)
    {
        BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
        Set<Bean<?>> beans = beanManager.getBeans(EntityManager.class);
        Bean<?> bean = beanManager.resolve(beans);

        if (bean == null)
        {
            throw new IllegalStateException("Could not find EntityManager with default qualifier.");
        }
        
        globalEntityManagerIsNormalScope = beanManager.isNormalScope(bean.getScope());
        if (globalEntityManagerIsNormalScope)
        {
            globalEntityManager = (EntityManager) beanManager.getReference(bean,
                    EntityManager.class,
                    beanManager.createCreationalContext(bean));       
        }

        this.globalEntityManagerInitialized = true;
    }
}
 
Example 4
Source File: EntityManagerBean.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
private Object findValidatorFactory(final BeanManager bm) {
    try {
        final Class<?> type = Thread.currentThread().getContextClassLoader().loadClass("javax.validation.ValidatorFactory");
        final Bean<?> bean = bm.resolve(bm.getBeans(type));
        if (bean == null || !bm.isNormalScope(bean.getScope())) {
            return null;
        }
        return bm.getReference(bean, type, bm.createCreationalContext(null));
    } catch (final NoClassDefFoundError | ClassNotFoundException e) {
        return null;
    }
}
 
Example 5
Source File: CDIUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> Instance<T> findBean(Class<T> clazz, Bus bus) {
    BeanManager beanManager = bus == null ? getCurrentBeanManager() : getCurrentBeanManager(bus);
    Bean<?> bean = beanManager.getBeans(clazz).iterator().next();
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    Instance<T> instance = new Instance<>((T) beanManager.getReference(bean, clazz, ctx), 
        beanManager.isNormalScope(bean.getScope()) ? () -> { } : ctx::release);
    return instance;
}
 
Example 6
Source File: OpenEJBLifecycle.java    From tomee with Apache License 2.0 5 votes vote down vote up
private void starts(final BeanManager beanManager, final Class<?> clazz) {
    final Bean<?> bean = beanManager.resolve(beanManager.getBeans(clazz));
    if (!beanManager.isNormalScope(bean.getScope())) {
        throw new IllegalStateException("Only normal scoped beans can use @Startup - likely @ApplicationScoped");
    }

    final CreationalContext<Object> creationalContext = beanManager.createCreationalContext(null);
    beanManager.getReference(bean, clazz, creationalContext).toString();
    // don't release now, will be done by the context - why we restrict it to normal scoped beans
}
 
Example 7
Source File: MBeanExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private boolean isNormalScope(final Set<Annotation> annotations, final BeanManager bm)
{
    for (Annotation annotation : annotations)
    {
        if (bm.isNormalScope(annotation.annotationType()))
        {
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: JAXRSCdiResourceExtension.java    From cxf with Apache License 2.0 4 votes vote down vote up
boolean isCxfSingleton(final BeanManager beanManager, final Bean<?> bean) {
    return beanManager.isNormalScope(bean.getScope()) || isConsideredSingleton(bean.getScope());
}
 
Example 9
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);
}