You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ode.apache.org by mr...@apache.org on 2009/02/07 02:42:15 UTC

svn commit: r741811 - in /ode/sandbox/simpel: etc/log/ lang/src/main/java/org/apache/ode/embed/ lang/src/main/java/org/apache/ode/simpel/ server/src/main/java/org/apache/ode/lifecycle/ server/src/test/java/org.apache.ode.lifecycle/

Author: mriou
Date: Sat Feb  7 01:42:15 2009
New Revision: 741811

URL: http://svn.apache.org/viewvc?rev=741811&view=rev
Log:
Improved server logging and error reporting. Ability to load external js script files in the process environment with js load function.

Modified:
    ode/sandbox/simpel/etc/log/log4j.properties
    ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/EmbeddedStore.java
    ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/GeronimoTxFactory.java
    ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/ServerLifecycle.java
    ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/CompilationException.java
    ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/SimPELCompiler.java
    ode/sandbox/simpel/server/src/main/java/org/apache/ode/lifecycle/ScriptBasedStore.java
    ode/sandbox/simpel/server/src/test/java/org.apache.ode.lifecycle/TestLifecycle.java

Modified: ode/sandbox/simpel/etc/log/log4j.properties
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/etc/log/log4j.properties?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/etc/log/log4j.properties (original)
+++ ode/sandbox/simpel/etc/log/log4j.properties Sat Feb  7 01:42:15 2009
@@ -22,16 +22,15 @@
 log4j.category.org.mortbay=ERROR
 log4j.category.org.hibernate.type=WARN
 log4j.category.org.objectweb=ERROR
-log4j.category.org.apache.ode=WARN
-log4j.category.org.apache.ode.simpel=INFO
+log4j.category.org.apache.ode=INFO
 
 # Console appender
 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
-log4j.appender.CONSOLE.layout.ConversionPattern=%p - %C{1}.%M(%L) | %m%n
+log4j.appender.CONSOLE.layout.ConversionPattern=%m%n
 
 log4j.appender.FILE=org.apache.log4j.FileAppender
-log4j.appender.FILE.File=simplex.log
+log4j.appender.FILE.File=../log/simplex.log
 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
 log4j.appender.FILE.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p (%13F:%L) %3x - %m%n
 log4j.appender.FILE.append=false

Modified: ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/EmbeddedStore.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/EmbeddedStore.java?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/EmbeddedStore.java (original)
+++ ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/EmbeddedStore.java Sat Feb  7 01:42:15 2009
@@ -67,6 +67,10 @@
         return ll;
     }
 
+    protected void start() {
+        // Nothing special to do
+    }
+
     protected void fireEvent(ProcessStoreEvent pse) {
         for (ProcessStoreListener psl : _listeners) psl.onProcessStoreEvent(pse);
     }

