Search in sources :

Example 1 with AssemblyDescriptor$JAXB.readAssemblyDescriptor

use of org.apache.openejb.jee.AssemblyDescriptor$JAXB.readAssemblyDescriptor in project tomee by apache.

the class AutoConfig method resolveDestinationLinks.

/**
 * Set resource id in all message-destination-refs and MDBs that are using message destination links.
 */
private void resolveDestinationLinks(final AppModule appModule) throws OpenEJBException {
    // build up a link resolver
    final LinkResolver<MessageDestination> destinationResolver = new LinkResolver<>();
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly != null) {
            for (final MessageDestination destination : assembly.getMessageDestination()) {
                destinationResolver.add(ejbModule.getModuleUri(), destination.getMessageDestinationName(), destination);
            }
        }
    }
    for (final ClientModule clientModule : appModule.getClientModules()) {
        for (final MessageDestination destination : clientModule.getApplicationClient().getMessageDestination()) {
            destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
        }
    }
    for (final WebModule webModule : appModule.getWebModules()) {
        for (final MessageDestination destination : webModule.getWebApp().getMessageDestination()) {
            destinationResolver.add(appModule.getModuleUri(), destination.getMessageDestinationName(), destination);
        }
    }
    // remember the type of each destination so we can use it to fillin MDBs that don't declare destination type
    final Map<MessageDestination, String> destinationTypes = new HashMap<>();
    // if MessageDestination does not have a mapped name assigned, give it the destination from the MDB
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly == null) {
            continue;
        }
        final URI moduleUri = ejbModule.getModuleUri();
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            // MDB destination is deploymentId if none set
            if (bean instanceof MessageDrivenBean) {
                final MessageDrivenBean mdb = (MessageDrivenBean) bean;
                final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
                if (ejbDeployment == null) {
                    throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
                }
                // skip destination refs without a destination link
                final String link = mdb.getMessageDestinationLink();
                if (link == null || link.length() == 0) {
                    continue;
                }
                // resolve the destination... if we don't find one it is a configuration bug
                final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
                if (destination == null) {
                    throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
                }
                // get the destinationId is the mapped name
                String destinationId = destination.getMappedName();
                if (destinationId == null) {
                    // if we don't have a mapped name use the destination of the mdb
                    final Properties properties = mdb.getActivationConfig().toProperties();
                    destinationId = properties.getProperty("destination");
                    destination.setMappedName(destinationId);
                }
                if (mdb.getMessageDestinationType() != null && !destinationTypes.containsKey(destination)) {
                    destinationTypes.put(destination, mdb.getMessageDestinationType());
                }
                // destination identifier
                ResourceLink resourceLink = ejbDeployment.getResourceLink("openejb/destination");
                if (resourceLink == null) {
                    resourceLink = new ResourceLink();
                    resourceLink.setResRefName("openejb/destination");
                    ejbDeployment.addResourceLink(resourceLink);
                }
                resourceLink.setResId(destinationId);
            }
        }
    }
    // resolve all message destination refs with links and assign a ref id to the reference
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly == null) {
            continue;
        }
        final URI moduleUri = ejbModule.getModuleUri();
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
            if (ejbDeployment == null) {
                throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
            }
            for (final MessageDestinationRef ref : bean.getMessageDestinationRef()) {
                // skip destination refs with a resource link already assigned
                if (ref.getMappedName() == null && ejbDeployment.getResourceLink(ref.getName()) == null) {
                    final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
                    if (destinationId != null) {
                        // build the link and add it
                        final ResourceLink resourceLink = new ResourceLink();
                        resourceLink.setResId(destinationId);
                        resourceLink.setResRefName(ref.getName());
                        ejbDeployment.addResourceLink(resourceLink);
                    }
                }
            }
        }
    }
    for (final ClientModule clientModule : appModule.getClientModules()) {
        final URI moduleUri = clientModule.getModuleUri();
        for (final MessageDestinationRef ref : clientModule.getApplicationClient().getMessageDestinationRef()) {
            final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
            if (destinationId != null) {
                // for client modules we put the destinationId in the mapped name
                ref.setMappedName(destinationId);
            }
        }
    }
    for (final WebModule webModule : appModule.getWebModules()) {
        final URI moduleUri = URLs.uri(webModule.getModuleId());
        for (final MessageDestinationRef ref : webModule.getWebApp().getMessageDestinationRef()) {
            final String destinationId = resolveDestinationId(ref, appModule, moduleUri, destinationResolver, destinationTypes);
            if (destinationId != null) {
                // for web modules we put the destinationId in the mapped name
                ref.setMappedName(destinationId);
            }
        }
    }
    // the info from the destination (which got filled in from the references)
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
        if (assembly == null) {
            continue;
        }
        final URI moduleUri = URLs.uri(ejbModule.getModuleId());
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            // MDB destination is deploymentId if none set
            if (bean instanceof MessageDrivenBean) {
                final MessageDrivenBean mdb = (MessageDrivenBean) bean;
                if (!isJms(mdb)) {
                    continue;
                }
                final EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
                if (ejbDeployment == null) {
                    throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
                }
                // if destination type is already set in, continue
                String destinationType = mdb.getMessageDestinationType();
                if (destinationType != null) {
                    continue;
                }
                final String link = mdb.getMessageDestinationLink();
                if (link != null && link.length() != 0) {
                    // resolve the destination... if we don't find one it is a configuration bug
                    final MessageDestination destination = destinationResolver.resolveLink(link, moduleUri);
                    if (destination == null) {
                        throw new OpenEJBException("Message destination " + link + " for message driven bean " + mdb.getEjbName() + " not found");
                    }
                    destinationType = destinationTypes.get(destination);
                }
                if (destinationType == null) {
                    // couldn't determine type... we'll have to guess
                    // if destination name contains the string "queue" or "topic" we use that
                    final Properties properties = mdb.getActivationConfig().toProperties();
                    final String destination = properties.getProperty("destination").toLowerCase();
                    if (destination.contains("queue")) {
                        destinationType = Queue.class.getName();
                    } else if (destination.contains("topic")) {
                        destinationType = Topic.class.getName();
                    } else {
                        // Queue is the default
                        destinationType = Queue.class.getName();
                    }
                    logger.info("Auto-configuring a message driven bean " + ejbDeployment.getDeploymentId() + " destination " + properties.getProperty("destination") + " to be destinationType " + destinationType);
                }
                if (destinationType != null) {
                    mdb.getActivationConfig().addProperty("destinationType", destinationType);
                    mdb.setMessageDestinationType(destinationType);
                    // topics need a clientId and subscriptionName
                    if ("javax.jms.Topic".equals(destinationType)) {
                        final Properties properties = mdb.getActivationConfig().toProperties();
                        if (!properties.containsKey("clientId")) {
                            mdb.getActivationConfig().addProperty("clientId", ejbDeployment.getDeploymentId());
                        }
                        if (!properties.containsKey("subscriptionName")) {
                            mdb.getActivationConfig().addProperty("subscriptionName", ejbDeployment.getDeploymentId() + "_subscription");
                        }
                    }
                }
            }
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) MessageDestination(org.apache.openejb.jee.MessageDestination) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) HashMap(java.util.HashMap) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties) URI(java.net.URI) MessageDestinationRef(org.apache.openejb.jee.MessageDestinationRef) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) LinkResolver(org.apache.openejb.util.LinkResolver) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) ResourceLink(org.apache.openejb.jee.oejb3.ResourceLink) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) Queue(javax.jms.Queue)

