tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1297254 - in /openejb/trunk/openejb: container/openejb-core/src/main/java/org/apache/openejb/assembler/ container/openejb-core/src/main/java/org/apache/openejb/cdi/ container/openejb-core/src/main/java/org/apache/openejb/config/ container/...
Date Mon, 05 Mar 2012 22:22:38 GMT
Author: rmannibucau
Date: Mon Mar  5 22:22:38 2012
New Revision: 1297254

URL: http://svn.apache.org/viewvc?rev=1297254&view=rev
Log:
rewriting a bit the way scan.xml is used (to valid), adding bm variable in scripting language
to be able to use CDI some simple beans

Added:
    openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/Bar.java
    openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/BeanManagerHelperTest.java
Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBValidatorService.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurableClasspathArchive.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/EmptyResourcesClassLoader.java
    openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/CliRunnable.java
    openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/OpenEJBScripter.java
    openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/OpenEJBGroovyShellTest.java

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java?rev=1297254&r1=1297253&r2=1297254&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/DeployerEjb.java
Mon Mar  5 22:22:38 2012
@@ -41,6 +41,7 @@ import javax.ejb.TransactionManagement;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.math.BigInteger;
 import java.security.SecureRandom;
 import java.util.Collection;
@@ -214,6 +215,7 @@ public class DeployerEjb implements Depl
         }
 
         // dump it
+        OutputStream os = null;
         try {
             final AdditionalDeployments additionalDeployments;
             if (config.exists() && config.length() > 0) {
@@ -242,9 +244,12 @@ public class DeployerEjb implements Depl
                     }
                 }
             }