Modified: ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/GeronimoTxFactory.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/GeronimoTxFactory.java?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/GeronimoTxFactory.java (original)
+++ ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/GeronimoTxFactory.java Sat Feb  7 01:42:15 2009
@@ -35,7 +35,7 @@
         }
 
         public TransactionManager getTransactionManager() {
-            __log.info("Using embedded Geronimo transaction manager");
+            __log.debug("Using embedded Geronimo transaction manager");
             try {
                 Object obj = new org.apache.geronimo.transaction.manager.GeronimoTransactionManager();
                 return (TransactionManager) obj;

Modified: ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/ServerLifecycle.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/ServerLifecycle.java?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/ServerLifecycle.java (original)
+++ ode/sandbox/simpel/lang/src/main/java/org/apache/ode/embed/ServerLifecycle.java Sat Feb  7 01:42:15 2009
@@ -68,10 +68,13 @@
         registerEventListeners();
 
         _server.start();
+        _store.start();
+        __log.info("Up and ready to serve.");
     }
 
     public void clean() {
         EngineWebResource.stopRestfulServer();
+        _db.shutdown();
         _server = null;
         _txMgr = null;
         _executorService = null;

Modified: ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/CompilationException.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/CompilationException.java?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/CompilationException.java (original)
+++ ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/CompilationException.java Sat Feb  7 01:42:15 2009
@@ -26,7 +26,7 @@
 
     public CompilationException(List<Error> errors) {
         super();
-        StringBuffer msg = new StringBuffer("Errors during process compilation:\n");
+        StringBuffer msg = new StringBuffer();
         for (Error error : errors)
             msg.append(error.line).append(":").append(error.column).append(" ").append(error.message).append("\n");
         this.compilationMessages = msg.toString();

Modified: ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/SimPELCompiler.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/SimPELCompiler.java?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/SimPELCompiler.java (original)
+++ ode/sandbox/simpel/lang/src/main/java/org/apache/ode/simpel/SimPELCompiler.java Sat Feb  7 01:42:15 2009
@@ -28,6 +28,7 @@
 import java.util.HashMap;
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
+import java.text.MessageFormat;
 
 public class SimPELCompiler {
 
@@ -41,25 +42,45 @@
         this.el = el;
     }
 
+    public OProcess compileProcess(File processDoc, Descriptor desc) {
+        StringBuffer scriptCnt = new StringBuffer();
+        try {
+            String thisLine;
+            BufferedReader r = new BufferedReader(new FileReader(processDoc));
+            while ((thisLine = r.readLine()) != null) scriptCnt.append(thisLine).append("\n");
+            r.close();
+        } catch (IOException e) {
+            fatalCompilationError("Couldn't read process file: " + processDoc.getAbsolutePath());
+        }
+
+        return compileProcess(processDoc, scriptCnt.toString(), desc);
+    }
+
     public OProcess compileProcess(String processDoc, Descriptor desc) {
+        return compileProcess(new File("."), processDoc, desc);
+    }
+
+    public OProcess compileProcess(File f, String processDoc, Descriptor desc) {
         // Isolating the process definition from the header containing global state definition (Javascript
         // functions and shared objects)
         Pattern p = Pattern.compile("process [a-zA-Z_]*", Pattern.MULTILINE);
         Matcher m = p.matcher(processDoc);
-        if (!m.find()) throw new CompilationException("Couldn't find any process declaration.");
+        if (!m.find())
+            fatalCompilationError("Couldn't find any process declaration in file.");
+
         String header = processDoc.substring(0, m.start());
         String processDef = processDoc.substring(m.start(), processDoc.length());
 
         byte[] globals = null;
         if (header.trim().length() > 0)
-            globals = buildGlobalState(header, desc);
+            globals = buildGlobalState(f, header, desc);
 
         OProcess model = buildModel(processDef, desc);
         if (globals != null) model.globalState = globals;
         return model;
     }
 
-    private byte[] buildGlobalState(String header, Descriptor desc) {
+    private byte[] buildGlobalState(File f, String header, Descriptor desc) {
         Context cx = Context.enter();
         cx.setOptimizationLevel(-1);
         Scriptable sharedScope = cx.initStandardObjects();
@@ -68,9 +89,13 @@
         newScope.setPrototype(sharedScope);
         newScope.setParentScope(null);
 
-        // Setting the configuration hash first
-        cx.evaluateString(newScope, "var processConfig = {};", "<cmd>", 1, null);
-        cx.evaluateString(newScope, header, "<cmd>", 1, null);
+        // Setting some globals part of the environment in which processes execute
+        cx.evaluateString(newScope, MessageFormat.format(GLOBALS, f.getParentFile().getAbsolutePath()), "<cmd>", 1, null);
+        try {
+            cx.evaluateString(newScope, header, f.getAbsolutePath(), 1, null);
+        } catch (Exception e) {
+            fatalCompilationError("Error when interpreting definitions in the process header: " + e.toString());
+        }
 
         NativeObject processConf = (NativeObject) cx.evaluateString(newScope, "processConfig;", "<cmd>", 1, null);
         if (desc.getAddress() == null && processConf.has("address", null)) desc.setAddress((String) processConf.get("address", null));
@@ -83,12 +108,24 @@
             out.writeObject(newScope);
             out.close();
         } catch (IOException e) {
-            throw new CompilationException("Error when interpreting definitions in the process header.", e);
+            fatalCompilationError("Error when serializing header definitions: " + e.toString());
         }
 
         return baos.toByteArray();
     }
 
+    private static final String GLOBALS =
+            "var processConfig = '{}'; \n" +
+
+            "function load(f) '{' \n" +
+            "    var cnt = \"\"; \n" +
+            "    var l = \"\"; \n" +
+            "    var r = new java.io.BufferedReader(new java.io.FileReader(\"{0}/\" + f)); \n" +
+            "    while ((l = r.readLine()) != null) cnt = cnt + l + \"\\n\"; \n" +
+            "    r.close(); \n" +
+            "    return eval(cnt); \n" +
+            "'}'";
+
     private OProcess buildModel(String processDef, Descriptor desc) {
         ANTLRReaderStream charstream = null;
         try {
@@ -173,9 +210,6 @@
                     if (signature) params.add(txt);
                     else body.append(txt);
                 }
-                System.out.println("Found function: " + funcTree.getChild(0) + "(" + params + ") {"
-                        + body.toString() + "}");
-
                 toRemove.add(m);
             }
         }
@@ -225,5 +259,11 @@
         return false;
     }
 
+    private void fatalCompilationError(String s) {
+        CompilationException.Error err = new CompilationException.Error(0, 0, s, null);
+        ArrayList<CompilationException.Error> errs = new ArrayList<CompilationException.Error>();
+        errs.add(err);
+        throw new CompilationException(errs);
+    }
 }
 

