Creating an XML-RPC Client

An XML-RPC server can be called over the Internet, over a network, or within a single computer. Any web server that has a URL, a unique web address, can be accessed with the protocol. The LottoClient class will extend XML-RPC functionality to LottoMadness, the lottery-testing app created in Hour 15. A client can be implemented with either the XmlRpcClient class in the org.apache.xmlrpc.applet package (for applets) or the SimpleXmlRpcClient class in org.apache.xmlrpc.applet (for other classes). The reason to split them in this manner is security. Applets run a restricted environment that prohibits some capabilities of the Java language, such as making network connections to any computer other than the one hosting the applet. LottoClient works with XmlRpcClient, connecting to a server at the URL http://localhost:4404:

String server = "http://localhost";
int port = 4404;
XmlRpcClient client = new XmlRpcClient(server + ":" + port);


The client's execute() method submits an XML-RPC request to the designated server:

Boolean result = (Boolean) client.execute("lotto.sendResult", params);


The first method argument identifies the XML-RPC method: lotto.sendResult. When this is called, the sendResult() method of the XML-RPC handler class, LottoCounter, will be executed. The second holds a Vector object with all of the method arguments, in order. Vectors, a data structure that functions as an array that can change in size, were covered in Hour 12, "Making the Most of Existing Objects." The sendResult() method takes six integer arguments:

Vector params = new Vector();
params.add(plays);
params.add(win3);
params.add(win4);
params.add(win5);
params.add(win6);
Boolean result = (Boolean) client.execute("lotto.sendResult", params);


When the XML-RPC method returns an object, the execute() method returns this object, which must be converted using casting. When the method returns a basic data type, the matching class is returned. So a boolean value returned by sendResult() becomes a Boolean object when returned by execute(). The LottoClient class has a send() method that submits XML-RPC requests to the lottery server:

LottoClient lc = new LottoClient();
lc.send(plays, win3, win4, win5, win6);


Enter the text of Listing 25.4 and save the file as LottoClient.java when you're done.

Listing 25.4. The Full Text of LottoClient.java
 1: import java.io.*;
 2: import java.net.*;
 3: import java.util.*;
 4: import org.apache.xmlrpc.*;
 5:
 6: public class LottoClient {
 7: XmlRpcClient client;
 8: String server = "http://localhost";
 9: int port = 4404;
10:
11: public LottoClient() {
12: try {
13: client = new XmlRpcClient(server + ":" + port);
14: } catch (MalformedURLException exception) {
15: System.out.println("Bad URL: " + server + ":" + port);
16: }
17: }
18:
19: public void send(int plays, int win3, int win4, int win5, int win6)
20: throws IOException, XmlRpcException {
21:
22: Vector<Integer> params = new Vector<Integer>();
23: params.add(plays);
24: params.add(win3);
25: params.add(win4);
26: params.add(win5);
27: params.add(win6);
28: Boolean result = (Boolean) client.execute("lotto.sendResult", params);
29: }
30:
31: public static void main(String[] arguments) {
32: try {
33: int plays = Integer.parseInt(arguments[0]);
34: int win3 = Integer.parseInt(arguments[1]);
35: int win4 = Integer.parseInt(arguments[2]);
36: int win5 = Integer.parseInt(arguments[3]);
37: int win6 = Integer.parseInt(arguments[4]);
38: LottoClient lc = new LottoClient();
39: lc.send(plays, win3, win4, win5, win6);
40: } catch (Exception exception) {
41: System.out.println("Error: " + exception.getMessage());
42: exception.printStackTrace();
43: }
44: }
45: }


Compile and run the app with five arguments—the number of drawings, the number of 3-of-6 wins, 4-of-6 wins, 5-of-6 wins, and grand prize "I'm rich! I'm rich!" 6-of-6 jackpots:

java LottoClient 725 10 8 1 0


Try this out, then open the lotto.log file. You should see a new result element near the bottom of the document with the submitted results:

<result>
 <plays>725</plays>
 <win3>10</win3>
 <win4>8</win4>
 <win5>1</win5>
 <win6>0</win6>
 <date>Thu Jul 14 00:46:26 EDT 2005</date>
</result>


      
Comments