Search in sources :

Example 1 with ServerFederation

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

the class TomcatLoader method initialize.

public void initialize(final Properties properties) throws Exception {
    // better than static (we are sure we don't hit it too eagerly) and doesn't cost more since uses static block
    Warmup.warmup();
    //Install Log
    OptionsLog.install();
    // install conf/openejb.xml and conf/logging.properties files
    final String openejbWarDir = properties.getProperty("tomee.war");
    if (openejbWarDir != null) {
        final Paths paths = new Paths(new File(openejbWarDir));
        if (paths.verify()) {
            final Installer installer = new Installer(paths);
            if (installer.getStatus() != Status.INSTALLED) {
                installer.installConfigFiles(false);
            }
        }
    }
    // Not thread safe
    if (OpenEJB.isInitialized()) {
        ejbServer = SystemInstance.get().getComponent(EjbServer.class);
        return;
    }
    final File conf = new File(SystemInstance.get().getBase().getDirectory(), "conf");
    for (final String possibleTomeePaths : ConfigUtils.deducePaths("tomee.xml")) {
        final File tomeeXml = new File(conf, possibleTomeePaths);
        if (tomeeXml.exists()) {
            // use tomee.xml instead of openejb.xml
            SystemInstance.get().setProperty("openejb.configuration", tomeeXml.getAbsolutePath());
            SystemInstance.get().setProperty("openejb.configuration.class", Tomee.class.getName());
        }
    }
    // set tomcat pool
    try {
        // in embedded mode we can easily remove it so check we can use it before setting it
        final Class<?> creatorClass = TomcatLoader.class.getClassLoader().loadClass("org.apache.tomee.jdbc.TomEEDataSourceCreator");
        SystemInstance.get().setProperty(ConfigurationFactory.OPENEJB_JDBC_DATASOURCE_CREATOR, creatorClass.getName());
    } catch (final Throwable ignored) {
    // will use the defaul tone
    }
    // tomcat default behavior is webapp, simply keep it, it is overridable by system property too
    SystemInstance.get().setProperty("openejb.default.deployment-module", System.getProperty("openejb.default.deployment-module", "org.apache.openejb.config.WebModule"));
    //Those are set by TomcatHook, why re-set here???
    System.setProperty("openejb.home", SystemInstance.get().getHome().getDirectory().getAbsolutePath());
    System.setProperty("openejb.base", SystemInstance.get().getBase().getDirectory().getAbsolutePath());
    // Install tomcat thread context listener
    ThreadContext.addThreadContextListener(new TomcatThreadContextListener());
    // set ignorable libraries from a tomee property instead of using the standard openejb one
    // don't ignore standard openejb exclusions file
    final Class<?> scanner = Class.forName("org.apache.tomcat.util.scan.StandardJarScanFilter", true, TomcatLoader.class.getClassLoader());
    final Set<String> forcedScanJar = Set.class.cast(Reflections.get(scanner, null, "defaultScanSet"));
    final Set<String> forcedSkipJar = Set.class.cast(Reflections.get(scanner, null, "defaultSkipSet"));
    NewLoaderLogic.addAdditionalCustomFilter(forcedSkipJar.isEmpty() ? null : new TomcatToXbeanFilter(forcedSkipJar), forcedScanJar.isEmpty() ? null : new TomcatToXbeanFilter(forcedScanJar));
    // now we use the default tomcat filter so no need to do it
    // System.setProperty(Constants.SKIP_JARS_PROPERTY, Join.join(",", exclusions));
    // Install tomcat war builder
    TomcatWebAppBuilder tomcatWebAppBuilder = (TomcatWebAppBuilder) SystemInstance.get().getComponent(WebAppBuilder.class);
    if (tomcatWebAppBuilder == null) {
        tomcatWebAppBuilder = new TomcatWebAppBuilder();
        tomcatWebAppBuilder.start();
        SystemInstance.get().setComponent(WebAppBuilder.class, tomcatWebAppBuilder);
    }
    SystemInstance.get().setComponent(ParentClassLoaderFinder.class, tomcatWebAppBuilder);
    // set webapp deployer reusing tomcat deployer instead of our custom deployer for war
    SystemInstance.get().setComponent(WebAppDeployer.class, new TomcatWebappDeployer());
    // for compatibility purpose, no more used normally by our trunk
    SystemInstance.get().setComponent(WebDeploymentListeners.class, new WebDeploymentListeners());
    // tomee webapp enricher
    final TomEEClassLoaderEnricher classLoaderEnricher = new TomEEClassLoaderEnricher();
    SystemInstance.get().setComponent(WebAppEnricher.class, classLoaderEnricher);
    // add common lib even in ear "lib" part (if the ear provides myfaces for instance)
    final ClassLoaderEnricher enricher = SystemInstance.get().getComponent(ClassLoaderEnricher.class);
    if (null != enricher) {
        for (final URL url : classLoaderEnricher.enrichment(null)) {
            // we rely on the fact we know what the impl does with null but that's fine
            enricher.addUrl(url);
        }
    }
    // optional services
    if (optionalService(properties, "org.apache.tomee.webservices.TomeeJaxRsService")) {
        // in embedded mode we use regex, in tomcat we use tomcat servlet mapping
        SystemInstance.get().setProperty("openejb.rest.wildcard", "*");
    }
    optionalService(properties, "org.apache.tomee.webservices.TomeeJaxWsService");
    // Start OpenEJB
    ejbServer = new EjbServer();
    SystemInstance.get().setComponent(EjbServer.class, ejbServer);
    OpenEJB.init(properties, new ServerFederation());
    TomcatJndiBuilder.importOpenEJBResourcesInTomcat(SystemInstance.get().getComponent(OpenEjbConfiguration.class).facilities.resources, TomcatHelper.getServer());
    final Properties ejbServerProps = new Properties();
    ejbServerProps.putAll(properties);
    for (final String prop : new String[] { "serializer", "gzip" }) {
        // ensure -Dejbd.xxx are read
        final String value = SystemInstance.get().getProperty("ejbd." + prop);
        if (value != null) {
            ejbServerProps.put(prop, value);
        }
    }
    ejbServerProps.setProperty("openejb.ejbd.uri", "http://127.0.0.1:8080/tomee/ejb");
    ejbServer.init(ejbServerProps);
    // Add our naming context listener to the server which registers all Tomcat resources with OpenEJB
    final StandardServer standardServer = TomcatHelper.getServer();
    final OpenEJBNamingContextListener namingContextListener = new OpenEJBNamingContextListener(standardServer);
    // Standard server has no state property, so we check global naming context to determine if server is started yet
    if (standardServer.getGlobalNamingContext() != null) {
        namingContextListener.start();
    }
    standardServer.addLifecycleListener(namingContextListener);
    // Process all applications already started.  This deploys EJBs, PersistenceUnits
    // and modifies JNDI ENC references to OpenEJB managed objects such as EJBs.
    processRunningApplications(tomcatWebAppBuilder, standardServer);
    final ClassLoader cl = TomcatLoader.class.getClassLoader();
    if (SystemInstance.get().getOptions().get("openejb.servicemanager.enabled", true)) {
        final String clazz = SystemInstance.get().getOptions().get("openejb.service.manager.class", (String) null);
        try {
            manager = clazz == null ? new TomEEServiceManager() : (ServiceManager) cl.loadClass(clazz).newInstance();
        } catch (final ClassNotFoundException cnfe) {
            logger.severe("can't find the service manager " + clazz + ", the TomEE one will be used");
            manager = new TomEEServiceManager();
        }
        manager.init();
        manager.start(false);
    } else {
        // WS
        try {
            final ServerService cxfService = (ServerService) cl.loadClass("org.apache.openejb.server.cxf.CxfService").newInstance();
            cxfService.init(properties);
            cxfService.start();
            services.add(cxfService);
        } catch (final ClassNotFoundException ignored) {
        // no-op
        } catch (final Exception e) {
            logger.log(Level.SEVERE, "Webservices failed to start", e);
        }
        // REST
        try {
            final ServerService restService = (ServerService) cl.loadClass("org.apache.openejb.server.cxf.rs.CxfRSService").newInstance();
            restService.init(properties);
            restService.start();
            services.add(restService);
        } catch (final ClassNotFoundException ignored) {
        // no-op
        } catch (final Exception e) {
            logger.log(Level.SEVERE, "REST failed to start", e);
        }
    }
    if (SystemInstance.get().getOptions().get(TOMEE_NOSHUTDOWNHOOK_PROP, (String) null) != null) {
        final Field daemonField = Bootstrap.class.getDeclaredField("daemon");
        final boolean acc = daemonField.isAccessible();
        try {
            daemonField.setAccessible(true);
            final Bootstrap daemon = (Bootstrap) daemonField.get(null);
            if (daemon != null) {
                final Field catalinaField = Bootstrap.class.getDeclaredField("catalinaDaemon");
                final boolean catalinaAcc = catalinaField.isAccessible();
                catalinaField.setAccessible(true);
                try {
                    Catalina.class.getMethod("setUseShutdownHook", boolean.class).invoke(catalinaField.get(daemon), false);
                } finally {
                    catalinaField.setAccessible(catalinaAcc);
                }
            }
        } finally {
            daemonField.setAccessible(acc);
        }
    }
}
Also used : Tomee(org.apache.openejb.config.sys.Tomee) Installer(org.apache.tomee.installer.Installer) Properties(java.util.Properties) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) URL(java.net.URL) OpenEjbConfiguration(org.apache.openejb.assembler.classic.OpenEjbConfiguration) Field(java.lang.reflect.Field) StandardServer(org.apache.catalina.core.StandardServer) ServiceManager(org.apache.openejb.server.ServiceManager) ServerService(org.apache.openejb.server.ServerService) EjbServer(org.apache.openejb.server.ejbd.EjbServer) Bootstrap(org.apache.catalina.startup.Bootstrap) Paths(org.apache.tomee.installer.Paths) ServerFederation(org.apache.openejb.core.ServerFederation) ClassLoaderEnricher(org.apache.openejb.component.ClassLoaderEnricher) ServiceException(org.apache.openejb.server.ServiceException) TomcatWebappDeployer(org.apache.tomee.catalina.deployment.TomcatWebappDeployer) File(java.io.File) Catalina(org.apache.catalina.startup.Catalina)

