Search in sources :

Example 1 with DefinitionException

use of javax.enterprise.inject.spi.DefinitionException in project core by weld.

the class DummyExtension method replaceObserverMethod.

void replaceObserverMethod(@Observes ProcessObserverMethod<Number, ?> event) {
    if (!checkExperimentalObserver(event.getObserverMethod().getObservedQualifiers())) {
        return;
    }
    // first, verify getBeanClass() check
    ObserverMethod<Number> wrongObserver = new ForwardingObserverMethod<Number>(event.getObserverMethod()) {

        @Override
        public Class<?> getBeanClass() {
            return Number.class;
        }
    };
    try {
        event.setObserverMethod(wrongObserver);
        throw new RuntimeException("Expected exception not thrown");
    } catch (DefinitionException expected) {
    }
    ObserverMethod<Number> replacement = new ForwardingObserverMethod<Number>(event.getObserverMethod()) {

        @Override
        public Type getObservedType() {
            return Integer.class;
        }

        @Override
        public Set<Annotation> getObservedQualifiers() {
            Set<Annotation> qualifiers = new HashSet<>(delegate().getObservedQualifiers());
            qualifiers.add(new NamedLiteral("experimental"));
            return qualifiers;
        }
    };
    event.setObserverMethod(replacement);
}
Also used : DefinitionException(javax.enterprise.inject.spi.DefinitionException) NamedLiteral(org.jboss.weld.literal.NamedLiteral) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet)

Example 2 with DefinitionException

use of javax.enterprise.inject.spi.DefinitionException in project tomee by apache.

the class Assembler method validateCdiResourceProducers.

