Search in sources :

Example 16 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class ApplicationProperties method readPropertiesFiles.

private void readPropertiesFiles(final AppModule appModule) throws OpenEJBException {
    final Collection<DeploymentModule> deploymentModule = appModule.getDeploymentModule();
    // We intentionally add the AppModule itself LAST so its properties trump all
    deploymentModule.add(appModule);
    for (final DeploymentModule module : deploymentModule) {
        final Object o = module.getAltDDs().get("application.properties");
        if (o instanceof URL) {
            final URL url = (URL) o;
            try {
                final Properties properties = IO.readProperties(url);
                appModule.getProperties().putAll(properties);
            } catch (final IOException e) {
                throw new OpenEJBException("Cannot read application.properties: " + url, e);
            }
        } else if (o instanceof Properties) {
            appModule.getProperties().putAll((Properties) o);
        } else if (o != null) {
            throw new OpenEJBException("Unknown application.properties type: " + o.getClass().getName());
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL)

Example 17 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class AutoConfig method processResourceRef.

private void processResourceRef(final ResourceRef ref, final EjbDeployment ejbDeployment, final AppResources appResources, final EjbModule ejbModule) throws OpenEJBException {
    // skip destinations with lookup name
    if (ref.getLookupName() != null) {
        return;
    }
    // skip destinations with a global jndi name
    final String mappedName = ref.getMappedName() == null ? "" : ref.getMappedName();
    if (mappedName.startsWith("jndi:")) {
        return;
    }
    final String refName = ref.getName();
    final String refType = getType(ref, ejbModule.getClassLoader());
    // skip references such as URLs which are automatically handled by the server
    if (ignoredReferenceTypes.contains(refType)) {
        final ResourceInfo resourceInfo = configFactory.getResourceInfo(refName.replace("java:", "").replace("comp/env/", ""));
        if (resourceInfo != null) {
            ref.setMappedName("jndi:" + (resourceInfo.id.startsWith("java:") ? resourceInfo.id : "openejb:Resource/" + resourceInfo.id));
        }
        return;
    }
    try {
        final Class<?> clazz = ejbModule.getClassLoader().loadClass(refType);
        if (clazz.isAnnotationPresent(ManagedBean.class)) {
            return;
        }
    } catch (final Throwable t) {
    // no-op
    }
    try {
        ResourceLink link = ejbDeployment.getResourceLink(refName);
        if (link == null) {
            String id = mappedName.length() == 0 ? ref.getName() : mappedName;
            if (id.startsWith("java:")) {
                id = id.substring("java:".length());
            }
            if (id.startsWith("/")) {
                id = id.substring(1);
            }
            try {
                final AppModule appModule = ejbModule.getAppModule();
                if (appModule != null) {
                    final String newId = findResourceId(appModule.getModuleId() + '/' + id.replace("java:", "").replaceAll("^comp/env/", ""), refType, new Properties(), appResources);
                    if (newId != null) {
                        // app scoped resources, try to find it without creating it first
                        id = getResourceId(ejbModule.getModuleId(), newId, refType, appResources);
                    } else {
                        id = getResourceId(ejbDeployment.getDeploymentId(), id, refType, appResources);
                    }
                } else {
                    id = getResourceId(ejbDeployment.getDeploymentId(), id, refType, appResources);
                }
            } catch (final OpenEJBException e) {
                // changing the message to be explicit
                throw new OpenEJBException("Can't find resource for " + ref.getOrigin() + ". (" + e.getMessage() + ")", e.getCause());
            }
            logger.info("Auto-linking resource-ref '" + refName + "' in bean " + ejbDeployment.getDeploymentId() + " to Resource(id=" + id + ")");
            link = new ResourceLink();
            link.setResId(id);
            link.setResRefName(refName);
            ejbDeployment.addResourceLink(link);
        } else {
            final String id = getResourceId(ejbDeployment.getDeploymentId(), link.getResId(), refType, appResources);
            link.setResId(id);
            link.setResRefName(refName);
        }
    } catch (final OpenEJBException ex) {
        if (!(ref instanceof ContextRef)) {
            throw ex;
        }
    }
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) OpenEJBException(org.apache.openejb.OpenEJBException) ResourceLink(org.apache.openejb.jee.oejb3.ResourceLink) PersistenceContextRef(org.apache.openejb.jee.PersistenceContextRef) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties)

Example 18 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class AutoConfig method checkUnitDataSourceRefs.

private void checkUnitDataSourceRefs(final PersistenceUnit unit) throws OpenEJBException {
    final Properties required = new Properties();
    // check that non-jta-data-source does NOT point to a JtaManaged=true datasource
    required.put("JtaManaged", "true");
    final String invalidNonJta = findResourceId(unit.getNonJtaDataSource(), "DataSource", required, null);
    if (invalidNonJta != null) {
        throw new OpenEJBException("PeristenceUnit " + unit.getName() + " <non-jta-data-source> points to a jta managed Resource.  Update Resource \"" + invalidNonJta + "\" to \"JtaManaged=false\", use a different Resource, or delete the <non-jta-data-source> element and a default will be supplied if possible.");
    }
    // check that jta-data-source does NOT point to a JtaManaged=false datasource
    required.put("JtaManaged", "false");
    final String invalidJta = findResourceId(unit.getJtaDataSource(), "DataSource", required, null);
    if (invalidJta != null) {
        throw new OpenEJBException("PeristenceUnit " + unit.getName() + " <jta-data-source> points to a non jta managed Resource.  Update Resource \"" + invalidJta + "\" to \"JtaManaged=true\", use a different Resource, or delete the <jta-data-source> element and a default will be supplied if possible.");
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties)

Example 19 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class AutoConfig method getResourceId.

private String getResourceId(final String beanName, String resourceId, final String type, final Properties required, AppResources appResources) throws OpenEJBException {
    resourceId = normalizeResourceId(resourceId);
    if (resourceId == null) {
        return null;
    }
    if (appResources == null) {
        appResources = EMPTY_APP_RESOURCES;
    }
    // skip references such as URL which are automatically handled by the server
    if (type != null && ignoredReferenceTypes.contains(type)) {
        return null;
    }
    // check for existing resource with specified resourceId and type and properties
    // check first in app namespace
    String id = findResourceId(beanName + '/' + resourceId, type, required, appResources);
    if (id != null) {
        return id;
    }
    id = findResourceId(resourceId, type, required, appResources);
    if (id != null) {
        return id;
    }
    // expand search to any type -- may be asking for a reference to a sub-type
    id = findResourceId(resourceId, null, required, appResources);
    if (id != null) {
        return id;
    }
    // app resources
    if (appResources.appId != null && !appResources.appId.isEmpty() && resourceId.startsWith(appResources.appId + '/')) {
        id = findResourceId(resourceId.substring(appResources.appId.length() + 1), type, required, appResources);
        if (id != null) {
            return id;
        }
    }
    // throw an exception or log an error
    final String shortName = toShortName(resourceId);
    final String message = "No existing resource found while attempting to Auto-link unmapped resource-ref '" + resourceId + "' of type '" + type + "' for '" + beanName + "'.  Looked for Resource(id=" + resourceId + ") and Resource(id=" + shortName + ")";
    if (!autoCreateResources) {
        throw new OpenEJBException(message);
    }
    logger.debug(message);
    // if there is a provider with the specified name. use it
    if (ServiceUtils.hasServiceProvider(resourceId)) {
        final ResourceInfo resourceInfo = configFactory.configureService(resourceId, ResourceInfo.class);
        return installResource(beanName, resourceInfo);
    } else if (ServiceUtils.hasServiceProvider(shortName)) {
        final ResourceInfo resourceInfo = configFactory.configureService(shortName, ResourceInfo.class);
        return installResource(beanName, resourceInfo);
    }
    // if there are any resources of the desired type, use the first one
    id = firstMatching(beanName, type, required, appResources);
    if (id != null) {
        return id;
    }
    // Auto create a resource using the first provider that can supply a resource of the desired type
    return autoCreateResource(type, required, beanName);
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo)

Example 20 with OpenEJBException

use of org.apache.openejb.OpenEJBException in project tomee by apache.

the class AutoConfig method deploy.

private void deploy(final EjbModule ejbModule, final AppResources appResources) throws OpenEJBException {
    final OpenejbJar openejbJar;
    if (ejbModule.getOpenejbJar() != null) {
        openejbJar = ejbModule.getOpenejbJar();
    } else {
        openejbJar = new OpenejbJar();
        ejbModule.setOpenejbJar(openejbJar);
    }
    final Map<String, EjbDeployment> deployments = openejbJar.getDeploymentsByEjbName();
    for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
        final EjbDeployment ejbDeployment = deployments.get(bean.getEjbName());
        if (ejbDeployment == null) {
            throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
        }
        final String beanType = getType(bean);
        final Class<? extends ContainerInfo> containerInfoType = ConfigurationFactory.getContainerInfoType(beanType);
        logger.debug("Bean type of bean {0} is {1}", bean.getEjbName(), beanType);
        if (ejbDeployment.getContainerId() == null && !skipMdb(bean)) {
            logger.debug("Container for bean {0} is not set, looking for a suitable container", bean.getEjbName());
            String containerId = getUsableContainer(containerInfoType, bean, appResources);
            if (containerId == null) {
                logger.debug("Suitable container for bean {0} not found, creating one", bean.getEjbName());
                containerId = createContainer(containerInfoType, ejbDeployment, bean);
            }
            logger.debug("Setting container ID {0} for bean {1}", containerId, bean.getEjbName());
            ejbDeployment.setContainerId(containerId);
        }
        logger.debug("Container ID for bean {0} is {1}", bean.getEjbName(), ejbDeployment.getContainerId());
        // create the container if it doesn't exist
        final List<String> containerIds = configFactory.getContainerIds();
        final Collection<ContainerInfo> containerInfos = appResources.getContainerInfos();
        for (final ContainerInfo containerInfo : containerInfos) {
            containerIds.add(containerInfo.id);
        }
        if (!containerIds.contains(ejbDeployment.getContainerId()) && !skipMdb(bean)) {
            logger.debug("Desired container {0} not found. Containers available: {1}. Creating a new container.", ejbDeployment.getContainerId(), Join.join(", ", containerIds));
            createContainer(containerInfoType, ejbDeployment, bean);
        }
        // Resource reference
        for (final ResourceRef ref : bean.getResourceRef()) {
            processResourceRef(ref, ejbDeployment, appResources, ejbModule);
        }
        // Resource env reference
        for (final JndiReference ref : bean.getResourceEnvRef()) {
            processResourceEnvRef(ref, ejbDeployment, appResources, ejbModule.getClassLoader());
        }
        // Message destination reference
        for (final MessageDestinationRef ref : bean.getMessageDestinationRef()) {
            processResourceEnvRef(ref, ejbDeployment, appResources, ejbModule.getClassLoader());
        }
        // mdb message destination id
        if (autoCreateResources && bean instanceof MessageDrivenBean) {
            final MessageDrivenBean mdb = (MessageDrivenBean) bean;
            final ResourceLink resourceLink = ejbDeployment.getResourceLink("openejb/destination");
            if (resourceLink != null) {
                try {
                    final String destinationId = getResourceEnvId(bean.getEjbName(), resourceLink.getResId(), mdb.getMessageDestinationType(), appResources);
                    resourceLink.setResId(destinationId);
                } catch (final OpenEJBException e) {
                    // The MDB doesn't need the auto configured "openejb/destination" env entry
                    ejbDeployment.removeResourceLink("openejb/destination");
                }
            }
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) MessageDestinationRef(org.apache.openejb.jee.MessageDestinationRef) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) JndiReference(org.apache.openejb.jee.JndiReference) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) ResourceLink(org.apache.openejb.jee.oejb3.ResourceLink) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo) MdbContainerInfo(org.apache.openejb.assembler.classic.MdbContainerInfo) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) ResourceRef(org.apache.openejb.jee.ResourceRef)

Aggregations

OpenEJBException (org.apache.openejb.OpenEJBException)192 IOException (java.io.IOException)54 NamingException (javax.naming.NamingException)35 URL (java.net.URL)31 MalformedURLException (java.net.MalformedURLException)30 BeanContext (org.apache.openejb.BeanContext)30 ApplicationException (org.apache.openejb.ApplicationException)29 ArrayList (java.util.ArrayList)28 File (java.io.File)27 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)24 Method (java.lang.reflect.Method)22 SystemException (org.apache.openejb.SystemException)22 HashMap (java.util.HashMap)18 ThreadContext (org.apache.openejb.core.ThreadContext)18 RemoteException (java.rmi.RemoteException)14 EJBException (javax.ejb.EJBException)14 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)14 HashSet (java.util.HashSet)13 Properties (java.util.Properties)13 EJBAccessException (javax.ejb.EJBAccessException)13