Still in the flow
A first (and slightly modified) version of the famous Calculator example is working with Java continuations :) It currently looks like this:
But there is no integration with Cocoon yet. You have to run it like this:
A lot of things are still missing …or are just dead ugly. But I’d like to hear
some comments from my fellow Cocooner’s first before going down that
road. Looking forward doing this face-to-face on the GetTogether :))
final public class CalculatorFlow extends AbstractCocoonFlow {
public void run() {
CocoonEnvironment env;
float a, b;
String op;
String uri = "page/";
HashMap parameters = new HashMap();
env = sendPageAndWait(uri + "getNumberA", parameters);
a = Float.parseFloat(env.getRequest().getParameter("a"));
parameters.put("a", new Float(a));
env = sendPageAndWait(uri + "getNumberB", parameters);
b = Float.parseFloat(env.getRequest().getParameter("b"));
parameters.put("b", new Float(b));
env = sendPageAndWait(uri + "getOperator", parameters);
op = env.getRequest().getParameter("operator");
if ("plus".equals(op)) {
parameters.put("result", new Float(a + b));
sendPageAndWait(uri + "displayResult", parameters);
} else if ("minus".equals(op)) {
parameters.put("result", new Float(a - b));
sendPageAndWait(uri + "displayResult", parameters);
} else if ("multiply".equals(op)) {
parameters.put("result", new Float(a * b));
sendPageAndWait(uri + "displayResult", parameters);
} else if ("divide".equals(op)) {
if (b == 0) {
//sendPage("Error: Division by zero!");
} else {
parameters.put("result", new Float(a / b));
sendPageAndWait(uri + "displayResult", parameters);
}
} else {
//sendPage("Error: Unkown operator!");
}
}
public static void calculator() throws Exception {
Classloader loader = new Classloader();
Class clazz = loader.loadClass("org.apache.cocoon.jflow.test.CalculatorFlow");
Flow flow = (Flow) clazz.newInstance();
Continuation c = null;
System.out.println("*** starting flow");
c = flow.continueWith(flow.createContinuation(c),
new CocoonEnvironment(null));
System.out.println("*** returning from flow");
System.out.println("*** starting flow");
c = flow.continueWith(flow.createContinuation(c),
new CocoonEnvironment(AbstractCocoonFlow.createRequest( new String[][] { {"a","1"}} )));
System.out.println("*** returning from flow");
System.out.println("*** starting flow");
c = flow.continueWith(flow.createContinuation(c),
new CocoonEnvironment(AbstractCocoonFlow.createRequest( new String[][] { {"b","1"}} )));
System.out.println("*** returning from flow");
System.out.println("*** starting flow");
c = flow.continueWith(flow.createContinuation(c),
new CocoonEnvironment(AbstractCocoonFlow.createRequest( new String[][] { {"operator","plus"}} )));
System.out.println("*** returning from flow");
if (c == null) {
System.out.println("*** end of flow");
}
}