Search in sources :

Example 6 with MessageDrivenBean

use of org.apache.openejb.jee.MessageDrivenBean in project tomee by apache.

the class LegacyProcessor method process.

public static void process(final Class<?> clazz, final EnterpriseBean bean) {
    if (bean instanceof SessionBean) {
        final SessionBean sessionBean = (SessionBean) bean;
        if (sessionBean.getSessionType() == STATEFUL && SessionSynchronization.class.isAssignableFrom(clazz)) {
            try {
                sessionBean.getAfterBegin().add(new LifecycleCallback(clazz.getMethod("afterBegin")));
                sessionBean.getBeforeCompletion().add(new LifecycleCallback(clazz.getMethod("beforeCompletion")));
                sessionBean.getAfterCompletion().add(new LifecycleCallback(clazz.getMethod("afterCompletion", boolean.class)));
            } catch (final NoSuchMethodException e) {
            //Ignore, should never happen
            }
        }
        if (javax.ejb.SessionBean.class.isAssignableFrom(clazz)) {
            final ResourceEnvRef ref = new ResourceEnvRef("javax.ejb.SessionBean/sessionContext", SessionContext.class);
            final InjectionTarget target = new InjectionTarget();
            target.setInjectionTargetClass(clazz);
            target.setInjectionTargetName("sessionContext");
            ref.getInjectionTarget().add(target);
            sessionBean.getResourceEnvRef().add(ref);
        }
    }
    if (bean instanceof MessageDrivenBean) {
        final MessageDrivenBean messageDrivenBean = (MessageDrivenBean) bean;
        if (javax.ejb.MessageDrivenBean.class.isAssignableFrom(clazz)) {
            final ResourceEnvRef ref = new ResourceEnvRef("javax.ejb.MessageDrivenBean/messageDrivenContext", MessageDrivenContext.class);
            final InjectionTarget target = new InjectionTarget();
            target.setInjectionTargetClass(clazz);
            target.setInjectionTargetName("messageDrivenContext");
            ref.getInjectionTarget().add(target);
            messageDrivenBean.getResourceEnvRef().add(ref);
        }
    }
}
Also used : SessionSynchronization(javax.ejb.SessionSynchronization) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) InjectionTarget(org.apache.openejb.jee.InjectionTarget) LifecycleCallback(org.apache.openejb.jee.LifecycleCallback) SessionBean(org.apache.openejb.jee.SessionBean) ResourceEnvRef(org.apache.openejb.jee.ResourceEnvRef)

Example 7 with MessageDrivenBean

use of org.apache.openejb.jee.MessageDrivenBean in project tomee by apache.

the class MdbConfigTest method createJaxbMdb.

private MessageDrivenBean createJaxbMdb(final String ejbName, final String mdbClass, final String messageListenerInterface) {
    final MessageDrivenBean bean = new MessageDrivenBean(ejbName);
    bean.setEjbClass(mdbClass);
    bean.setMessagingType(messageListenerInterface);
    final ActivationConfig activationConfig = new ActivationConfig();
    activationConfig.getActivationConfigProperty().add(new ActivationConfigProperty("destination", ejbName));
    activationConfig.getActivationConfigProperty().add(new ActivationConfigProperty("destinationType", "javax.jms.Queue"));
    bean.setActivationConfig(activationConfig);
    return bean;
}
Also used : ActivationConfigProperty(org.apache.openejb.jee.ActivationConfigProperty) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) ActivationConfig(org.apache.openejb.jee.ActivationConfig)

Example 8 with MessageDrivenBean

use of org.apache.openejb.jee.MessageDrivenBean 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<MessageDestination>();
    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<MessageDestination, String>();
    // 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, 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, 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, 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 9 with MessageDrivenBean

use of org.apache.openejb.jee.MessageDrivenBean in project tomee by apache.

the class AutoConfig method matchContainer.

private String matchContainer(final Class<? extends ContainerInfo> containerInfoType, final Object bean, final Collection<ContainerInfo> list) {
    for (final ContainerInfo containerInfo : list) {
        if (containerInfo.getClass().equals(containerInfoType)) {
            // MDBs must match message listener interface type
            if (MessageDrivenBean.class.isInstance(bean)) {
                final MessageDrivenBean messageDrivenBean = (MessageDrivenBean) bean;
                final String messagingType = messageDrivenBean.getMessagingType();
                if (containerInfo.properties.get("MessageListenerInterface").equals(messagingType)) {
                    return containerInfo.id;
                }
            } else {
                return containerInfo.id;
            }
        }
    }
    return null;
}
Also used : MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) ContainerInfo(org.apache.openejb.assembler.classic.ContainerInfo)

Example 10 with MessageDrivenBean

use of org.apache.openejb.jee.MessageDrivenBean 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 Class<? extends ContainerInfo> containerInfoType = ConfigurationFactory.getContainerInfoType(getType(bean));
        if (ejbDeployment.getContainerId() == null && !skipMdb(bean)) {
            String containerId = getUsableContainer(containerInfoType, bean, appResources);
            if (containerId == null) {
                containerId = createContainer(containerInfoType, ejbDeployment, bean);
            }
            ejbDeployment.setContainerId(containerId);
        }
        // create the container if it doesn't exist
        final List<String> containerIds = configFactory.getContainerIds();
        containerIds.addAll(appResources.getContainerIds());
        if (!containerIds.contains(ejbDeployment.getContainerId()) && !skipMdb(bean)) {
            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) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) ResourceRef(org.apache.openejb.jee.ResourceRef)

Aggregations

MessageDrivenBean (org.apache.openejb.jee.MessageDrivenBean)26 EjbJar (org.apache.openejb.jee.EjbJar)13 Properties (java.util.Properties)9 Assembler (org.apache.openejb.assembler.classic.Assembler)8 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)8 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)8 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)7 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)7 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)7 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)6 MessageDrivenBeanInfo (org.apache.openejb.assembler.classic.MessageDrivenBeanInfo)5 List (java.util.List)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 ActivationConfig (org.apache.openejb.jee.ActivationConfig)4 ActivationConfigProperty (org.apache.openejb.jee.ActivationConfigProperty)4 AppModule (org.apache.openejb.config.AppModule)3 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)3 EjbModule (org.apache.openejb.config.EjbModule)3 AppModuleBuilder (org.apache.openejb.core.builder.AppModuleBuilder)3 MdbBuilder (org.apache.openejb.core.builder.MdbBuilder)3