Search in sources :

Example 41 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class ActivationConfigPropertyOverride method deploy.

@Override
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    final Properties system = new Properties();
    system.putAll(SystemInstance.get().getProperties());
    system.putAll(appModule.getProperties());
    system.putAll(JavaSecurityManagers.getSystemProperties());
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        final EjbJar ejbJar = ejbModule.getEjbJar();
        final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
        final Properties module = new Properties();
        module.putAll(openejbJar.getProperties());
        module.putAll(system);
        final Map<String, EjbDeployment> deployments = openejbJar.getDeploymentsByEjbName();
        for (final EnterpriseBean bean : ejbJar.getEnterpriseBeans()) {
            final String ejbName = bean.getEjbName();
            final EjbDeployment ejbDeployment = deployments.get(ejbName);
            if (!(bean instanceof MessageDrivenBean)) {
                continue;
            }
            final Properties properties = new Properties();
            properties.putAll(module);
            properties.putAll(ejbDeployment.getProperties());
            final MessageDrivenBean mdb = (MessageDrivenBean) bean;
            // override with placeholding
            if (mdb.getActivationConfig() != null) {
                for (final ActivationConfigProperty property : mdb.getActivationConfig().getActivationConfigProperty()) {
                    final String originalValue = property.getActivationConfigPropertyValue();
                    final String value = PropertyPlaceHolderHelper.simpleValue(originalValue);
                    if (value != null && !originalValue.equals(value)) {
                        property.setActivationConfigPropertyValue(value);
                    }
                }
            }
            // now try to use special keys
            final Properties overrides = new Properties();
            final MdbContainerDetails mdbContainer = getMdbContainer(appModule, ejbDeployment.getContainerId(), appModule.getModuleId());
            if (mdbContainer != null) {
                overrides.putAll(ConfigurationFactory.getOverrides(properties, "mdb.container." + mdbContainer.getContainerId() + ".activation", "EnterpriseBean"));
                overrides.putAll(ConfigurationFactory.getOverrides(mdbContainer.getProperties(), "activation", "EnterpriseBean"));
            }
            overrides.putAll(ConfigurationFactory.getOverrides(properties, "mdb.activation", "EnterpriseBean"));
            overrides.putAll(ConfigurationFactory.getOverrides(properties, mdb.getMessagingType() + ".activation", "EnterpriseBean"));
            overrides.putAll(ConfigurationFactory.getOverrides(properties, ejbName + ".activation", "EnterpriseBean"));
            overrides.putAll(ConfigurationFactory.getOverrides(properties, ejbDeployment.getDeploymentId() + ".activation", "EnterpriseBean"));
            // If we don't have any overrides, skip to the next
            if (overrides.size() == 0) {
                continue;
            }
            if (mdb.getActivationConfig() == null) {
                mdb.setActivationConfig(new ActivationConfig());
            }
            final List<ActivationConfigProperty> activationConfigList = mdb.getActivationConfig().getActivationConfigProperty();
            for (final Map.Entry<Object, Object> entry : overrides.entrySet()) {
                final Object property = String.valueOf(entry.getKey());
                final Object value = String.valueOf(entry.getValue());
                ActivationConfigProperty activationConfigProperty = this.findActivationProperty(activationConfigList, property.toString());
                if (activationConfigProperty != null) {
                    logger.info(String.format("Found %s bean with activation-config property %s=%s to override", ejbName, activationConfigProperty.getActivationConfigPropertyName(), activationConfigProperty.getActivationConfigPropertyValue()));
                    logger.info(String.format("Overriding %s bean activation-config property.%s=%s", ejbName, property, value));
                    activationConfigProperty.setActivationConfigPropertyValue(entry.getValue().toString());
                } else {
                    logger.info(String.format("Adding %s bean activation-config property %s=%s", ejbName, property, value));
                    activationConfigProperty = new ActivationConfigProperty();
                    activationConfigProperty.setActivationConfigPropertyName(property.toString());
                    activationConfigProperty.setActivationConfigPropertyValue(value.toString());
                    activationConfigList.add(activationConfigProperty);
                }
            }
        }
    }
    return appModule;
}
Also used : EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) Properties(java.util.Properties) ActivationConfig(org.apache.openejb.jee.ActivationConfig) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) ActivationConfigProperty(org.apache.openejb.jee.ActivationConfigProperty) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Map(java.util.Map) EjbJar(org.apache.openejb.jee.EjbJar)