Example 2 with ServerFederation

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

the class Server method init.

// TODO: Remove it once init() suits our (initialisation) needs 
@Override
public void init(final Properties props) throws Exception {
    this.props = props;
    final SystemInstance system = SystemInstance.get();
    final File home = system.getHome().getDirectory();
    system.setProperty(DeploymentFilterable.CLASSPATH_INCLUDE, system.getProperty(DeploymentFilterable.CLASSPATH_INCLUDE, ".*/" + home.getName() + "/lib/.*"));
    system.setProperty(DeploymentFilterable.CLASSPATH_REQUIRE_DESCRIPTOR, system.getProperty(DeploymentFilterable.CLASSPATH_REQUIRE_DESCRIPTOR, "true"));
    system.setProperty(DeploymentFilterable.CLASSPATH_FILTER_SYSTEMAPPS, system.getProperty(DeploymentFilterable.CLASSPATH_FILTER_SYSTEMAPPS, "false"));
    OpenEJB.init(props, new ServerFederation());
    if (SystemInstance.get().getOptions().get("openejb.nobanner", (String) null) == null) {
        System.out.println("[init] OpenEJB Remote Server");
    }
    if (manager == null) {
        manager = ServiceManager.getManager();
    }
    manager.init();
}
Also used : ServerFederation(org.apache.openejb.core.ServerFederation) SystemInstance(org.apache.openejb.loader.SystemInstance) File(java.io.File)

