use of org.apache.openejb.config.AppModule in project tomee by apache.
the class JavaLookupScopesTest method test.
public void test() throws Exception {
final AppContext app;
{
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 EjbJar ejbJar = new EjbJar("testmodule");
ejbJar.addEnterpriseBean(new SingletonBean(Bean.class));
// Deploy the bean a second time to simulate situations
// where the same java:module java:app java:global names
// are re-declared in a compatible way
ejbJar.addEnterpriseBean(new SingletonBean("Other", Bean.class));
final EjbModule ejbModule = new EjbModule(ejbJar);
final AppModule module = new AppModule(ejbModule);
app = assembler.createApplication(config.configureApplication(module));
}
final BeanContext bean = app.getBeanContexts().get(0);
final ModuleContext module = bean.getModuleContext();
{
// app context lookups
final Context context = app.getAppJndiContext();
assertTrue(context.lookup("app") instanceof Context);
assertTrue(context.lookup("app/AppName") instanceof String);
assertTrue(context.lookup("app/green") instanceof DataSource);
assertTrue(context.lookup("app/testmodule") instanceof Context);
assertTrue(context.lookup("app/testmodule/Bean") instanceof Bean);
assertTrue(context.lookup("app/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
assertTrue(context.lookup("app/testmodule/Other") instanceof Bean);
assertTrue(context.lookup("app/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
assertEquals("testmodule", context.lookup("app/AppName"));
}
{
// module context lookups
final Context context = module.getModuleJndiContext();
assertTrue(context.lookup("module") instanceof Context);
assertTrue(context.lookup("module/ModuleName") instanceof String);
assertTrue(context.lookup("module/blue") instanceof DataSource);
assertTrue(context.lookup("module/Bean") instanceof Bean);
assertTrue(context.lookup("module/Bean!" + Bean.class.getName()) instanceof Bean);
assertEquals("testmodule", context.lookup("module/ModuleName"));
// TODO the Module JNDI context *should* be able to see the App context
}
{
final Context context = bean.getJndiContext();
assertTrue(context.lookup("comp") instanceof Context);
assertTrue(context.lookup("comp/EJBContext") instanceof EJBContext);
assertTrue(context.lookup("comp/TimerService") instanceof TimerService);
assertTrue(context.lookup("comp/TransactionManager") instanceof TransactionManager);
assertTrue(context.lookup("comp/TransactionSynchronizationRegistry") instanceof TransactionSynchronizationRegistry);
assertTrue(context.lookup("comp/WebServiceContext") instanceof WebServiceContext);
assertTrue(context.lookup("comp/env") instanceof Context);
assertTrue(context.lookup("comp/env") instanceof Context);
assertTrue(context.lookup("comp/env/orange") instanceof DataSource);
assertTrue(context.lookup("comp/env/" + Bean.class.getName()) instanceof Context);
assertTrue(context.lookup("comp/env/" + Bean.class.getName() + "/red") instanceof DataSource);
assertTrue(context.lookup("module") instanceof Context);
assertTrue(context.lookup("module/ModuleName") instanceof String);
assertTrue(context.lookup("module/blue") instanceof DataSource);
assertTrue(context.lookup("module/Bean") instanceof Bean);
assertTrue(context.lookup("module/Bean!" + Bean.class.getName()) instanceof Bean);
assertTrue(context.lookup("module/Other") instanceof Bean);
assertTrue(context.lookup("module/Other!" + Bean.class.getName()) instanceof Bean);
assertTrue(context.lookup("app") instanceof Context);
assertTrue(context.lookup("app/AppName") instanceof String);
assertTrue(context.lookup("app/green") instanceof DataSource);
assertTrue(context.lookup("app/testmodule") instanceof Context);
assertTrue(context.lookup("app/testmodule/Bean") instanceof Bean);
assertTrue(context.lookup("app/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
assertTrue(context.lookup("app/testmodule/Other") instanceof Bean);
assertTrue(context.lookup("app/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
assertTrue(context.lookup("global") instanceof Context);
assertTrue(context.lookup("global/yellow") instanceof DataSource);
assertTrue(context.lookup("global/testmodule") instanceof Context);
assertTrue(context.lookup("global/testmodule/Bean") instanceof Bean);
assertTrue(context.lookup("global/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
assertTrue(context.lookup("global/testmodule/Other") instanceof Bean);
assertTrue(context.lookup("global/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
assertEquals("testmodule", context.lookup("app/AppName"));
assertEquals("testmodule", context.lookup("module/ModuleName"));
}
}
use of org.apache.openejb.config.AppModule in project tomee by apache.
the class CustomMdbContainerTest method test.
public void test() throws Exception {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
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
EmailBean.lifecycle.clear();
final AppModule app = new AppModule(this.getClass().getClassLoader(), "testapp");
final Connector connector = new Connector("email-ra");
final ResourceAdapter adapter = new ResourceAdapter(EmailResourceAdapter.class);
connector.setResourceAdapter(adapter);
final InboundResourceadapter inbound = adapter.setInboundResourceAdapter(new InboundResourceadapter());
final MessageAdapter messageAdapter = inbound.setMessageAdapter(new MessageAdapter());
final MessageListener listener = messageAdapter.addMessageListener(new MessageListener(EmailConsumer.class, EmailAccountInfo.class));
listener.getActivationSpec().addRequiredConfigProperty("address");
app.getConnectorModules().add(new ConnectorModule(connector));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new MessageDrivenBean(EmailBean.class));
app.getEjbModules().add(new EjbModule(ejbJar));
final AppInfo appInfo = config.configureApplication(app);
assembler.createApplication(appInfo);
final InitialContext initialContext = new InitialContext();
final EmailResourceAdapter ra = (EmailResourceAdapter) initialContext.lookup("java:openejb/Resource/email-raRA");
final Properties headers = new Properties();
headers.put("To", "dblevins@apache.org");
headers.put("From", "dblevins@visi.com");
headers.put("Subject", "Hello");
ra.deliverEmail(headers, "How's it going?");
final Stack<Lifecycle> lifecycle = EmailBean.lifecycle;
final List expected = Arrays.asList(Lifecycle.values());
Assert.assertEquals(Join.join("\n", expected), Join.join("\n", lifecycle));
}
use of org.apache.openejb.config.AppModule in project tomee by apache.
the class NoMessageDeliveryTest method test.
public void test() throws Exception {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
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
EmailBean.lifecycle.clear();
final AppModule app = new AppModule(this.getClass().getClassLoader(), "testapp");
final Connector connector = new Connector("email-ra");
final ResourceAdapter adapter = new ResourceAdapter(EmailResourceAdapter.class);
connector.setResourceAdapter(adapter);
final InboundResourceadapter inbound = adapter.setInboundResourceAdapter(new InboundResourceadapter());
final MessageAdapter messageAdapter = inbound.setMessageAdapter(new MessageAdapter());
final MessageListener listener = messageAdapter.addMessageListener(new MessageListener(EmailConsumer.class, EmailAccountInfo.class));
listener.getActivationSpec().addRequiredConfigProperty("address");
app.getConnectorModules().add(new ConnectorModule(connector));
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new MessageDrivenBean(EmailBean.class));
app.getEjbModules().add(new EjbModule(ejbJar));
final AppInfo appInfo = config.configureApplication(app);
assembler.createApplication(appInfo);
final InitialContext initialContext = new InitialContext();
final EmailResourceAdapter ra = (EmailResourceAdapter) initialContext.lookup("java:openejb/Resource/email-raRA");
final Properties headers = new Properties();
headers.put("To", "dblevins@apache.org");
headers.put("From", "dblevins@visi.com");
headers.put("Subject", "Hello");
ra.deliverEmail(headers, "How's it going?");
final Stack<Lifecycle> lifecycle = EmailBean.lifecycle;
final List expected = Arrays.asList(Lifecycle.CONSTRUCTOR, Lifecycle.INJECTION, Lifecycle.POST_CONSTRUCT);
Assert.assertEquals(Join.join("\n", expected), Join.join("\n", lifecycle));
}
use of org.apache.openejb.config.AppModule in project tomee by apache.
the class EntityManagerPropogationTest method setUp.
public void setUp() throws OpenEJBException, IOException, NamingException {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
System.setProperty("openejb.embedded", "true");
// Boot up the minimum required OpenEJB components
final Assembler assembler = new Assembler();
final ConfigurationFactory config = new ConfigurationFactory();
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
// Start creating the test application
// Create an ejb-jar.xml for this app
final EjbJar ejbJar = new EjbJar();
ejbJar.addEnterpriseBean(new StatefulBean("PleaseCloseMyExtendedEm", PleaseCloseMyExtendedEmBean.class));
ejbJar.addEnterpriseBean(new StatefulBean("PleaseCloseMyEm", PleaseCloseMyEmBean.class));
ejbJar.addEnterpriseBean(new StatelessBean("PleaseCloseMyLessEm", PleaseCloseMyEmBean.class));
// Add six beans and link them all in a chain
addStatefulBean(ejbJar, ExtendedContextBean.class, "Extended", "Extendedx2");
addStatefulBean(ejbJar, ExtendedContextBean.class, "Extendedx2", "Extendedx3");
addStatefulBean(ejbJar, ExtendedContextBean.class, "Extendedx3", "Extendedx4");
addStatefulBean(ejbJar, ExtendedContextBean.class, "Extendedx4", "Extendedx5");
addStatefulBean(ejbJar, ExtendedContextBean.class, "ExtendedToTransaction", "StatelessTransactionToExtended");
addStatefulBean(ejbJar, ExtendedContextBean.class, "Extendedx5", "Extendedx6");
ejbJar.addEnterpriseBean(new StatefulBean("Extendedx6", EndNodeBean.class));
// Add six beans and link them all in a chain
addStatefulBean(ejbJar, TransactionContextBean.class, "Transaction", "Transactionx2");
addStatefulBean(ejbJar, TransactionContextBean.class, "Transactionx2", "Transactionx3");
addStatefulBean(ejbJar, TransactionContextBean.class, "Transactionx3", "Transactionx4");
addStatefulBean(ejbJar, TransactionContextBean.class, "Transactionx4", "Transactionx5");
addStatefulBean(ejbJar, TransactionContextBean.class, "Transactionx5", "Transactionx6");
addStatefulBean(ejbJar, TransactionContextBean.class, "TransactionToExtended", "Extendedx5");
addStatelessBean(ejbJar, TransactionContextBean.class, "StatelessTransactionToExtended", "Extendedx5");
ejbJar.addEnterpriseBean(new StatefulBean("Transactionx6", EndNodeBean.class));
ejbJar.setAssemblyDescriptor(new AssemblyDescriptor());
ejbJar.getAssemblyDescriptor().addApplicationException(IllegalArgumentException.class, false, true);
ejbJar.getAssemblyDescriptor().addApplicationException(ArgumentException.class, false, true);
// List<ContainerTransaction> declared = ejbJar.getAssemblyDescriptor().getContainerTransaction();
// declared.add(new ContainerTransaction(TransAttribute.REQUIRED, ExtendedContextBean.class.getName(), "Extendedx5", "*"));
// declared.add(new ContainerTransaction(TransAttribute.REQUIRED, ExtendedContextBean.class.getName(), "TransactionToExtended", "*"));
final EjbModule ejbModule = new EjbModule(ejbJar);
// Create an "ear"
final AppModule appModule = new AppModule(ejbModule.getClassLoader(), "test-app");
// Add the ejb-jar.xml to the ear
appModule.getEjbModules().add(ejbModule);
// Create a persistence-unit for this app
final PersistenceUnit unit = new PersistenceUnit("testUnit");
unit.addClass(Color.class);
unit.setProperty("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true)");
unit.getProperties().setProperty("openjpa.RuntimeUnenhancedClasses", "supported");
// Add the persistence.xml to the "ear"
appModule.addPersistenceModule(new PersistenceModule("root", new Persistence(unit)));
// Configure and assemble the ear -- aka. deploy it
final AppInfo info = config.configureApplication(appModule);
assembler.createApplication(info);
}
use of org.apache.openejb.config.AppModule 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();
}
}
Aggregations