Example 2 with AssemblyDescriptor$JAXB.readAssemblyDescriptor

use of org.apache.openejb.jee.AssemblyDescriptor$JAXB.readAssemblyDescriptor in project tomee by apache.

the class DebuggableVmHackery method deploy.

public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final EjbJar ejbJar = ejbModule.getEjbJar();
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        final Map<String, EjbDeployment> deployments = openejbJar.getDeploymentsByEjbName();
        ejbJar.setRelationships(null);
        final List<String> removed = new ArrayList<>();
        for (final EnterpriseBean bean : ejbJar.getEnterpriseBeans()) {
            final String ejbName = bean.getEjbName();
            final EjbDeployment ejbDeployment = deployments.get(ejbName);
            pruneRefs(bean, ejbDeployment);
            // }
            if (!(bean instanceof MessageDrivenBean) && !(bean instanceof EntityBean)) {
                continue;
            }
            ejbJar.removeEnterpriseBean(ejbName);
            openejbJar.removeEjbDeployment(ejbDeployment);
            removed.add(ejbName);
            final AssemblyDescriptor assemblyDescriptor = ejbJar.getAssemblyDescriptor();
            if (assemblyDescriptor != null) {
                for (final MethodPermission permission : copy(assemblyDescriptor.getMethodPermission())) {
                    for (final Method method : copy(permission.getMethod())) {
                        if (method.getEjbName().equals(ejbName)) {
                            permission.getMethod().remove(method);
                        }
                    }
                    if (permission.getMethod().size() == 0) {
                        assemblyDescriptor.getMethodPermission().remove(permission);
                    }
                }
                for (final ContainerTransaction transaction : copy(assemblyDescriptor.getContainerTransaction())) {
                    for (final Method method : copy(transaction.getMethod())) {
                        if (method.getEjbName().equals(ejbName)) {
                            transaction.getMethod().remove(method);
                        }
                    }
                    if (transaction.getMethod().size() == 0) {
                        assemblyDescriptor.getContainerTransaction().remove(transaction);
                    }
                }
                for (final InterceptorBinding binding : copy(assemblyDescriptor.getInterceptorBinding())) {
                    if (binding.getEjbName().equals(ejbName)) {
                        assemblyDescriptor.getInterceptorBinding().remove(binding);
                    }
                }
            }
        }
        // Drop any ejb ref to with an ejb-link to a removed ejb
        for (final EnterpriseBean bean : ejbJar.getEnterpriseBeans()) {
            bean.getEjbLocalRefMap().keySet().removeAll(removed);
            bean.getEjbRefMap().keySet().removeAll(removed);
        }
        for (final Interceptor interceptor : ejbJar.getInterceptors()) {
            pruneRefs(interceptor, new EjbDeployment());
        }
    }
    return appModule;
}
Also used : EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) ArrayList(java.util.ArrayList) Method(org.apache.openejb.jee.Method) MethodPermission(org.apache.openejb.jee.MethodPermission) InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) ContainerTransaction(org.apache.openejb.jee.ContainerTransaction) EntityBean(org.apache.openejb.jee.EntityBean) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) Interceptor(org.apache.openejb.jee.Interceptor) EjbJar(org.apache.openejb.jee.EjbJar)