Example 3 with ServerFederation

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

the class DynamicConnectionStrategyTest method test.

public void test() throws Exception {
    ConnectionManager.registerStrategy("test", new TestConnectionStrategy());
    EjbServer ejbServer = new EjbServer();
    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());
    ServicePool pool = new ServicePool(ejbServer, 10);
    ServiceDaemon serviceDaemon = new ServiceDaemon(pool, 0, "localhost");
    serviceDaemon.start();
    int port = serviceDaemon.getPort();
    Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    ConfigurationFactory config = new ConfigurationFactory();
    EjbModule ejbModule = new EjbModule(new EjbJar(), new OpenejbJar());
    EjbJar ejbJar = ejbModule.getEjbJar();
    OpenejbJar openejbJar = ejbModule.getOpenejbJar();
    StatelessBean statelessBean = ejbJar.addEnterpriseBean(new StatelessBean(WidgetBean.class));
    EjbDeployment deployment = openejbJar.addEjbDeployment(statelessBean);
    deployment.getProperties().put("openejb.client.connection.strategy", "test");
    assembler.createApplication(config.configureApplication(ejbModule));
    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);
    Context context = new InitialContext(props);
    Widget remote = (Widget) context.lookup("WidgetBeanRemote");
    assertFalse(TestConnectionStrategy.called.get());
    remote.echo("foo");
    assertTrue(TestConnectionStrategy.called.get());
    serviceDaemon.stop();
    OpenEJB.destroy();
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ServerFederation(org.apache.openejb.core.ServerFederation) ServicePool(org.apache.openejb.server.ServicePool) EjbModule(org.apache.openejb.config.EjbModule) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar)

