Search in sources :

Example 16 with ServerFederation

use of org.apache.openejb.core.ServerFederation in project tomee by apache.

the class FailoverTest method _testCleanShutdown.

public void _testCleanShutdown() throws Exception {
    agent = new TestAgent();
    try {
        //            Properties initProps = System.getProperties();
        final Properties initProps = new Properties();
        initProps.setProperty("openejb.deployments.classpath.include", "");
        initProps.setProperty("openejb.deployments.classpath.filter.descriptors", "true");
        OpenEJB.init(initProps, new ServerFederation());
    } catch (Exception e) {
    }
    final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    final ConfigurationFactory config = new ConfigurationFactory();
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(Target.class));
    assembler.createApplication(config.configureApplication(ejbJar));
    SystemInstance.get().setComponent(DiscoveryAgent.class, agent);
    final ServerService red = server(Host.RED);
    final ServerService blue = server(Host.BLUE);
    final ServerService green = server(Host.GREEN);
    red.start();
    blue.start();
    green.start();
    final TargetRemote target = getBean(red);
    assertEquals(Host.RED, target.getHost());
    red.stop();
    assertEquals(Host.BLUE, target.getHost());
    blue.stop();
    assertEquals(Host.GREEN, target.getHost());
    green.stop();
    try {
        target.getHost();
        fail("EJBException should have been thrown");
    } catch (EJBException e) {
    // pass
    }
    red.start();
    assertEquals(Host.RED, target.getHost());
}
Also used : ServerFederation(org.apache.openejb.core.ServerFederation) StatelessBean(org.apache.openejb.jee.StatelessBean) ServerService(org.apache.openejb.server.ServerService) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) Properties(java.util.Properties) OpenEJBException(org.apache.openejb.OpenEJBException) EJBException(javax.ejb.EJBException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException) EJBException(javax.ejb.EJBException) ServiceException(org.apache.openejb.server.ServiceException) EjbJar(org.apache.openejb.jee.EjbJar)

Example 17 with ServerFederation

use of org.apache.openejb.core.ServerFederation in project tomee by apache.

the class AppClientTest method test.

public void test() throws Exception {
    final EjbServer ejbServer = new EjbServer();
    final Properties initProps = new Properties();
    initProps.setProperty("openejb.deployments.classpath.include", "");
    initProps.setProperty("openejb.deployments.classpath.filter.descriptors", "true");
    OpenEJB.init(initProps, new ServerFederation());
    ejbServer.init(new Properties());
    final ServicePool pool = new ServicePool(ejbServer, 10);
    final ServiceDaemon serviceDaemon = new ServiceDaemon(pool, 0, "localhost");
    serviceDaemon.start();
    int port = serviceDaemon.getPort();
    final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    final ConfigurationFactory config = new ConfigurationFactory();
    final EjbModule ejbModule = new EjbModule(new EjbJar("testejbmodule"), new OpenejbJar());
    final EjbJar ejbJar = ejbModule.getEjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(Orange.class));
    final ClassLoader loader = this.getClass().getClassLoader();
    final ClientModule clientModule = new ClientModule(new ApplicationClient(), loader, "orange-client", OrangeAppClient.class.getName(), "orange-client");
    final AppModule appModule = new AppModule(loader, "testapp");
    appModule.getClientModules().add(clientModule);
    appModule.getEjbModules().add(ejbModule);
    assembler.createApplication(config.configureApplication(appModule));
    final Properties props = new Properties();
    props.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
    props.put("java.naming.provider.url", "ejbd://127.0.0.1:" + port);
    props.put("openejb.client.moduleId", "orange-client");
    Context context = new InitialContext(props);
    final Object home = context.lookup("comp/env/home");
    assertTrue(home instanceof OrangeHome);
    OrangeHome orangeHome = (OrangeHome) home;
    final OrangeRemote orangeRemote = orangeHome.create();
    assertEquals("bat", orangeRemote.echo("tab"));
    final Object business = context.lookup("comp/env/business");
    assertTrue(business instanceof OrangeBusinessRemote);
    OrangeBusinessRemote orangeBusinessRemote = (OrangeBusinessRemote) business;
    assertEquals("nap", orangeBusinessRemote.echo("pan"));
    final Object dataSourceObject = context.lookup("comp/env/datasource");
    assertTrue(dataSourceObject instanceof DataSource);
    //        DataSource dataSource = (DataSource) dataSourceObject;
    //        assertEquals("nap", orangeBusinessRemote.echo("pan"));
    props.put("openejb.client.moduleId", "openejb/global");
    context = new InitialContext(props);
    final Object global = context.lookup("global/testapp/testejbmodule/Orange!" + OrangeBusinessRemote.class.getName());
    assertTrue(global instanceof OrangeBusinessRemote);
    OrangeBusinessRemote globalOrangeBusinessRemote = (OrangeBusinessRemote) global;
    assertEquals("nap", globalOrangeBusinessRemote.echo("pan"));
    serviceDaemon.stop();
    OpenEJB.destroy();
}
Also used : AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) Properties(java.util.Properties) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbJar(org.apache.openejb.jee.EjbJar) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ServerFederation(org.apache.openejb.core.ServerFederation) ApplicationClient(org.apache.openejb.jee.ApplicationClient) ServicePool(org.apache.openejb.server.ServicePool) ClientModule(org.apache.openejb.config.ClientModule) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) EJBObject(javax.ejb.EJBObject) Assembler(org.apache.openejb.assembler.classic.Assembler)

