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