Search in sources :

Example 71 with StatelessBean

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

the class DeploymentContextOptionsTest method testAllLevels.

public void testAllLevels() throws Exception {
    SystemInstance.get().setProperty("color", "orangeSystem");
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    {
        assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
        assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    }
    {
        // Setup the descriptor information
        final EjbModule ejbModule = new EjbModule(new EjbJar(), new OpenejbJar());
        final EjbJar ejbJar = ejbModule.getEjbJar();
        final StatelessBean statelessBean = ejbJar.addEnterpriseBean(new StatelessBean(WidgetBean.class));
        {
            // Set at BeanContext level
            final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
            final EjbDeployment deployment = openejbJar.addEjbDeployment(statelessBean);
            deployment.getProperties().put("color", "orangeBean");
        }
        {
            // Set at ModuleContext level
            final OpenejbJar openejbJar = ejbModule.getOpenejbJar();
            openejbJar.getProperties().put("color", "orangeModule");
        }
        final AppModule appModule = new AppModule(ejbModule);
        {
            // Set at AppContext level
            appModule.getProperties().put("color", "orangeApp");
        }
        assembler.createApplication(config.configureApplication(appModule));
    }
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    final BeanContext beanContext = containerSystem.getBeanContext("WidgetBean");
    assertOption(beanContext.getOptions(), "color", "orangeBean");
    assertOption(beanContext.getModuleContext().getOptions(), "color", "orangeModule");
    assertOption(beanContext.getModuleContext().getAppContext().getOptions(), "color", "orangeApp");
    assertOption(SystemInstance.get().getOptions(), "color", "orangeSystem");
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) AppModule(org.apache.openejb.config.AppModule) StatelessBean(org.apache.openejb.jee.StatelessBean) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbModule(org.apache.openejb.config.EjbModule) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar)

Example 72 with StatelessBean

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

the class DeploymentContextPropertiesTest method testAppContextProperties.

public void testAppContextProperties() throws Exception {
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    {
        assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
        assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    }
    {
        // Setup the descriptor information
        final EjbModule ejbModule = new EjbModule(new EjbJar(), new OpenejbJar());
        final EjbJar ejbJar = ejbModule.getEjbJar();
        ejbJar.addEnterpriseBean(new StatelessBean(WidgetBean.class));
        final AppModule appModule = new AppModule(ejbModule);
        appModule.getProperties().setProperty("color", "orange");
        final AppInfo appInfo = config.configureApplication(appModule);
        assertProperty(appInfo.properties, "color", "orange");
        assembler.createApplication(appInfo);
    }
    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
    final BeanContext beanContext = containerSystem.getBeanContext("WidgetBean");
    final Properties properties = beanContext.getModuleContext().getAppContext().getProperties();
    assertProperty(properties, "color", "orange");
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) AppModule(org.apache.openejb.config.AppModule) StatelessBean(org.apache.openejb.jee.StatelessBean) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbModule(org.apache.openejb.config.EjbModule) Assembler(org.apache.openejb.assembler.classic.Assembler) Properties(java.util.Properties) EjbJar(org.apache.openejb.jee.EjbJar) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 73 with StatelessBean

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

the class DynamicDataSourceTest method route.