Example 18 with ServerFederation

use of org.apache.openejb.core.ServerFederation in project tomee by apache.

the class SerializerTest method invokeRemote.

private void invokeRemote(final Properties serverProps, final String serializer) throws Exception {
    final EjbServer ejbServer = new EjbServer();
    final Properties initProps = new Properties();
    initProps.put(DeploymentsResolver.DEPLOYMENTS_CLASSPATH_PROPERTY, "false");
    OpenEJB.init(initProps, new ServerFederation());
    ejbServer.init(serverProps);
    final ServiceDaemon serviceDaemon = new ServiceDaemon(ejbServer, 0, "localhost");
    serviceDaemon.start();
    final int port = serviceDaemon.getPort();
    final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    final ConfigurationFactory config = new ConfigurationFactory();
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(AnEjbRemote.class));
    assembler.createApplication(config.configureApplication(ejbJar));
    try {
        final Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
        props.put(Context.PROVIDER_URL, "ejbd://127.0.0.1:" + port);
        if (serializer != null) {
            props.put(JNDIContext.SERIALIZER, serializer);
        }
        final Context context = new InitialContext(props);
        final AnInterfaceRemote client = AnInterfaceRemote.class.cast(context.lookup("AnEjbRemoteRemote"));
        assertNotNull(client);
        final OutputNotSerializable out = client.call(new InputNotSerilizable("cloud"));
        assertEquals("cloud", out.name);
    } finally {
        serviceDaemon.stop();
        OpenEJB.destroy();
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) JNDIContext(org.apache.openejb.client.JNDIContext) ServerFederation(org.apache.openejb.core.ServerFederation) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) EjbServer(org.apache.openejb.server.ejbd.EjbServer) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) RemoteInitialContextFactory(org.apache.openejb.client.RemoteInitialContextFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar)

Example 19 with ServerFederation

use of org.apache.openejb.core.ServerFederation in project tomee by apache.

the class AuthentWithRequestTest method invoke.

