王玲,余秋樺
(廣東郵電職業(yè)技術(shù)學(xué)院計(jì)算機(jī)系,廣東廣州510630)
用XFire框架實(shí)現(xiàn)W ebService的研究與實(shí)踐
王玲,余秋樺
(廣東郵電職業(yè)技術(shù)學(xué)院計(jì)算機(jī)系,廣東廣州510630)
XFire框架通過(guò)提供簡(jiǎn)單的API支持WebService各項(xiàng)標(biāo)準(zhǔn)協(xié)議,從而方便快速地開發(fā)WebService應(yīng)用。XFire作為WebService的實(shí)現(xiàn)框架,它內(nèi)部將功能各異的接口抽象成了具有共同行為和屬性的服務(wù)。有兩種方式將POJO發(fā)布成Web服務(wù)。一種方式是直接使用Web服務(wù)接口和Web服務(wù)實(shí)現(xiàn)類來(lái)發(fā)布;另一種方式是基于JSR181標(biāo)準(zhǔn)和注釋技術(shù)將被注釋的POJO發(fā)布成Web服務(wù)。
XFire;WebService;POJO
XFire是codeHaus組織提供的一個(gè)開源框架,主要功能就是支持將簡(jiǎn)單java對(duì)象(POJO)通過(guò)簡(jiǎn)單的方式發(fā)布成WebService,不僅充分發(fā)揮了POJO的作用,還簡(jiǎn)化了Java應(yīng)用轉(zhuǎn)化為WebService的步驟和過(guò)程。
在xfire中是用org.codehaus.xfire.service.Service這個(gè)類來(lái)表示抽象出來(lái)的結(jié)果,如圖1所示中可以看到其主要實(shí)現(xiàn)了兩個(gè)接口:Visitable和HandlerSupport,繼承了一個(gè)類AbstractContext。
圖1 XFire框架基本結(jié)構(gòu)圖
Visitable接口就是可以被外界的系統(tǒng)訪問。AbstractContext抽象類,其內(nèi)部是一個(gè)map類,并提供了相應(yīng)的setter、getter、remove方法來(lái)操作上下文信息。xfire將客戶端、服務(wù)端的各種操作抽象為Handler,并且Handler除了必備的invoke外,還額外定義了其他幾個(gè)屬性:
1)getAfter:返回handler執(zhí)行后要進(jìn)行的動(dòng)作。
2)handleFault:處理在handler執(zhí)行過(guò)程中發(fā)生的錯(cuò)誤。
3)Phase:這個(gè)handler所處于的階段,每個(gè)phase有優(yōu)先級(jí)的定義。
4)getBefore:返回handler執(zhí)行前要進(jìn)行的動(dòng)作。
5)role:表示這個(gè)服務(wù)應(yīng)用于那些角色。
XFire將各種handler又劃分為3類:
6)FaultHandler發(fā)生異常時(shí)的各項(xiàng)操作,主要是FaultSender通過(guò)輸出channel傳輸信息;CustomFault-Handler從異常信息中構(gòu)建一個(gè)定制化的詳情。
7)InHandler服務(wù)端進(jìn)行的各項(xiàng)操作,主要是ServiceInvocationHandler獲取傳入的參數(shù)(InMessage)、執(zhí)行service、創(chuàng)建返回結(jié)果(OutMessage);PostInvocationHandler發(fā)送結(jié)果到客戶端。
8)OutHandler客戶端進(jìn)行的各項(xiàng)操作,主要是OutMessageSender根據(jù)輸出渠道(Channel)發(fā)送消息。
Service給出了一個(gè)服務(wù)的整體模板。對(duì)于一個(gè)具體的服務(wù)所表示的接口及其所具備的方法功能,都存放在ServiceInfo這個(gè)類中,通過(guò)反射的方式將接口中方法信息類信息都進(jìn)行了存放,供使用。功能的實(shí)現(xiàn)主要包括了兩類:客戶端的輸出和服務(wù)端的輸入處理及輸出。
2.1服務(wù)器端配置文件
XFire的配置web.xml:
<?xm l version="1.0"encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/
javaee"xm lns:xsi="http://www.w3.org/2001/XMLSchema -instance"version="2.5"xsi:schemaLocation="http:// java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/
javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http. XFireConfigurableServlet
</servlet-class>
<load-on-startup>0</load-on-startup></servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
XFire默認(rèn)加載services.xml:
<beans xmlns="http://xfire.codehaus.org/config/1. 0">
<service>
<name>driver</name>
<serviceClass>接口名</serviceClass>
<implementationClass>
實(shí)現(xiàn)類名
</implementationClass>
<style>wrapped</style>
<use>literal</use>
<scope>application</scope>
</service>
</beans>
2.2服務(wù)器端實(shí)現(xiàn)類實(shí)例
public class Driver implements IDriver{
private String key="";
//注冊(cè)用戶
public String registerUser(String userInfoRequest){
boolean isOK=true;
String returnData="";
JSONObject json=null;
try{
json=JSONObject.fromObject(userInfoRequest);
if(!String.valueOf(json.get("key")).equals(key)){
isOK=false;
returnData="系統(tǒng)驗(yàn)證錯(cuò)誤";
}
}catch(Exception e){
isOK=false;
returnData="請(qǐng)求格式錯(cuò)誤";
}
if(isOK){
HashMap<String,String>map=new HashMap<String,String>();
try{
String returnCode=DriverOP.setNewUser(userInfoRequest);
map.put("returnCode",returnCode);
if(returnCode.equals("1")){
map.put("returnData","注冊(cè)成功");
}else if(returnCode.equals("2")){
map.put("returnData","用戶名已經(jīng)存在");
}else if(returnCode.equals("0")){
map.put("returnData","注冊(cè)不成功");
}
Gson gson=new Gson();
returnData=gson.toJson(map,
new TypeToken<HashMap<String,String>>(){
}.getType());
}catch(Exception e){
returnData="請(qǐng)求格式錯(cuò)誤";
}
}
return returnData;
}
}
2.3and roid客戶端實(shí)例
public class SoapRequest implements Runnable{
private static final String ADDRESS_URL="http://192.168.1.41:3366/Company/services/manager";
private static final String SERVER_NAME_SPACE ="http://entry.company.com";
private static SoapRequest soapRequest;
private SoapHandler soapHandler;
private static final int SEND_MESSAGE=1;
private static final int QUEIT_MESSAGE=2;
private static ContextmContext;
public static void InitSoap(Context context){
mContext=context;
if(soapRequest==null){
soapRequest=new SoapRequest();
new Thread(soapRequest).start();
}
}
public static SoapRequest getInstance(){
return soapRequest;
}
public void SendMsg(RequestInfo requestInfo){
if(soapHandler!=null){
Messagemsg=new Message();
msg.what=SEND_MESSAGE;
msg.obj=requestInfo;
soapHandler.sendMessage(msg);
}
}
public void exit(){
if(soapHandler!=null){
soapHandler.sendEmptyMessage(QUEIT_MESSAGE);
}}
@Override
public void run(){
//TODO Auto-generated method stub
Looper.prepare();
soapHandler=new SoapHandler();
Looper.loop();
Log.d("SoapRequest","exit soap thread");
}
private class SoapHandler extends Handler{
@Override
public void handleMessage(Messagemsg){
//TODO Auto-generatedmethod stub
super.handleMessage(msg);
switch(msg.what){
case SEND_MESSAGE:
RequestInfo requestInfo=(RequestInfo)msg.obj;
RequestResultInfo requestResultInfo=Request(requestInfo);
Intent intent=new Intent();
intent.setAction(requestResultInfo.ReceiverBroadcast);
mContext.sendBroadcast(intent);
break;
case QUEIT_MESSAGE:
this.getLooper().quit();
break;
default:
break;
}
}
}
private RequestResultInfo Request(RequestInfo requestInfo){
SoapObject request=new SoapObject(SERVER_NAME_SPACE,requestInfo.RequestMethod);
request.addProperty(requestInfo.RequestProperty,requestInfo.RequestJson);
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
new MarshalBase64().register(envelope);
HttpTransportSE transport=new HttpTransportSE(ADDRESS_URL);
transport.debug=true;
String result=null;
RequestResultInfo requestResultInfo=new RequestResultInfo();
requestResultInfo.ReceiverBroadcast=requestInfo. ReceiverBroadcast;
try{
transport.call(null,envelope);
SoapObjectobject=(SoapObject)envelope.bodyIn;
result=object.getProperty(0).toString();;
requestResultInfo.Result=result;
}catch(IOException e){
//TODO Auto-generated catch block
e.printStackTrace();
}catch(Xm lPullParserException e){
//TODO Auto-generated catch block
e.printStackTrace();
}
return requestResultInfo;
}
}
XFire框架中,以上是直接使用Web服務(wù)接口和Web服務(wù)實(shí)現(xiàn)類來(lái)實(shí)現(xiàn)將POJO發(fā)布成Web服務(wù);另一種方式是基于JSR181標(biāo)準(zhǔn)和注釋技術(shù)將被注釋的POJO發(fā)布成Web服務(wù)。客戶端是以android為例。這是個(gè)服務(wù)器和客戶端連接的實(shí)際案例。
[1]胡強(qiáng).Web服務(wù)流程的結(jié)構(gòu)范式及其判定算法[J].計(jì)算機(jī)學(xué)報(bào),2015(1):178-190.
[2]張威.基于Xfire實(shí)現(xiàn)跨平臺(tái)的Flex消息訂閱服務(wù)[J].湖北工業(yè)大學(xué)學(xué)報(bào),2015(1):39-42.
[3]白光佩.中通供應(yīng)商業(yè)務(wù)系統(tǒng)的Android手機(jī)客戶端設(shè)計(jì)與實(shí)現(xiàn)[D].濟(jì)南:山東大學(xué),2015.
[4]龔瑞琴,畢利.基于Web Service的Android技術(shù)應(yīng)用研究[J].電子技術(shù)應(yīng)用,2014(1):134-136.
[5]王曉禹,石麗.基于JSON實(shí)現(xiàn)Android智能終端與Web服務(wù)器“面向?qū)ο蟆钡男畔⒔粨Q[J].數(shù)字技術(shù)與應(yīng)用,2012(4):224-225.
Research and practiceon using XFire to achieveW ebService
WANG Ling,YU Qiu-hua
(Computer DepartmentofGuangdong VocationalCollegeofPostand Telecom,Guangzhou,Guangdong,China 510630)
XFire provides simple API for supporting standard protocols of WebService so as to rapidly develop WebService applications.Being the implementation framework ofWebService,XFiremakes the different interfaces into serviceswhich have joint conductand attribute.There're twomethods for publishing POJO toweb service,usingweb service interface and implementation classor JSR181 standard and annotation technology.
XFire;WebService;POJO
10.3969/j.issn.2095-7661.2016.03.010】
TP393.09
A
2095-7661(2016)03-0035-03
2016-06-27
王玲(1971-),女,廣東廣州人,廣東郵電職業(yè)技術(shù)學(xué)院講師,碩士,研究方向:計(jì)算機(jī)應(yīng)用開發(fā)、軟件教育。
廣東大學(xué)生科技創(chuàng)新培育專項(xiàng)資金項(xiàng)目(項(xiàng)目編號(hào):pdjh2015b0726)。
湖南郵電職業(yè)技術(shù)學(xué)院學(xué)報(bào)2016年3期