www.developer.com/xml/article.php/3098661

Back to Article

An Introduction to Apache Cocoon 2.1
By Michael Melhem
October 24, 2003

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.

  Go to page: Prev  1  2  

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES