Search in sources :

Example 1 with DefinitionException

use of org.exoplatform.container.context.DefinitionException in project kernel by exoplatform.

the class TestExoContainer method testScope.

private void testScope(RootContainer container, ContextManager manager, boolean oldComponents) throws Exception {
    assertFalse(ContainerUtil.isSingleton(getClass(1, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(2, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(3, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(4, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(5, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(6, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(7, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(8, oldComponents)));
    assertTrue(ContainerUtil.isSingleton(getClass(9, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(10, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(11, oldComponents)));
    assertTrue(ContainerUtil.isSingleton(getClass(12, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(13, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(14, oldComponents)));
    try {
        ContainerUtil.isSingleton(getClass(15, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.isSingleton(getClass(16, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.isSingleton(getClass(17, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.isSingleton(getClass(18, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.isSingleton(getClass(19, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.isSingleton(getClass(20, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    assertTrue(ContainerUtil.isSingleton(getClass(21, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(22, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(23, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(24, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(25, oldComponents)));
    assertFalse(ContainerUtil.isSingleton(getClass(26, oldComponents)));
    assertNull(ContainerUtil.getScope(getClass(1, oldComponents), true));
    assertNull(ContainerUtil.getScope(getClass(2, oldComponents), true));
    assertSame(Singleton.class, ContainerUtil.getScope(getClass(3, oldComponents), true));
    assertSame(Singleton.class, ContainerUtil.getScope(getClass(4, oldComponents), true));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(23, oldComponents), true));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(24, oldComponents), true));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(25, oldComponents), true));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(26, oldComponents), true));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(1, oldComponents)));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(2, oldComponents)));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(3, oldComponents)));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(4, oldComponents)));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(5, oldComponents)));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(6, oldComponents)));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(7, oldComponents)));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(8, oldComponents)));
    assertSame(Singleton.class, ContainerUtil.getScope(getClass(9, oldComponents)));
    assertSame(Dependent.class, ContainerUtil.getScope(getClass(10, oldComponents)));
    assertSame(RequestScoped.class, ContainerUtil.getScope(getClass(11, oldComponents)));
    assertSame(Singleton.class, ContainerUtil.getScope(getClass(12, oldComponents)));
    assertSame(Dependent.class, ContainerUtil.getScope(getClass(13, oldComponents)));
    assertSame(RequestScoped.class, ContainerUtil.getScope(getClass(14, oldComponents)));
    try {
        ContainerUtil.getScope(getClass(15, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.getScope(getClass(16, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.getScope(getClass(17, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.getScope(getClass(18, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.getScope(getClass(19, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    try {
        ContainerUtil.getScope(getClass(20, oldComponents));
    } catch (DefinitionException e) {
    // OK
    }
    assertSame(Singleton.class, ContainerUtil.getScope(getClass(21, oldComponents)));
    assertSame(Dependent.class, ContainerUtil.getScope(getClass(22, oldComponents)));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(23, oldComponents)));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(24, oldComponents)));
    assertSame(MyNormalScope.class, ContainerUtil.getScope(getClass(25, oldComponents)));
    assertSame(MyPseudoScope.class, ContainerUtil.getScope(getClass(26, oldComponents)));
    container.registerComponentImplementation(getClass(1, oldComponents));
    container.registerComponentImplementation(getClass(2, oldComponents));
    container.registerComponentImplementation(getClass(3, oldComponents));
    container.registerComponentImplementation(getClass(4, oldComponents));
    container.registerComponentImplementation(getClass(5, oldComponents));
    container.registerComponentImplementation(getClass(6, oldComponents));
    container.registerComponentImplementation(getClass(7, oldComponents));
    container.registerComponentImplementation(getClass(8, oldComponents));
    container.registerComponentImplementation(getClass(9, oldComponents));
    container.registerComponentImplementation(getClass(10, oldComponents));
    container.registerComponentImplementation(getClass(11, oldComponents));
    container.registerComponentImplementation(getClass(12, oldComponents));
    container.registerComponentImplementation(getClass(13, oldComponents));
    container.registerComponentImplementation(getClass(14, oldComponents));
    container.registerComponentImplementation(getClass(15, oldComponents));
    container.registerComponentImplementation(getClass(16, oldComponents));
    container.registerComponentImplementation(getClass(17, oldComponents));
    container.registerComponentImplementation(getClass(18, oldComponents));
    container.registerComponentImplementation(getClass(19, oldComponents));
    container.registerComponentImplementation(getClass(20, oldComponents));
    container.registerComponentImplementation(getClass(21, oldComponents));
    container.registerComponentImplementation(getClass(22, oldComponents));
    container.registerComponentImplementation(getClass(23, oldComponents));
    container.registerComponentImplementation(getClass(24, oldComponents));
    container.registerComponentImplementation(getClass(25, oldComponents));
    container.registerComponentImplementation(getClass(26, oldComponents));
    assertNotNull(container.getComponentInstanceOfType(getClass(1, oldComponents)));
    assertNotNull(container.getComponentInstanceOfType(getClass(2, oldComponents)));
    if (oldComponents) {
        assertSame(container.getComponentInstanceOfType(getClass(1, oldComponents)), container.getComponentInstanceOfType(getClass(1, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(1, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(1, oldComponents)).getId());
        assertSame(container.getComponentInstanceOfType(getClass(2, oldComponents)), container.getComponentInstanceOfType(getClass(2, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(2, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(2, oldComponents)).getId());
    } else {
        assertNotSame(container.getComponentInstanceOfType(getClass(1, oldComponents)), container.getComponentInstanceOfType(getClass(1, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(1, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(1, oldComponents)).getId());
        assertNotSame(container.getComponentInstanceOfType(getClass(2, oldComponents)), container.getComponentInstanceOfType(getClass(2, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(2, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(2, oldComponents)).getId());
    }
    assertNotNull(container.getComponentInstanceOfType(getClass(3, oldComponents)));
    assertSame(container.getComponentInstanceOfType(getClass(3, oldComponents)), container.getComponentInstanceOfType(getClass(3, oldComponents)));
    assertEquals(container.getComponentInstanceOfType(getClass(3, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(3, oldComponents)).getId());
    assertNotNull(container.getComponentInstanceOfType(getClass(4, oldComponents)));
    assertSame(container.getComponentInstanceOfType(getClass(4, oldComponents)), container.getComponentInstanceOfType(getClass(4, oldComponents)));
    assertEquals(container.getComponentInstanceOfType(getClass(4, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(4, oldComponents)).getId());
    assertNotNull(container.getComponentInstanceOfType(getClass(5, oldComponents)));
    assertNotNull(container.getComponentInstanceOfType(getClass(6, oldComponents)));
    if (manager == null && oldComponents) {
        assertSame(container.getComponentInstanceOfType(getClass(5, oldComponents)), container.getComponentInstanceOfType(getClass(5, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(5, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(5, oldComponents)).getId());
        assertSame(container.getComponentInstanceOfType(getClass(6, oldComponents)), container.getComponentInstanceOfType(getClass(6, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(6, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(6, oldComponents)).getId());
    } else {
        assertNotSame(container.getComponentInstanceOfType(getClass(5, oldComponents)), container.getComponentInstanceOfType(getClass(5, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(5, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(5, oldComponents)).getId());
        assertNotSame(container.getComponentInstanceOfType(getClass(6, oldComponents)), container.getComponentInstanceOfType(getClass(6, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(6, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(6, oldComponents)).getId());
    }
    ServletRequest req = createProxy(ServletRequest.class, new HashMap<Object, Object>());
    if (manager != null)
        manager.<ServletRequest>getContext(RequestScoped.class).activate(req);
    assertNotNull(container.getComponentInstanceOfType(getClass(7, oldComponents)));
    assertNotNull(container.getComponentInstanceOfType(getClass(8, oldComponents)));
    if (manager == null && !oldComponents) {
        assertNotSame(container.getComponentInstanceOfType(getClass(7, oldComponents)), container.getComponentInstanceOfType(getClass(7, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(7, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(7, oldComponents)).getId());
        assertNotSame(container.getComponentInstanceOfType(getClass(8, oldComponents)), container.getComponentInstanceOfType(getClass(8, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(8, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(8, oldComponents)).getId());
    } else {
        assertSame(container.getComponentInstanceOfType(getClass(7, oldComponents)), container.getComponentInstanceOfType(getClass(7, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(7, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(7, oldComponents)).getId());
        assertSame(container.getComponentInstanceOfType(getClass(8, oldComponents)), container.getComponentInstanceOfType(getClass(8, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(8, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(8, oldComponents)).getId());
    }
    assertNotNull(container.getComponentInstanceOfType(getClass(11, oldComponents)));
    assertNotNull(container.getComponentInstanceOfType(getClass(14, oldComponents)));
    if (manager == null && !oldComponents) {
        assertNotSame(container.getComponentInstanceOfType(getClass(11, oldComponents)), container.getComponentInstanceOfType(getClass(11, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(11, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(11, oldComponents)).getId());
        assertNotSame(container.getComponentInstanceOfType(getClass(14, oldComponents)), container.getComponentInstanceOfType(getClass(14, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(14, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(14, oldComponents)).getId());
    } else {
        assertSame(container.getComponentInstanceOfType(getClass(11, oldComponents)), container.getComponentInstanceOfType(getClass(11, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(11, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(11, oldComponents)).getId());
        assertSame(container.getComponentInstanceOfType(getClass(14, oldComponents)), container.getComponentInstanceOfType(getClass(14, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(14, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(14, oldComponents)).getId());
    }
    if (manager != null)
        manager.<ServletRequest>getContext(RequestScoped.class).deactivate(req);
    try {
        container.getComponentInstanceOfType(getClass(7, oldComponents)).getId();
        if (manager != null)
            fail("An exception is expected as we are out of a request context");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(8, oldComponents)).getId();
        if (manager != null)
            fail("An exception is expected as we are out of a request context");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(11, oldComponents)).getId();
        if (manager != null)
            fail("An exception is expected as we are out of a request context");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(14, oldComponents)).getId();
        if (manager != null)
            fail("An exception is expected as we are out of a request context");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
    assertNotNull(container.getComponentInstanceOfType(getClass(9, oldComponents)));
    assertSame(container.getComponentInstanceOfType(getClass(9, oldComponents)), container.getComponentInstanceOfType(getClass(9, oldComponents)));
    assertEquals(container.getComponentInstanceOfType(getClass(9, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(9, oldComponents)).getId());
    assertNotNull(container.getComponentInstanceOfType(getClass(10, oldComponents)));
    assertNotNull(container.getComponentInstanceOfType(getClass(13, oldComponents)));
    if (manager == null && oldComponents) {
        assertSame(container.getComponentInstanceOfType(getClass(10, oldComponents)), container.getComponentInstanceOfType(getClass(10, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(10, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(10, oldComponents)).getId());
        assertSame(container.getComponentInstanceOfType(getClass(13, oldComponents)), container.getComponentInstanceOfType(getClass(13, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(13, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(13, oldComponents)).getId());
    } else {
        assertNotSame(container.getComponentInstanceOfType(getClass(10, oldComponents)), container.getComponentInstanceOfType(getClass(10, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(10, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(10, oldComponents)).getId());
        assertNotSame(container.getComponentInstanceOfType(getClass(13, oldComponents)), container.getComponentInstanceOfType(getClass(13, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(13, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(13, oldComponents)).getId());
    }
    assertNotNull(container.getComponentInstanceOfType(getClass(12, oldComponents)));
    assertSame(container.getComponentInstanceOfType(getClass(12, oldComponents)), container.getComponentInstanceOfType(getClass(12, oldComponents)));
    assertEquals(container.getComponentInstanceOfType(getClass(12, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(12, oldComponents)).getId());
    try {
        container.getComponentInstanceOfType(getClass(15, oldComponents));
        if (manager != null || !oldComponents)
            fail("An exception is expected as the scope is invalid");
    } catch (Exception e1) {
        // ok
        if (manager == null && oldComponents)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(16, oldComponents));
        if (manager != null || !oldComponents)
            fail("An exception is expected as the scope is invalid");
    } catch (Exception e1) {
        // ok
        if (manager == null && oldComponents)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(17, oldComponents));
        if (manager != null || !oldComponents)
            fail("An exception is expected as the scope is invalid");
    } catch (Exception e1) {
        // ok
        if (manager == null && oldComponents)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(18, oldComponents));
        if (manager != null || !oldComponents)
            fail("An exception is expected as the scope is invalid");
    } catch (Exception e1) {
        // ok
        if (manager == null && oldComponents)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(19, oldComponents));
        if (manager != null || !oldComponents)
            fail("An exception is expected as the scope is invalid");
    } catch (Exception e1) {
        // ok
        if (manager == null && oldComponents)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(20, oldComponents));
        if (manager != null || !oldComponents)
            fail("An exception is expected as the scope is invalid");
    } catch (Exception e1) {
        // ok
        if (manager == null && oldComponents)
            throw e1;
    }
    assertNotNull(container.getComponentInstanceOfType(getClass(21, oldComponents)));
    assertSame(container.getComponentInstanceOfType(getClass(21, oldComponents)), container.getComponentInstanceOfType(getClass(21, oldComponents)));
    assertEquals(container.getComponentInstanceOfType(getClass(21, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(21, oldComponents)).getId());
    assertNotNull(container.getComponentInstanceOfType(getClass(22, oldComponents)));
    if (manager == null && oldComponents) {
        assertSame(container.getComponentInstanceOfType(getClass(22, oldComponents)), container.getComponentInstanceOfType(getClass(22, oldComponents)));
        assertEquals(container.getComponentInstanceOfType(getClass(22, oldComponents)).getId(), container.getComponentInstanceOfType(getClass(22, oldComponents)).getId());
    } else {
        assertNotSame(container.getComponentInstanceOfType(getClass(22, oldComponents)), container.getComponentInstanceOfType(getClass(22, oldComponents)));
        assertFalse(container.getComponentInstanceOfType(getClass(22, oldComponents)).getId() == container.getComponentInstanceOfType(getClass(22, oldComponents)).getId());
    }
    try {
        container.getComponentInstanceOfType(getClass(23, oldComponents));
        if (manager != null)
            fail("An exception is expected as the scope and the default scopes are unknown");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(24, oldComponents));
        if (manager != null)
            fail("An exception is expected as the scope and the default scopes are unknown");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(25, oldComponents));
        if (manager != null)
            fail("An exception is expected as the scope and the default scopes are unknown");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
    try {
        container.getComponentInstanceOfType(getClass(26, oldComponents));
        if (manager != null)
            fail("An exception is expected as the scope and the default scopes are unknown");
    } catch (Exception e1) {
        // ok
        if (manager == null)
            throw e1;
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) DefinitionException(org.exoplatform.container.context.DefinitionException) DefinitionException(org.exoplatform.container.context.DefinitionException)

Example 2 with DefinitionException

use of org.exoplatform.container.context.DefinitionException in project kernel by exoplatform.

the class ContainerUtil method getScope.

/**
 * Gives the scope defined for the given class
 * @param clazz the class for which we want the scope
 * @param ignoreExplicit indicates whether the explicit scope must be ignored
 * @return a class representing the annotation type of the scope
 * @throws DefinitionException in case the definition of the scope is not correct
 */
public static Class<? extends Annotation> getScope(Class<?> clazz, boolean ignoreExplicit) throws DefinitionException {
    Annotation[] annotations = clazz.getAnnotations();
    Class<? extends Annotation> scope = null;
    Class<? extends Annotation> defaultScope = null;
    boolean hasStereotype = false;
    for (int i = 0; i < annotations.length; i++) {
        Annotation annotation = annotations[i];
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (!ignoreExplicit && (annotationType.isAnnotationPresent(Scope.class) || annotationType.isAnnotationPresent(NormalScope.class))) {
            if (scope != null) {
                throw new DefinitionException("You cannot set several scopes to the class " + clazz.getName());
            }
            scope = annotationType;
        } else if (annotationType.isAnnotationPresent(Stereotype.class)) {
            hasStereotype = true;
            Annotation[] stereotypeAnnotations = annotationType.getAnnotations();
            for (int j = 0; j < stereotypeAnnotations.length; j++) {
                Annotation stereotypeAnnotation = stereotypeAnnotations[j];
                Class<? extends Annotation> stereotypeAnnotationType = stereotypeAnnotation.annotationType();
                if (stereotypeAnnotationType.isAnnotationPresent(Scope.class) || stereotypeAnnotationType.isAnnotationPresent(NormalScope.class)) {
                    if (defaultScope != null && !defaultScope.equals(stereotypeAnnotationType)) {
                        throw new DefinitionException("The class " + clazz.getName() + " has stereotypes with different default scope");
                    }
                    defaultScope = stereotypeAnnotationType;
                }
            }
        }
    }
    if (scope != null)
        return scope;
    if (defaultScope != null)
        return defaultScope;
    if (hasStereotype) {
        throw new DefinitionException("The class " + clazz.getName() + " has at least one stereotype but doesn't have any scope, please set an explicit scope");
    }
    return null;
}
Also used : NormalScope(javax.enterprise.context.NormalScope) Scope(javax.inject.Scope) NormalScope(javax.enterprise.context.NormalScope) Stereotype(javax.enterprise.inject.Stereotype) DefinitionException(org.exoplatform.container.context.DefinitionException) Annotation(java.lang.annotation.Annotation)

Example 3 with DefinitionException

use of org.exoplatform.container.context.DefinitionException in project kernel by exoplatform.

the class MX4JComponentAdapter method create.

private T create(ContextManager manager, boolean retryAllowed) {
    Class<? extends Annotation> scope = getScope(true, false);
    if (scope.equals(Unknown.class) || scope.equals(Singleton.class) || scope.equals(Dependent.class) || scope.equals(ApplicationScoped.class)) {
        return create();
    }
    final Context ctx = manager.getContext(scope);
    if (ctx == null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("The scope {} is unknown, thus we will create the component {} out of a scope context.", scope.getName(), getComponentImplementation().getName());
        }
        if (!retryAllowed)
            throw new IllegalArgumentException("The scope and the default scope of the class " + getComponentImplementation().getName() + " are unknown");
        try {
            Class<? extends Annotation> defaultScope = ContainerUtil.getScope(getComponentImplementation(), true);
            setScope(scope, defaultScope);
            return create(manager, false);
        } catch (DefinitionException e) {
            throw new IllegalArgumentException("The scope of the class " + getComponentImplementation().getName() + " is unknown and we cannot get a clear default scope: " + e.getMessage());
        }
    }
    NormalScope normalScope = scope.getAnnotation(NormalScope.class);
    if (normalScope != null) {
        // A proxy is expected
        if (normalScope.passivating() && !Serializable.class.isAssignableFrom(getComponentImplementation())) {
            throw new IllegalArgumentException("As the scope " + scope.getName() + " is a passivating scope, we expect only serializable objects and " + getComponentImplementation().getName() + " is not serializable.");
        }
        try {
            lock.lock();
            if (proxy != null)
                return proxy;
            T result = ContainerUtil.createProxy(getComponentImplementation(), new Provider<T>() {

                public T get() {
                    return createInstance(ctx);
                }
            });
            return proxy = result;
        } finally {
            lock.unlock();
        }
    }
    return createInstance(ctx);
}
Also used : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) NormalScope(javax.enterprise.context.NormalScope) Singleton(javax.inject.Singleton) DefinitionException(org.exoplatform.container.context.DefinitionException) ApplicationScoped(javax.enterprise.context.ApplicationScoped)

Aggregations

DefinitionException (org.exoplatform.container.context.DefinitionException)3 NormalScope (javax.enterprise.context.NormalScope)2 Annotation (java.lang.annotation.Annotation)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Context (javax.enterprise.context.spi.Context)1 CreationalContext (javax.enterprise.context.spi.CreationalContext)1 Stereotype (javax.enterprise.inject.Stereotype)1 Scope (javax.inject.Scope)1 Singleton (javax.inject.Singleton)1 ServletRequest (javax.servlet.ServletRequest)1