tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1490699 - in /tomee/tomee/trunk/container/openejb-core/src: main/java/org/apache/openejb/config/sys/ main/java/org/apache/openejb/testing/ test/java/org/apache/openejb/config/ test/resources/
Date Fri, 07 Jun 2013 16:01:40 GMT
Author: rmannibucau
Date: Fri Jun  7 16:01:40 2013
New Revision: 1490699

URL: http://svn.apache.org/r1490699
Log:
TOMEE-966 starting some poc about a json config

Added:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java
    tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java
    tomee/tomee/trunk/container/openejb-core/src/test/resources/openejb.json
Modified:
    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/SaxOpenejb.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java

Added: 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=1490699&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java
(added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/JSonConfigReader.java
Fri Jun  7 16:01:40 2013
@@ -0,0 +1,132 @@
+/*
+ * 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.config.sys;
+
+import org.apache.openejb.OpenEJBException;
+import org.xml.sax.Attributes;
+import org.xml.sax.helpers.AttributesImpl;
+
+import javax.script.Bindings;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.SimpleBindings;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+public class JSonConfigReader {
+    private static Map map(final Object rawMap) {
+        return Map.class.cast(rawMap);
+    }
+
+    public static Openejb read(final String json) throws OpenEJBException {
+        final Bindings bindings = new SimpleBindings();
+        bindings.put("json", json);
+
+        final SaxOpenejb config = new SaxOpenejb();
+
+        final ScriptEngineManager manager = new ScriptEngineManager();
+        ScriptEngine engine = manager.getEngineByName("rhino");
+        if (engine == null) { // try another engine
+            engine = manager.getEngineByExtension("js");
+        }
+        if (engine == null) {
+            throw new OpenEJBException("rhino not available");
+        }
+
+        try {
+            config.startDocument();
+            config.startElement(null, "openejb", null, new AttributesImpl());
+
+            engine.eval("var openejbConfig = JSON.parse(json)", bindings);
+            final Map<String, Map<String, Map<String, String>>> rawConfiguration
= map(bindings.get("openejbConfig"));
+
+            {
+                final Map<String, Map<String, Map<String, String>>> resources
= map(rawConfiguration.get("resources"));
+                if (resources != null) {
+                    for (final Map.Entry<String, Map<String, Map<String, String>>>
resource : resources.entrySet()) {
+                        final AttributesImpl attributes = toAttributes(map(resource.getValue()),
"properties");
+                        attributes.addAttribute(null, "id", "id", null, resource.getKey());
+                        if (attributes.getIndex("type") == -1) {
+                            attributes.addAttribute(null, "type", "type", null, "DataSource");
+                        }
+                        config.startElement(null, "Resource", "Resource", attributes);
+
+                        final String propertiesAsStr = toString(map(resource.getValue().get("properties")));
+
+                        config.characters(propertiesAsStr.toCharArray(), 0, propertiesAsStr.length());
+                        // properties
+                        config.endElement(null, "Resource", "Resource");
+                    }
+                }
+            }
+
+            {
+                final Map<String, Map<String, String>> containers = map(rawConfiguration.get("containers"));
+                if (containers != null) {
+                    // TODO
+                }
+            }
+
+            // TODO: see org.apache.openejb.config.sys.SaxOpenejb.Root.startElement() for
all parent tags)
+
+            config.endElement(null, "openejb", null);
+        } catch (final Exception e) {
+            throw new OpenEJBException(e.getMessage(), e);
+        }
+
+        return config.getOpenejb();
+    }
+
+    private static String toString(final Map<String, ?> properties) {
+        if (properties == null) {
+            return "";
+        }
+        final Properties builder = new Properties();
+        for (final Map.Entry<String, ?> entry : properties.entrySet()) {
+            builder.put(entry.getKey(), entry.getValue().toString());
+        }
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            builder.store(baos, "");
+        } catch (final IOException e) {
+            // no-op
+        }
+        return new String(baos.toByteArray());
+    }
+
+    private static AttributesImpl toAttributes(final Map<String, String> map, final
String... ignored) {
+        final AttributesImpl attributes = new AttributesImpl();
+        for (final Map.Entry<String, String> entry : map.entrySet()) {
+            final String key = entry.getKey();
+            boolean add = true;
+            for (final String i : ignored) {
+                if (key.equals(i)) {
+                    add = false;
+                    break;
+                }
+            }
+
+            if (add) {
+                attributes.addAttribute(null, key, key, null, entry.getValue());
+            }
+        }
+        return attributes;
+    }
+}

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=1490699&r1=1490698&r2=1490699&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
Fri Jun  7 16:01:40 2013
@@ -184,51 +184,8 @@ public abstract class JaxbOpenejb {
         SAXParser parser = Saxs.namespaceAwareFactory().newSAXParser();
 
         final ServicesJar servicesJar1 = new ServicesJar();
-        final PropertiesAdapter propertiesAdapter = new PropertiesAdapter();
 
-        parser.parse(inputSource, new DefaultHandler() {
-            private ServiceProvider provider;
-            private StringBuilder content;
-
-            public void startDocument() throws SAXException {
-            }
-
-            public void startElement(String uri, String localName, String qName, Attributes
att) throws SAXException {
-                if (!localName.equals("ServiceProvider")) return;
-
-                provider = new ServiceProvider();
-                provider.setId(att.getValue("","id"));
-                provider.setService(att.getValue("", "service"));
-                provider.setFactoryName(att.getValue("", "factory-name"));
-                provider.setConstructor(att.getValue("", "constructor"));
-                provider.setClassName(att.getValue("", "class-name"));
-                provider.setParent(att.getValue("", "parent"));
-                String typesString = att.getValue("", "types");
-                if (typesString != null){
-                    final ListAdapter listAdapter = new ListAdapter();
-                    final List<String> types = listAdapter.unmarshal(typesString);
-                    provider.getTypes().addAll(types);
-                }
-                servicesJar1.getServiceProvider().add(provider);
-            }
-
-            public void characters(char ch[], int start, int length) throws SAXException
{
-                if (content == null) content = new StringBuilder();
-                content.append(ch, start, length);
-            }
-
-            public void endElement(String uri, String localName, String qName) throws SAXException
{
-                if (provider == null || content == null) return;
-
-                try {
-                    provider.getProperties().putAll(propertiesAdapter.unmarshal(content.toString()));
-                } catch (Exception e) {
-                    throw new SAXException(e);
-                }
-                provider = null;
-                content = null;
-            }
-        });
+        parser.parse(inputSource, new OpenEJBHandler(servicesJar1));
         ServicesJar servicesJar = servicesJar1;
         return servicesJar;
     }
@@ -245,6 +202,11 @@ public abstract class JaxbOpenejb {
             } else {
                 in = IO.read(new File(configFile));
             }
+
+            if (configFile.endsWith(".json")) {
+                return JSonConfigReader.read(IO.slurp(in));
+            }
+
             return readConfig(new InputSource(in));
         } catch (MalformedURLException e) {
             throw new OpenEJBException("Unable to resolve location " + configFile, e);
@@ -439,4 +401,55 @@ public abstract class JaxbOpenejb {
     public static TransactionManager createTransactionManager() {
         return new TransactionManager();
     }
+
+    private static class OpenEJBHandler extends DefaultHandler {
+        private final PropertiesAdapter propertiesAdapter = new PropertiesAdapter();
+        private final ServicesJar servicesJar;
+
+        private ServiceProvider provider;
+        private StringBuilder content;
+
+        public OpenEJBHandler(final ServicesJar servicesJar1) {
+            this.servicesJar = servicesJar1;
+        }
+
+        public void startDocument() throws SAXException {
+        }
+
+        public void startElement(String uri, String localName, String qName, Attributes att)
throws SAXException {
+            if (!localName.equals("ServiceProvider")) return;
+
+            provider = new ServiceProvider();
+            provider.setId(att.getValue("","id"));
+            provider.setService(att.getValue("", "service"));
+            provider.setFactoryName(att.getValue("", "factory-name"));
+            provider.setConstructor(att.getValue("", "constructor"));
+            provider.setClassName(att.getValue("", "class-name"));
+            provider.setParent(att.getValue("", "parent"));
+            String typesString = att.getValue("", "types");
+            if (typesString != null){
+                final ListAdapter listAdapter = new ListAdapter();
+                final List<String> types = listAdapter.unmarshal(typesString);
+                provider.getTypes().addAll(types);
+            }
+            servicesJar.getServiceProvider().add(provider);
+        }
+
+        public void characters(char ch[], int start, int length) throws SAXException {
+            if (content == null) content = new StringBuilder();
+            content.append(ch, start, length);
+        }
+
+        public void endElement(String uri, String localName, String qName) throws SAXException
{
+            if (provider == null || content == null) return;
+
+            try {
+                provider.getProperties().putAll(propertiesAdapter.unmarshal(content.toString()));
+            } catch (Exception e) {
+                throw new SAXException(e);
+            }
+            provider = null;
+            content = null;
+        }
+    }
 }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/SaxOpenejb.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/SaxOpenejb.java?rev=1490699&r1=1490698&r2=1490699&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/SaxOpenejb.java
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/sys/SaxOpenejb.java
Fri Jun  7 16:01:40 2013
@@ -58,7 +58,11 @@ class SaxOpenejb extends StackHandler {
         push(new Document());
     }
 
-    private class Root extends DefaultHandler {
+    Openejb getOpenejb() {
+        return openejb;
+    }
+
+    public class Root extends DefaultHandler {
 
         @Override
         public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {

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=1490699&r1=1490698&r2=1490699&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
Fri Jun  7 16:01:40 2013
@@ -32,6 +32,7 @@ import org.apache.openejb.config.Configu
 import org.apache.openejb.config.ConnectorModule;
 import org.apache.openejb.config.DeploymentLoader;
 import org.apache.openejb.config.EjbModule;
+import org.apache.openejb.config.sys.JSonConfigReader;
 import org.apache.openejb.config.PersistenceModule;
 import org.apache.openejb.config.WebModule;
 import org.apache.openejb.config.sys.JaxbOpenejb;
@@ -54,6 +55,7 @@ import org.apache.openejb.jee.jpa.unit.P
 import org.apache.openejb.jee.jpa.unit.PersistenceUnit;
 import org.apache.openejb.jee.oejb3.EjbDeployment;
 import org.apache.openejb.jee.oejb3.OpenejbJar;
+import org.apache.openejb.loader.IO;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.ContainerSystem;
 import org.apache.openejb.util.Join;
@@ -79,6 +81,7 @@ import javax.naming.Context;
 import javax.naming.InitialContext;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
@@ -291,11 +294,21 @@ public final class ApplicationComposers 
             } else if (Openejb.class.isInstance(o)) {
                 openejb = Openejb.class.cast(o);
             } else if (String.class.isInstance(o)) {
-                final URL url = Thread.currentThread().getContextClassLoader().getResource(String.class.cast(o));
-                if (url ==null) {
+                final String path = String.class.cast(o);
+                final URL url = Thread.currentThread().getContextClassLoader().getResource(path);
+                if (url == null) {
                     throw new IllegalArgumentException(o.toString() + " not found");
                 }
-                openejb = JaxbOpenejb.readConfig(new InputSource(url.openStream()));
+                final InputStream in = url.openStream();
+                try {
+                    if (path.endsWith(".json")) {
+                        openejb = JSonConfigReader.read(IO.slurp(in));
+                    } else {
+                        openejb = JaxbOpenejb.readConfig(new InputSource(in));
+                    }
+                } finally {
+                    IO.close(in);
+                }
             }
         }
 

Added: 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=1490699&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java
(added)
+++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/config/JSonConfigTest.java
Fri Jun  7 16:01:40 2013
@@ -0,0 +1,56 @@
+/*
+ * 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.config;
+
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.resource.jdbc.dbcp.BasicDataSource;
+import org.apache.openejb.testing.AppResource;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testing.Module;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(ApplicationComposer.class)
+public class JSonConfigTest {
+    @Configuration
+    public String config() {
+        return "openejb.json";
+    }
+
+    @Module
+    public EjbJar jar() {
+        return new EjbJar();
+    }
+
+    @AppResource
+    private Context ctx;
+
+    @Test
+    public void checkDsIsHere() throws NamingException {
+        final BasicDataSource ds = BasicDataSource.class.cast(ctx.lookup("openejb:Resource/json-datasource"));
+        assertNotNull(ds);
+        assertEquals(123, ds.getMaxActive());
+        assertEquals("jdbc:hsqldb:mem:json", ds.getJdbcUrl());
+    }
+}

Added: tomee/tomee/trunk/container/openejb-core/src/test/resources/openejb.json
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/resources/openejb.json?rev=1490699&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/resources/openejb.json (added)
+++ tomee/tomee/trunk/container/openejb-core/src/test/resources/openejb.json Fri Jun  7 16:01:40
2013
@@ -0,0 +1,16 @@
+{
+    "resources": {
+        "json-datasource": {
+            "properties": {
+                "JdbcUrl": "jdbc:hsqldb:mem:json",
+                "MaxActive": "123",
+                "JtaManaged": "false"
+            }
+        }
+    },
+    "containers": {
+        "stateless": {
+            "AccessTimeout": "10 seconds"
+        }
+    }
+}



Mime
View raw message