Example 4 with ServerFederation

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

the class FailoverConnectionFactoryTest method test.

public void test() throws Exception {
    ConnectionManager.registerStrategy("test", new TestConnectionStrategy());
    EjbServer ejbServer = new EjbServer();
    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());
    ServicePool pool = new ServicePool(ejbServer, 10);
    ServiceDaemon serviceDaemon = new ServiceDaemon(pool, 0, "localhost");
    serviceDaemon.start();
    int port = serviceDaemon.getPort();
    Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
    ConfigurationFactory config = new ConfigurationFactory();
    EjbModule ejbModule = new EjbModule(new EjbJar(), new OpenejbJar());
    EjbJar ejbJar = ejbModule.getEjbJar();
    OpenejbJar openejbJar = ejbModule.getOpenejbJar();
    StatelessBean statelessBean = ejbJar.addEnterpriseBean(new StatelessBean(WidgetBean.class));
    EjbDeployment deployment = openejbJar.addEjbDeployment(statelessBean);
    deployment.getProperties().put("openejb.client.connection.strategy", "test");
    assembler.createApplication(config.configureApplication(ejbModule));
    Properties props = new Properties();
    props.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
    props.put("java.naming.provider.url", "failover:sticky:ejbd://agwdt:9999,ejbd://127.0.0.1:" + port);
    Context context = new InitialContext(props);
    Widget remote = (Widget) context.lookup("WidgetBeanRemote");
    assertFalse(TestConnectionStrategy.called.get());
    remote.echo("foo");
    assertTrue(TestConnectionStrategy.called.get());
    serviceDaemon.stop();
    OpenEJB.destroy();
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) ServerFederation(org.apache.openejb.core.ServerFederation) ServicePool(org.apache.openejb.server.ServicePool) EjbModule(org.apache.openejb.config.EjbModule) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar)

Example 5 with ServerFederation

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

the class KeepAilveTest method test.

public void test() throws Exception {
    final EjbServer ejbServer = new EjbServer();
    final KeepAliveServer keepAliveServer = new KeepAliveServer(ejbServer, false);
    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(keepAliveServer, 10, 5000, true);
    final ServiceDaemon serviceDaemon = new ServiceDaemon(pool, 0, "localhost");
    serviceDaemon.start();
    try {
        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(EchoBean.class));
        assembler.createApplication(config.configureApplication(ejbJar));
        // good creds
        final int threads = 1;
        final CountDownLatch latch = new CountDownLatch(threads);
        final Collection<Thread> th = new ArrayList<>(threads);
        for (int i = 0; i < threads; i++) {
            final Client client = new Client(latch, i, port);
            th.add(thread(client, false));
        }
        final boolean await = latch.await(60, TimeUnit.SECONDS);
        assertTrue(await);
        for (final Thread t : th) {
            t.join(1000);
        }
    } finally {
        serviceDaemon.stop();
        OpenEJB.destroy();
    }
}
Also used : ServerFederation(org.apache.openejb.core.ServerFederation) ServicePool(org.apache.openejb.server.ServicePool) ArrayList(java.util.ArrayList) Properties(java.util.Properties) CountDownLatch(java.util.concurrent.CountDownLatch) StatelessBean(org.apache.openejb.jee.StatelessBean) ServiceDaemon(org.apache.openejb.server.ServiceDaemon) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar)

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