Example 3 with AssemblyDescriptor$JAXB.readAssemblyDescriptor

use of org.apache.openejb.jee.AssemblyDescriptor$JAXB.readAssemblyDescriptor in project tomee by apache.

the class RemoveWebServices method deploy.

public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final EjbJar ejbJar = ejbModule.getEjbJar();
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        final Map<String, EjbDeployment> deployments = openejbJar.getDeploymentsByEjbName();
        for (final EnterpriseBean bean : ejbJar.getEnterpriseBeans()) {
            final String ejbName = bean.getEjbName();
            final EjbDeployment ejbDeployment = deployments.get(ejbName);
            // Clear any <service-ref> references from ejbs
            bean.getServiceRef().clear();
            if (!(bean instanceof SessionBean)) {
                continue;
            }
            final SessionBean sessionBean = (SessionBean) bean;
            if (sessionBean.getServiceEndpoint() == null) {
                continue;
            }
            sessionBean.setServiceEndpoint(null);
            // if not, then we should just delete it
            if (sessionBean.getHome() != null) {
                continue;
            }
            if (sessionBean.getLocalHome() != null) {
                continue;
            }
            if (sessionBean.getBusinessLocal().size() > 0) {
                continue;
            }
            if (sessionBean.getBusinessRemote().size() > 0) {
                continue;
            }
            // Ok, delete away...
            ejbJar.removeEnterpriseBean(ejbName);
            openejbJar.removeEjbDeployment(ejbDeployment);
            // As well, let's get rid of any transaction or security attributes
            // associated with the bean we just deleted.
            final AssemblyDescriptor assemblyDescriptor = ejbJar.getAssemblyDescriptor();
            if (assemblyDescriptor != null) {
                for (final MethodPermission permission : copy(assemblyDescriptor.getMethodPermission())) {
                    for (final Method method : copy(permission.getMethod())) {
                        if (method.getEjbName().equals(ejbName)) {
                            permission.getMethod().remove(method);
                        }
                    }
                    if (permission.getMethod().size() == 0) {
                        assemblyDescriptor.getMethodPermission().remove(permission);
                    }
                }
                for (final ContainerTransaction transaction : copy(assemblyDescriptor.getContainerTransaction())) {
                    for (final Method method : copy(transaction.getMethod())) {
                        if (method.getEjbName().equals(ejbName)) {
                            transaction.getMethod().remove(method);
                        }
                    }
                    if (transaction.getMethod().size() == 0) {
                        assemblyDescriptor.getContainerTransaction().remove(transaction);
                    }
                }
                for (final InterceptorBinding binding : copy(assemblyDescriptor.getInterceptorBinding())) {
                    if (binding.getEjbName().equals(ejbName)) {
                        assemblyDescriptor.getInterceptorBinding().remove(binding);
                    }
                }
            }
        }
        // Clear any <service-ref> references from interceptors
        for (final Interceptor interceptor : ejbJar.getInterceptors()) {
            interceptor.getServiceRef().clear();
        }
    }
    return appModule;
}
Also used : EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) Method(org.apache.openejb.jee.Method) SessionBean(org.apache.openejb.jee.SessionBean) MethodPermission(org.apache.openejb.jee.MethodPermission) InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) ContainerTransaction(org.apache.openejb.jee.ContainerTransaction) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) Interceptor(org.apache.openejb.jee.Interceptor) EjbJar(org.apache.openejb.jee.EjbJar)

