UpdatePropertyCommand object in Java

Drawer QR Code 2d barcode in Java UpdatePropertyCommand object

Listing 5.14 UpdatePropertyCommand object
Draw Quick Response Code In Java
Using Barcode drawer for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
QR Code Decoder In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
planets.commands.UpdatePropertyCommand=function(owner,field,value){ this.id=this.owner.id+"_"+field; this.obj=owner; this.field=field; this.value=value; } planets.commands.UpdatePropertyCommand.toRequestString=function(){
GS1 DataBar Limited Creator In Java
Using Barcode generation for Java Control to generate, create GS1 DataBar-14 image in Java applications.
www.OnBarcode.com
Data Matrix Drawer In Java
Using Barcode drawer for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
Writing to the server
Encoding UPCA In Java
Using Barcode generation for Java Control to generate, create GS1 - 12 image in Java applications.
www.OnBarcode.com
Matrix Barcode Encoder In Java
Using Barcode generator for Java Control to generate, create Matrix image in Java applications.
www.OnBarcode.com
return { type:"updateProperty", id:this.id, planetId:this.owner.id, field:this.field, value:this.value }.simpleXmlify("command"); } planets.commands.UpdatePropertyCommand.parseResponse=function(docEl){ var attrs=docEl.attributes; var status=attrs.getNamedItem("status").value; if (status!="ok"){ var reason=attrs.getNamedItem("message").value; alert("failed to update " +this.field+" to "+this.value +"\n\n"+reason); } }
Barcode Drawer In Java
Using Barcode creation for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Encoding Leitcode In Java
Using Barcode maker for Java Control to generate, create Leitcode image in Java applications.
www.OnBarcode.com
The command simply provides a unique ID for the command and encapsulates the parameters needed on the server. The toRequestString() function writes itself as a piece of XML, using a custom function that we have attached to the Object prototype:
QR Code 2d Barcode Maker In .NET
Using Barcode creator for .NET Control to generate, create QR Code JIS X 0510 image in .NET applications.
www.OnBarcode.com
QR Code JIS X 0510 Maker In None
Using Barcode maker for Online Control to generate, create QR Code image in Online applications.
www.OnBarcode.com
Object.prototype.simpleXmlify=function(tagname){ var xml="<"+tagname; for (i in this){ if (!this[i] instanceof Function){ xml+=" "+i+"=\""+this[i]+"\""; } } xml+="/>"; return xml; }
QR Code 2d Barcode Encoder In None
Using Barcode generation for Software Control to generate, create QR Code ISO/IEC18004 image in Software applications.
www.OnBarcode.com
Printing EAN 128 In Java
Using Barcode printer for Android Control to generate, create GTIN - 128 image in Android applications.
www.OnBarcode.com
This will create a simple XML tag like this (formatted by hand for clarity):
Creating Code 128A In None
Using Barcode creator for Software Control to generate, create Code 128 image in Software applications.
www.OnBarcode.com
Make European Article Number 13 In Java
Using Barcode generator for Android Control to generate, create EAN-13 Supplement 5 image in Android applications.
www.OnBarcode.com
<command type='updateProperty' id='001_diameter' planetId='mercury' field='diameter' value='3'/>
Decoding PDF 417 In Visual C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Scanning Barcode In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Note that the unique ID consists only of the planet ID and the property name. We can t send multiple edits of the same value to the server. If we do edit a property several times before the queue fires, each later value will overwrite earlier ones.
Encoding UPC Code In None
Using Barcode drawer for Software Control to generate, create UPC Symbol image in Software applications.
www.OnBarcode.com
Recognize UPC Symbol In Visual C#
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
The role of the server
Scanning UPC-A Supplement 2 In Visual Basic .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Universal Product Code Version A Printer In None
Using Barcode generator for Microsoft Word Control to generate, create GTIN - 12 image in Word applications.
www.OnBarcode.com
The POST data sent to the server will contain one or more of these tags, depending on the polling frequency and how busy the user is. The server process needs to process each command and store the results in a similar response. Our CommandQueue s onload will match each tag in the response to the Command object in the sent queue and then invoke that Command s parseResponse method. In this case, we are simply looking for a status attribute, so the response might look like this:
<commands> <command id='001_diameter' status='ok'/> <command id='003_albedo' status='failed' message='value out of range'/> <command id='004_hairColor' status='failed' message='invalid property name'/> </commands>
Mercury s diameter has been updated, but two other updates have failed, and a reason has been given in each case. Our user has been informed of the problems (in a rather basic fashion using the alert() function) and can take remedial action. The server-side component that handles these requests needs to be able to break the request data into commands and assign each command to an appropriate handler object for processing. As each command is processed, the result will be written back to the HTTP response. A simple implementation of a Java servlet for handling this task is given in listing 5.15.
Listing 5.15 CommandServlet.java
public class CommandServlet extends HttpServlet { private Map commandTypes=null; public void init() throws ServletException { ServletConfig config=getServletConfig(); commandTypes=new HashMap(); Configure handlers on startup boolean more=true; for(int counter=1;more;counter++){ String typeName=config.getInitParameter("type"+counter); String typeImpl=config.getInitParameter("impl"+counter); if (typeName==null || typeImpl==null){ more=false; }else{ try{ Class cls=Class.forName(typeImpl); commandTypes.put(typeName,cls); }catch (ClassNotFoundException clanfex){ this.log(
Writing to the server
"couldn't resolve handler class name " +typeImpl); } } } } protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws IOException{ resp.setContentType("text/xml"); Process a request Reader reader=req.getReader(); Writer writer=resp.getWriter(); try{ SAXBuilder builder=new SAXBuilder(false); Document doc=builder.build(reader); Process XML data Element root=doc.getRootElement(); if ("commands".equals(root.getName())){ for(Iterator iter=root.getChildren("command").iterator(); iter.hasNext();){ Element el=(Element)(iter.next()); String type=el.getAttributeValue("type"); XMLCommandProcessor command=getCommand(type,writer); if (command!=null){ Element result=command.processXML(el); Delegate to handler writer.write(result.toString()); } } }else{ sendError(writer, "incorrect document format - " +"expected top-level command tag"); } }catch (JDOMException jdomex){ sendError(writer,"unable to parse request document"); } }
private XMLCommandProcessor getCommand (String type,Writer writer) throws IOException{ Match handler to command XMLCommandProcessor cmd=null; Class cls=(Class)(commandTypes.get(type)); if (cls!=null){ try{ cmd=(XMLCommandProcessor)(cls.newInstance()); }catch (ClassCastException castex){ sendError(writer, "class "+cls.getName() +" is not a command");
The role of the server
} catch (InstantiationException instex) { sendError(writer, "not able to create class "+cls.getName()); } catch (IllegalAccessException illex) { sendError(writer, "not allowed to create class "+cls.getName()); } }else{ sendError(writer,"no command type registered for "+type); } return cmd; }` private void sendError (Writer writer,String message) throws IOException{ writer.write("<error msg='"+message+"'/>"); writer.flush(); } }
The servlet maintains a map of XMLCommandProcessor objects that are configured here through the ServletConfig interface b. A more mature framework might provide its own XML config file. When processing an incoming POST request c, we use JDOM to parse the XML data d and then iterate through the <command> tags matching type attributes to XMLCommandProcessors e. The map holds class definitions, from which we create live instances using reflection in the getCommand() method f. The XMLCommandProcessor interface consists of a single method:
public interface XMLCommandProcessor { 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.
Copyright © OnBarcode.com . All rights reserved.