tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1491347 - in /tomee/tomee/trunk/container/openejb-core/src: main/java/org/apache/openejb/config/ main/java/org/apache/openejb/config/sys/ main/java/org/apache/openejb/testing/ test/java/org/apache/openejb/assembler/classic/ test/java/org/a...
Date Mon, 10 Jun 2013 06:45:05 GMT
Author: rmannibucau
Date: Mon Jun 10 06:45:04 2013
New Revision: 1491347

URL: http://svn.apache.org/r1491347
Log:
TOMEE-966 supporting resources.json too

Added:
    tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourcesJsonTest.java
      - copied, changed from r1491345, tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/OpenEJBXmlByModuleTest.java
    tomee/tomee/trunk/container/openejb-core/src/test/resources/appresource.resources.json
Modified:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JaxbOpenejb.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Openejb.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Resources.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
    tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
Mon Jun 10 06:45:04 2013
@@ -1555,7 +1555,7 @@ public class DeploymentLoader implements
                 if (files != null) {
                     for (final File file : files) {
                         final String name = file.getName();
-                        if (RESOURCES_XML.equals(name)) {
+                        if (RESOURCES_XML.equals(name) || "resources.json".equals(name))
{
                             if (!descriptors.containsKey(name)) {
                                 descriptors.put(name, file.toURI().toURL());
                             } else {

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ReadDescriptors.java
Mon Jun 10 06:45:04 2013
@@ -17,6 +17,7 @@
 package org.apache.openejb.config;
 
 import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.config.sys.JSonConfigReader;
 import org.apache.openejb.config.sys.JaxbOpenejb;
 import org.apache.openejb.config.sys.Resources;
 import org.apache.openejb.core.webservices.WsdlResolver;
@@ -263,13 +264,26 @@ public class ReadDescriptors implements 
     }
 
     public static void readResourcesXml(final Module module) {
-        final Source url = getSource(module.getAltDDs().get("resources.xml"));
-        if (url != null) {
-            try {
-                final Resources openejb = JaxbOpenejb.unmarshal(Resources.class, url.get());
-                module.initResources(openejb);
-            } catch (Exception e) {
-                logger.warning("can't read " + url.toString() + " to load resources for module
" + module.toString(), e);
+        { // xml
+            final Source url = getSource(module.getAltDDs().get("resources.xml"));
+            if (url != null) {
+                try {
+                    final Resources openejb = JaxbOpenejb.unmarshal(Resources.class, url.get());
+                    module.initResources(openejb);
+                } catch (Exception e) {
+                    logger.warning("can't read " + url.toString() + " to load resources for
module " + module.toString(), e);
+                }
+            }
+        }
+        {
+            final Source url = getSource(module.getAltDDs().get("resources.json"));
+            if (url != null) {
+                try {
+                    final Resources openejb = JSonConfigReader.read(Resources.class, url.get());
+                    module.initResources(openejb);
+                } catch (Exception e) {
+                    logger.warning("can't read " + url.toString() + " to load resources for
module " + module.toString(), e);
+                }
             }
         }
     }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java
Mon Jun 10 06:45:04 2013
@@ -20,11 +20,13 @@ import org.apache.openejb.OpenEJBExcepti
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.util.SimpleJSonParser;
 import org.xml.sax.helpers.AttributesImpl;
+import org.xml.sax.helpers.DefaultHandler;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Map;
 import java.util.Properties;
 
@@ -35,21 +37,42 @@ public class JSonConfigReader {
         return Map.class.cast(rawMap);
     }
 
-    public static Openejb read(final InputStream is) throws OpenEJBException {
-        final SaxOpenejb config = new SaxOpenejb();
+    public static <T> T read(final Class<T> clazz, final InputStream is) throws
OpenEJBException {
+        if (Openejb.class.equals(clazz) || Tomee.class.equals(clazz)) {
+            final SaxOpenejb handler = read(is, "openejb",
+                    Arrays.asList("Resource", "Container", "JndiProvider", "TransactionManager",
"ConnectionManager",
+                            "ProxyFactory", "Connector", "Deployments", "Import", "Service",
"SecurityService"),
+                    new SaxOpenejb());
+
+            return clazz.cast(handler.getOpenejb());
+        } else if (Resources.class.equals(clazz)) {
+            final Resources resources = new Resources();
+
+            // reuse openejb parser since we use saw logic and not jaxb one
+            final Openejb openejb = read(is, "openejb",
+                    Arrays.asList("Resource", "Container", "JndiProvider", "Connector", "Import",
"Service"),
+                    new SaxOpenejb()).getOpenejb();
+
+            resources.getContainer().addAll(openejb.getContainer());
+            resources.getResource().addAll(openejb.getResource());
+            resources.getService().addAll(openejb.getServices());
+            resources.getConnector().addAll(openejb.getConnector());
+            resources.getJndiProvider().addAll(openejb.getJndiProvider());
 
+            return clazz.cast(resources);
+        }
+        throw new IllegalArgumentException(clazz.getName() + " not supported");
+    }
 
+    private static <T extends DefaultHandler> T read(final InputStream is, final String
mainRoot, final Collection<String> roots, final T handler) throws OpenEJBException {
         try {
-            config.startDocument();
-            config.startElement(null, "openejb", null, new AttributesImpl());
+            handler.startDocument();
+            handler.startElement(null, mainRoot, null, new AttributesImpl());
 
             final Map<?, ?> jsConfig = map(SimpleJSonParser.read(is));
             jsConfig.remove(COMMENT_KEY);
 
-            for (final String root :
-                    Arrays.asList("Resource", "Container", "JndiProvider", "TransactionManager",
"ConnectionManager",
-                            "ProxyFactory", "Connector", "Deployments", "Import", "Service",
"SecurityService")) {
-
+            for (final String root : roots) {
                 final String currentRoot;
                 if (root.endsWith("s")) {
                     currentRoot = root.toLowerCase();
@@ -67,22 +90,22 @@ public class JSonConfigReader {
                             attributes.addAttribute(null, "id", "id", null, resource.getKey());
                         }
 
-                        if ("resources".equals(currentRoot) && attributes.getIndex("type")
== -1) {
+                        if ("resources".equals(currentRoot) && attributes.getIndex("type")
== -1 && attributes.getIndex("class-name") == -1 && attributes.getIndex("provider")
== -1) {
                             attributes.addAttribute(null, "type", "type", null, "DataSource");
                         }
 
-                        config.startElement(null, root, root, attributes);
+                        handler.startElement(null, root, root, attributes);
 
                         final String propertiesAsStr = toString(map(resource.getValue().get("properties")));
 
-                        config.characters(propertiesAsStr.toCharArray(), 0, propertiesAsStr.length());
+                        handler.characters(propertiesAsStr.toCharArray(), 0, propertiesAsStr.length());
                         // properties
-                        config.endElement(null, root, root);
+                        handler.endElement(null, root, root);
                     }
                 }
             }
 
-            config.endElement(null, "openejb", null);
+            handler.endElement(null, mainRoot, null);
 
             // global config
             if (jsConfig.containsKey("system-properties")) {
@@ -102,7 +125,7 @@ public class JSonConfigReader {
             throw new OpenEJBException(e.getMessage(), e);
         }
 
-        return config.getOpenejb();
+        return handler;
     }
 
     private static void setProperties(final String prefix, final Map<?, ?> sysProps)
{

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JaxbOpenejb.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JaxbOpenejb.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JaxbOpenejb.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JaxbOpenejb.java
Mon Jun 10 06:45:04 2013
@@ -204,7 +204,7 @@ public abstract class JaxbOpenejb {
             }
 
             if (configFile.endsWith(".json")) {
-                return JSonConfigReader.read(in);
+                return JSonConfigReader.read(Openejb.class, in);
             }
 
             return readConfig(new InputSource(in));

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Openejb.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Openejb.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Openejb.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Openejb.java
Mon Jun 10 06:45:04 2013
@@ -230,7 +230,7 @@ public class Openejb {
      * Objects of the following type(s) are allowed in the list
      * {@link Connector }
      */
-    private List<Connector> getConnector() {
+    public List<Connector> getConnector() {
         if (connector == null) {
             connector = new ArrayList<Connector>();
         }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Resources.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Resources.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Resources.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/Resources.java
Mon Jun 10 06:45:04 2013
@@ -45,7 +45,7 @@ import java.util.List;
  * </pre>
  */
 @XmlAccessorType(XmlAccessType.FIELD)
-@XmlType(name = "", propOrder = { "container", "resource", "service", "connector" })
+@XmlType(name = "", propOrder = { "container", "resource", "service", "connector", "jndiProvider"
})
 @XmlRootElement(name = "resources")
 public class Resources {
 
@@ -61,6 +61,9 @@ public class Resources {
     @XmlElement(name = "Connector")
     protected List<Connector> connector;
 
+    @XmlElement(name = "JndiProvider")
+    protected List<JndiProvider> jndiProvider;
+
     public List<Resource> getResource() {
         if (resource == null) {
             resource = new ArrayList<Resource>();
@@ -84,13 +87,20 @@ public class Resources {
         return service;
     }
 
-    private List<Connector> getConnector() {
+    public List<Connector> getConnector() {
         if (connector == null) {
             connector = new ArrayList<Connector>();
         }
         return this.connector;
     }
 
+    public List<JndiProvider> getJndiProvider() {
+        if (jndiProvider == null) {
+            jndiProvider = new ArrayList<JndiProvider>();
+        }
+        return this.jndiProvider;
+    }
+
     public void add(Object service) {
         if (service instanceof Resource) {
             getResource().add((Resource) service);
@@ -98,6 +108,8 @@ public class Resources {
             getService().add((Service) service);
         } else if (service instanceof Connector) {
             getConnector().add((Connector) service);
+        } else if (JndiProvider.class.isInstance(service)) {
+            getJndiProvider().add(JndiProvider.class.cast(service));
         }
     }
 }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java
Mon Jun 10 06:45:04 2013
@@ -302,7 +302,7 @@ public final class ApplicationComposers 
                 final InputStream in = url.openStream();
                 try {
                     if (path.endsWith(".json")) {
-                        openejb = JSonConfigReader.read(in);
+                        openejb = JSonConfigReader.read(Openejb.class, in);
                     } else {
                         openejb = JaxbOpenejb.readConfig(new InputSource(in));
                     }

Copied: tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourcesJsonTest.java
(from r1491345, tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/OpenEJBXmlByModuleTest.java)
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourcesJsonTest.java?p2=tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourcesJsonTest.java&p1=tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/OpenEJBXmlByModuleTest.java&r1=1491345&r2=1491347&rev=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/OpenEJBXmlByModuleTest.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/ResourcesJsonTest.java
Mon Jun 10 06:45:04 2013
@@ -41,10 +41,10 @@ import static junit.framework.Assert.ass
 import static junit.framework.Assert.assertNotNull;
 import static junit.framework.Assert.assertTrue;
 
-public class OpenEJBXmlByModuleTest {
+public class ResourcesJsonTest {
     private Context context = null;
 
-    private UselessBean bean;
+    private ConfiguredThroughJSonBean bean;
 
     @Before
     public void setUp() throws OpenEJBException, NamingException, IOException {
@@ -54,23 +54,23 @@ public class OpenEJBXmlByModuleTest {
         assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
         assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
 
-        AppModule app = new AppModule(OpenEJBXmlByModuleTest.class.getClassLoader(), OpenEJBXmlByModuleTest.class.getSimpleName());
+        AppModule app = new AppModule(ResourcesJsonTest.class.getClassLoader(), ResourcesJsonTest.class.getSimpleName());
 
         EjbJar ejbJar = new EjbJar();
-        ejbJar.addEnterpriseBean(new SingletonBean(UselessBean.class));
+        ejbJar.addEnterpriseBean(new SingletonBean(ConfiguredThroughJSonBean.class));
         app.getEjbModules().add(new EjbModule(ejbJar));
-        app.getEjbModules().iterator().next().getAltDDs().put("resources.xml", getClass().getClassLoader().getResource("META-INF/resource/appresource.openejb.xml"));
+        app.getEjbModules().iterator().next().getAltDDs().put("resources.json", getClass().getClassLoader().getResource("appresource.resources.json"));
 
         assembler.createApplication(config.configureApplication(app));
 
         Properties properties = new Properties();
-        properties.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
+        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
         properties.setProperty("openejb.embedded.initialcontext.close", "destroy");
 
         // some hack to be sure to call destroy()
         context = new InitialContext(properties);
 
-        bean = (UselessBean) context.lookup("UselessBeanLocalBean");
+        bean = (ConfiguredThroughJSonBean) context.lookup("ConfiguredThroughJSonBeanLocalBean");
     }
 
     @After
@@ -99,10 +99,11 @@ public class OpenEJBXmlByModuleTest {
         public String attr = "ok";
     }
 
-    public static class UselessBean {
+    public static class ConfiguredThroughJSonBean {
         @Resource(name = "DS")
         private DataSource ds;
-        @Resource(name = "My Resource", type = MyResource.class)
+
+        @Resource(name = "My Resource")
         private MyResource rs;
 
         public DataSource datasource() {

Modified: tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java?rev=1491347&r1=1491346&r2=1491347&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java
Mon Jun 10 06:45:04 2013
@@ -27,13 +27,11 @@ import org.apache.openejb.resource.jdbc.
 import org.apache.openejb.testing.AppResource;
 import org.apache.openejb.testing.Configuration;
 import org.apache.openejb.testing.Module;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
-
 import java.io.IOException;
 
 import static org.junit.Assert.assertEquals;
@@ -73,7 +71,7 @@ public class JSonConfigTest {
 
     @Test
     public void simpleRead() throws IOException, OpenEJBException {
-        final Openejb openejb = JSonConfigReader.read(Thread.currentThread().getContextClassLoader().getResource(config()).openStream());
+        final Openejb openejb = JSonConfigReader.read(Openejb.class, Thread.currentThread().getContextClassLoader().getResource(config()).openStream());
 
         assertEquals(1, openejb.getResource().size());
         final Resource resource = openejb.getResource().iterator().next();

Added: tomee/tomee/trunk/container/openejb-core/src/test/resources/appresource.resources.json
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/resources/appresource.resources.json?rev=1491347&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/resources/appresource.resources.json
(added)
+++ tomee/tomee/trunk/container/openejb-core/src/test/resources/appresource.resources.json
Mon Jun 10 06:45:04 2013
@@ -0,0 +1,14 @@
+{
+    "resources": {
+        "DS": {
+            "properties": {
+                "JdbcUrl": "not:used:url",
+                "UserName": "foo",
+                "Password": "bar"
+            }
+        },
+        "My Resource": {
+            "class-name": "org.apache.openejb.assembler.classic.ResourcesJsonTest$MyResource"
+        }
+    }
+}



Mime
View raw message