Example 4 with AssemblyDescriptor$JAXB.readAssemblyDescriptor

use of org.apache.openejb.jee.AssemblyDescriptor$JAXB.readAssemblyDescriptor in project tomee by apache.

the class CheckAssemblyBindings method validate.

public void validate(final EjbModule ejbModule) {
    checkUnusedInterceptors(ejbModule);
    final Map<String, EnterpriseBean> ejbsByName = ejbModule.getEjbJar().getEnterpriseBeansByEjbName();
    final AssemblyDescriptor assembly = ejbModule.getEjbJar().getAssemblyDescriptor();
    if (assembly == null) {
        return;
    }
    for (final InterceptorBinding binding : assembly.getInterceptorBinding()) {
        final List<String> interceptorClasses = binding.getInterceptorClass();
        if (binding.getInterceptorOrder() != null) {
            interceptorClasses.addAll(binding.getInterceptorOrder().getInterceptorClass());
        }
        if (binding.getEjbName() != null && !binding.getEjbName().equals("*") && !ejbsByName.containsKey(binding.getEjbName())) {
            fail("InterceptorBinding", "interceptorBinding.noSuchEjbName", binding.getEjbName(), join(",", interceptorClasses));
        }
        if (binding.getMethod() != null) {
            if (binding.getEjbName() == null) {
                fail("InterceptorBinding", "interceptorBinding.ejbNameRequiredWithMethod", binding.getMethod().getMethodName(), join(",", interceptorClasses));
            }
        }
    }
    for (final MethodPermission permission : assembly.getMethodPermission()) {
        for (final Method method : permission.getMethod()) {
            if (method.getEjbName() == null) {
                fail("MethodPermission", "methodPermission.ejbNameRequired", method.getMethodName(), join(",", permission.getRoleName()));
            } else if (method.getEjbName().equals("*")) {
            // NOPMD
            // no-op. Just continue the loop.
            } else if (!ejbsByName.containsKey(method.getEjbName())) {
                fail("MethodPermission", "methodPermission.noSuchEjbName", method.getEjbName(), method.getMethodName(), join(",", permission.getRoleName()));
            }
        }
    }
    for (final ContainerTransaction transaction : assembly.getContainerTransaction()) {
        for (final Method method : transaction.getMethod()) {
            if (method.getEjbName() == null) {
                fail("ContainerTransaction", "containerTransaction.ejbNameRequired", method.getMethodName(), transaction.getTransAttribute());
            } else if (method.getEjbName().equals("*")) {
            // NOPMD
            // no-op. Just continue the loop.
            } else if (!ejbsByName.containsKey(method.getEjbName())) {
                fail("ContainerTransaction", "containerTransaction.noSuchEjbName", method.getEjbName(), method.getMethodName(), transaction.getTransAttribute());
            }
        }
    }
}
Also used : InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) ContainerTransaction(org.apache.openejb.jee.ContainerTransaction) Method(org.apache.openejb.jee.Method) AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) MethodPermission(org.apache.openejb.jee.MethodPermission)