Example 42 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class AutoConfig method processActivationConfig.

/**
 * Set destination, destinationType, clientId and subscriptionName in the MDB activation config.
 */
private void processActivationConfig(final EjbModule ejbModule) 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()) {
        if (bean instanceof MessageDrivenBean) {
            final MessageDrivenBean mdb = (MessageDrivenBean) bean;
            if (mdb.getActivationConfig() == null) {
                mdb.setActivationConfig(new ActivationConfig());
            }
            if (!isJms(mdb)) {
                continue;
            }
            final EjbDeployment ejbDeployment = deployments.get(bean.getEjbName());
            if (ejbDeployment == null) {
                throw new OpenEJBException("No ejb deployment found for ejb " + bean.getEjbName());
            }
            final Properties properties = mdb.getActivationConfig().toProperties();
            String destination = properties.getProperty("destinationName", properties.getProperty("destinationLookup"));
            if (destination != null) {
                if (destination.startsWith("openejb:Resource/")) {
                    destination = destination.substring("openejb:Resource/".length());
                }
                if (destination.startsWith("java:openejb/Resource/")) {
                    destination = destination.substring("java:openejb/Resource/".length());
                }
                mdb.getActivationConfig().addProperty("destination", destination);
                // Remove destinationName as it is not in the standard ActivationSpec
                final List<ActivationConfigProperty> list = mdb.getActivationConfig().getActivationConfigProperty();
                final Iterator<ActivationConfigProperty> iterator = list.iterator();
                while (iterator.hasNext()) {
                    final ActivationConfigProperty configProperty = iterator.next();
                    final String activationConfigPropertyName = configProperty.getActivationConfigPropertyName();
                    if (activationConfigPropertyName.equals("destinationName") || activationConfigPropertyName.equals("destinationLookup")) {
                        iterator.remove();
                        // we suppose we have only one of both we should be the case
                        break;
                    }
                }
            } else {
                destination = properties.getProperty("destination");
            }
            if (destination == null) {
                // EE 7/EJB 3.2
                destination = properties.getProperty("destinationLookup");
            }
            // String destination = properties.getProperty("destination", properties.getProperty("destinationName"));
            if (destination == null) {
                destination = ejbDeployment.getDeploymentId();
                mdb.getActivationConfig().addProperty("destination", destination);
            }
            // destination identifier
            ResourceLink link = ejbDeployment.getResourceLink("openejb/destination");
            if (link == null && mdb.getMessageDestinationLink() == null) {
                link = new ResourceLink();
                link.setResId(destination);
                link.setResRefName("openejb/destination");
                ejbDeployment.addResourceLink(link);
            }
            // destination type
            String destinationType = properties.getProperty("destinationType");
            if (destinationType == null && mdb.getMessageDestinationType() != null) {
                destinationType = mdb.getMessageDestinationType();
                mdb.getActivationConfig().addProperty("destinationType", destinationType);
            }
            if (mdb.getMessageDestinationType() == null) {
                mdb.setMessageDestinationType(destinationType);
            }
            // topics need a clientId and subscriptionName
            if ("javax.jms.Topic".equals(destinationType)) {
                if (Boolean.parseBoolean(SystemInstance.get().getProperty("openejb.activemq.deploymentId-as-clientId", ejbModule.getProperties().getProperty("openejb.activemq.deploymentId-as-clientId", "true"))) && !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) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) Properties(java.util.Properties) SuperProperties(org.apache.openejb.util.SuperProperties) ActivationConfig(org.apache.openejb.jee.ActivationConfig) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) ActivationConfigProperty(org.apache.openejb.jee.ActivationConfigProperty) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) ResourceLink(org.apache.openejb.jee.oejb3.ResourceLink) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment)

Example 43 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class ActivationConfigPropertyOverrideTest method testMdbOverrideOpenejbJar.

