Online Book Reader

Home Category

AJAX In Action [105]

By Root 4002 0
{

Element processXML(Element el);

}

The interface depends upon the JDOM libraries for a convenient object-based representation of XML, using Element objects as both argument and return type. A simple implementation of this interface for updating planetary data is given in listing 5.16.

Listing 5.16 PlanetUpdateCommandProcessor.java

public class PlanetUpdateCommandProcessor

implements XMLCommandProcessor {

public Element processXML(Element el) {

Element result=new Element("command"); b

Create XML result node

String id=el.getAttributeValue("id");

Licensed to jonathan zheng

Writing to the server

205

result.setAttribute("id",id);

String status=null;

String reason=null;

String planetId=el.getAttributeValue("planetId");

String field=el.getAttributeValue("field");

String value=el.getAttributeValue("value");

Planet planet=findPlanet(planetId); c

Access domain model

if (planet==null){

status="failed";

reason="no planet found for id "+planetId;

}else{

Double numValue=new Double(value);

Object[] args=new Object[]{ numValue };

String method = "set"+field.substring(0,1).toUpperCase()

+field.substring(1);

Statement statement=new Statement(planet,method,args);

try {

statement.execute(); d

Update domain model

status="ok";

} catch (Exception e) {

status="failed";

reason="unable to set value "+value+" for field "+field;

}

}

result.setAttribute("status",status);

if (reason!=null){

result.setAttribute("reason",reason);

}

return result;

}

private Planet findPlanet(String planetId) {

// TODO use hibernate e Use ORM for domain model

return null;

}

}

As well as using JDOM to parse the incoming XML, we use it here to generate XML, building up a root node b and its children programmatically in the processXML() method. We access the server-side domain model using the findPlanet() method c, once we have a unique ID to work with. findPlanet() isn’t implemented here, for the sake of brevity—typically an ORM such as Hibernate would be used to talk to the database behind the scenes e. We use reflection to update the domain model d and then return the JDOM object that we have constructed, where it will be serialized by the servlet. Licensed to jonathan zheng

206

CHAPTER 5

The role of the server

This provides a sketch of the complete lifecycle of our queue-based architecture for combining many small domain model updates into a single HTTP transaction. It combines the ability to execute fine-grained synchronization between the client and server domain models with the need to manage server traffic effectively. As we noted in section 5.3, it may provide a solution for JSF and similar frameworks, in which the structure of the user interface and interaction model is held tightly by the server. In our case, though, it simply provides an efficient way of updating the domain models across the tiers.

This concludes our tour of client/server communication techniques for this chapter and our overview of key design issues for Ajax applications. Along the way, we’ve developed the start of a pattern language for Ajax server requests and a better understanding of the technical options available to us for implementing these. 5.6 Summary

We began this chapter by looking at the key roles of the application server in Ajax, of delivering the client code to the browser, and supplying the client with data once it is running. We looked at the common implementation languages on the server side and took a tour of the common types of server-side frameworks of the day. These are largely designed to serve classic web applications, and we considered how they can adapt to Ajax. The server-side framework space is crowded and fast moving, and rather than looking at particular products, we categorized in terms of generic architectures. This reduced the field to three main approaches: the Model2 frameworks, component-based frameworks, and service-oriented architectures. SOA seems to provide the most natural fit for Ajax, although the others can be adapted with varying degrees of success. We

Return Main Page Previous Page Next Page

®Online Book Reader