Example 5 with AssemblyDescriptor$JAXB.readAssemblyDescriptor

use of org.apache.openejb.jee.AssemblyDescriptor$JAXB.readAssemblyDescriptor in project tomee by apache.

the class AnnotationDeployerTest method applicationExceptionInheritanceTest.

@Test
public /**
 *  For http://issues.apache.org/jira/browse/OPENEJB-980
 */
void applicationExceptionInheritanceTest() throws Exception {
    EjbModule ejbModule = testModule();
    final AnnotationDeployer.DiscoverAnnotatedBeans discvrAnnBeans = new AnnotationDeployer.DiscoverAnnotatedBeans();
    ejbModule = discvrAnnBeans.deploy(ejbModule);
    final AssemblyDescriptor assemblyDescriptor = ejbModule.getEjbJar().getAssemblyDescriptor();
    org.apache.openejb.jee.ApplicationException appEx = assemblyDescriptor.getApplicationException(BusinessException.class);
    assertThat(appEx, notNullValue());
    assertThat(appEx.getExceptionClass(), is(BusinessException.class.getName()));
    assertThat(appEx.isRollback(), is(true));
    // inheritance is now handled at runtime, only explicitly mentioned exceptions are in the assembly descriptor
    appEx = assemblyDescriptor.getApplicationException(ValueRequiredException.class);
    assertThat(appEx, nullValue());
}
Also used : AssemblyDescriptor(org.apache.openejb.jee.AssemblyDescriptor) Test(org.junit.Test)

Aggregations

AssemblyDescriptor (org.apache.openejb.jee.AssemblyDescriptor)16 InterceptorBinding (org.apache.openejb.jee.InterceptorBinding)13 EjbJar (org.apache.openejb.jee.EjbJar)12 Interceptor (org.apache.openejb.jee.Interceptor)12 StatelessBean (org.apache.openejb.jee.StatelessBean)7 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)6 Module (org.apache.openejb.testing.Module)5 NamedMethod (org.apache.openejb.jee.NamedMethod)4 ArrayList (java.util.ArrayList)3 Assembler (org.apache.openejb.assembler.classic.Assembler)3 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)3 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)3 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)3 EjbModule (org.apache.openejb.config.EjbModule)3 ContainerTransaction (org.apache.openejb.jee.ContainerTransaction)3 Method (org.apache.openejb.jee.Method)3 MethodPermission (org.apache.openejb.jee.MethodPermission)3 StatefulBean (org.apache.openejb.jee.StatefulBean)3 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)3 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)3