Author: rmannibucau
Date: Sun Jun  9 19:16:08 2013
New Revision: 1491267

URL: http://svn.apache.org/r1491267
Log:
TOMEE-966 parsing manually the json to avoid a dep + to make it working in java 
6

Added:
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/SimpleJSonParser.java
Modified:
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigUtils.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/testing/ApplicationComposers.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
    
tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigUtils.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigUtils.java?rev=1491267&r1=1491266&r2=1491267&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigUtils.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigUtils.java
 Sun Jun  9 19:16:08 2013
@@ -51,48 +51,50 @@ public class ConfigUtils {
     public static String searchForConfiguration(String path, Properties props) 
throws OpenEJBException {
         File file = null;
         if (path != null) {
-            /*
-             * [1] Try finding the file relative to the current working
-             * directory
-             */
-            file = new File(path);
-            if (file != null && file.exists() && file.isFile()) {
-                return file.getAbsolutePath();
-            }
-
-            /*
-             * [2] Try finding the file relative to the openejb.base directory
-             */
-            try {
-                file = SystemInstance.get().getBase().getFile(path);
-                if (file != null && file.exists() && file.isFile()) {
+            for (final String p : deducePaths(path)) {
+                /*
+                 * [1] Try finding the file relative to the current working
+                 * directory
+                 */
+                file = new File(path);
+                if (file.exists() && file.isFile()) {
                     return file.getAbsolutePath();
                 }
-            } catch (FileNotFoundException ignored) {
-            } catch (IOException ignored) {
-            }
 
-            /*
-             * [3] Try finding the file relative to the openejb.home directory
-             */
-            try {
-                file = SystemInstance.get().getHome().getFile(path);
-                if (file != null && file.exists() && file.isFile()) {
-                    return file.getAbsolutePath();
+                /*
+                 * [2] Try finding the file relative to the openejb.base 
directory
+                 */
+                try {
+                    file = SystemInstance.get().getBase().getFile(path);
+                    if (file != null && file.exists() && file.isFile()) {
+                        return file.getAbsolutePath();
+                    }
+                } catch (FileNotFoundException ignored) {
+                } catch (IOException ignored) {
                 }
-            } catch (FileNotFoundException ignored) {
-            } catch (IOException ignored) {
-            }
 
-            /*
-             * [4] Consider path as a URL resource - file: and jar: accepted 
by JaxbOpenejb.readConfig(String configFile)
-             */
-            try {
-                // verify if it's parseable according to URL rules
-                new URL(path);
-                // it's so return it unchanged
-                return path;
-            } catch (MalformedURLException ignored) {
+                /*
+                 * [3] Try finding the file relative to the openejb.home 
directory
+                 */
+                try {
+                    file = SystemInstance.get().getHome().getFile(path);
+                    if (file != null && file.exists() && file.isFile()) {
+                        return file.getAbsolutePath();
+                    }
+                } catch (FileNotFoundException ignored) {
+                } catch (IOException ignored) {
+                }
+
+                /*
+                 * [4] Consider path as a URL resource - file: and jar: 
accepted by JaxbOpenejb.readConfig(String configFile)
+                 */
+                try {
+                    // verify if it's parseable according to URL rules
+                    new URL(path);
+                    // it's so return it unchanged
+                    return path;
+                } catch (MalformedURLException ignored) {
+                }
             }
             
             logger.warning("Cannot find the configuration file [" + path + "], 
Trying conf/openejb.xml instead.");
@@ -117,6 +119,10 @@ public class ConfigUtils {
                 return file.getAbsolutePath();
             }
 
+            file = SystemInstance.get().getConf("openejb.json");
+            if (file != null && file.exists() && file.isFile()) {
+                return file.getAbsolutePath();
+            }
 
             if (EnvProps.extractConfigurationFiles()) {
 
@@ -144,6 +150,13 @@ public class ConfigUtils {
         return (file == null || !file.exists()) ? null : 
file.getAbsolutePath();
     }
 
+    public static String[] deducePaths(final String path) {
+        if (path.endsWith(".xml")) { // try json too, this is just a common 
way matching our defaults
+            return new String[] { path, path.substring(0, path.length() - 
"xml".length()) + "json" };
+        }
+        return new String[] { path };
+    }
+
     public static File createConfig(File config) throws java.io.IOException {
         ResourceFinder finder = new ResourceFinder("");
         URL defaultConfig = finder.find("default.openejb.conf");

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=1491267&r1=1491266&r2=1491267&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
 Sun Jun  9 19:16:08 2013
@@ -17,12 +17,14 @@
 package org.apache.openejb.config.sys;
 
 import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.util.SimpleJSonParser;
 import org.xml.sax.helpers.AttributesImpl;
 
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.Arrays;
 import java.util.Map;
 import java.util.Properties;
@@ -32,23 +34,15 @@ public class JSonConfigReader {
         return Map.class.cast(rawMap);
     }
 
-    public static Openejb read(final String json) throws OpenEJBException {
+    public static Openejb read(final InputStream is) throws OpenEJBException {
         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());
 
-            final Map<?, ?> jsConfig = map(engine.eval("eval(" + json + ")")); 
// TODO: see how to do so in java 6
+            final Map<?, ?> jsConfig = map(SimpleJSonParser.read(is)); // 
TODO: see how to do so in java 6
 
             for (final String root :
                     Arrays.asList("Resource", "Container", "JndiProvider", 
"TransactionManager", "ConnectionManager",
@@ -65,9 +59,11 @@ public class JSonConfigReader {
                 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 (!"deployments".equals(currentRoot)) {
+                            attributes.addAttribute(null, "id", "id", null, 
resource.getKey());
+                        }
 
-                        if ("resources".equals(root) && 
attributes.getIndex("type") == -1) {
+                        if ("resources".equals(currentRoot) && 
attributes.getIndex("type") == -1) {
                             attributes.addAttribute(null, "type", "type", 
null, "DataSource");
                         }
 

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=1491267&r1=1491266&r2=1491267&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
 Sun Jun  9 19:16:08 2013
@@ -204,7 +204,7 @@ public abstract class JaxbOpenejb {
             }
 
             if (configFile.endsWith(".json")) {
-                return JSonConfigReader.read(IO.slurp(in));
+                return JSonConfigReader.read(in);
             }
 
             return readConfig(new InputSource(in));

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=1491267&r1=1491266&r2=1491267&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
 Sun Jun  9 19:16:08 2013
@@ -302,7 +302,7 @@ public final class ApplicationComposers 
                 final InputStream in = url.openStream();
                 try {
                     if (path.endsWith(".json")) {
-                        openejb = JSonConfigReader.read(IO.slurp(in));
+                        openejb = JSonConfigReader.read(in);
                     } else {
                         openejb = JaxbOpenejb.readConfig(new InputSource(in));
                     }

Added: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/SimpleJSonParser.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/SimpleJSonParser.java?rev=1491267&view=auto
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/SimpleJSonParser.java
 (added)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/util/SimpleJSonParser.java
 Sun Jun  9 19:16:08 2013
@@ -0,0 +1,96 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+// only maps json objects, doesn't handle array, number, true/false/null
+public class SimpleJSonParser {
+    public static Object read(final InputStream is) throws IOException {
+        Map<String, Object> json = null;
+
+        int read;
+        char current;
+        while ((read = is.read()) != -1) {
+            current = (char) read; // cast after otherwise -1 test will likely 
fail if the input file is not correct
+
+            if (current == '{') {
+                json = new HashMap<String, Object>();
+            } else if (current == '}') {
+                return json;
+            } else if (current == '"') {
+                final StringBuilder b =  new StringBuilder();
+                do {
+                    read = is.read();
+                    current = (char) read;
+
+                    b.append(current);
+                } while (current != -1 && current != '\"');
+
+                if (current == -1) {
+                    throw new IllegalArgumentException("String should be 
between \"");
+                }
+
+                final String value = b.substring(0, b.length() - 1); // remove 
last "
+                if (valueRead(is, json, value)) {
+                    return value;
+                }
+            } else if (current != ':' && current != ',' && 
!isWhiteSpace(current)) {
+                final StringBuilder b =  new StringBuilder().append(current);
+                do {
+                    read = is.read();
+                    current = (char) read;
+
+                    b.append(current);
+                } while (current != -1 && !isWhiteSpace(current) && current != 
',');
+
+                final String value = b.substring(0, b.length() - 1); // remove 
last character
+                if (valueRead(is, json, value)) {
+                    return value;
+                }
+            }
+            // else skip
+        }
+
+        throw new IllegalArgumentException("Please check input, a } is 
probably missing");
+    }
+
+    private static boolean valueRead(final InputStream is, final Map<String, 
Object> json, final String value) throws IOException {
+        int read;
+        do {
+            read = is.read();
+        } while (read != -1 && read != ':' && read == ',');
+
+        if (json != null) {
+            json.put(value, read(is));
+        } else {
+            return true;
+        }
+        return false;
+    }
+
+    private static boolean isWhiteSpace(final char current) {
+        return current == ' ' || current == '\n' || current == '\r' || current 
== '\t';
+    }
+
+    private SimpleJSonParser() {
+        // no-op
+    }
+}

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=1491267&r1=1491266&r2=1491267&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
 Sun Jun  9 19:16:08 2013
@@ -16,6 +16,11 @@
  */
 package org.apache.openejb.config;
 
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.config.sys.Container;
+import org.apache.openejb.config.sys.JSonConfigReader;
+import org.apache.openejb.config.sys.Openejb;
+import org.apache.openejb.config.sys.Resource;
 import org.apache.openejb.jee.EjbJar;
 import org.apache.openejb.junit.ApplicationComposer;
 import org.apache.openejb.resource.jdbc.dbcp.BasicDataSource;
@@ -29,10 +34,12 @@ import org.junit.runner.RunWith;
 import javax.naming.Context;
 import javax.naming.NamingException;
 
+import java.io.IOException;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
-@Ignore("needs java 7 ATM")
 @RunWith(ApplicationComposer.class)
 public class JSonConfigTest {
     @Configuration
@@ -55,4 +62,23 @@ public class JSonConfigTest {
         assertEquals(123, ds.getMaxActive());
         assertEquals("jdbc:hsqldb:mem:json", ds.getJdbcUrl());
     }
+
+    @Test
+    public void simpleRead() throws IOException, OpenEJBException {
+        final Openejb openejb = 
JSonConfigReader.read(Thread.currentThread().getContextClassLoader().getResource(config()).openStream());
+
+        assertEquals(1, openejb.getResource().size());
+        final Resource resource = openejb.getResource().iterator().next();
+        assertEquals("json-datasource", resource.getId());
+        
assertTrue("123".equals(resource.getProperties().getProperty("MaxActive")));
+        
assertTrue("jdbc:hsqldb:mem:json".equals(resource.getProperties().getProperty("JdbcUrl")));
+
+        assertEquals(1, openejb.getDeployments().size());
+        assertEquals("apps", 
openejb.getDeployments().iterator().next().getDir());
+
+        assertEquals(1, openejb.getContainer().size());
+        final Container container = openejb.getContainer().iterator().next();
+        assertEquals("STATELESS", container.getType());
+        assertEquals("10 seconds", 
container.getProperties().getProperty("AccessTimeout"));
+    }
 }

Modified: 
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=1491267&r1=1491266&r2=1491267&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/resources/openejb.json 
(original)
+++ tomee/tomee/trunk/container/openejb-core/src/test/resources/openejb.json 
Sun Jun  9 19:16:08 2013
@@ -3,11 +3,16 @@
         "json-datasource": {
             "properties": {
                 "JdbcUrl": "jdbc:hsqldb:mem:json",
-                "MaxActive": "123",
-                "JtaManaged": "false"
+                "MaxActive": 123,
+                "JtaManaged": false
             }
         }
     },
+    "deployments": {
+        "apps": {
+            "dir": "apps"
+        }
+    },
     "containers": {
         "stateless": {
             "type": "STATELESS",

Modified: 
tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java?rev=1491267&r1=1491266&r2=1491267&view=diff
==============================================================================
--- 
tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java
 (original)
+++ 
tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/TomcatLoader.java
 Sun Jun  9 19:16:08 2013
@@ -30,6 +30,7 @@ import org.apache.openejb.assembler.clas
 import org.apache.openejb.assembler.classic.WebAppBuilder;
 import org.apache.openejb.classloader.WebAppEnricher;
 import org.apache.openejb.component.ClassLoaderEnricher;
+import org.apache.openejb.config.ConfigUtils;
 import org.apache.openejb.config.ConfigurationFactory;
 import org.apache.openejb.config.NewLoaderLogic;
 import org.apache.openejb.config.sys.Tomee;
@@ -165,10 +166,12 @@ public class TomcatLoader implements Loa
         }
 
         final File conf = new 
File(SystemInstance.get().getBase().getDirectory(), "conf");
-        final File tomeeXml = new File(conf, "tomee.xml");
-        if (tomeeXml.exists()) { // use tomee.xml instead of openejb.xml
-            SystemInstance.get().setProperty("openejb.configuration", 
tomeeXml.getAbsolutePath());
-            SystemInstance.get().setProperty("openejb.configuration.class", 
Tomee.class.getName());
+        for (final String possibleTomeePaths : 
ConfigUtils.deducePaths("tomee.xml")) {
+            final File tomeeXml = new File(conf, possibleTomeePaths);
+            if (tomeeXml.exists()) { // use tomee.xml instead of openejb.xml
+                SystemInstance.get().setProperty("openejb.configuration", 
tomeeXml.getAbsolutePath());
+                
SystemInstance.get().setProperty("openejb.configuration.class", 
Tomee.class.getName());
+            }
         }
 
         // set tomcat pool


Reply via email to