@Test
public void testMdbOverrideOpenejbJar() throws Exception {
    SystemInstance.reset();
    final Assembler assembler = new Assembler();
    final ConfigurationFactory config = new ConfigurationFactory();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    {
        final EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new MessageDrivenBean(Orange.class));
        ejbJar.addEnterpriseBean(new MessageDrivenBean(Yellow.class));
        final OpenejbJar openejbJar = new OpenejbJar();
        final Properties properties = openejbJar.getProperties();
        properties.setProperty("mdb.activation.maxSessions", "20");
        properties.setProperty("mdb.activation.maxMessagesPerSessions", "100");
        properties.setProperty("mdb.activation.destinationType", "javax.jms.Queue");
        properties.setProperty("mdb.activation.destination", "OVERRIDDEN.QUEUE");
        final EjbModule ejbModule = new EjbModule(ejbJar, openejbJar);
        final EjbJarInfo ejbJarInfo = config.configureApplication(ejbModule);
        assertEquals(2, ejbJarInfo.enterpriseBeans.size());
        final MessageDrivenBeanInfo orange = (MessageDrivenBeanInfo) ejbJarInfo.enterpriseBeans.get(0);
        final MessageDrivenBeanInfo yellow = (MessageDrivenBeanInfo) ejbJarInfo.enterpriseBeans.get(1);
        assertEquals("20", orange.activationProperties.get("maxSessions"));
        assertEquals("100", orange.activationProperties.get("maxMessagesPerSessions"));
        assertEquals("javax.jms.Queue", orange.activationProperties.get("destinationType"));
        assertEquals("OVERRIDDEN.QUEUE", orange.activationProperties.get("destination"));
        assertEquals("20", yellow.activationProperties.get("maxSessions"));
        assertEquals("100", yellow.activationProperties.get("maxMessagesPerSessions"));
        assertEquals("javax.jms.Queue", yellow.activationProperties.get("destinationType"));
        assertEquals("OVERRIDDEN.QUEUE", yellow.activationProperties.get("destination"));
    }
    // Verify the openejb-jar level overrides do not affect other apps
    {
        final EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new MessageDrivenBean(Orange.class));
        ejbJar.addEnterpriseBean(new MessageDrivenBean(Yellow.class));
        final EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
        assertEquals(2, ejbJarInfo.enterpriseBeans.size());
        final MessageDrivenBeanInfo orange = (MessageDrivenBeanInfo) ejbJarInfo.enterpriseBeans.get(0);
        final MessageDrivenBeanInfo yellow = (MessageDrivenBeanInfo) ejbJarInfo.enterpriseBeans.get(1);
        assertEquals("7", orange.activationProperties.get("maxSessions"));
        assertEquals("4", orange.activationProperties.get("maxMessagesPerSessions"));
        assertEquals("javax.jms.Queue", orange.activationProperties.get("destinationType"));
        assertEquals("ORANGE.QUEUE", orange.activationProperties.get("destination"));
        assertEquals("5", yellow.activationProperties.get("maxSessions"));
        assertEquals("10", yellow.activationProperties.get("maxMessagesPerSessions"));
        assertEquals("javax.jms.Topic", yellow.activationProperties.get("destinationType"));
        assertEquals("YELLOW.TOPIC", yellow.activationProperties.get("destination"));
    }
}
Also used : OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) Properties(java.util.Properties) EjbJar(org.apache.openejb.jee.EjbJar) Test(org.junit.Test) ActivationContainerOverwriteBothConfigurationTest(org.apache.openejb.activemq.ActivationContainerOverwriteBothConfigurationTest)

Example 44 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class MessageDestinationTest method test.