-            JaxbOpenejb.marshal(AdditionalDeployments.class, additionalDeployments, IO.write(config));
+            os = IO.write(config);
+            JaxbOpenejb.marshal(AdditionalDeployments.class, additionalDeployments, os);
         } catch (Exception e) {
             LOGGER.error("can't save the added app, will not be present next time you'll
start", e);
+        } finally {
+            IO.close(os);
         }
     }
 

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBValidatorService.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBValidatorService.java?rev=1297254&r1=1297253&r2=1297254&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBValidatorService.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/cdi/OpenEJBValidatorService.java
Mon Mar  5 22:22:38 2012
@@ -33,7 +33,7 @@ public class OpenEJBValidatorService imp
     @Override
     public ValidatorFactory getDefaultValidatorFactory() {
         try {
-            return (ValidatorFactory)new InitialContext().lookup("java:comp/ValidatorFactory");
+            return (ValidatorFactory) new InitialContext().lookup("java:comp/ValidatorFactory");
         } catch (NamingException e) {
             throw new OpenEJBRuntimeException(e);
         }
@@ -42,7 +42,7 @@ public class OpenEJBValidatorService imp
     @Override
     public Validator getDefaultValidator() {
         try {
-            return (Validator)new InitialContext().lookup("java:comp/Validator");
+            return (Validator) new InitialContext().lookup("java:comp/Validator");
         } catch (NamingException e) {
             throw new OpenEJBRuntimeException(e);
         }

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurableClasspathArchive.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurableClasspathArchive.java?rev=1297254&r1=1297253&r2=1297254&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurableClasspathArchive.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurableClasspathArchive.java
Mon Mar  5 22:22:38 2012
@@ -1,5 +1,6 @@
 package org.apache.openejb.config;
 
+import org.apache.openejb.core.EmptyResourcesClassLoader;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.xbean.finder.archive.Archive;
 import org.apache.xbean.finder.archive.ClassesArchive;
@@ -12,10 +13,10 @@ import org.apache.xbean.finder.filter.Pa
 
 import java.io.IOException;
 import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -59,13 +60,23 @@ public class ConfigurableClasspathArchiv
 
     public static Archive archive(final Module module, final URL location, boolean forceDescriptor)
{
         final ClassLoader loader = module.getClassLoader();
+        final String name = "META-INF/" + name();
         try {
-            final URL scanXml = (URL) module.getAltDDs().get(SystemInstance.get().getProperty(SCAN_XML_PROPERTY,
SCAN_XML_NAME));
-            if (scanXml == null) {
+            URL scanXml = new URLClassLoader(new URL[] { location }, new EmptyResourcesClassLoader()).getResource(name);
+            if (scanXml == null && !forceDescriptor) {
                 return ClasspathArchive.archive(loader, location);
+            } else if (scanXml == null) {
+                return new ClassesArchive();
+            }
+
+            // read descriptors
+            ScanUtil.ScanHandler scan;
+            if (scanXml != null) {
+                scan = ScanUtil.read(scanXml);
+            } else {
+                scan = new ScanUtil.ScanHandler();
             }
 
-            final ScanUtil.ScanHandler scan = ScanUtil.read(scanXml);
             final Archive packageArchive = packageArchive(scan.getPackages(), loader, location);
             final Archive classesArchive = classesArchive(scan.getPackages(), scan.getClasses(),
loader);
 
@@ -83,6 +94,10 @@ public class ConfigurableClasspathArchiv
         }
     }
 
+    private static String name() {
+        return SystemInstance.get().getProperty(SCAN_XML_PROPERTY, SCAN_XML_NAME);
+    }
+
     public static Archive packageArchive(final Set<String> packageNames, final ClassLoader
loader, final URL url) {
         if (!packageNames.isEmpty()) {
             return new FilteredArchive(ClasspathArchive.archive(loader, url), filters(packageNames));
@@ -132,22 +147,24 @@ public class ConfigurableClasspathArchiv
         return false;
     }
 
-    protected static class FakeModule extends Module {
+    public static class FakeModule extends Module {
         public FakeModule(final ClassLoader loader) {
-            this(loader, Collections.EMPTY_MAP);
+            this(loader, null);
         }
 
         public FakeModule(final ClassLoader loader, final Map<String, Object> altDD)
 {
-            this(loader, altDD, System.getProperty(SCAN_XML_PROPERTY, SCAN_XML_NAME));
+            this(loader, altDD, name());
         }
 
         public FakeModule(final ClassLoader loader, final Map<String, Object> altDD,
final String name) {
             super(false);
             setClassLoader(loader);
 
-            URL scanXml = (URL) altDD.get(name);
-            if (scanXml == null) {
-                scanXml = loader.getResource(name);
+            final URL scanXml;
+            if (altDD == null) {
+                scanXml = loader.getResource("META-INF/" + name);
+            } else {
+                scanXml = (URL) altDD.get(name);
             }
             if (scanXml != null) {
                 getAltDDs().put(SCAN_XML_NAME, scanXml);

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/EmptyResourcesClassLoader.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/EmptyResourcesClassLoader.java?rev=1297254&r1=1297253&r2=1297254&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/EmptyResourcesClassLoader.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/core/EmptyResourcesClassLoader.java
Mon Mar  5 22:22:38 2012
@@ -5,6 +5,10 @@ import java.net.URL;
 import java.util.Enumeration;
 
 public class EmptyResourcesClassLoader extends ClassLoader {
+    public EmptyResourcesClassLoader() {
+        super(null);
+    }
+
     @Override
     public Enumeration<URL> getResources(String name) throws IOException {
         return new Enumeration<URL>() {

Modified: openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/CliRunnable.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/CliRunnable.java?rev=1297254&r1=1297253&r2=1297254&view=diff
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/CliRunnable.java
(original)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/CliRunnable.java
Mon Mar  5 22:22:38 2012
@@ -35,6 +35,7 @@ import org.apache.xbean.recipe.Option;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -88,7 +89,7 @@ public class CliRunnable implements Runn
             UrlSet urlSet = new UrlSet(loader);
             urlSet = urlSet.exclude(loader.getParent());
 
-            final IAnnotationFinder finder = new AnnotationFinder(new ConfigurableClasspathArchive(loader,
true, urlSet.getUrls()));
+            final IAnnotationFinder finder = new AnnotationFinder(new ConfigurableClasspathArchive(new
ConfigurableClasspathArchive.FakeModule(loader, Collections.EMPTY_MAP), true, urlSet.getUrls()));
             for (Annotated<Class<?>> cmd : finder.findMetaAnnotatedClasses(Command.class))
{
                 try {
                     final Command annotation = cmd.getAnnotation(Command.class);

Modified: openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/OpenEJBScripter.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/OpenEJBScripter.java?rev=1297254&r1=1297253&r2=1297254&view=diff
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/OpenEJBScripter.java
(original)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/main/java/org/apache/openejb/server/cli/OpenEJBScripter.java
Mon Mar  5 22:22:38 2012
@@ -16,11 +16,15 @@
  */
 package org.apache.openejb.server.cli;
 
+import org.apache.openejb.AppContext;
 import org.apache.openejb.BeanContext;
+import org.apache.openejb.OpenEJBRuntimeException;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.ContainerSystem;
 import org.apache.openejb.util.proxy.ProxyEJB;
 
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.script.Bindings;
 import javax.script.ScriptContext;
 import javax.script.ScriptEngine;
@@ -30,6 +34,7 @@ import javax.script.ScriptException;
 import javax.script.SimpleBindings;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
 public class OpenEJBScripter {
@@ -78,6 +83,8 @@ public class OpenEJBScripter {
 
     private static Bindings binding() {
         final Bindings bindings = new SimpleBindings();
+        bindings.put("bm", new BeanManagerHelper());
+
         final ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
         for (BeanContext beanContext : cs.deployments()) {
             if (BeanContext.Comp.class.equals(beanContext.getBeanClass())) {
@@ -100,4 +107,43 @@ public class OpenEJBScripter {
         }
         return bindings;
     }
+
+    public static class BeanManagerHelper {
+        public Object beanFromClass(final String appName, final String classname) {
+            final AppContext appContext = appContext(appName);
+            final BeanManager bm = appContext.getBeanManager();
+            final Class<?> clazz;
+            try {
+                clazz = appContext.getClassLoader().loadClass(classname);
+            } catch (ClassNotFoundException e) {
+                throw new OpenEJBRuntimeException(e);
+            }
+            final Set<Bean<?>> beans = bm.getBeans(clazz);
+            return instance(bm, beans, clazz);
+        }
+
+        public Object beanFromName(final String appName, final String name) {
+            final BeanManager bm = beanManager(appName);
+            final Set<Bean<?>> beans = bm.getBeans(name);
+            return instance(bm, beans, Object.class);
+        }
+
+        private <T> T instance(final BeanManager bm, final Set<Bean<?>>
beans, final Class<T> clazz) {
+            final Bean<?> bean = bm.resolve(beans);
+            return (T) bm.getReference(bean, clazz, bm.createCreationalContext(bean));
+        }
+
+        private BeanManager beanManager(final String appName) {
+            return appContext(appName).getBeanManager();
+        }
+
+        private AppContext appContext(final String appName) {
+            final ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
+            final AppContext appContext = cs.getAppContext(appName);
+            if (appContext == null) {
+                throw new OpenEJBRuntimeException("can't find application " + appName);
+            }
+            return appContext;
+        }
+    }
 }

Added: openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/Bar.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/Bar.java?rev=1297254&view=auto
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/Bar.java
(added)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/Bar.java
Mon Mar  5 22:22:38 2012
@@ -0,0 +1,10 @@
+package org.apache.openejb.server.groovy;
+
+import javax.inject.Named;
+
+@Named
+public class Bar {
+    public String test() {
+        return "ok";
+    }
+}

Added: openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/BeanManagerHelperTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/BeanManagerHelperTest.java?rev=1297254&view=auto
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/BeanManagerHelperTest.java
(added)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/BeanManagerHelperTest.java
Mon Mar  5 22:22:38 2012
@@ -0,0 +1,52 @@
+package org.apache.openejb.server.groovy;
+
+import org.apache.openejb.jee.Beans;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.junit.Module;
+import org.apache.openejb.server.cli.OpenEJBScripter;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.inject.Named;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(ApplicationComposer.class)
+public class BeanManagerHelperTest {
+    private static OpenEJBScripter.BeanManagerHelper helper = new OpenEJBScripter.BeanManagerHelper();
+
+    @Module
+    public Beans ejbJar() {
+        final Beans beans = new Beans();
+        beans.addManagedClass(Pojo.class);
+        return beans;
+    }
+
+    @Before
+    public void resetID() {
+        Pojo.ID = 5;
+    }
+
+    @Test
+    public void getInstanceFromClass() {
+        for (int i = 1; i < 5; i++) {
+            final Pojo pojo = (Pojo) helper.beanFromClass("BeanManagerHelperTest", Pojo.class.getName());
+            assertEquals(5 + i, pojo.id);
+        }
+    }
+
+    @Test
+    public void getInstanceFromName() {
+        for (int i = 1; i < 5; i++) {
+            final Pojo pojo = (Pojo) helper.beanFromName("BeanManagerHelperTest", "pojo");
+            assertEquals(5 + i, pojo.id);
+        }
+    }
+
+    @Named
+    public static class Pojo {
+        public static int ID;
+        public int id = ++ID;
+    }
+}

Modified: openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/OpenEJBGroovyShellTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/OpenEJBGroovyShellTest.java?rev=1297254&r1=1297253&r2=1297254&view=diff
==============================================================================
--- openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/OpenEJBGroovyShellTest.java
(original)
+++ openejb/trunk/openejb/server/openejb-common-cli/src/test/java/org/apache/openejb/server/groovy/OpenEJBGroovyShellTest.java
Mon Mar  5 22:22:38 2012
@@ -24,6 +24,8 @@ import org.junit.Test;
 import javax.ejb.embeddable.EJBContainer;
 import javax.script.ScriptException;
 
+import java.util.Properties;
+
 import static org.junit.Assert.assertEquals;
 
 public class OpenEJBGroovyShellTest {
@@ -31,7 +33,9 @@ public class OpenEJBGroovyShellTest {
 
     @BeforeClass
     public static void start() {
-        container = EJBContainer.createEJBContainer();
+        final Properties properties = new Properties();
+        properties.setProperty(EJBContainer.APP_NAME, OpenEJBGroovyShellTest.class.getSimpleName());
+        container = EJBContainer.createEJBContainer(properties);
     }
 
     @AfterClass
@@ -45,4 +49,11 @@ public class OpenEJBGroovyShellTest {
         final Object out = shell.evaluate("groovy", "Foo.foo()");
         assertEquals("foo", out);
     }
+
+    @Test
+    public void callUsingCDI() throws ScriptException {
+        final OpenEJBScripter shell = new OpenEJBScripter();
+        final Object out = shell.evaluate("groovy", "bm.beanFromName('OpenEJBGroovyShellTest',
'bar').test()");
+        assertEquals("ok", out);
+    }
 }



Mime
View raw message