javax.enterprise.context.NormalScope Java Examples

The following examples show how to use javax.enterprise.context.NormalScope. 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: DestroyableBase.java    From actframework with Apache License 2.0 6 votes vote down vote up
public Class<? extends Annotation> scope() {
    if (null == scope) {
        synchronized (this) {
            if (null == scope) {
                Class<?> c = getClass();
                if (c.isAnnotationPresent(RequestScoped.class)) {
                    scope = RequestScoped.class;
                } else if (c.isAnnotationPresent(SessionScoped.class)) {
                    scope = SessionScoped.class;
                } else if (c.isAnnotationPresent(ApplicationScoped.class)) {
                    scope = ApplicationScoped.class;
                } else {
                    scope = NormalScope.class;
                }
            }
        }
    }
    return scope;
}
 
Example #2
Source File: ContextConfigurator.java    From quarkus with Apache License 2.0 5 votes vote down vote up
ContextConfigurator(Class<? extends Annotation> scopeAnnotation, Consumer<ContextConfigurator> configuratorConsumer) {
    this.consumed = new AtomicBoolean(false);
    this.scopeAnnotation = Objects.requireNonNull(scopeAnnotation);
    this.params = new HashMap<>();
    this.configuratorConsumer = configuratorConsumer;
    this.isNormal = scopeAnnotation.isAnnotationPresent(NormalScope.class);
}
 
Example #3
Source File: ArcContainerImpl.java    From quarkus with Apache License 2.0 5 votes vote down vote up
boolean isNormalScope(Class<? extends Annotation> annotationType) {
    if (annotationType.isAnnotationPresent(NormalScope.class)) {
        return true;
    }
    for (InjectableContext context : contexts) {
        if (context.getScope().equals(annotationType) && context.isNormal()) {
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: ExcludedBeansExtension.java    From weld-junit with Apache License 2.0 5 votes vote down vote up
<T> void excludeBeans(@Observes @WithAnnotations({Scope.class, NormalScope.class}) ProcessAnnotatedType<T> pat) {

        if (excludedBeanClasses.contains(pat.getAnnotatedType().getJavaClass())) {
            pat.veto();
            return;
        }

        Set<Type> typeClosure = pat.getAnnotatedType().getTypeClosure();
        for (Type excludedBeanType : excludedBeanTypes) {
            if (typeClosure.contains(excludedBeanType)) {
                pat.veto();
                return;
            }
        }
    }
 
Example #5
Source File: MockBean.java    From weld-junit with Apache License 2.0 5 votes vote down vote up
private static Set<Annotation> getScopes(AnnotatedElement element) {
    Set<Annotation> scopes = new HashSet<>();
    for (Annotation annotation : element.getAnnotations()) {
        if (annotation.annotationType().isAnnotationPresent(Scope.class) || annotation.annotationType().isAnnotationPresent(NormalScope.class)) {
            scopes.add(annotation);
        }
    }
    return scopes;
}
 
Example #6
Source File: AbstractWeldInitiator.java    From weld-junit with Apache License 2.0 5 votes vote down vote up
/**
 * Activate and deactivate contexts for the given normal scopes for the lifetime of the initialized Weld container, by default for each test method
 * execution.
 * <p>
 * {@link ApplicationScoped} is ignored as it is always active.
 * </p>
 *
 * @param normalScopes
 * @return self
 */
@SafeVarargs
public final T activate(Class<? extends Annotation>... normalScopes) {
    for (Class<? extends Annotation> scope : normalScopes) {
        if (ApplicationScoped.class.equals(scope)) {
            continue;
        }
        if (!scope.isAnnotationPresent(NormalScope.class)) {
            throw new IllegalArgumentException("Only annotations annotated with @NormalScope are supported!");
        }
        this.scopesToActivate.add(scope);
    }
    return self();
}
 
Example #7
Source File: CdiResourceProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    final Class beanClass = Object.class;
    when(bean.getBeanClass()).thenReturn(beanClass);
    when(beanManager.isNormalScope(any())).thenAnswer(invocationOnMock ->
            Class.class.cast(invocationOnMock.getArguments()[0]).isAnnotationPresent(NormalScope.class));
    when(beanManager.getReference(eq(bean), eq(beanClass), any()))
            .thenAnswer(i -> new Object()); // ensure to create one instance per call, this is what we test
}
 
Example #8
Source File: RestClientCdiTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawTypes"})
public void testCreationalContextsNotReleasedOnClientCloseUsingNormalScope() throws Exception {
    IMocksControl control = EasyMock.createStrictControl();
    BeanManager mockedBeanMgr = control.createMock(BeanManager.class);
    CreationalContext<?> mockedCreationalCtx = control.createMock(CreationalContext.class);
    Bean<?> mockedBean = control.createMock(Bean.class);
    List<String> stringList = new ArrayList<>(Collections.singleton("xyz"));

    EasyMock.expect(mockedBeanMgr.getBeans(List.class))
            .andReturn(Collections.singleton(mockedBean));
    EasyMock.expect(mockedBeanMgr.createCreationalContext(mockedBean))
            .andReturn((CreationalContext) mockedCreationalCtx);
    EasyMock.expect(mockedBeanMgr.getReference(mockedBean, List.class, mockedCreationalCtx))
            .andReturn(stringList);
    EasyMock.expect(mockedBean.getScope())
            .andReturn((Class) NormalScope.class);
    EasyMock.expect(mockedBeanMgr.isNormalScope(NormalScope.class))
            .andReturn(true);
    control.replay();

    Bus bus = new ExtensionManagerBus();
    bus.setExtension(mockedBeanMgr, BeanManager.class);

    Instance<List> i = CDIUtils.getInstanceFromCDI(List.class, bus);

    i.release();

    control.verify();
}
 
Example #9
Source File: ArcContainerImpl.java    From quarkus with Apache License 2.0 4 votes vote down vote up
boolean isScope(Class<? extends Annotation> annotationType) {
    if (annotationType.isAnnotationPresent(Scope.class) || annotationType.isAnnotationPresent(NormalScope.class)) {
        return true;
    }
    return contexts.stream().map(InjectableContext::getScope).filter(annotationType::equals).findAny().isPresent();
}
 
Example #10
Source File: ClassScanning.java    From weld-junit with Apache License 2.0 4 votes vote down vote up
private static boolean hasBeanDefiningAnnotation(Class<?> clazz) {
    return isAnnotated(clazz, NormalScope.class) || isAnnotated(clazz, Dependent.class) ||
            isAnnotated(clazz, Interceptor.class) || isAnnotated(clazz, Decorator.class) ||
            isAnnotated(clazz, Stereotype.class);
}
 
Example #11
Source File: InjectableContext.java    From quarkus with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @return {@code true} if this context represents a normal scope
 */
default boolean isNormal() {
    return getScope().isAnnotationPresent(NormalScope.class);
}