Episode 11:Develop Simple Google Wave Robots using the WadRobotFramework 120
Since we are going to be writing a Wave Robot, we need some additional files on the client side. These additional files (JAR files) are required for the additional Wave API’s and also for deployment in your WEB-INF\lib folder, so that they are correctly deployed and available to the run-time engine. These JAR files do not ship along with the Google Eclipse plugin, so you will need to download them for a website. The Google code website for the JAR files is:
http://code.google.com/p/wave-robot-java-client/downloads/list
The web page when you navigate to the above URL is shown below:
Download all the above files to your machine.
The WadRobotFramework JAR file is available at the following location :
http://code.google.com/p/wadrobotframework/downloads/list
Once you have downloaded the files, follow these steps to setup your Project Build Path and runtime correctly.
1. Copy all the 5 JAR files to the WEB-INF\lib folder of your Eclipse Project. After copying you should see the files as shown in the project hierarchy below:
Episode 11:Develop Simple Google Wave Robots using the WadRobotFramework 122
2. Right-click on the Project in the Project Hierarchy. Select Properties and then Java Build Path. Click on Add JARs and then select the 5 JAR files from your Project WEB- INF\lib folder.
3. Your Project Build Path should like the screenshot below.
Click on OK to proceed. This completes your Build Path setup with the Google Wave Robot JAR and WadRobotFramework JAR files.
Writing the Simple Robot: MyAppenderRobot :
MyAppenderRobot.java
Let us first create our Simple Robot Java class based on the WadRobotFramework. This Simple Robot is the one that will react to a new Blip. So all we will do is to make the robot append to the blip, when the blip is submitted. The Robot demonstrated here will be straightforward and you can easily modify it to make it react in your own way.
As discussed, this is known as the BlipAppenderRobot and so all we need to do is to extend the BlipAppenderRobot class in the WadRobotFramework and provide our simple implementation.
The steps are straightforward and given below. All we need to do is write our class that extends the org.wadael.waverobotfrmwrk.simple.BlipAppenderRobot class and provide an implementation for the getTextToAppend method.
Follow these steps:
1. Create a new Java class within the same package. The New Java Class dialog is shown below. I have named the class MyAppenderRobot as shown below. Click on the
Episode 11:Develop Simple Google Wave Robots using the WadRobotFramework 124
2. In the Superclass Selection dialog shown below, type the word BlipAppenderRobot (some part of it is also OK as the screenshot shows below) in the Choose a type field as shown. This will bring up the correct Matching items i.e.
org.wadael.waverobotfrmwrk.simple.BlipAppenderRobot. Click on OK.
This will generate the code and you simply replace it with the following code listing given below:
packagecom.gaejexperiments.waverobot;
importorg.wadael.waverobotfrmwrk.simple.BlipAppenderRobot;
publicclassMyAppenderRobot extendsBlipAppenderRobot {
@Override
return"I am appending some text too"; }
@Override
protectedString getRobotSelfIntroduction() { return"I am the Appender Robot";
}
}
Let us go through the code now:
1. We have extended the BlipAppenderRobot since all we want to do in this Robot is to react to the blip and append our own Blip
2. All we need to do as part of extending the BlipAppenderRobot class is to implement the getTextToAppend method. This method gets passed one parameter msg that is the blip text that was submitted. So in all essence, you could inspect what the text was in the blip submitted and then react to it. The implementation simply returns the string that it wants to append. The WadRobotFramework’s BlipAppenderRobot will take care of creating a Blip and appending it to the current blip.
3. We also implement an optional method named getRobotSelfIntroduction. This method returns a string and you can return something that identifies the addition of your Robot to the Wave. So when your Robot is added as a participant and if you have
implemented the getRobotSelfIntroduction method, then it will display this message out. It is sort of announcing to the other wave participants about your presence.
That is all there is to implemented the MyAppenderRobot. If you would have studied the earlier tutorial on writing a Google Wave Robot, you would have noticed that the
WadRobotFramework has done away with all the Event processing that you had to handle yourself and also shielded you from methods that you need to know to create a blip and append to it.
Configuring the MyAppenderRobot in web.xml
We need to add the MyAppenderRobot in the <servlet/> and <servlet-mapping/>
entry to the web.xml file. This file is present in the WEB-INF folder of the project. The necessary fragment to be added to your web.xml file are shown below.
<servlet> <servlet-name>MyAppenderRobot</servlet-name> <servlet-class>com.gaejexperiments.waverobot.MyAppenderRobot</servlet- class> </servlet> <servlet-mapping> <servlet-name>MyAppenderRobot</servlet-name> <url-pattern>/_wave/robot/jsonrpc</url-pattern>
Episode 11:Develop Simple Google Wave Robots using the WadRobotFramework 126
</servlet-mapping>
In the above fragment, you will note that url-pattern /_wave/robot/jsonrpc has to be mapped to the Robot Servlet that you have written. This is because the Google Wave system will invoke this url to communicate with your Robot using its protocol.