Author: dblevins
Date: Sun Sep 11 00:37:58 2011
New Revision: 1167658

URL: http://svn.apache.org/viewvc?rev=1167658&view=rev
Log:
Abstracting out basic TomEE Embedded bits

Added:
    
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Configuration.java
    
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Container.java
Modified:
    
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEConfiguration.java
    
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEContainer.java

Added: 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Configuration.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Configuration.java?rev=1167658&view=auto
==============================================================================
--- 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Configuration.java
 (added)
+++ 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Configuration.java
 Sun Sep 11 00:37:58 2011
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.arquillian;
+
+/**
+* @version $Rev$ $Date$
+*/
+public class Configuration {
+
+    private int httpPort = 8080;
+    private int stopPort = 8005;
+    protected String dir;
+
+    public int getHttpPort() {
+        return httpPort;
+    }
+
+    public void setHttpPort(int httpPort) {
+        this.httpPort = httpPort;
+    }
+
+    public int getStopPort() {
+        return stopPort;
+    }
+
+    public void setStopPort(int stopPort) {
+        this.stopPort = stopPort;
+    }
+
+    public String getDir() {
+        return dir;
+    }
+
+    public void setDir(String dir) {
+        this.dir = dir;
+    }
+}

Added: 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Container.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Container.java?rev=1167658&view=auto
==============================================================================
--- 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Container.java
 (added)
+++ 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/Container.java
 Sun Sep 11 00:37:58 2011