Modified: ode/sandbox/simpel/server/src/main/java/org/apache/ode/lifecycle/ScriptBasedStore.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/server/src/main/java/org/apache/ode/lifecycle/ScriptBasedStore.java?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/server/src/main/java/org/apache/ode/lifecycle/ScriptBasedStore.java (original)
+++ ode/sandbox/simpel/server/src/main/java/org/apache/ode/lifecycle/ScriptBasedStore.java Sat Feb  7 01:42:15 2009
@@ -18,12 +18,18 @@
 public class ScriptBasedStore extends EmbeddedStore {
     private static final Logger __log = Logger.getLogger(ScriptBasedStore.class);
 
+    public static long POLLING_FREQ = 2000;
+
     private File _scriptsDir;
     private File _workDir;
 
     public ScriptBasedStore(File scriptsDir, File workDir) {
         _scriptsDir = scriptsDir;
         _workDir = workDir;
+    }
+
+    @Override
+    protected void start() {
         Thread poller = new Thread(new ScriptPoller());
         poller.setDaemon(true);
         poller.start();
@@ -72,6 +78,7 @@
                     ArrayList<File> toRebuild = new ArrayList<File>(unknown);
                     toRebuild.addAll(newer);
                     for (File p : toRebuild) {
+                        __log.debug("Recompiling " + p);
                         ProcessModel oprocess = compileProcess(p);
                         fireEvent(new ProcessStoreEvent(ProcessStoreEvent.Type.DEPLOYED, oprocess.getQName(), null));
                         fireEvent(new ProcessStoreEvent(ProcessStoreEvent.Type.ACTIVATED, oprocess.getQName(), null));
@@ -85,44 +92,36 @@
                     }
 
                     try {
-                        Thread.sleep(2000);
+                        Thread.sleep(POLLING_FREQ);
                     } catch (InterruptedException e) {
                         // whatever
                         e.printStackTrace();
                     }
                 } catch (Throwable t) {
-                    __log.info(t.toString() + "\nDeployment aborted.", t);
+                    if (t instanceof CompilationException)
+                        __log.info(t.getMessage() + "\nDeployment aborted.");
+                    else
+                        __log.error("Unexpected error during compilation.", t);
                 }
             }
         }
 