private void validateCdiResourceProducers(final AppContext appContext, final AppInfo info) {
    if (appContext.getWebBeansContext() == null) {
        return;
    }
    // validate @Produces @Resource/@PersistenceX/@EJB once all is bound to JNDI - best case - or with our model
    if (appContext.isStandaloneModule() && !appContext.getProperties().containsKey("openejb.cdi.skip-resource-validation")) {
        final Map<String, Object> bindings = appContext.getWebContexts().isEmpty() ? appContext.getBindings() : appContext.getWebContexts().iterator().next().getBindings();
        if (bindings != null && appContext.getWebBeansContext() != null && appContext.getWebBeansContext().getBeanManagerImpl().isInUse()) {
            for (final Bean<?> bean : appContext.getWebBeansContext().getBeanManagerImpl().getBeans()) {
                if (ResourceBean.class.isInstance(bean)) {
                    final ResourceReference reference = ResourceBean.class.cast(bean).getReference();
                    String jndi = reference.getJndiName().replace("java:", "");
                    if (reference.getJndiName().startsWith("java:/")) {
                        jndi = jndi.substring(1);
                    }
                    Object lookup = bindings.get(jndi);
                    if (lookup == null && reference.getAnnotation(EJB.class) != null) {
                        final CdiPlugin plugin = CdiPlugin.class.cast(appContext.getWebBeansContext().getPluginLoader().getEjbPlugin());
                        if (!plugin.isSessionBean(reference.getResourceType())) {
                            // local beans are here and access is O(1) instead of O(n)
                            boolean ok = false;
                            for (final BeanContext bc : appContext.getBeanContexts()) {
                                if (bc.getBusinessLocalInterfaces().contains(reference.getResourceType()) || bc.getBusinessRemoteInterfaces().contains(reference.getResourceType())) {
                                    ok = true;
                                    break;
                                }
                            }
                            if (!ok) {
                                throw new DefinitionException("EJB " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast to " + reference.getResourceType());
                            }
                        }
                    }
                    if (Reference.class.isInstance(lookup)) {
                        try {
                            lookup = Reference.class.cast(lookup).getContent();
                        } catch (final Exception e) {
                            // surely too early, let's try some known locations
                            if (JndiUrlReference.class.isInstance(lookup)) {
                                checkBuiltInResourceTypes(reference, JndiUrlReference.class.cast(lookup).getJndiName());
                            }
                            continue;
                        }
                    } else if (lookup == null) {
                        // TODO: better validation with lookups in tomee, should be in TWAB surely but would split current code
                        final Resource r = Resource.class.cast(reference.getAnnotation(Resource.class));
                        if (r != null) {
                            if (!r.lookup().isEmpty()) {
                                checkBuiltInResourceTypes(reference, r.lookup());
                            } else if (!r.name().isEmpty()) {
                                final String name = "comp/env/" + r.name();
                                boolean done = false;
                                for (final WebAppInfo w : info.webApps) {
                                    for (final EnvEntryInfo e : w.jndiEnc.envEntries) {
                                        if (name.equals(e.referenceName)) {
                                            if (e.type != null && !reference.getResourceType().getName().equals(e.type)) {
                                                throw new DefinitionException("Env Entry " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast to " + reference.getResourceType());
                                            }
                                            done = true;
                                            break;
                                        }
                                    }
                                    if (done) {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (lookup != null && !reference.getResourceType().isInstance(lookup)) {
                        throw new DefinitionException("Resource " + reference.getJndiName() + " in " + reference.getOwnerClass() + " can't be cast, instance is " + lookup);
                    }
                }
            }
        }
    }
}
Also used : CdiPlugin(org.apache.openejb.cdi.CdiPlugin) ResourceBean(org.apache.webbeans.component.ResourceBean) Resource(javax.annotation.Resource) DestroyableResource(org.apache.openejb.api.resource.DestroyableResource) JndiUrlReference(org.apache.openejb.core.ivm.naming.JndiUrlReference) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) ResourceReference(org.apache.webbeans.spi.api.ResourceReference) DefinitionException(javax.enterprise.inject.spi.DefinitionException)

Example 3 with DefinitionException

use of javax.enterprise.inject.spi.DefinitionException in project tomee by apache.

the class CdiPlugin method defineSessionBean.

@Override
public <T> Bean<T> defineSessionBean(final Class<T> clazz, final BeanAttributes<T> attributes, final AnnotatedType<T> annotatedType) {
    final BeanContext bc = findBeanContext(webBeansContext, clazz);
    final Class<?> superClass = bc.getManagedClass().getSuperclass();
    if (annotatedType.isAnnotationPresent(Specializes.class)) {
        if (superClass != Object.class && !isSessionBean(superClass)) {
            throw new DefinitionException("You can only specialize another EJB: " + clazz);
        }
        final BeanContext parentBc = findBeanContext(webBeansContext, superClass);
        final List<Class> businessLocalInterfaces = new ArrayList<>(parentBc.getBusinessLocalInterfaces());
        for (final Class<?> api : bc.getBusinessLocalInterfaces()) {
            businessLocalInterfaces.removeAll(GenericsUtil.getTypeClosure(api));
        }
        if (!businessLocalInterfaces.isEmpty()) {
            throw new DefinitionException("You can only specialize another EJB with at least the same API: " + clazz);
        }
    }
    final CdiEjbBean<T> bean = new OpenEJBBeanBuilder<>(bc, webBeansContext, annotatedType, attributes).createBean(clazz, !annotatedType.isAnnotationPresent(Vetoed.class));
    bc.set(CdiEjbBean.class, bean);
    bc.set(CurrentCreationalContext.class, new CurrentCreationalContext());
    validateDisposeMethods(bean);
    validateScope(bean);
    final Set<ObserverMethod<?>> observerMethods;
    if (bean.isEnabled()) {
        observerMethods = new ObserverMethodsBuilder<>(webBeansContext, bean.getAnnotatedType()).defineObserverMethods(bean, true);
    } else {
        observerMethods = new HashSet<>();
    }
    final WebBeansUtil webBeansUtil = webBeansContext.getWebBeansUtil();
    final Set<ProducerFieldBean<?>> producerFields = new ProducerFieldBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerFields(bean);
    final Set<ProducerMethodBean<?>> producerMethods = new ProducerMethodBeansBuilder(bean.getWebBeansContext(), bean.getAnnotatedType()).defineProducerMethods(bean, producerFields);
    final Map<ProducerMethodBean<?>, AnnotatedMethod<?>> annotatedMethods = new HashMap<>();
    for (final ProducerMethodBean<?> producerMethod : producerMethods) {
        final AnnotatedMethod<?> method = webBeansContext.getAnnotatedElementFactory().newAnnotatedMethod(producerMethod.getCreatorMethod(), annotatedType);
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for " + "ProducerMethods. Look at logs for further details");
        annotatedMethods.put(producerMethod, method);
    }
    final Map<ProducerFieldBean<?>, AnnotatedField<?>> annotatedFields = new HashMap<>();
    for (final ProducerFieldBean<?> producerField : producerFields) {
        if (!Modifier.isStatic(producerField.getCreatorField().getModifiers())) {
            throw new DefinitionException("In an EJB all producer fields should be static");
        }
        webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducer event observers for" + " ProducerFields. Look at logs for further details");
        annotatedFields.put(producerField, webBeansContext.getAnnotatedElementFactory().newAnnotatedField(producerField.getCreatorField(), webBeansContext.getAnnotatedElementFactory().newAnnotatedType(producerField.getBeanClass())));
    }
    final Map<ObserverMethod<?>, AnnotatedMethod<?>> observerMethodsMap = new HashMap<>();
    for (final ObserverMethod<?> observerMethod : observerMethods) {
        final ObserverMethodImpl<?> impl = (ObserverMethodImpl<?>) observerMethod;
        final AnnotatedMethod<?> method = impl.getObserverMethod();
        observerMethodsMap.put(observerMethod, method);
    }
    validateProduceMethods(bean, producerMethods);
    validateObserverMethods(bean, observerMethodsMap);
    final BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
    // Fires ProcessManagedBean
    final GProcessSessionBean event = new GProcessSessionBean(Bean.class.cast(bean), annotatedType, bc.getEjbName(), bean.getEjbType());
    beanManager.fireEvent(event, true);
    event.setStarted();
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessSessionBean event observers for managed beans. Look at logs for further details");
    // Fires ProcessProducerMethod
    webBeansUtil.fireProcessProducerMethodBeanEvent(annotatedMethods, annotatedType);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerMethod event observers for producer method beans. Look at logs for further details");
    // Fires ProcessProducerField
    webBeansUtil.fireProcessProducerFieldBeanEvent(annotatedFields);
    webBeansUtil.inspectDeploymentErrorStack("There are errors that are added by ProcessProducerField event observers for producer field beans. Look at logs for further details");
    if (!webBeansUtil.isAnnotatedTypeDecoratorOrInterceptor(annotatedType)) {
        for (final ProducerMethodBean<?> producerMethod : producerMethods) {
            beanManager.addBean(producerMethod);
        }
        for (final ProducerFieldBean<?> producerField : producerFields) {
            beanManager.addBean(producerField);
        }
    }
    beanManager.addBean(bean);
    return bean;
}
Also used : AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ArrayList(java.util.ArrayList) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) Bean(javax.enterprise.inject.spi.Bean) OwbBean(org.apache.webbeans.component.OwbBean) AbstractOwbBean(org.apache.webbeans.component.AbstractOwbBean) ProducerFieldBeansBuilder(org.apache.webbeans.component.creation.ProducerFieldBeansBuilder) GProcessSessionBean(org.apache.webbeans.portable.events.generics.GProcessSessionBean) ObserverMethodsBuilder(org.apache.webbeans.component.creation.ObserverMethodsBuilder) ObserverMethodImpl(org.apache.webbeans.event.ObserverMethodImpl) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ProducerMethodBean(org.apache.webbeans.component.ProducerMethodBean) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) ProducerFieldBean(org.apache.webbeans.component.ProducerFieldBean) BeanContext(org.apache.openejb.BeanContext) WebBeansUtil(org.apache.webbeans.util.WebBeansUtil) ProducerMethodBeansBuilder(org.apache.webbeans.component.creation.ProducerMethodBeansBuilder) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Aggregations

DefinitionException (javax.enterprise.inject.spi.DefinitionException)3 BeanContext (org.apache.openejb.BeanContext)2 IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 Annotation (java.lang.annotation.Annotation)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Resource (javax.annotation.Resource)1 AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 Bean (javax.enterprise.inject.spi.Bean)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1