Axis2
可以通过模块(Module
)进行扩展。Axis2
模块至少需要有两个类,这两个类分别实现了Module
和Handler
接口。开发和使用一个Axis2
模块的步骤如下:
2.
编写实现Handler
接口的类。该类是Axis2
模块的业务处理类。
package
service;
public class MyService
{
public String getGreeting(String name)
{
return " 您好 " + name;
}
}
public class MyService
{
public String getGreeting(String name)
{
return " 您好 " + name;
}
}
第1步:编写LoggingModule类
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 ;
}
}
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类
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;
}
}
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文件,内容如下:第3步:编写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 >
< 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 >
<
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 >


< 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 >
<
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 >
< 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.mar
module\LoggingModule.class
module\LogHandler.class
meta-inf\module.xml
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。