August 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

An Introduction to Apache Cocoon 2.1

  • October 24, 2003
  • By Michael Melhem
  • Send Email »
  • More Articles »

Cocoon Flow, MVC+, and Continuations

Cocoon supports the Model View Controller (MVC) pattern in the form of "Cocoon flow" as an added layer of control on top of the sitemap.

Traditionally, interactive Web applications have been modeled after "finite state machines." In these models, Flow Logic is fragmented throughout the application to intercept responses from the client and determine the next machine state. Cocoon, with the help of Continuations, breaks through this paradig, allowing the flow logic to be described sequentially and within a single location.

What are Continuations? Continuations are a means to save the current state of execution, allowing for resumption of execution at an arbitrary later date. The concept does not involve any suspending of server threads or the like, but rather, involves the dumping of current state and stack information to memory or disk. This saved state is associated with a Continuation ID. To resume execution from a particular point, a Cocoon application merely needs quote the Continuation ID.

Continuations describing a single flow are linked together in a child/parent relationship, forming a tree. This means, for example, that the situation where a user hits the browser back button no longer presents a problematic case for the Web developer. This is because the Cocoon flow controller in this case will simply resume from the previous parent continuation, maintaining a consistent state. Any subsequent path the user takes creates a different branch in the continuations tree. Thus, during the life of a Web application, forests of Continuation trees can be formed, growing and receding as dictated. Continuations are automatically expired after a specified time-to-live. They also can be manually expired.

With Cocoon MCV, the Model would be the Java code; the component pipelines represent the Views, and the Controller is the Cocoon flow controller. Currently, the Cocoon flow Controller logic is implemented as serverside JavaScript. The advantages of severside JavaScript include quick prototyping and reloading. (There is talk of also adding support for other languages, including Java, for flow control.)

Perhaps the easiest way to illustrate the concept of Cocoon flow and Continuations is via a simple example. This example is based on a working sample from the Cocoon samples site.

function registerUserFlow()
{
   while (true) {
    cocoon.sendPageAndWait("page/getUserInfo.html",{ });
    // At this point, a Continuation is generated and saved
    // "waiting" to be resumed from the same point once a response
    // is returned from the user. Typically, the response is in
    // the shape of an HTML form submit.

    // The following three lines retrieve information from the
    // response
    login = cocoon.request.getParameter("login");
    firstName = cocoon.request.getParameter("firstName");
    lastName = cocoon.request.getParameter("lastName");

    // Here, the flow calls the "Model" to ascertain what the next
    // "View" should be.
    var existingUser = userRegistry.isLoginNameTaken(login);

    if (!existingUser) {
      user = new Packages...flow.prefs.User(login,, firstName,
                                            lastName);
      userRegistry.addUser(user);
      break;
    } else {
      errorMsg = "Login name '" + login + "' is already in use,
                                           please choose another
                                           name";
    }
  }
  var session = cocoon.session;
  cocoon.sendPage("page/registrationSuccessful.html", {"user" :
                                                        user});
  // Here, we send a page to the user but we don't save the state
  // and we don't wait for a response.
}

The flow controller above takes care of the steps necessary for the registration of a new user. As long as the user does not successfully log in, the login page will be re-displayed with an error message. There are two views: namely, page/getUserInfo.html, and page/registrationSuccessful.html. These views are generated by the corresponding sitemap component pipeline. The Model here is the userRegistry Java class.

Note how quick and easy it would be to change application flow logic by altering the above script.

Conclusion

While perhaps not the most optimal solution for small, one-page Web sites, Cocoon has many advantages over competing technologies when it comes to larger applications. This is especially true for Web operations that employ multiple people for development and maintenance, and which require internalization, content aggregation, XML transformations, and multi-channeling.

Apache Cocoon's capabilities are derived from simple but powerful concepts such as Component Pipelines, SoC, and Continuations. Grounded in these solid principles and concepts, and with a strong active developer community behind it, Apache Cocoon is gaining momentum as a premier Web Application framework.

Resources

http://cocoon.apache.org

http://wiki.cocoondev.org/Wiki.jsp

http://marc.theaimsgroup.com/

http://avalon.apache.org/

About the Author

Michael Melhem is an experienced Web Developer/Engineer with strong expertise building scalable Java XML based Web solutions for large commercial institutions. Michael is committer on the Apache Cocoon project and member of the Cocoon PMC. He is currently employed by ManageSoft Corp.



Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel