header

Torsten Curdt’s weblog

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");
        }
    }

  • This looks really cool - I must take a closer look at Brakes.



    How about hosting Brakes at (say) SourceForge for a while so we can use it on other things rather than just Cocoon? I can think of a few projects I'd like to try this on
  • That would be great! I already would have done so ...but in the first place there are some licence issues that I'd like to check first. Although the Academic Software Licence is ment to be compatible with the Apache Software Licence it's not quite clear if we can just adopt the whole brakes project that way. Maybe an official contribution of brakes is necessary.



    In the second place Stefano also had some concerns if it is the right time to come up with another option for flow. Maybe we should learn to use it efficiently first ...finding good patterns and things like that. Knowing where we want to go with flow.



    On the other hand on the GetTogether I heard so many people ask "Why did you guys use Javascript for flow? Why not Java?"

    Reasons why they would like to see a Java flow implementation were



    a) Javascript on the server-side is hard to sell to their customers

    b) We are inside a java environment anyway - so why use another language? Additional glue to go from one language to another should not be necessary

    c) They like to program flow in a decent IDE
  • Chris Oliver
    Torsten,



    Would you mind putting your code into the cocoon scratchpad so others (like me) can help you develop it?
  • Yes, Brakes *was* a university project and is now being discontinued. Maybe we can resurrect it.

    I already have an upgraded version (bcel cvs head) sitting on my disk.



    I already contacted Tim to hear his opinion about this. But it's also a question if *we* (Cocooners) want to promote a second option for flow.
blog comments powered by Disqus