@Test
public void route() throws Exception {
    System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    // resources
    for (int i = 1; i <= 3; i++) {
        final String dbName = "database" + i;
        final Resource resourceDs = new Resource(dbName, "DataSource");
        final Properties p = resourceDs.getProperties();
        p.put("JdbcDriver", "org.hsqldb.jdbcDriver");
        p.put("JdbcUrl", "jdbc:hsqldb:mem:db" + i);
        p.put("UserName", "sa");
        p.put("Password", "");
        p.put("JtaManaged", "true");
        assembler.createResource(config.configureService(resourceDs, ResourceInfo.class));
    }
    final Resource resourceRouter = new Resource("My Router", "org.apache.openejb.router.test.DynamicDataSourceTest$DeterminedRouter", "org.router:DeterminedRouter");
    resourceRouter.getProperties().setProperty("DatasourceNames", "database1 database2 database3");
    resourceRouter.getProperties().setProperty("DefaultDataSourceName", "database1");
    assembler.createResource(config.configureService(resourceRouter, ResourceInfo.class));
    final Resource resourceRoutedDs = new Resource("Routed Datasource", "org.apache.openejb.resource.jdbc.Router", "RoutedDataSource");
    resourceRoutedDs.getProperties().setProperty("Router", "My Router");
    assembler.createResource(config.configureService(resourceRoutedDs, ResourceInfo.class));
    // containers
    final StatelessSessionContainerInfo statelessContainerInfo = config.configureService(StatelessSessionContainerInfo.class);
    assembler.createContainer(statelessContainerInfo);
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(RoutedEJBBean.class));
    ejbJar.addEnterpriseBean(new StatelessBean(UtilityBean.class));
    final EjbModule ejbModule = new EjbModule(ejbJar);
    // Create an "ear"
    final AppModule appModule = new AppModule(ejbModule.getClassLoader(), "test-dynamic-data-source");
    appModule.getEjbModules().add(ejbModule);
    // Create a persistence-units
    final PersistenceUnit unit = new PersistenceUnit("router");
    unit.addClass(Person.class);
    unit.getProperties().put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
    unit.setTransactionType(TransactionType.JTA);
    unit.setJtaDataSource("Routed Datasource");
    appModule.addPersistenceModule(new PersistenceModule("root", new Persistence(unit)));
    for (int i = 1; i <= 3; i++) {
        final PersistenceUnit u = new PersistenceUnit("db" + i);
        u.addClass(Person.class);
        u.getProperties().put("openjpa.jdbc.SynchronizeMappings", "buildSchema");
        u.setTransactionType(TransactionType.JTA);
        u.setJtaDataSource("database" + i);
        appModule.addPersistenceModule(new PersistenceModule("root", new Persistence(u)));
    }
    assembler.createApplication(config.configureApplication(appModule));
    // context
    final Context ctx = new InitialContext();
    // running persist on all "routed" databases
    final List<String> databases = new ArrayList<>();
    databases.add("database1");
    databases.add("database2");
    databases.add("database3");
    // convinient bean to create tables for each persistence unit
    final Utility utility = (Utility) ctx.lookup("UtilityBeanLocal");
    utility.initDatabase();
    final RoutedEJB ejb = (RoutedEJB) ctx.lookup("RoutedEJBBeanLocal");
    for (int i = 0; i < 18; i++) {
        final String name = "record " + i;
        final String db = databases.get(i % 3);
        ejb.persist(i, name, db);
    }
    // assert database records number using jdbc
    for (int i = 1; i <= 3; i++) {
        final Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:db" + i, "sa", "");
        final Statement st = connection.createStatement();
        final ResultSet rs = st.executeQuery("select count(*) from \"DynamicDataSourceTest$Person\"");
        rs.next();
        assertEquals(6, rs.getInt(1));
        st.close();
        connection.close();
    }
}
Also used : StatelessSessionContainerInfo(org.apache.openejb.assembler.classic.StatelessSessionContainerInfo) AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) ArrayList(java.util.ArrayList) LocalInitialContextFactory(org.apache.openejb.core.LocalInitialContextFactory) Properties(java.util.Properties) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) ResultSet(java.sql.ResultSet) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJar(org.apache.openejb.jee.EjbJar) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) PersistenceContext(javax.persistence.PersistenceContext) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) Statement(java.sql.Statement) Resource(org.apache.openejb.config.sys.Resource) Connection(java.sql.Connection) PersistenceModule(org.apache.openejb.config.PersistenceModule) InitialContext(javax.naming.InitialContext) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) StatelessBean(org.apache.openejb.jee.StatelessBean) Assembler(org.apache.openejb.assembler.classic.Assembler) Test(org.junit.Test)

Example 74 with StatelessBean

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

the class ResourceLocalEmInjectionTest method app.

@Module
public StatelessBean app() throws Exception {
    final StatelessBean bean = new StatelessBean(PersistManager.class);
    bean.setLocalBean(new Empty());
    return bean;
}
Also used : Empty(org.apache.openejb.jee.Empty) StatelessBean(org.apache.openejb.jee.StatelessBean) Module(org.apache.openejb.testing.Module)

Example 75 with StatelessBean

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

the class TransactionAttributesTest method test.