public void test() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final ClassLoader cl = this.getClass().getClassLoader();
    final AppModule app = new AppModule(cl, "app");
    final MessageDestinationRef webMessageDestRef = new MessageDestinationRef();
    webMessageDestRef.setMessageDestinationRefName("jms/myqueue");
    webMessageDestRef.setMessageDestinationType("javax.jms.Queue");
    webMessageDestRef.setMessageDestinationUsage(MessageDestinationUsage.PRODUCES);
    webMessageDestRef.setMessageDestinationLink("ejb.jar#myqueue");
    final WebApp webApp = new WebApp();
    webApp.setMetadataComplete(true);
    webApp.getMessageDestinationRef().add(webMessageDestRef);
    final WebModule webModule = new WebModule(webApp, "web", cl, "war", "web");
    app.getWebModules().add(webModule);
    final EjbJar ejbJar = new EjbJar();
    final StatelessBean statelessBean = new StatelessBean(GreenBean.class);
    final MessageDestinationRef ejbMessageDestRef = new MessageDestinationRef();
    ejbMessageDestRef.setMessageDestinationRefName("jms/myqueue");
    ejbMessageDestRef.setMessageDestinationType("javax.jms.Queue");
    ejbMessageDestRef.setMessageDestinationUsage(MessageDestinationUsage.PRODUCES);
    ejbMessageDestRef.setMessageDestinationLink("myqueue");
    statelessBean.getMessageDestinationRef().add(ejbMessageDestRef);
    ejbJar.addEnterpriseBean(statelessBean);
    final MessageDestination queue = new MessageDestination();
    queue.setMessageDestinationName("myqueue");
    ejbJar.getAssemblyDescriptor().getMessageDestination().add(queue);
    final EjbModule ejbModule = new EjbModule(cl, "ejb.jar", ejbJar, new OpenejbJar());
    app.getEjbModules().add(ejbModule);
    final AppInfo info = config.configureApplication(app);
    assembler.createApplication(info);
    final Object beanQueue = assembler.getContainerSystem().getBeanContext("GreenBean").getJndiContext().lookup("comp/env/jms/myqueue");
    Assert.assertTrue(beanQueue instanceof Queue);
    Assert.assertEquals(1, info.webApps.size());
    final List<ResourceEnvReferenceInfo> resourceEnvRefs = info.webApps.get(0).jndiEnc.resourceEnvRefs;
    boolean found = false;
    for (final ResourceEnvReferenceInfo resourceEnvRef : resourceEnvRefs) {
        if (!"comp/env/jms/myqueue".equals(resourceEnvRef.referenceName))
            continue;
        found = true;
        Assert.assertEquals("jms/myqueue", resourceEnvRef.resourceID);
        Assert.assertEquals("javax.jms.Queue", resourceEnvRef.resourceEnvRefType);
    }
    Assert.assertTrue(found);
}
Also used : MessageDestination(org.apache.openejb.jee.MessageDestination) LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) ResourceEnvReferenceInfo(org.apache.openejb.assembler.classic.ResourceEnvReferenceInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) MessageDestinationRef(org.apache.openejb.jee.MessageDestinationRef) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) StatelessBean(org.apache.openejb.jee.StatelessBean) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) Queue(javax.jms.Queue) WebApp(org.apache.openejb.jee.WebApp) EjbJar(org.apache.openejb.jee.EjbJar)

Example 45 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class MappedNameBuilderTest method testIgnoreMappedNameIfOpenejbJarModuleDoesntExist.

public void testIgnoreMappedNameIfOpenejbJarModuleDoesntExist() throws Exception {
    AppModule appModule = new AppModule(new FakeClassLoader(), "");
    final EjbJar ejbJar = new EjbJar();
    final SessionBean sessionBean = new SessionBean("SessionBean", "org.superbiz.SessionBean", SessionType.STATELESS);
    sessionBean.setMappedName("MappedName");
    ejbJar.addEnterpriseBean(sessionBean);
    appModule.getEjbModules().add(new EjbModule(ejbJar, null));
    appModule = new MappedNameBuilder().deploy(appModule);
    final OpenejbJar openejbJar = appModule.getEjbModules().get(0).getOpenejbJar();
    assertNull(openejbJar);
}
Also used : OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) SessionBean(org.apache.openejb.jee.SessionBean) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)65 EjbJar (org.apache.openejb.jee.EjbJar)52 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)37 EjbModule (org.apache.openejb.config.EjbModule)30 Properties (java.util.Properties)20 StatelessBean (org.apache.openejb.jee.StatelessBean)19 Assembler (org.apache.openejb.assembler.classic.Assembler)18 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)16 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)12 ContainerSystem (org.apache.openejb.spi.ContainerSystem)12 SingletonBean (org.apache.openejb.jee.SingletonBean)11 Module (org.apache.openejb.testing.Module)11 AppModule (org.apache.openejb.config.AppModule)8 PojoDeployment (org.apache.openejb.jee.oejb3.PojoDeployment)8 InitialContext (javax.naming.InitialContext)7 OpenEJBException (org.apache.openejb.OpenEJBException)7 Resources (org.apache.openejb.config.sys.Resources)7 Service (org.apache.openejb.config.sys.Service)7 MessageDrivenBean (org.apache.openejb.jee.MessageDrivenBean)7 File (java.io.File)6