Axis2可以通過模塊(Module)進(jìn)行擴(kuò)展。Axis2模塊至少需要有兩個(gè)類,這兩個(gè)類分別實(shí)現(xiàn)了Module和Handler接口。開發(fā)和使用一個(gè)Axis2模塊的步驟如下:
1. 編寫實(shí)現(xiàn)Module接口的類。Axis2模塊在進(jìn)行初始化、銷毀等動(dòng)作時(shí)會(huì)調(diào)用該類中相應(yīng)的方法)。
2. 編寫實(shí)現(xiàn)Handler接口的類。該類是Axis2模塊的業(yè)務(wù)處理類。
3. 編寫module.xml文件。該文件放在META-INF目錄中,用于配置Axis2模塊。
4. 在axis2.xml文件中配置Axis2模塊。
5. 在services.xml文件中配置Axis2模塊。每一個(gè)Axis2模塊都需要使用<module>元素引用才能使用。
6. 發(fā)布Axis2模塊。需要使用jar命令將Axis2模塊壓縮成.mar包(文件擴(kuò)展名必須是.mar),然后將.mar文件放在
<Tomcat安裝目錄>\webapps\axis2\WEB-INF\modules目錄中。
先來編寫一個(gè)WebService類,代碼如下:
package service;
public class MyService
{
public String getGreeting(String name)
{
return "您好 " + name;
}
}
下面我們來編寫一個(gè)記錄請(qǐng)求和響應(yīng)SOAP消息的Axis2模塊。當(dāng)客戶端調(diào)用WebService方法時(shí),該Axis2模塊會(huì)將請(qǐng)求和響應(yīng)SOAP消息輸出到Tomcat控制臺(tái)上。
第1步:編寫LoggingModule類
LoggingModule類實(shí)現(xiàn)了Module接口,代碼如下:
package module;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.modules.Module;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;
public class LoggingModule implements Module
{
// initialize the module
public void init(ConfigurationContext configContext, AxisModule module)
throws AxisFault
{
System.out.println("init");
}
public void engageNotify(AxisDescription axisDescription) throws AxisFault
{
}
// shutdown the module
public void shutdown(ConfigurationContext configurationContext)
throws AxisFault
{
System.out.println("shutdown");
}
public String[] getPolicyNamespaces()
{
return null;
}
public void applyPolicy(Policy policy, AxisDescription axisDescription)
throws AxisFault
{
}
public boolean canSupportAssertion(Assertion assertion)
{
return true;
}
}
在本例中LoggingModule類并沒實(shí)現(xiàn)實(shí)際的功能,但該類必須存在。當(dāng)Tomcat啟動(dòng)時(shí)會(huì)裝載該Axis2模塊,同時(shí)會(huì)調(diào)用LoggingModule類的init方法,并在Tomcat控制臺(tái)中輸出“init”。
第2步:編寫LogHandler類
LogHandler類實(shí)現(xiàn)了Handler接口,代碼如下:
package module;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class LogHandler extends AbstractHandler implements Handler
{
private static final Log log = LogFactory.getLog(LogHandler.class);
private String name;
public String getName()
{
return name;
}
public InvocationResponse invoke(MessageContext msgContext)
throws AxisFault
{
// 向Tomcat控制臺(tái)輸出請(qǐng)求和響應(yīng)SOAP消息
log.info(msgContext.getEnvelope().toString());
return InvocationResponse.CONTINUE;
}
public void revoke(MessageContext msgContext)
{
log.info(msgContext.getEnvelope().toString());
}
public void setName(String name)
{
this.name = name;
}
}
LogHandler類的核心方法是invoke,當(dāng)使用該Axis2模塊的WebService的方法被調(diào)用時(shí),LogHandler類的invoke方法被調(diào)用。
第3步:編寫module.xml文件
在META-INF目錄中建立一個(gè)module.xml文件,內(nèi)容如下:
<module name="logging" class="module.LoggingModule">
<InFlow>
<handler name="InFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</InFlow>
<OutFlow>
<handler name="OutFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</OutFlow>
<OutFaultFlow>
<handler name="FaultOutFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</OutFaultFlow>
<InFaultFlow>
<handler name="FaultInFlowLogHandler" class="module.LogHandler">
<order phase="loggingPhase"/>
</handler>
</InFaultFlow>
</module>
第4步:在axis2.xml文件中配置Axis2模塊
打開axis2.xml文件,分別在如下四個(gè)<phaseOrder>元素中加入<phase name="loggingPhase"/>:
<phaseOrder type="InFlow">
<phase name="soapmonitorPhase"/>
<phase name="loggingPhase"/>
</phaseOrder>
<phaseOrder type="OutFlow">
<phase name="Security"/>
<phase name="loggingPhase"/>
</phaseOrder>
<phaseOrder type="InFaultFlow">
<phase name="soapmonitorPhase"/>
<phase name="loggingPhase"/>
</phaseOrder>
<phaseOrder type="OutFaultFlow">
<phase name="Security"/>
<phase name="loggingPhase"/>
</phaseOrder>
第5步:在services.xml文件中引用logging模塊
services.xml文件的內(nèi)容如下:
<service name="myService">
<description>
使用logging模塊
</description>
<!-- 引用logging模塊 -->
<module ref="logging"/>
<parameter name="ServiceClass">
service.MyService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
第6步:發(fā)布logging模塊
到現(xiàn)在為止,我們應(yīng)用可以建立兩個(gè)發(fā)行包:logging.mar和service.aar。其中logging.mar文件是Axis2模塊的發(fā)行包,該包的目錄結(jié)構(gòu)如下:
logging.mar
module\LoggingModule.class
module\LogHandler.class
META-INF\module.xml
service.aar文件是本例編寫的WebService發(fā)行包,該包的目錄結(jié)構(gòu)如下:
service.aar
service\MyService.class
META-INF\services.xml
將logging.mar文件放在<Tomcat安裝目錄>\webapps\axis2\WEB-INF\modules目錄中,將service.aar文件放在<Tomcat安裝目錄>\webapps\axis2\WEB-INF\services目錄中。要注意的是,如果modules目錄中包含了modules.list文件,Axis2會(huì)只裝載在該文件中引用的Axis2模塊,因此,必須在該文件中引用logging模塊,該文件的內(nèi)容如下:
addressing-1.4.1.mar
soapmonitor-1.4.1.mar
ping-1.4.1.mar
mex-1.4.1.mar
axis2-scripting-1.4.1.mar
logging.mar
如果modules目錄中不包含modules.list文件,則Axis2會(huì)裝載modules文件中的所有Axis2模塊。
現(xiàn)在啟動(dòng)Tomcat,使用如下的C#代碼調(diào)用MyService的getGreeting方法則會(huì)在Tomcat控制臺(tái)中輸出相應(yīng)的請(qǐng)求和響應(yīng)SOAP消息。
// async是引用MyService的服務(wù)名
async.myService my = new WSC.asyn.myService();
MessageBox.Show(my.getGreeting("中國"));
MessageBox.Show("完成調(diào)用");
在執(zhí)行上面的代碼后,在Tomcat控制臺(tái)中輸出的信息如下圖所示。