-        private ProcessModel compileProcess(File pfile) throws IOException {
+        private ProcessModel compileProcess(File pfile) throws IOException, CompilationException {
             File targetCbp = new File(_workDir, noExt(relativePath(_scriptsDir, pfile)) + ".cbp");
             targetCbp.getParentFile().mkdirs();
 
-            String thisLine;
-            StringBuffer scriptCnt = new StringBuffer();
-            BufferedReader r = new BufferedReader(new FileReader(pfile));
-            while ((thisLine = r.readLine()) != null) scriptCnt.append(thisLine).append("\n");
-            r.close();
-
-            ProcessModel oprocess;
-            try {
-                FileOutputStream cbpFos = new FileOutputStream(targetCbp, false);
-                Descriptor desc = new Descriptor();
-                oprocess = _compiler.compileProcess(scriptCnt.toString(), desc);
-                Serializer ser = new Serializer();
-                ser.writePModel(oprocess, cbpFos);
-                cbpFos.close();
-
-                _processes.put(oprocess.getQName(), oprocess);
-                _descriptors.put(oprocess.getQName(), desc);
-
-                fireEvent(new ProcessStoreEvent(ProcessStoreEvent.Type.DEPLOYED, oprocess.getQName(), null));
-                fireEvent(new ProcessStoreEvent(ProcessStoreEvent.Type.ACTIVATED, oprocess.getQName(), null));
-            } catch (CompilationException e) {
-                throw new RuntimeException("There were errors during the compilation of a SimPEL process:\n" + e.toString());
-            }
+            FileOutputStream cbpFos = new FileOutputStream(targetCbp, false);
+            Descriptor desc = new Descriptor();
+            ProcessModel oprocess = _compiler.compileProcess(pfile, desc);
+            Serializer ser = new Serializer();
+            ser.writePModel(oprocess, cbpFos);
+            cbpFos.close();
+
+            _processes.put(oprocess.getQName(), oprocess);
+            _descriptors.put(oprocess.getQName(), desc);
+
+            fireEvent(new ProcessStoreEvent(ProcessStoreEvent.Type.DEPLOYED, oprocess.getQName(), null));
+            fireEvent(new ProcessStoreEvent(ProcessStoreEvent.Type.ACTIVATED, oprocess.getQName(), null));
 
             return oprocess;
         }

Modified: ode/sandbox/simpel/server/src/test/java/org.apache.ode.lifecycle/TestLifecycle.java
URL: http://svn.apache.org/viewvc/ode/sandbox/simpel/server/src/test/java/org.apache.ode.lifecycle/TestLifecycle.java?rev=741811&r1=741810&r2=741811&view=diff
==============================================================================
--- ode/sandbox/simpel/server/src/test/java/org.apache.ode.lifecycle/TestLifecycle.java (original)
+++ ode/sandbox/simpel/server/src/test/java/org.apache.ode.lifecycle/TestLifecycle.java Sat Feb  7 01:42:15 2009
@@ -13,6 +13,20 @@
 import com.sun.jersey.api.client.ClientResponse;
 
 public class TestLifecycle extends TestCase {
+    private File rootDir;
+    private StandaloneServer server;
+
+    @Override
+    protected void setUp() throws Exception {
+        rootDir = new File(new File(getClass().getClassLoader().getResource("marker").getFile()).getParent());
+        server = new StandaloneServer(rootDir);
+        server.start();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        server.stop();
+    }
 
     private static final String HELLO_WORLD =
             "processConfig.address = \"/hello\";\n" +
@@ -25,15 +39,9 @@
             "}";
 
     public void testFSDeployUndeploy() throws Exception {
-        String rootDir = new File(getClass().getClassLoader().getResource("marker").getFile()).getParent();
-        StandaloneServer.main(new String[] { rootDir });
-
-        File pfile = new File(rootDir, "scripts/helloworld.simpel");
-        FileOutputStream fos = new FileOutputStream(pfile);
-        fos.write(HELLO_WORLD.getBytes());
-        fos.close();
-
-        Thread.sleep(3000);
+        writeProcessFile("helloworld.simpel", HELLO_WORLD);
+        Thread.sleep(5000);
+        
         ClientConfig cc = new DefaultClientConfig();
         Client c = Client.create(cc);
 
@@ -46,10 +54,51 @@
         assertTrue(resp.getMetadata().get("Location").get(0), resp.getMetadata().get("Location").get(0).matches(".*/hello/[0-9]*"));
         assertTrue(resp.getStatus() == 201);
 
-        pfile.delete();
-        Thread.sleep(3000);
+        new File(rootDir, "scripts/helloworld.simpel").delete();
+        Thread.sleep(300);
 
         assertTrue(!new File(rootDir, "work/helloworld.cbp").exists());
+        new File(rootDir, "work/helloworld.cbp").delete();
+    }
+
+    public void testDeployEmpty() throws Exception {
+        writeProcessFile("empty.simpel", "");
+
+        File cbp = new File(rootDir, "work/empty.cbp");
+        assertTrue(cbp.exists());
+        assertTrue(cbp.length() == 0);
+
+        writeProcessFile("empty.simpel", HELLO_WORLD);
+        cbp = new File(rootDir, "work/empty.cbp");
+        assertTrue(cbp.exists());
+        assertTrue(cbp.length() > 0);
+
+        new File(rootDir, "scripts/empty.simpel").delete();
+        cbp.delete();
+    }
+
+    public void testLoadInProcess() throws Exception {
+        writeProcessFile("foo.js", "var foo = 2;\n");
+        writeProcessFile("load-js.simpel", "load('foo.js');\n" + HELLO_WORLD);
+
+        Thread.sleep(2000);
+        File cbp = new File(rootDir, "work/load-js.cbp");
+        assertTrue(cbp.exists());
+        assertTrue(cbp.length() > 0);
+
+        new File(rootDir, "scripts/load-js.simpel").delete();
+        cbp.delete();
+    }
+
+    private void writeProcessFile(String filename, String content) throws Exception {
+        // speeding things up
+        ScriptBasedStore.POLLING_FREQ = 50;
+
+        File pfile = new File(rootDir, "scripts/" + filename);
+        FileOutputStream fos = new FileOutputStream(pfile);
+        fos.write(content.getBytes());
+        fos.close();
+        Thread.sleep(1000);
     }
 
 }
\ No newline at end of file