@Test
public void invoke() throws Exception {
    final EjbServer ejbServer = new EjbServer();
    OpenEJB.init(new PropertiesBuilder().p(DeploymentsResolver.DEPLOYMENTS_CLASSPATH_PROPERTY, "false").build(), new ServerFederation());
    ejbServer.init(new Properties());
    final ServiceDaemon serviceDaemon = new ServiceDaemon(ejbServer, 0, "localhost");
    serviceDaemon.start();
    final int port = serviceDaemon.getPort();
    final Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    final ConfigurationFactory config = new ConfigurationFactory();
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(RemoteWithSecurity.class));
    assembler.createApplication(config.configureApplication(ejbJar));
    try {
        {
            // ok case
            final Context context = new InitialContext(new PropertiesBuilder().p(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName()).p(Context.PROVIDER_URL, "ejbd://127.0.0.1:" + port).p(JNDIContext.AUTHENTICATE_WITH_THE_REQUEST, "true").p("java.naming.security.principal", "foo").p("java.naming.security.credentials", "bar").p("openejb.authentication.realmName", "LM").build());
            final AnInterfaceRemote client = AnInterfaceRemote.class.cast(context.lookup("RemoteWithSecurityRemote"));
            assertNotNull(client);
            assertEquals("foo", client.call());
        }
        {
            // now the failing case
            final Context context = new InitialContext(new PropertiesBuilder().p(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName()).p(Context.PROVIDER_URL, "ejbd://127.0.0.1:" + port).p(JNDIContext.AUTHENTICATE_WITH_THE_REQUEST, "true").p("java.naming.security.principal", "wrong").p("java.naming.security.credentials", "wrong").p("openejb.authentication.realmName", "LM").build());
            final AnInterfaceRemote client = AnInterfaceRemote.class.cast(context.lookup("RemoteWithSecurityRemote"));
            try {
                client.call();
            } catch (final EJBException e) {
                if (!LoginException.class.isInstance(e.getCause())) {
                    e.printStackTrace();
                }
                assertTrue(LoginException.class.isInstance(e.getCause()));
            }
        }
    } finally {
        serviceDaemon.stop();
        OpenEJB.destroy();
    }
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) JNDIContext(org.apache.openejb.client.JNDIContext) ServerFederation(org.apache.openejb.core.ServerFederation) Properties(java.util.Properties) PropertiesBuilder(org.apache.openejb.testng.PropertiesBuilder) InitialContext(javax.naming.InitialContext) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) EjbServer(org.apache.openejb.server.ejbd.EjbServer) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) RemoteInitialContextFactory(org.apache.openejb.client.RemoteInitialContextFactory) LoginException(javax.security.auth.login.LoginException) Assembler(org.apache.openejb.assembler.classic.Assembler) EJBException(javax.ejb.EJBException) EjbJar(org.apache.openejb.jee.EjbJar) Test(org.junit.Test)

Example 20 with ServerFederation

use of org.apache.openejb.core.ServerFederation in project tomee by apache.

the class HsqlServiceTest method testShouldStartDatabaseServerAndReadFromDatabase.

@Test
public void testShouldStartDatabaseServerAndReadFromDatabase() throws Exception {
    Properties serviceProps = new Properties();
    final Properties initProps = new Properties();
    initProps.put(DeploymentsResolver.DEPLOYMENTS_CLASSPATH_PROPERTY, Boolean.toString(false));
    initProps.put("movieDatabase", "new://Resource?type=DataSource");
    initProps.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
    initProps.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
    OpenEJB.init(initProps, new ServerFederation());
    int port = NetworkUtil.getNextAvailablePort();
    System.out.println("Using port " + port);
    HsqlService service = new HsqlService();
    serviceProps.put("bind", "127.0.0.1");
    serviceProps.put("port", String.valueOf(port));
    serviceProps.put("disabled", "false");
    service.init(serviceProps);
    service.start();
    Thread.sleep(5000);
    Connection connection = null;
    PreparedStatement stmt = null;
    try {
        Class.forName("org.hsqldb.jdbc.JDBCDriver");
        connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:" + port + "/moviedb", "sa", "");
        stmt = connection.prepareStatement("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");
        stmt.execute();
        stmt.close();
        connection.close();
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : ServerFederation(org.apache.openejb.core.ServerFederation) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ServerFederation (org.apache.openejb.core.ServerFederation)20 Properties (java.util.Properties)18 Assembler (org.apache.openejb.assembler.classic.Assembler)15 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)15 EjbJar (org.apache.openejb.jee.EjbJar)15 StatelessBean (org.apache.openejb.jee.StatelessBean)15 InitialContext (javax.naming.InitialContext)13 ServiceDaemon (org.apache.openejb.server.ServiceDaemon)12 Context (javax.naming.Context)11 ServicePool (org.apache.openejb.server.ServicePool)7 EjbServer (org.apache.openejb.server.ejbd.EjbServer)5 EjbModule (org.apache.openejb.config.EjbModule)4 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)4 EJBException (javax.ejb.EJBException)3 NamingException (javax.naming.NamingException)3 StatelessSessionContainerInfo (org.apache.openejb.assembler.classic.StatelessSessionContainerInfo)3 RemoteInitialContextFactory (org.apache.openejb.client.RemoteInitialContextFactory)3 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)3 ServerService (org.apache.openejb.server.ServerService)3 ServiceException (org.apache.openejb.server.ServiceException)3