加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService大讲堂之Axis2(9):编写Axis2模块(Module)

发布时间:2020-12-16 23:56:45 所属栏目:安全 来源:网络整理
导读:Axis2 可以通过模块( Module )进行扩展。 模块至少需要有两个类,这两个类分别实现了 和 Handler 接口。开发和使用一个 模块的步骤如下: 1.? 编写实现 Module 接口的类。 Axis2 模块在进行初始化、销毁等动作时会调用该类中相应的方法)。 2.? 编写实现 H
Axis2 可以通过模块( Module )进行扩展。 模块至少需要有两个类,这两个类分别实现了 Handler 接口。开发和使用一个 模块的步骤如下:
1.? 编写实现Module 接口的类。Axis2 模块在进行初始化、销毁等动作时会调用该类中相应的方法)。
2.? 编写实现Handler 接口的类。该类是Axis2 模块的业务处理类。
3.? 编写module.xml 文件。该文件放在META-INF 目录中,用于配置Axis2 模块。
4.? axis2.xml 文件中配置Axis2 模块。
5.? services.xml 文件中配置Axis2 模块。每一个Axis2 模块都需要使用<module> 元素引用才能使用。
6.? 发布Axis2 模块。需要使用jar 命令将Axis2 模块压缩成.mar 包(文件扩展名必须是.mar ),然后将.mar 文件放在
<Tomcat 安装目录>webappsaxis2WEB-INFmodules 目录中。????
??? 先来编写一个WebService类,代码如下:
package ?service;

public ? class ?MyService
{
????
?String?getGreeting(String?name)
????{
????????
return " 您好? + ?name;
????}
}
???? 下面我们来编写一个记录请求和响应SOAP 消息的Axis2 模块。当客户端调用WebService 方法时,该Axis2 模块会将请求和响应SOAP 消息输出到Tomcat 控制台上。
1步:编写LoggingModule
????LoggingModule 类实现了Module 接口,代码如下:
?module;

import ?org.apache.axis2.AxisFault;
?org.apache.axis2.context.ConfigurationContext;
?org.apache.axis2.description.AxisDescription;
?org.apache.axis2.description.AxisModule;
?org.apache.axis2.modules.Module;
?org.apache.neethi.Assertion;
?org.apache.neethi.Policy;

?LoggingModule? implements ?Module
{
????
// ?initialize?the?module
???? void ?init(ConfigurationContext?configContext,?AxisModule?module)
????????????
throws ?AxisFault
????{
????????System.out.println(
init );
????}
????
?engageNotify(AxisDescription?axisDescription)? ?AxisFault
????{
????}
????
?shutdown?the?module ?shutdown(ConfigurationContext?configurationContext)
????????????
shutdown ?String[]?getPolicyNamespaces()
????{
????????
null ;
????}
????
?applyPolicy(Policy?policy,?AxisDescription?axisDescription)
????????????
boolean ?canSupportAssertion(Assertion?assertion)
????{
????????
true ;
????}
}
???? 在本例中LoggingModule 类并没实现实际的功能,但该类必须存在。当Tomcat 启动时会装载该Axis2 模块,同时会调用LoggingModule 类的init 方法,并在Tomcat 控制台中输出“init ”。
2步:编写LogHandler
????LogHandler 类实现了Handler 接口,代码如下:
?org.apache.axis2.context.MessageContext;
?org.apache.axis2.engine.Handler;
?org.apache.axis2.handlers.AbstractHandler;
?org.apache.commons.logging.Log;
?org.apache.commons.logging.LogFactory;

?LogHandler? extends ?AbstractHandler? ?Handler
{
????
private static final ?Log?log? = ?LogFactory.getLog(LogHandler. );
????
?String?name;
????
?String?getName()
????{
????????
?name;
????}
????
?InvocationResponse?invoke(MessageContext?msgContext)
????????????
?AxisFault
????{
????????
??向Tomcat控制台输出请求和响应SOAP消息 ????????log.info(msgContext.getEnvelope().toString());
????????
?InvocationResponse.CONTINUE;
????}
????
?revoke(MessageContext?msgContext)
????{
????????log.info(msgContext.getEnvelope().toString());
????}
????
?setName(String?name)
????{
????????
this .name? ????LogHandler 类的核心方法是invoke ,当使用该Axis2 模块的WebService 的方法被调用时,LogHandler 类的invoke 方法被调用。????

3步:编写module.xml文件????
???? 在META-INF目录中建立一个module.xml文件,内容如下:
< module? name ="logging" ?class ="module.LoggingModule" >
????
InFlow
????????
handler? ="InFlowLogHandler" ="module.LogHandler"
????????????
order? phase ="loggingPhase" /> </ handler OutFlow ="OutFlowLogHandler" ?
????????


????
OutFaultFlow ="FaultOutFlowLogHandler" InFaultFlow ="FaultInFlowLogHandler" module
4步:在axis2.xml文件中配置Axis2模块
???? 打开axis2.xml 文件,分别在如下四个<phaSEOrder> 元素中加入 <phase name="loggingPhase"/>
phaSEOrder? type ="InFlow"
????

?


??? < phase? ="soapmonitorPhase" phaSEOrder ="OutFlow"
????

?


????
="Security" ="InFaultFlow" ="OutFaultFlow" 5步:在services.xml文件中引用logging模块
??? services.xml 文件的内容如下:
service? ="myService" description
????????使用logging模块
????
<!-- ??引用logging模块?? --> ref ="logging" parameter? ="ServiceClass"
????????service.MyService???
????
parameter messageReceivers messageReceiver? mep ="http://www.w3.org/2004/08/wsdl/in-out"
????????????class
="org.apache.axis2.rpc.receivers.RPCMessageReceiver" service 6步:发布logging模块
???? 到现在为止,我们应用可以建立两个发行包:logging.mar service.aar 。其中logging.mar 文件是Axis2 模块的发行包,该包的目录结构如下:
logging.mar
????moduleLoggingModule.class
????moduleLogHandler.class
????META-INFmodule.xml
????service.aar 文件是本例编写的WebService 发行包,该包的目录结构如下:
service.aar
????serviceMyService.class
????META-INFservices.xml
???? logging.mar 文件放在<Tomcat 安装目录>webappsaxis2WEB-INFmodules 目录中,将service.aar 文件放在<Tomcat 安装目录>webappsaxis2WEB-INFservices 目录中。要注意的是,如果modules 目录中包含了modules.list 文件,Axis2 会只装载在该文件中引用的Axis2 模块,因此,必须在该文件中引用logging 模块,该文件的内容如下:
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
???? 如果modules 目录中不包含modules.list 文件,则Axis2 会装载modules 文件中的所有Axis2 模块。
???? 现在启动Tomcat ,使用如下的C# 代码调用MyService getGreeting 方法则会在Tomcat 控制台中输出相应的请求和响应SOAP 消息。
??async是引用MyService的服务名 async.myService?my? new ?WSC.asyn.myService();
MessageBox.Show(my.getGreeting(
中国 ));
MessageBox.Show(
完成调用 );

?

本文出自 “李宁的极客世界” 博客,请务必保留此出处http://www.voidcn.com/article/p-dpshwwxw-bkp.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读