|
JWS: Web Services in Java, by David Bau
WSJ Vol 02 Issue 04 - pg.40
Listing 1: An example purchase order
(SOAP envelope not shown)
<purchaseOrder>
<customerInformation>
<customerName>Acme Manufacturing
Inc</customerName>
<customerNumber>332</customerNumber>
</customerInformation>
<basket>
<lineitem>
<description>High Temperature
Hex Bolt #66</description>
<price>0.47</price>
<catalogNumber>17466</catalogNumber>
<quantity>220</quantity>
</lineitem>
<lineitem>
<description>High Temperature
Hex Nut #66</description>
<price>0.47</price>
<catalogNumber>18266</catalogNumber>
<quantity>200</quantity>
</lineitem>
</basket>
</purchaseOrder>
Listing 2: OrderingSystem.jws Web Service class OrderingSystem
{
/**
* @jws:operation
* @jws:parameter-xml xml-map::
* <purchaseOrder>
* <customerInformation>
* <customerNumber>{customerNo}</customerNumber>
* </customerInformation>
* <basket>
* <lineitem xm:multiple="item in items">
* <catalogNumber>{item.catNumber}</catalogNumber>
* <quantity>{item.quantity}</quantity>
* </lineitem>
* </basket>
* </purchaseOrder>
* ::
*/
public void submitOrder(int
customerNo, LineItems[] items)
{
// logic omitted for now
}
static public class LineItems
{
public int catNumber;
public int quantity;
}
}
Listing 3: ChargeCheck.jws Web Service class ChargeCheck
{
Callback callback;
/**
* @jws:control
*/
CustomerCreditDatabase creditDB;
/**
* @jws:operation
* @jws:conversation phase=start
* @jws:protocol jms-soap=true
* @jws:parameter-xml xml-map::
* <request-charge>
* <customer>{customerNo}</customer>
* <amount>{amount}</amount>
* </request-charge>
* ::
*/
public void processCharge
(int customerNo, double amount)
{
double currentLimit;
currentLimit = creditDB.
getLimit(customerNo);
if (amount < currentLimit)
{
creditDB.setLimit(customerNo,
currentLimit amount);
callback.sendResult(true);
}
else
{
callback.sendResult(false);
}
}
interface Callback
{
/**
* @jws:conversation
phase=finish
* @jws:protocol
jms-soap=true
* @jws:parameter-xml
xml-map::
* <charge allow="{allowed}"/>
* ::
*/
void sendResult
(boolean allowed);
}
}
Listing 4: CustomerCreditDatabase.ctrl
import weblogic.jws.control.DatabaseControl;
/** @jws:connection datasource-jndi-name="db.customercredit" */
interface CustomerCreditDatabase extends DatabaseControl
{
/**
* @jws:sql statement=
* "select curlimit from credit where custno = {num}"
*/
double getLimit(int num);
/**
* @jws:sql statement=
* "update credit set curlimit={amt} where custno = {num}"
*/
double setLimit(int num, double amount);
}
Listing 5: ChargeCheckControl.ctrl
import weblogic.jws.control.ServiceControl;
/**
* @jws:location http-url="http://checkserver/ChargeCheck.jws"
* @jws:protocol jms-soap=true http-soap=false
*/
interface ChargeCheckControl extends ServiceControl
{
/**
* @jws:parameter-xml xml-map::
* <request-charge>
* <customer>{customerNo}</customer>
* <amount>{amount}</amount>
* </request-charge>
* ::
*/
public void processCharge(int customerNo, double amount);
interface Callback
{
/**
* @jws:parameter-xml xml-map::
* <charge allow="{allowed}"/>
* ::
*/
void sendResult(boolean allowed);
}
}
Listing 6: OrderingSystem.jws (integrated)
class OrderingSystem
{
Callback callback;
/**
* @jws:control
*/
ChargeCheckControl chargeCheck;
/**
* @jws:operation
* @jws:conversation phase=start
* @jws:message-buffer enable=true
* @jws:input xml-map::
* <purchaseOrder>
* <customerInformation>
* <customerNumber>{customerNo}</customerNumber>
* </customerInformation>
* <basket>
* <lineitem xm:multiple="item in items">
* <catalogNumber>{item.catNumber}</catalogNumber>
* <quantity>{item.quantity}</quantity>
* <price>{item.price}</price>
* </lineitem>
* </basket>
* </purchaseOrder>
* ::
*/
public void submitOrder(int customerNo, LineItems[] items)
{
double amount = 0.0;
for (int i = 0; i < items.length; i++)
amount += items[i].price;
chargeCheck.processCharge(customerNo, amount);
}
void chargeCheck_sendResult(boolean ok)
{
if (ok) callback.sendAck();
else callback.sendError("Not enough credit.");
}
static public class LineItems
{
public int catNumber;
public int quantity;
public int price;
}
interface Callback
{
/**
* @jws:message-buffer enable=true retryDelay="3 min"
* @jws:conversation phase=finish
* @jws:output xml-map::
* <acknowledgement>
* <message>Purchase Order is OK</message>
* </acknowledgement>
* ::
*/
void sendAck();
/**
* @jws:message-buffer enable=true retryDelay="3 min"
* @jws:conversation phase=finish
* @jws:output xml-map::
* <error>
* <problem>{message}</problem>
* </error>
* ::
*/
void sendError(String problem);
}
}
|