Control Flow is free!
After some late night hacking sessions, I've been able to rip Cocoon's Control Flow component free of Cocoon. I removed all dependencies on Cocoon and Avalon and pared my standalone version of Control Flow down to only 14 classes and a couple of JavaScript functions.
Below is the simple JavaScript code that I'm using to test Control Flow. This code will eventually run on the server side and will send a web page to the user when it needs input. The forwardAndWait()
method puts the name of the page or Struts forward into a 'tray' object and suspends execution. Eventually, it will suspend execution of the script, call a JSP page or a Struts forward, and wait for the response to go out and the request to come back in.
function test(tray) { log.info("I called my script!"); forwardAndWait(tray, "page1.html"); log.info("I'm back in the script"); forwardAndWait(tray, "page2.html"); log.info("all done!"); }
Below is the code I'm using to run and test this script. Step #1 sets up the 'tray' that I use to get the forward name and continuation id out of the JavaScript world. I introduced the tray concept, and I don't really like it but I'm not sure how else to get the values I need out of JavaScript. Step #2 and #4 are the interesting bits. Step #2 calls the JavaScript method, which executes and then suspends the script execution inside the 1st JavaScript forwardAndWait() call. Step #4 resumes execution of the script using the continuation id found in the tray.
JavaScriptInterpreter interp = new JavaScriptInterpreter(); interp.initialize(); interp.register("C:\\allfiles\\scratch\\StrutsFlow\\src\\flow\\system.js"); interp.register("C:\\allfiles\\scratch\\StrutsFlow\\tests\\flow\\test.js"); Scriptable s = interp.enterContext(); // 1. setup tray param to hold forward List params = new LinkedList(); List tray = new LinkedList(); params.add(new Argument("tray", tray)); // 2. call function interp.callFunction("test", params); // 3. funcion suspended, now tray should contain forward name assertTrue(((String)tray.get(0)).equals("page1.html")); // and the continuation id String kontid = (String)tray.get(1); // 4. resume exection of script using that contination id interp.handleContinuation(kontid, new LinkedList()); // 5. function suspeded again, now tray should contain second forward name assertTrue(((String)tray.get(0)).equals("page2.html")); // 6. resume function, so it may complete execution kontid = (String)tray.get(1); interp.handleContinuation(kontid, new LinkedList()); interp.exitContext(s);
And below is the debug output from running the test code above.
JS-INFO: I called my script! JS-DEBUG: WK: Just Created New Continuation 4d281a763e7c1b3f5a7b4e244b2e224729112160 JS-INFO: I'm back in the script JS-DEBUG: WK: Just Created New Continuation 877d715e813c5a7c4e7f1a56612826332f528167 JS-INFO: all done!
Next, I'll try to implement the same NumberGuess example from the Control Flow tutorial, but with no Cocoon - only JSP and Servlets.
Posted by Matt Raible on March 16, 2004 at 09:34 AM EST #
Posted by Ugo Cei on March 16, 2004 at 11:14 AM EST #