惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - netshuai

免安装Oracle客户端使用PL/SQL连接Oracle的2种方法 优化MyEclipse7.5 WebService大讲堂之Axis2(10):使用soapmonitor模块监视soap请求与响应消息 WebService大讲堂之Axis2(7):将Spring的装配JavaBean发布成WebService WebService大讲堂之Axis2(8):异步调用WebService WebService大讲堂之Axis2(5):会话(Session)管理 WebService大讲堂之Axis2(6):跨服务会话(Session)管理 WebService大讲堂之Axis2(4):二进制文件传输 WebService大讲堂之Axis2(3):使用services.xml文件发布WebService WebService大讲堂之Axis2(2):复合类型数据的传递 (转)WebService大讲堂之Axis2(1):用POJO实现0配置的WebService J2EE项目代码编写规范 Dom4j入门 编码 GBK 的不可映射字符 ognl表达式用法笔记 - netshuai - 博客园 struts2中减少action数量(通配符使用) struts2校验框架之Visitor校验器 struts2.1.6 datetimepicker标签不能使用的问题解决 加快MyEclipse启动速度
WebService大讲堂之Axis2(9):编写Axis2模块(Module)
netshuai · 2009-09-20 · via 博客园 - netshuai

    Axis2可以通过模块(Module)进行扩展。Axis2模块至少需要有两个类,这两个类分别实现了ModuleHandler接口。开发和使用一个Axis2模块的步骤如下:

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安装目录>\webapps\axis2\WEB-INF\modules目录中。   
    先来编写一个WebService类,代码如下:

package service;public class MyService
{
    
public String getGreeting(String name)
    {
        
return "您好 " + name;
    }
}

    下面我们来编写一个记录请求和响应SOAP消息的Axis2模块。当客户端调用WebService方法时,该Axis2模块会将请求和响应SOAP消息输出到Tomcat控制台上。

1步:编写LoggingModule

    LoggingModule类实现了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类并没实现实际的功能,但该类必须存在。当Tomcat启动时会装载该Axis2模块,同时会调用LoggingModule类的init方法,并在Tomcat控制台中输出“init”。

2步:编写LogHandler

    LogHandler类实现了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控制台输出请求和响应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,当使用该Axis2模块的WebService的方法被调用时,LogHandler类的invoke方法被调用。    3步:编写module.xml文件   

   

在META-INF目录中建立一个module.xml文件,内容如下:

<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文件,分别在如下四个<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文件的内容如下:

<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步:发布logging模块

    到现在为止,我们应用可以建立两个发行包:logging.marservice.aar。其中logging.mar文件是Axis2模块的发行包,该包的目录结构如下:

logging.mar

    module\LoggingModule.class

    module\LogHandler.class

    META-INF\module.xml

    service.aar文件是本例编写的WebService发行包,该包的目录结构如下:

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会只装载在该文件中引用的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

logging.mar

    如果modules目录中不包含modules.list文件,则Axis2会装载modules文件中的所有Axis2模块。

    现在启动Tomcat,使用如下的C#代码调用MyServicegetGreeting方法则会在Tomcat控制台中输出相应的请求和响应SOAP消息。

//  async是引用MyService的服务名
async.myService my = new WSC.asyn.myService();
MessageBox.Show(my.getGreeting(
"中国"));
MessageBox.Show(
"完成调用");

    在执行上面的代码后,在Tomcat控制台中输出的信息如下图所示。