Search in sources :

Example 1 with LinkResolver

use of org.apache.openejb.util.LinkResolver 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 LinkResolver

use of org.apache.openejb.util.LinkResolver in project tomee by apache.

the class CheckDependsOn method validate.

public void validate(final AppModule appModule) {
    module = appModule;
    final LinkResolver<Bean> app = new LinkResolver<>();
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final Resolver<Bean> resolver = new Resolver(app, new LinkResolver<Bean>());
        for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
            final Bean b = new Bean(bean, ejbModule, ejbModule.getModuleUri(), resolver);
            resolver.module.add(ejbModule.getModuleUri(), bean.getEjbName(), b);
            resolver.app.add(ejbModule.getModuleUri(), bean.getEjbName(), b);
        }
    }
    for (final Bean bean : app.values()) {
        final EnterpriseBean enterpriseBean = bean.bean;
        if (!(enterpriseBean instanceof SessionBean)) {
            continue;
        }
        final SessionBean sessionBean = (SessionBean) enterpriseBean;
        if (sessionBean.getSessionType() != SessionType.SINGLETON) {
            continue;
        }
        for (final String ejbName : sessionBean.getDependsOn()) {
            final Bean referee = bean.resolveLink(ejbName);
            if (referee == null) {
                bean.module.getValidation().fail(enterpriseBean.getEjbName(), "dependsOn.noSuchEjb", ejbName);
            } else {
                bean.dependsOn.add(referee);
            }
        }
    }
    try {
        References.sort(new ArrayList<Bean>(app.values()), new References.Visitor<Bean>() {

            public String getName(final Bean t) {
                return t.getId();
            }

            public Set<String> getReferences(final Bean t) {
                final LinkedHashSet<String> refs = new LinkedHashSet<>();
                for (final Bean bean : t.dependsOn) {
                    refs.add(bean.getId());
                }
                return refs;
            }
        });
    } catch (final CircularReferencesException e) {
        for (final List<Bean> circuit : e.getCircuits()) {
            final List<String> ejbNames = new ArrayList<>(circuit.size());
            for (final Bean bean : circuit) {
                ejbNames.add(bean.bean.getEjbName());
            }
            fail("EAR", "dependsOn.circuit", Join.join(" -> ", ejbNames), ejbNames.get(0));
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) LinkResolver(org.apache.openejb.util.LinkResolver) EjbModule(org.apache.openejb.config.EjbModule) SessionBean(org.apache.openejb.jee.SessionBean) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) SessionBean(org.apache.openejb.jee.SessionBean) LinkResolver(org.apache.openejb.util.LinkResolver) References(org.apache.openejb.util.References) CircularReferencesException(org.apache.openejb.util.CircularReferencesException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with LinkResolver

use of org.apache.openejb.util.LinkResolver in project tomee by apache.

the class LinkResolverTest method test.

public void test() throws Exception {
    final LinkResolver<Thing> resolver = new LinkResolver<>();
    resolver.add("my/module.jar", "one", Thing.ONE);
    resolver.add("some/other.jar", "two", Thing.TWO);
    final URI moduleUri = URI.create("my/module.jar");
    assertEquals(Thing.ONE, resolver.resolveLink("one", moduleUri));
    assertEquals(Thing.ONE, resolver.resolveLink("module.jar#one", moduleUri));
    assertEquals(Thing.ONE, resolver.resolveLink("../my/module.jar#one", moduleUri));
    assertEquals(Thing.ONE, resolver.resolveLink("../my/./module.jar#one", moduleUri));
    assertEquals(Thing.TWO, resolver.resolveLink("two", moduleUri));
    assertEquals(Thing.TWO, resolver.resolveLink("../some/./other.jar#two", moduleUri));
}
Also used : LinkResolver(org.apache.openejb.util.LinkResolver) URI(java.net.URI)

Aggregations

LinkResolver (org.apache.openejb.util.LinkResolver)3 URI (java.net.URI)2 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Properties (java.util.Properties)1 Set (java.util.Set)1 Queue (javax.jms.Queue)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 EjbModule (org.apache.openejb.config.EjbModule)1 AssemblyDescriptor (org.apache.openejb.jee.AssemblyDescriptor)1 MessageDestination (org.apache.openejb.jee.MessageDestination)1 MessageDestinationRef (org.apache.openejb.jee.MessageDestinationRef)1 MessageDrivenBean (org.apache.openejb.jee.MessageDrivenBean)1 SessionBean (org.apache.openejb.jee.SessionBean)1 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)1 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)1 ResourceLink (org.apache.openejb.jee.oejb3.ResourceLink)1