@@ -0,0 +1,228 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.arquillian;
+
+import org.apache.catalina.startup.Bootstrap;
+import org.apache.openejb.NoSuchApplicationException;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.UndeployException;
+import org.apache.openejb.assembler.classic.AppInfo;
+import org.apache.openejb.assembler.classic.Assembler;
+import org.apache.openejb.config.ConfigurationFactory;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.tomcat.catalina.TomcatLoader;
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.runtime.log.Log4JLogChute;
+import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
+
+import javax.naming.NamingException;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Container {
+    private Bootstrap bootstrap;
+    protected Configuration configuration;
+    private File catalinaDirectory;
+    private Map<String, String> moduleIds = new HashMap<String, String>();
+    private ConfigurationFactory configurationFactory;
+    private Assembler assembler;
+
+    protected void setup(Configuration configuration) {
+        this.configuration = configuration;
+    }
+
+    protected void startInternal() throws Exception {
+        catalinaDirectory = new File(configuration.getDir());
+        if (catalinaDirectory.exists()) {
+            catalinaDirectory.delete();
+        }
+
+        catalinaDirectory.mkdirs();
+        catalinaDirectory.deleteOnExit();
+
+        createTomcatDirectories(catalinaDirectory);
+        copyConfigs(catalinaDirectory);
+
+        // Bootstrap Tomcat
+        System.out.println("Starting TomEE from: " + 
catalinaDirectory.getAbsolutePath());
+
+        String catalinaBase = catalinaDirectory.getAbsolutePath();
+        System.setProperty("openejb.deployments.classpath", "false");
+        System.setProperty("catalina.home", catalinaBase);
+        System.setProperty("catalina.base", catalinaBase);
+        System.setProperty("openejb.home", catalinaBase);
+        System.setProperty("openejb.base", catalinaBase);
+        System.setProperty("openejb.servicemanager.enabled", "false");
+
+        bootstrap = new Bootstrap();
+        bootstrap.start();
+
+        // Bootstrap OpenEJB
+        Properties properties = new Properties();
+        properties.setProperty("openejb.deployments.classpath", "false");
+        properties.setProperty("openejb.loader", "tomcat-system");
+        properties.setProperty("openejb.home", catalinaBase);
+        properties.setProperty("openejb.base", catalinaBase);
+        properties.setProperty("openejb.servicemanager.enabled", "false");
+
+        try {
+            Properties tomcatServerInfo = new Properties();
+            ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
+            
tomcatServerInfo.load(classLoader.getResourceAsStream("org/apache/catalina/util/ServerInfo.properties"));
+
+            String serverNumber = 
tomcatServerInfo.getProperty("server.number");
+            if (serverNumber == null) {
+                // Tomcat5 only has server.info
+                String serverInfo = 
tomcatServerInfo.getProperty("server.info");
+                if (serverInfo != null) {
+                    int slash = serverInfo.indexOf('/');
+                    serverNumber = serverInfo.substring(slash + 1);
+                }
+            }
+            if (serverNumber != null) {
+                System.setProperty("tomcat.version", serverNumber);
+            }
+
+            String serverBuilt = tomcatServerInfo.getProperty("server.built");
+            if (serverBuilt != null) {
+                System.setProperty("tomcat.built", serverBuilt);
+            }
+        } catch (Throwable e) {
+        }
+
+        new TomcatLoader().init(properties);
+
+        assembler = SystemInstance.get().getComponent(Assembler.class);
+        configurationFactory = new ConfigurationFactory();
+    }
+
+    protected void stopInternal() throws Exception {
+        bootstrap.stopServer();
+        deleteTree(catalinaDirectory);
+    }
+
+    protected void deploy(String name, File file) throws OpenEJBException, 
IOException, NamingException {
+        AppInfo appInfo = configurationFactory.configureApplication(file);
+        assembler.createApplication(appInfo);
+        moduleIds.put(name, appInfo.path);
+    }
+
+    protected void undeploy(String name) throws UndeployException, 
NoSuchApplicationException {
+        String moduleId = moduleIds.get(name);
+        assembler.destroyApplication(moduleId);
+    }
+
+    private void deleteTree(File file) {
+        if (file == null)
+            return;
+        if (!file.exists())
+            return;
+
+        if (file.isFile()) {
+            file.delete();
+            return;
+        }
+
+        if (file.isDirectory()) {
+            if (".".equals(file.getName()))
+                return;
+            if ("..".equals(file.getName()))
+                return;
+
+            File[] children = file.listFiles();
+
+            for (File child : children) {
+                deleteTree(child);
+            }
+
+            file.delete();
+        }
+    }
+
+    private void copyConfigs(File directory) throws Exception {
+        File confDir = new File(directory, "conf");
+        copyFileTo(confDir, "catalina.policy");
+        copyTemplateTo(confDir, "catalina.properties");
+        copyFileTo(confDir, "context.xml");
+        copyFileTo(confDir, "logging.properties");
+        copyFileTo(confDir, "openejb.xml");
+        copyFileTo(confDir, "server.xml");
+        copyFileTo(confDir, "tomcat-users.xml");
+        copyFileTo(confDir, "web.xml");
+    }
+
+    private void copyTemplateTo(File targetDir, String filename) throws 
Exception {
+        Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new 
Log4JLogChute());
+        Velocity.setProperty(Velocity.RESOURCE_LOADER, "class");
+        Velocity.setProperty("class.resource.loader.description", "Velocity 
Classpath Resource Loader");
+        Velocity.setProperty("class.resource.loader.class", 
ClasspathResourceLoader.class.getName());
+        Velocity.init();
+        Template template = 
Velocity.getTemplate("/org/apache/openejb/tomee/configs/" + filename);
+        VelocityContext context = new VelocityContext();
+        context.put("tomcatHttpPort", 
Integer.toString(configuration.getHttpPort()));
+        context.put("tomcatShutdownPort", 
Integer.toString(configuration.getStopPort()));
+        Writer writer = new FileWriter(new File(targetDir, filename));
+        template.merge(context, writer);
+        writer.flush();
+        writer.close();
+    }
+
+    private void copyFileTo(File targetDir, String filename) throws 
IOException {
+        InputStream is = 
getClass().getResourceAsStream("/org/apache/openejb/tomee/configs/" + filename);
+        FileOutputStream os = new FileOutputStream(new File(targetDir, 
filename));
+
+        copyStream(is, os);
+    }
+
+    private void copyStream(InputStream is, FileOutputStream os) throws 
IOException {
+        byte[] buffer = new byte[8192];
+        int bytesRead = -1;
+
+        while ((bytesRead = is.read(buffer)) > -1) {
+            os.write(buffer, 0, bytesRead);
+        }
+
+        is.close();
+        os.close();
+    }
+
+    private void createTomcatDirectories(File directory) {
+        createDirectory(directory, "apps");
+        createDirectory(directory, "conf");
+        createDirectory(directory, "lib");
+        createDirectory(directory, "logs");
+        createDirectory(directory, "webapps");
+        createDirectory(directory, "temp");
+        createDirectory(directory, "work");
+    }
+
+    private void createDirectory(File parent, String directory) {
+        new File(parent, directory).mkdirs();
+    }
+}

