Previous Section  < Day Day Up >  Next Section

Creating an XML-RPC Handler

Any Java class can take XML-RPC method calls, as long as it has public methods with return types and arguments that correspond properly with XML-RPC data types.

These classes are called XML-RPC handlers, because a server gives them the job of handling an incoming request.

Every public method of the class can be called over XML-RPC, as it is implemented by Apache XML-RPC, so you must keep this in mind when declaring methods.

The LottoCounter class in Listing 25.1 can handle XML-RPC calls. This class receives lottery results, storing them in an XML file named lotto.log.

This class and the rest of the hour's projects require XOM, the class library introduced in Hour 21. Download the library from the website http://www.cafeconleche.org/XOM, then add the file xom-1.0.jar to your Classpath environment variable (or the Classpath for this project).

Enter the text of the class and save it as LottoCounter.java. This program doesn't introduce any functionality that's new to this hour, putting to use the XML capabilities of the XOM class library.

Listing 25.1. The Full Text of LottoCounter.java

 1: import java.io.*;

 2: import java.util.*;

 3: import nu.xom.*;

 4:

 5: public class LottoCounter {

 6:     Document document;

 7:     Element root;

 8:     File logFile;

 9:

10:     public LottoCounter() throws IOException, ParsingException {

11:         Builder builder = new Builder();

12:         logFile = new File("lotto.log");

13:         if (!logFile.exists()) {

14:             // create a new XML document

15:             root = new Element("log");

16:             document = new Document(root);

17:             writeDocument();

18:         }

19:         // load the XML document from a file

20:         document = builder.build(logFile);

21:         root = document.getRootElement();

22:     }

23:

24:     public boolean sendResult(int plays, int win3, int win4, int win5, int win6)

25:         throws IOException, ParsingException {

26:

27:         // create the result element

28:         Element result = new Element("result");

29:

30:         // create subelements for each value

31:         Element playElement = new Element("plays");

32:         playElement.appendChild("" + plays);

33:         Element win3Element = new Element("win3");

34:         win3Element.appendChild("" + win3);

35:         Element win4Element = new Element("win4");

36:         win4Element.appendChild("" + win4);

37:         Element win5Element = new Element("win5");

38:         win5Element.appendChild("" + win5);

39:         Element win6Element = new Element("win6");

40:         win6Element.appendChild("" + win6);

41:         Element dateElement = new Element("date");

42:         Date now = new Date();

43:         dateElement.appendChild(now.toString());

44:

45:         // add subelements to result

46:         result.appendChild(playElement);

47:         result.appendChild(win3Element);

48:         result.appendChild(win4Element);

49:         result.appendChild(win5Element);

50:         result.appendChild(win6Element);

51:         result.appendChild(dateElement);

52:

53:         // add the result to the root

54:         root.appendChild(result);

55:

56:         // save the document

57:         writeDocument();

58:         return true;

59:    }

60:

61:    // write the document to a file

62:    private void writeDocument() throws IOException {

63:         FileOutputStream fileStream = new FileOutputStream(logFile);

64:         Serializer writer = new Serializer(fileStream);

65:         writer.setIndent(2);

66:         writer.write(document);

67:     }

68: }


This class, which can't be run directly because it lacks a main() method, has one public method called sendResult(). This method can be called to submit lottery results: the total number of drawings and the number of times that three, four, five or all six numbers matched the user's picks.

Though the constructor also is public, it cannot be called over XML-RPC.

Here's code that could test this class, submitting some results entered as command-line arguments:


public static void main(String[] arguments) {

    try {

        int plays = Integer.parseInt(arguments[0]);

        int win3 = Integer.parseInt(arguments[1]);

        int win4 = Integer.parseInt(arguments[2]);

        int win5 = Integer.parseInt(arguments[3]);

        int win6 = Integer.parseInt(arguments[4]);

        LottoCounter counter = new LottoCounter();

        counter.send(plays, win3, win4, win5, win6);

    } catch (Exception exception) {

        System.out.println("Error: " + exception.getMessage());

        exception.printStackTrace();

    }

}


Don't add this to the class—you'll be testing it in a matter of minutes over XML-RPC.

When this class receives lottery results, it assembles an XML file named lotto.log like the one in Listing 25.2. LottoCounter will create this file if it doesn't exist when the program first runs.

Listing 25.2. The Full Text of lotto.log

 1: <?xml version="1.0" encoding="UTF-8"?>

 2: <log>

 3:   <result>

 4:     <plays>3482</plays>

 5:     <win3>57</win3>

 6:     <win4>3</win4>

 7:     <win5>0</win5>

 8:     <win6>0</win6>

 9:     <date>Wed Jul 13 15:41:43 EDT 2005</date>

10:   </result>

11:   <result>

12:     <plays>779</plays>

13:     <win3>12</win3>

14:     <win4>0</win4>

15:     <win5>0</win5>

16:     <win6>0</win6>

17:     <date>Wed Jul 13 15:45:08 EDT 2005</date>

18:   </result>

19: </log>


This XML document has the root-level element log, which contains a result element for each submitted lottery run. The last submission in Listing 25.2 was from a user who conducted 779 lottery drawings, winning 3-out-of-6 12 times.

By the way

If I were to play the Florida Lotto that many times with that much success, I would have won from $12 to $18 after spending $770 on tickets. I'm beginning to think that "Win the Lottery" is not a fiscally sound retirement plan.


    Previous Section  < Day Day Up >  Next Section