tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1183405 - in /openejb/trunk/openejb/assembly/tomee/tomee-embedded/src: main/java/org/apache/tomee/embedded/ main/resources/META-INF/services/ test/ test/java/ test/java/org/ test/java/org/apache/ test/java/org/apache/tomee/ test/java/org/a...
Date Fri, 14 Oct 2011 16:08:49 GMT
Author: rmannibucau
Date: Fri Oct 14 16:08:49 2011
New Revision: 1183405

URL: http://svn.apache.org/viewvc?rev=1183405&view=rev
Log:
adding a first draft for TomEE embedded EJBContainer

Added:
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/resources/META-INF/services/
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/resources/META-INF/services/javax.ejb.spi.EJBContainerProvider
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/tomee/
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/EmbeddedTomEEContainerTest.java
Modified:
    openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java

Modified: openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java?rev=1183405&r1=1183404&r2=1183405&view=diff
==============================================================================
--- openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
(original)
+++ openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
Fri Oct 14 16:08:49 2011
@@ -66,6 +66,7 @@ public class Container {
     protected Configuration configuration;
     private File base;
     private Map<String, String> moduleIds = new HashMap<String, String>();
+    private Map<String, AppContext> appContexts = new HashMap<String, AppContext>();
     private ConfigurationFactory configurationFactory;
     private Assembler assembler;
     private final Tomcat tomcat;
@@ -208,13 +209,19 @@ public class Container {
 
     public void deploy(String name, File file) throws OpenEJBException, IOException, NamingException
{
         AppInfo appInfo = configurationFactory.configureApplication(file);
-        assembler.createApplication(appInfo);
+        appContexts.put(name, assembler.createApplication(appInfo));
         moduleIds.put(name, appInfo.path);
     }
 
     public void undeploy(String name) throws UndeployException, NoSuchApplicationException
{
         String moduleId = moduleIds.get(name);
         assembler.destroyApplication(moduleId);
+        moduleIds.remove(name);
+        appContexts.remove(name);
+    }
+
+    public AppContext getAppContexts(String moduleId) {
+        return appContexts.get(moduleId);
     }
 
     private void deleteTree(File file) {

Added: openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java?rev=1183405&view=auto
==============================================================================
--- openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
(added)
+++ openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/EmbeddedTomEEContainer.java
Fri Oct 14 16:08:49 2011
@@ -0,0 +1,99 @@
+package org.apache.tomee.embedded;
+
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.util.Exceptions;
+
+import javax.ejb.EJBException;
+import javax.ejb.embeddable.EJBContainer;
+import javax.ejb.spi.EJBContainerProvider;
+import javax.naming.Context;
+import javax.validation.ValidationException;
+import java.io.File;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+/**
+ * @author rmannibucau
+ */
+public class EmbeddedTomEEContainer extends EJBContainer {
+    private static EmbeddedTomEEContainer tomEEContainer;
+
+    private Container container = new Container();
+    private String appId;
+
+    private EmbeddedTomEEContainer(String id) {
+        appId = id;
+    }
+
+    @Override public void close() {
+        try {
+            if (tomEEContainer.container.getAppContexts(appId) != null) {
+                tomEEContainer.container.undeploy(appId);
+            }
+            tomEEContainer.container.stop();
+        } catch (Exception e) {
+            throw Exceptions.newEJBException(e);
+        }
+        tomEEContainer = null;
+    }
+
+    @Override public Context getContext() {
+        return tomEEContainer.container.getAppContexts(appId).getGlobalJndiContext();
+    }
+
+    public static class EmbeddedTomEEContainerProvider implements EJBContainerProvider {
+        @Override public EJBContainer createEJBContainer(Map<?, ?> properties) {
+            Object provider = properties.get(EJBContainer.PROVIDER);
+            if (provider != null && !provider.equals(EmbeddedTomEEContainer.class)
&& !provider.equals(EmbeddedTomEEContainer.class.getName())) {
+                return null;
+            }
+
+            if (tomEEContainer != null) {
+                return tomEEContainer;
+            }
+
+            final String appId = (String) properties.get(EJBContainer.APP_NAME);
+            final Object modules = properties.get(EJBContainer.MODULES);
+
+            tomEEContainer = new EmbeddedTomEEContainer(appId);
+            try {
+                tomEEContainer.container.start();
+
+                if (modules instanceof File) {
+                    tomEEContainer.container.deploy(appId, ((File) modules));
+                } else if (modules instanceof String) {
+                    tomEEContainer.container.deploy(appId, new File((String) modules));
+                } else {
+                    try {
+                        tomEEContainer.close();
+                    } catch (Exception e) {
+                        // no-op
+                    }
+                    tomEEContainer = null;
+                    throw Exceptions.newNoModulesFoundException();
+                }
+
+                return tomEEContainer;
+            } catch (OpenEJBException e) {
+                throw new EJBException(e);
+            } catch (MalformedURLException e) {
+                throw new EJBException(e);
+            } catch (ValidationException ve) {
+                throw ve;
+            } catch (Exception e) {
+                if (e instanceof EJBException) {
+                    throw (EJBException) e;
+                }
+                throw new RuntimeException("initialization exception", e);
+            } finally {
+                if (tomEEContainer == null) {
+                    try {
+                        tomEEContainer.close();
+                    } catch (Exception e) {
+                        // no-op
+                    }
+                }
+            }
+        }
+    }
+}

Added: openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/resources/META-INF/services/javax.ejb.spi.EJBContainerProvider
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/resources/META-INF/services/javax.ejb.spi.EJBContainerProvider?rev=1183405&view=auto
==============================================================================
--- openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/resources/META-INF/services/javax.ejb.spi.EJBContainerProvider
(added)
+++ openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/main/resources/META-INF/services/javax.ejb.spi.EJBContainerProvider
Fri Oct 14 16:08:49 2011
@@ -0,0 +1 @@
+org.apache.tomee.embedded.EmbeddedTomEEContainer$EmbeddedTomEEContainerProvider

Added: openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/EmbeddedTomEEContainerTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/EmbeddedTomEEContainerTest.java?rev=1183405&view=auto
==============================================================================
--- openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/EmbeddedTomEEContainerTest.java
(added)
+++ openejb/trunk/openejb/assembly/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/EmbeddedTomEEContainerTest.java
Fri Oct 14 16:08:49 2011
@@ -0,0 +1,61 @@
+package org.apache.tomee.embedded;
+
+import org.apache.openejb.OpenEjbContainer;
+import org.apache.openejb.util.IOUtils;
+import org.junit.Test;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.naming.Context;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Properties;
+import java.util.jar.JarFile;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+/**
+ * @author rmannibucau
+ */
+public class EmbeddedTomEEContainerTest {
+    @Test(expected = OpenEjbContainer.NoModulesFoundException.class) public void noModule()
{
+        Properties p = new Properties();
+        p.setProperty(EJBContainer.APP_NAME, "test");
+        p.setProperty(EJBContainer.PROVIDER, EmbeddedTomEEContainer.class.getName());
+        EJBContainer.createEJBContainer(p);
+    }
+
+    @Test public void containerTest() throws Exception {
+        Properties p = new Properties();
+        p.setProperty(EJBContainer.APP_NAME, "test");
+        p.setProperty(EJBContainer.PROVIDER, EmbeddedTomEEContainer.class.getName());
+        p.setProperty(EJBContainer.MODULES, createWar());
+        try {
+            EJBContainer container = EJBContainer.createEJBContainer(p);
+            assertNotNull(container);
+            assertNotNull(container.getContext());
+            container.close();
+        } finally {
+            new File(p.getProperty(EJBContainer.MODULES)).delete();
+        }
+    }
+
+    private String createWar() throws IOException {
+        File file = new File(System.getProperty("java.io.tmpdir") + "/tomee-" + Math.random());
+        file.mkdirs();
+        write("ok=true", new File(file, "index.html"));
+        write("<beans />", new File(file, "META-INF/beans.xml"));
+        return file.getAbsolutePath();
+    }
+
+    private static void write(String content, File file) throws IOException {
+        file.getParentFile().mkdirs();
+        FileWriter index = new FileWriter(file);
+        index.write(content);
+        index.close();
+    }
+}



Mime
View raw message