ax.rmi.PortableRemoteObject.narrow(objref, BookServiceFacadeHome.class);
System.out.println("call jboss======>>");
Collection result=facadeHome.create().findByCategory(category);//调用业务方法
System.out.println("get result======>>");
System.out.println(result.size());
// 使用BeanToSOAPEngine把调用结果转换成SOAP消息
otd.init(result,"GetAllBooks");
SOAPMessage ret=otd.getResult();
return ret;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
…
用JAXM开发Web服务(7)
时间:2011-02-07 IBM 陈亚强
可以看出,XMLBusinessDelegate只是调用EJB组件的业务方法,然后把构建SOAP消息的任务交给BeanToSOAPEngine,BeanToSOAPEngine是负责把包含了BookVO 的Collection转换成SOAP消息的专门的类。BeanToSOAPEngine的部分代码如例程9所示。
例程9 BeanToSOAPEngine的部分代码
…
public class BeanToSOAPEngine implements OTDEngine
{
Collection bookVos;//要处理的信息
SOAPMessage msg;//待返回的消息
String type;//type为返回消息的名字空间,如GetBookByCategory
public BeanToSOAPEngine()
{
try
{
MessageFactory mf = MessageFactory.newInstance();//获得MessageFactory的实例
msg = mf.createMessage();//从MessageFactory建立一个空的Message
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void init()
{
this.bookVos=c;
this.type=type;
}
public SOAPMessage getResult()
{
build();
return msg;
}
public void build()
{
try
{
SOAPPart part = msg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
//创建body名字空间
Name bodyName=envelope.createName(type,"books","http://hellking.webservice.com" );
SOAPBodyElement books=body.addBodyElement(bodyName);//增加body
//以下程序把Collection中的BookVO转化成SOAP消息
Iterator it=bookVos.iterator();
while(it.hasNext())
{
BookVO bookvo=(BookVO)it.next();
//构建book
Name bookName=envelope.createName(
"book","books","http://hellking.webservice.com");
SOAPElement book=books.addChildElement(bookName);
book.addAttribute(envelope.createName("id"),bookvo.getIsbn());
//构建name
Name elName=envelope.createName(
"name","books","http://hellking.webservice.com");
book.addChildElement(elName).addTextNode(bookvo.getName());
//构建publisher
Name elPublisher=envelope.createName(
"publisher","books","http://hellking.webservice.com");
book.addChildElement(elPublisher).addTextNode(bookvo.getPublisher());
//构建price
Name elPrice=envelope.createName(
"price","books","http://hellking.webservice.com");
book.addChildElement(elPrice).addTextNode(
new Float(bookvo.getPrice()).toString());
//构建category
Name elCategory=envelope.createName(
"categ
|