Search in sources :

Example 1 with ResourceReference

use of org.apache.webbeans.spi.api.ResourceReference 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)

Aggregations

IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Resource (javax.annotation.Resource)1 DefinitionException (javax.enterprise.inject.spi.DefinitionException)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1 NamingException (javax.naming.NamingException)1 ResourceAdapterInternalException (javax.resource.spi.ResourceAdapterInternalException)1 ValidationException (javax.validation.ValidationException)1 BeanContext (org.apache.openejb.BeanContext)1 DuplicateDeploymentIdException (org.apache.openejb.DuplicateDeploymentIdException)1 NoSuchApplicationException (org.apache.openejb.NoSuchApplicationException)1