Use Continuations to Develop Complex Web Applications
By Abhijit Belapurkar
2005-04-22
Continuations in Java code
Newer releases of Cocoon provide support for a pure-Java interpreter of the flow script. Listing 7 shows the source code for the pure-Java interpreter:
Listing 7. The application flow implemented in Java code
import org.apache.cocoon.components.flow.java.AbstractContinuable;
import org.apache.cocoon.components.flow.java.VarMap;
public class PosFlow extends AbstractContinuable
{
public void doSellItem()
{
double rate, amount, total, savings;
double discount, discrate;
int qty, delCost;
String zone, delOpt;
String url = "page/getRateAmt";
sendPageAndWait(url);
rate = Float.parseFloat(getRequest().getParameter("rate"));
qty = Integer.parseInt(getRequest().getParameter("qty"));
amount = rate*qty;
url="page/getZone";
sendPageAndWait(url, new VarMap().add("rate",rate).add("qty",qty));
zone = getRequest().getParameter("zone");
discount=0.02;
if (zone.equals("A"))
{
if (qty >= 100)
{
discount=0.1;
}
}
else if (zone.equals("B"))
{
if (qty >= 200)
{
discount=0.2;
}
}
discrate = 100*discount;
savings = discount*amount;
delCost=0;
delOpt = getRequest().getParameter("delOpt");
if (delOpt.equals("S"))
{
url="page/getShipOpt";
sendPageAndWait(url);
delCost = Integer.parseInt(getRequest().getParameter("delCost"));
}
total = amount + delCost - savings;
url="page/displayResult";
sendPageAndWait(url, new VarMap()
.add("discrate",discrate)
.add("total",total)
.add("savings",savings)
.add("delCost",delCost)
.add("amount", amount)
.add("discount", discount)
.add("zone", zone));
}
}
As Listing 7 shows, Cocoon provides an abstract class called AbstractContinuable with implementations for the sendPage and sendPageAndWait functions. The PosFlow class extends this abstract class and contains the business logic in a method called doSellItem.The implementation of this method is exactly the same as the JavaScript implementation of sellItem in Listing 4.
Sitemap for the Java implementation
You can see the sitemap for the Java-based application in Listing 8. As you'll note, even this looks very similar to the earlier sitemap. The only difference is that the flow language is specified as Java and the source of the script is specified as the PosFlow class.
Listing 8. The Cocoon sitemap for the Java-based implementation
<?xml version="1.0"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
<map:flow language="java">
<map:script src="PosFlow"/>
</map:flow>
<map:pipelines>
<map:pipeline>
<map:match pattern="page/*">
<map:generate type="jx" src="screens/{1}.xml"/>
<map:transform src="context://samples/common/style/xsl/html/simple-page2html.xsl">
<map:parameter name="servletPath" value="{request:servletPath}"/>
<map:parameter name="sitemapURI" value="{request:sitemapURI}"/>
<map:parameter name="contextPath" value="{request:contextPath}"/>
<map:parameter name="file" value="/samples/flow/jxrate/screens/{1}.xml"/>
<map:parameter name="remove" value="{0}"/>
</map:transform>
<map:serialize/>
</map:match>
</map:pipeline>
<map:pipeline>
<map:match pattern="continue.*">
<map:call continuation="{1}"/>
</map:match>
<map:match pattern="">
<map:call function="sellItem"/>
</map:match>
</map:pipeline>
</map:pipelines>
</map:sitemap>
Another small difference between the JavaScript and Java implementations is in the way the id for the current continuation is accessed within the XML templates corresponding to the application's HTML pages. You can see this yourself by studying the XML template for the getRateAmt page in the Java-based implementation, shown in Listing 9. The continuation id can be accessed by the JXPath expression #{$continuation/id}.
Listing 9. The XML file for the getRateAmt page
<?xml version="1.0"?>
<page>
<title>Get Rate and Quantity of item to be purchased</title>
<content>
<form method="post" action="continue.#{$continuation/id}">
<para>Enter Rate: <input type="text" name="rate"/></para>
<para>Enter Quantity: <input type="text" name="qty"/></para>
<input type="submit" name="submit" value="Next"/>
</form>
</content>
</page>
Tutorial Pages:
»
A Programming Paradigm to Simplify MVC for the Web
»
What is a Continuation, Anyway?
»
Problems in Conventional Web development
»
The Case for Continuations
»
User-Centered Navigation
»
You Make the Rules!
»
The Continuations Repository
»
An Example Application
»
Web Continuations in Apache Cocoon
»
The Application Sitemap
»
The Application Logic
»
Understanding the Application Logic
»
Resuming the Continuation
»
JavaScript vs. the Java language
» Continuations in Java code
»
Pros and Cons of Continuations
»
Conclusion
»
Resources
First published by IBM DeveloperWorks
|