public void test() throws Exception {
    final Assembler assembler = new Assembler();
    final ConfigurationFactory config = new ConfigurationFactory();
    assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(Color.class));
    ejbJar.addEnterpriseBean(new StatelessBean(Red.class));
    ejbJar.addEnterpriseBean(new StatelessBean(Crimson.class));
    ejbJar.addEnterpriseBean(new StatelessBean(Scarlet.class));
    final List<ContainerTransaction> declared = ejbJar.getAssemblyDescriptor().getContainerTransaction();
    declared.add(new ContainerTransaction(TransAttribute.REQUIRED, "*", "*", "*"));
    declared.add(new ContainerTransaction(TransAttribute.SUPPORTS, "*", "Crimson", "*"));
    declared.add(new ContainerTransaction(TransAttribute.SUPPORTS, Color.class.getName(), "Scarlet", "*"));
    declared.add(new ContainerTransaction(TransAttribute.NEVER, Red.class.getName(), "Scarlet", "red"));
    declared.add(new ContainerTransaction(TransAttribute.REQUIRED, "Scarlet", Scarlet.class.getMethod("scarlet")));
    ejbJar.getAssemblyDescriptor().addInterceptorBinding(new InterceptorBinding("*", AttributeInterceptor.class.getName()));
    final EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
    assembler.createApplication(ejbJarInfo);
    loadAttributes(ejbJarInfo, "Color");
    assertAttribute("Never", Color.class.getMethod("color"));
    assertAttribute("RequiresNew", Color.class.getMethod("color", Object.class));
    assertAttribute("Mandatory", Color.class.getMethod("color", String.class));
    assertAttribute("Mandatory", Color.class.getMethod("color", Boolean.class));
    assertAttribute("Mandatory", Color.class.getMethod("color", Integer.class));
    loadAttributes(ejbJarInfo, "Red");
    assertAttribute("Never", Red.class.getMethod("color"));
    assertAttribute("Required", Red.class.getMethod("color", Object.class));
    assertAttribute("Mandatory", Red.class.getMethod("color", String.class));
    assertAttribute("Mandatory", Red.class.getMethod("color", Boolean.class));
    assertAttribute("Mandatory", Red.class.getMethod("color", Integer.class));
    assertAttribute("RequiresNew", Red.class.getMethod("red"));
    assertAttribute("Required", Red.class.getMethod("red", Object.class));
    assertAttribute("Required", Red.class.getMethod("red", String.class));
    loadAttributes(ejbJarInfo, "Crimson");
    assertAttribute("Supports", Crimson.class.getMethod("color"));
    assertAttribute("Supports", Crimson.class.getMethod("color", Object.class));
    assertAttribute("Supports", Crimson.class.getMethod("color", String.class));
    assertAttribute("Supports", Crimson.class.getMethod("color", Boolean.class));
    assertAttribute("Supports", Crimson.class.getMethod("color", Integer.class));
    assertAttribute("RequiresNew", Crimson.class.getMethod("red"));
    assertAttribute("Supports", Crimson.class.getMethod("red", Object.class));
    assertAttribute("Supports", Crimson.class.getMethod("red", String.class));
    assertAttribute("RequiresNew", Crimson.class.getMethod("crimson"));
    assertAttribute("Supports", Crimson.class.getMethod("crimson", String.class));
    loadAttributes(ejbJarInfo, "Scarlet");
    assertAttribute("Never", Scarlet.class.getMethod("color"));
    assertAttribute("Required", Scarlet.class.getMethod("color", Object.class));
    assertAttribute("Supports", Scarlet.class.getMethod("color", String.class));
    assertAttribute("Supports", Scarlet.class.getMethod("color", Boolean.class));
    assertAttribute("Supports", Scarlet.class.getMethod("color", Integer.class));
    assertAttribute("RequiresNew", Scarlet.class.getMethod("red"));
    assertAttribute("Never", Scarlet.class.getMethod("red", Object.class));
    assertAttribute("Never", Scarlet.class.getMethod("red", String.class));
    assertAttribute("Required", Scarlet.class.getMethod("scarlet"));
    assertAttribute("NotSupported", Scarlet.class.getMethod("scarlet", String.class));
}
Also used : InterceptorBinding(org.apache.openejb.jee.InterceptorBinding) StatelessBean(org.apache.openejb.jee.StatelessBean) ContainerTransaction(org.apache.openejb.jee.ContainerTransaction) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

StatelessBean (org.apache.openejb.jee.StatelessBean)132 EjbJar (org.apache.openejb.jee.EjbJar)120 Assembler (org.apache.openejb.assembler.classic.Assembler)58 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)46 EjbModule (org.apache.openejb.config.EjbModule)39 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)33 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)33 InitialContext (javax.naming.InitialContext)31 Properties (java.util.Properties)30 Module (org.apache.openejb.testing.Module)25 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)19 StatelessSessionContainerInfo (org.apache.openejb.assembler.classic.StatelessSessionContainerInfo)18 AppModule (org.apache.openejb.config.AppModule)18 ProxyFactoryInfo (org.apache.openejb.assembler.classic.ProxyFactoryInfo)17 ServerFederation (org.apache.openejb.core.ServerFederation)15 Context (javax.naming.Context)14 ContainerSystem (org.apache.openejb.spi.ContainerSystem)14 LocalInitialContextFactory (org.apache.openejb.core.LocalInitialContextFactory)13 Empty (org.apache.openejb.jee.Empty)13 StatefulBean (org.apache.openejb.jee.StatefulBean)12