Search in sources :

Example 11 with Resource

use of org.apache.openejb.config.sys.Resource 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<String>();
    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 12 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class IgnoreDefaultTest method createNormalDs.

@Test
public void createNormalDs() throws OpenEJBException, NamingException {
    final Resource resource = new Resource(IgnoreDefaultTest.class.getName() + "#normal");
    resource.setType(DataSource.class.getName());
    final ResourceInfo info = configurationFactory.configureService(resource, ResourceInfo.class);
    assembler.createResource(info);
    check(resource.getId(), "sa", "");
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) Resource(org.apache.openejb.config.sys.Resource) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 13 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class JSonConfigTest method simpleRead.

@Test
public void simpleRead() throws IOException, OpenEJBException {
    final Openejb openejb = JSonConfigReader.read(Openejb.class, Thread.currentThread().getContextClassLoader().getResource(config()).openStream());
    assertEquals(1, openejb.getResource().size());
    final Resource resource = openejb.getResource().iterator().next();
    assertEquals("json-datasource", resource.getId());
    assertTrue("123".equals(resource.getProperties().getProperty("MaxTotal")));
    assertTrue("jdbc:hsqldb:mem:json".equals(resource.getProperties().getProperty("JdbcUrl")));
    assertEquals(1, openejb.getDeployments().size());
    assertEquals("apps", openejb.getDeployments().iterator().next().getDir());
    assertEquals(1, openejb.getContainer().size());
    final Container container = openejb.getContainer().iterator().next();
    assertEquals("STATELESS", container.getType());
    assertEquals("10 seconds", container.getProperties().getProperty("AccessTimeout"));
}
Also used : Container(org.apache.openejb.config.sys.Container) Resource(org.apache.openejb.config.sys.Resource) AppResource(org.apache.openejb.testing.AppResource) Openejb(org.apache.openejb.config.sys.Openejb) Test(org.junit.Test)

Example 14 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class ReadDescriptorsTest method testClassNotAvailable.

@Test
public void testClassNotAvailable() {
    final Resource resource = new Resource();
    resource.setClassName("not.a.real.Class");
    final Resources resources = new Resources();
    resources.add(resource);
    final Resources checkedResources = ReadDescriptors.check(resources);
    final Resource res = checkedResources.getResource().get(0);
    Assert.assertEquals("true", res.getProperties().getProperty("Lazy"));
    Assert.assertEquals("true", res.getProperties().getProperty("UseAppClassLoader"));
    Assert.assertEquals("true", res.getProperties().getProperty("InitializeAfterDeployment"));
}
Also used : Resource(org.apache.openejb.config.sys.Resource) Resources(org.apache.openejb.config.sys.Resources) Test(org.junit.Test)

Example 15 with Resource

use of org.apache.openejb.config.sys.Resource in project tomee by apache.

the class ReadDescriptorsTest method testClassAvailable.

@Test
public void testClassAvailable() {
    final Resource resource = new Resource();
    resource.setClassName("org.apache.openejb.config.ReadDescriptorsTest");
    final Resources resources = new Resources();
    resources.add(resource);
    final Resources checkedResources = ReadDescriptors.check(resources);
    final Resource res = checkedResources.getResource().get(0);
    Assert.assertNull(res.getProperties().getProperty("Lazy"));
    Assert.assertNull(res.getProperties().getProperty("UseAppClassLoader"));
    Assert.assertNull(res.getProperties().getProperty("InitializeAfterDeployment"));
}
Also used : Resource(org.apache.openejb.config.sys.Resource) Resources(org.apache.openejb.config.sys.Resources) Test(org.junit.Test)

Aggregations

Resource (org.apache.openejb.config.sys.Resource)30 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)15 Test (org.junit.Test)12 Properties (java.util.Properties)11 ArrayList (java.util.ArrayList)7 OpenEJBException (org.apache.openejb.OpenEJBException)6 File (java.io.File)5 IOException (java.io.IOException)5 Resources (org.apache.openejb.config.sys.Resources)5 DataSource (javax.sql.DataSource)4 Container (org.apache.openejb.config.sys.Container)4 Openejb (org.apache.openejb.config.sys.Openejb)4 URL (java.net.URL)3 HashMap (java.util.HashMap)3 InitialContext (javax.naming.InitialContext)3 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)3 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)3 SuperProperties (org.apache.openejb.util.SuperProperties)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 MalformedURLException (java.net.MalformedURLException)2