2006/11/04

Consuming Calculator.java WS

A well-known example, which is included in /Developer/ADC Reference Library/documentation/WebObjects/Web_Services/Web_Services.tar.gz, is a simple Calculator. To publish it is as simple as compile and run... however, to consume it inside Cocoa requires some extra knowledge; let us focus on the add method —the rest is completely analogous.

Once the service is running, the first step is to produce the "stubs"; this can be done using the command-line argument
Szamitogep# /Developer/Tools/WSMakeStubs
-x ObjC
-name Calculator
-url http://localhost:4210/WebObjects/
Calculator.woa/ws/Calculator?wsdl

which generates two objects: WSGeneratedObj.m and Calculator.m

Include these objects in a new Cocoa App project. Run Interface Builder by double-clicking the MainMenu.nib icon and, in the classes pane, create a new object, say CPO, with an NSForm *form outlet and an add: action. Create files, instantiate and connect. (of course I am supposing that you know how to do these =:o)

In the implementation of the -add: method, write the obvious line:
[[form cellAtIndex:2]
setDoubleValue:[[CalculatorService
add:[[form cellAtIndex:0] doubleValue]
in_addend2:[[form cellAtIndex:1] doubleValue]]
doubleValue]];

compile and run... you see?
It does not work!!

The problem is in the implementation of the -resultValue method of the object add:WSGeneratedObj (defined in Calculator.m, and generated by WSMakeStubs). If you change that implementation line from
return ... objectForKey: @"addReturn"];
to
return ... objectForKey: @"ns1:addReturn"];
it will work...

But, how to find out the correct key to use?

Simple... just before the return line of the -getResultDictionary method, inside WSGeneratedObj.m, add the line
NSLog(@"getResultDictionary>fResult = %@",fResult);
and bingo!

Finally, just for the record: I used this service to add 3.4 to 6.6... can you guess the result? Wrong! Sadly, my computer, a G4 Mac mini, does not know how to add... it brings as a result
9.999998099999999