Search in sources :

Example 6 with LifecycleException

use of org.jboss.arquillian.container.spi.client.container.LifecycleException in project tomee by apache.

the class TomEEWebappContainer method start.

@Override
public void start() throws LifecycleException {
    // see if TomEE is already running by checking the http port
    final int httpPort = configuration.getHttpPort();
    if (Setup.isRunning(configuration.getHost(), httpPort)) {
        logger.info(String.format("Tomcat found running on port %s", httpPort));
        return;
    }
    shutdown = true;
    final String s = File.separator;
    try {
        final File workingDirectory = new File(configuration.getDir());
        if (workingDirectory.exists()) {
            Files.assertDir(workingDirectory);
        } else {
            Files.mkdir(workingDirectory);
            Files.deleteOnExit(workingDirectory);
        }
        Files.readable(workingDirectory);
        Files.writable(workingDirectory);
        openejbHome = Setup.findHome(workingDirectory);
        Installer installer = null;
        if (openejbHome == null) {
            downloadTomcat(workingDirectory, configuration.getTomcatVersion(), configuration.getDir());
            openejbHome = Setup.findHome(workingDirectory);
            Files.deleteOnExit(openejbHome);
            final File webapp = new File(openejbHome, "webapps" + s + "tomee");
            Files.mkdir(webapp);
            downloadOpenEJBWebapp(webapp, configuration.getDir());
            System.setProperty("catalina.home", openejbHome.getAbsolutePath());
            System.setProperty("catalina.base", openejbHome.getAbsolutePath());
            System.setProperty("openejb.deploymentId.format", System.getProperty("openejb.deploymentId.format", "{appId}/{ejbJarId}/{ejbName}"));
            final Paths paths = new Paths(webapp);
            installer = new Installer(paths, true);
            if (!configuration.isUseInstallerServlet()) {
                installer.installAll();
            }
            wereOpenejbHomeSet = false;
        }
        Files.assertDir(openejbHome);
        Files.readable(openejbHome);
        Files.writable(openejbHome);
        Setup.configureServerXml(openejbHome, configuration);
        Setup.configureSystemProperties(openejbHome, configuration);
        Setup.exportProperties(openejbHome, configuration, true);
        final URL logging = Thread.currentThread().getContextClassLoader().getResource("default.remote.logging.properties");
        if (logging != null) {
            write(logging, new File(openejbHome, "conf" + s + "logging.properties"));
        }
        if (configuration.isRemoveUnusedWebapps()) {
            Setup.removeUselessWebapps(openejbHome, "tomee");
        }
        if (logger.isLoggable(Level.FINE)) {
            final Map<Object, Object> map = new TreeMap<>(System.getProperties());
            for (final Map.Entry<Object, Object> entry : map.entrySet()) {
                System.out.printf("%s = %s\n", entry.getKey(), entry.getValue());
            }
        }
        Setup.installArquillianBeanDiscoverer(openejbHome);
        if (!wereOpenejbHomeSet && configuration.isUseInstallerServlet()) {
            // instead of calling the Installer, let's just do like users do
            // call the servlet installer instead
            final String baseUrl = "http://" + configuration.getHost() + ":" + httpPort + "/tomee/installer";
            assert installer != null;
            installer.addTomEEAdminConfInTomcatUsers(true);
            final RemoteServer tmpContainer = new RemoteServer();
            tmpContainer.setPortStartup(httpPort);
            try {
                tmpContainer.start();
            } catch (final Exception e) {
                tmpContainer.destroy();
                throw e;
            }
            final URL url = new URL(baseUrl);
            logger.info("Calling TomEE Installer Servlet on " + url);
            for (int i = 0; i < Integer.getInteger("tomee.webapp.container.client.retries", 3); i++) {
                final URLConnection uc = url.openConnection();
                // dG9tZWU6dG9tZWU= --> Base64 of tomee:tomee
                final String authorizationString = "Basic dG9tZWU6dG9tZWU=";
                final int timeout = Integer.getInteger("tomee.webapp.container.client.timeout", 60000);
                uc.setConnectTimeout(timeout);
                uc.setReadTimeout(timeout);
                uc.setRequestProperty("Authorization", authorizationString);
                try {
                    final InputStream is = uc.getInputStream();
                    org.apache.openejb.loader.IO.slurp(is);
                    is.close();
                    break;
                } catch (final Exception e) {
                    logger.warning(e.getMessage());
                    Thread.sleep(1000);
                }
            }
            tmpContainer.stop();
            tmpContainer.getServer().waitFor();
        }
        container = new RemoteServer();
        container.setPortStartup(httpPort);
        container.start(Arrays.asList("-Dopenejb.system.apps=true", "-Dtomee.remote.support=true", "-Dorg.apache.openejb.servlet.filters=" + ArquillianFilterRunner.class.getName() + "=" + ServletMethodExecutor.ARQUILLIAN_SERVLET_MAPPING), "start", true);
        container.killOnExit();
    } catch (final Exception e) {
        if (null != container) {
            container.destroy();
        }
        throw new LifecycleException("Unable to start remote container on port: " + httpPort, e);
    }
}
Also used : ArquillianFilterRunner(org.apache.openejb.arquillian.common.ArquillianFilterRunner) LifecycleException(org.jboss.arquillian.container.spi.client.container.LifecycleException) Installer(org.apache.tomee.installer.Installer) InputStream(java.io.InputStream) TreeMap(java.util.TreeMap) URL(java.net.URL) LifecycleException(org.jboss.arquillian.container.spi.client.container.LifecycleException) IOException(java.io.IOException) URLConnection(java.net.URLConnection) Paths(org.apache.tomee.installer.Paths) File(java.io.File) TreeMap(java.util.TreeMap) Map(java.util.Map) RemoteServer(org.apache.openejb.config.RemoteServer)

Aggregations

LifecycleException (org.jboss.arquillian.container.spi.client.container.LifecycleException)6 IOException (java.io.IOException)4 NamingException (javax.naming.NamingException)3 DeploymentException (org.jboss.arquillian.container.spi.client.container.DeploymentException)3 File (java.io.File)2 RemoteServer (org.apache.openejb.config.RemoteServer)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 InitialContext (javax.naming.InitialContext)1 NoSuchApplicationException (org.apache.openejb.NoSuchApplicationException)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 UndeployException (org.apache.openejb.UndeployException)1 ArquillianFilterRunner (org.apache.openejb.arquillian.common.ArquillianFilterRunner)1 TestClassDiscoverer (org.apache.openejb.arquillian.common.TestClassDiscoverer)1