Modified: 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEConfiguration.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEConfiguration.java?rev=1167658&r1=1167657&r2=1167658&view=diff
==============================================================================
--- 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEConfiguration.java
 (original)
+++ 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEConfiguration.java
 Sun Sep 11 00:37:58 2011
@@ -22,35 +22,7 @@ import org.jboss.arquillian.spi.client.c
 import java.io.File;
 import java.io.IOException;
 
-public class TomEEConfiguration implements ContainerConfiguration {
-
-    private int httpPort = 8080;
-    private int stopPort = 8005;
-    private String dir;
-
-    public int getHttpPort() {
-        return httpPort;
-    }
-
-    public void setHttpPort(int httpPort) {
-        this.httpPort = httpPort;
-    }
-
-    public int getStopPort() {
-        return stopPort;
-    }
-
-    public void setStopPort(int stopPort) {
-        this.stopPort = stopPort;
-    }
-
-    public String getDir() {
-        return dir;
-    }
-
-    public void setDir(String dir) {
-        this.dir = dir;
-    }
+public class TomEEConfiguration extends Configuration implements 
ContainerConfiguration {
 
     public void validate() throws ConfigurationException {
         try {

Modified: 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEContainer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEContainer.java?rev=1167658&r1=1167657&r2=1167658&view=diff
==============================================================================
--- 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEContainer.java
 (original)
+++ 
openejb/trunk/sandbox/arquillian-tomee/arquillian-tomee-embedded/src/main/java/org/apache/openejb/arquillian/TomEEContainer.java
 Sun Sep 11 00:37:58 2011
@@ -28,6 +28,9 @@ import java.util.Properties;
 
 import org.apache.catalina.startup.Bootstrap;
 import org.apache.openejb.AppContext;
+import org.apache.openejb.NoSuchApplicationException;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.UndeployException;
 import org.apache.openejb.assembler.Deployer;
 import org.apache.openejb.assembler.classic.AppInfo;
 import org.apache.openejb.assembler.classic.Assembler;
@@ -49,88 +52,21 @@ import org.jboss.shrinkwrap.api.Archive;
 import org.jboss.shrinkwrap.api.exporter.ZipExporter;
 import org.jboss.shrinkwrap.descriptor.api.Descriptor;
 
-public class TomEEContainer implements DeployableContainer<TomEEConfiguration> 
{
+import javax.naming.NamingException;
 
-    private Bootstrap bootstrap;
-    private TomEEConfiguration configuration;
-    private File catalinaDirectory;
-    private Map<String, String> moduleIds = new HashMap<String, String>();
-    private Deployer deployer;
-    private ConfigurationFactory configurationFactory;
-    private Assembler assembler;
+public class TomEEContainer extends Container implements 
DeployableContainer<TomEEConfiguration> {
 
     public Class<TomEEConfiguration> getConfigurationClass() {
         return TomEEConfiguration.class;
     }
 
     public void setup(TomEEConfiguration configuration) {
-        this.configuration = configuration;
+        setup((Configuration)configuration);
     }
 
     public void start() throws LifecycleException {
         try {
-            catalinaDirectory = new File(configuration.getDir());
-            if (catalinaDirectory.exists()) {
-                catalinaDirectory.delete();
-            }
-
-            catalinaDirectory.mkdirs();
-            catalinaDirectory.deleteOnExit();
-
-            createTomcatDirectories(catalinaDirectory);
-            copyConfigs(catalinaDirectory);
-
-            // Bootstrap Tomcat
-            System.out.println("Starting TomEE from: " + 
catalinaDirectory.getAbsolutePath());
-
-            String catalinaBase = catalinaDirectory.getAbsolutePath();
-            System.setProperty("openejb.deployments.classpath", "false");
-            System.setProperty("catalina.home", catalinaBase);
-            System.setProperty("catalina.base", catalinaBase);
-            System.setProperty("openejb.home", catalinaBase);
-            System.setProperty("openejb.base", catalinaBase);
-            System.setProperty("openejb.servicemanager.enabled", "false");
-            
-            bootstrap = new Bootstrap();
-            bootstrap.start();
-            
-            // Bootstrap OpenEJB
-            Properties properties = new Properties();
-            properties.setProperty("openejb.deployments.classpath", "false");
-            properties.setProperty("openejb.loader", "tomcat-system");
-            properties.setProperty("openejb.home", catalinaBase);
-            properties.setProperty("openejb.base", catalinaBase);
-            properties.setProperty("openejb.servicemanager.enabled", "false");
-            
-            try {
-                Properties tomcatServerInfo = new Properties();
-                ClassLoader classLoader = 
Thread.currentThread().getContextClassLoader();
-                
tomcatServerInfo.load(classLoader.getResourceAsStream("org/apache/catalina/util/ServerInfo.properties"));
-
-                String serverNumber = 
tomcatServerInfo.getProperty("server.number");
-                if (serverNumber == null) {
-                    // Tomcat5 only has server.info
-                    String serverInfo = 
tomcatServerInfo.getProperty("server.info");
-                    if (serverInfo != null) {
-                        int slash = serverInfo.indexOf('/');
-                        serverNumber = serverInfo.substring(slash + 1);
-                    }
-                }
-                if (serverNumber != null) {
-                    System.setProperty("tomcat.version", serverNumber);
-                }
-
-                String serverBuilt = 
tomcatServerInfo.getProperty("server.built");
-                if (serverBuilt != null) {
-                    System.setProperty("tomcat.built", serverBuilt);
-                }
-            } catch (Throwable e) {
-            }
-
-                       new TomcatLoader().init(properties);
-
-            assembler = SystemInstance.get().getComponent(Assembler.class);
-            configurationFactory = new ConfigurationFactory();
+            startInternal();
 
         } catch (Exception e) {
             e.printStackTrace();
@@ -140,8 +76,7 @@ public class TomEEContainer implements D
 
     public void stop() throws LifecycleException {
         try {
-            bootstrap.stopServer();
-            deleteTree(catalinaDirectory);
+            stopInternal();
         } catch (Exception e) {
             throw new LifecycleException("Unable to stop server", e);
         }
@@ -155,13 +90,12 @@ public class TomEEContainer implements D
        try {
 
             final File tempDir = FileUtils.createTempDir();
-            final File file = new File(tempDir, archive.getName());
+            final String name = archive.getName();
+            final File file = new File(tempDir, name);
                archive.as(ZipExporter.class).exportTo(file, true);
 
 
-            AppInfo appInfo = configurationFactory.configureApplication(file);
-            assembler.createApplication(appInfo);
-            moduleIds.put(archive.getName(), appInfo.path);
+            deploy(name, file);
 
             HTTPContext httpContext = new HTTPContext("0.0.0.0", 
configuration.getHttpPort());
             return new ProtocolMetaData().addContext(httpContext);
@@ -173,41 +107,14 @@ public class TomEEContainer implements D
 
     public void undeploy(Archive<?> archive) throws DeploymentException {
        try {
-            String moduleId = moduleIds.get(archive.getName());
-            assembler.destroyApplication(moduleId);
+            final String name = archive.getName();
+            undeploy(name);
         } catch (Exception e) {
             e.printStackTrace();
             throw new DeploymentException("Unable to undeploy", e);
         }
     }
 
-    private void deleteTree(File file) {
-        if (file == null)
-            return;
-        if (!file.exists())
-            return;
-
-        if (file.isFile()) {
-            file.delete();
-            return;
-        }
-
-        if (file.isDirectory()) {
-            if (".".equals(file.getName()))
-                return;
-            if ("..".equals(file.getName()))
-                return;
-
-            File[] children = file.listFiles();
-
-            for (File child : children) {
-                deleteTree(child);
-            }
-
-            file.delete();
-        }
-    }
-
     public void deploy(Descriptor descriptor) throws DeploymentException {
         throw new UnsupportedOperationException("Not implemented");
     }
@@ -216,64 +123,4 @@ public class TomEEContainer implements D
         throw new UnsupportedOperationException("Not implemented");
     }
 
-    private void copyConfigs(File directory) throws Exception {
-        File confDir = new File(directory, "conf");
-        copyFileTo(confDir, "catalina.policy");
-        copyTemplateTo(confDir, "catalina.properties");
-        copyFileTo(confDir, "context.xml");
-        copyFileTo(confDir, "logging.properties");
-        copyFileTo(confDir, "openejb.xml");
-        copyFileTo(confDir, "server.xml");
-        copyFileTo(confDir, "tomcat-users.xml");
-        copyFileTo(confDir, "web.xml");
-    }
-
-    private void copyTemplateTo(File targetDir, String filename) throws 
Exception {
-        Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new 
Log4JLogChute());
-        Velocity.setProperty(Velocity.RESOURCE_LOADER, "class");
-        Velocity.setProperty("class.resource.loader.description", "Velocity 
Classpath Resource Loader");
-        Velocity.setProperty("class.resource.loader.class", 
ClasspathResourceLoader.class.getName());
-        Velocity.init();
-        Template template = 
Velocity.getTemplate("/org/apache/openejb/tomee/configs/" + filename);
-        VelocityContext context = new VelocityContext();
-        context.put("tomcatHttpPort", 
Integer.toString(configuration.getHttpPort()));
-        context.put("tomcatShutdownPort", 
Integer.toString(configuration.getStopPort()));
-        Writer writer = new FileWriter(new File(targetDir, filename));
-        template.merge(context, writer);
-        writer.flush();
-        writer.close();
-    }
-
-    private void copyFileTo(File targetDir, String filename) throws 
IOException {
-        InputStream is = 
getClass().getResourceAsStream("/org/apache/openejb/tomee/configs/" + filename);
-        FileOutputStream os = new FileOutputStream(new File(targetDir, 
filename));
-
-        copyStream(is, os);
-    }
-
-    private void copyStream(InputStream is, FileOutputStream os) throws 
IOException {
-        byte[] buffer = new byte[8192];
-        int bytesRead = -1;
-
-        while ((bytesRead = is.read(buffer)) > -1) {
-            os.write(buffer, 0, bytesRead);
-        }
-
-        is.close();
-        os.close();
-    }
-
-    private void createTomcatDirectories(File directory) {
-        createDirectory(directory, "apps");
-        createDirectory(directory, "conf");
-        createDirectory(directory, "lib");
-        createDirectory(directory, "logs");
-        createDirectory(directory, "webapps");
-        createDirectory(directory, "temp");
-        createDirectory(directory, "work");
-    }
-
-    private void createDirectory(File parent, String directory) {
-        new File(parent, directory).mkdirs();
-    }
 }


Reply via email to