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");
}
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");
}
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();
}
}
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;
}
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));
}
Aggregations