本文在分析Axis2 Guide的基础上调试成功并记录了如何在Axis2中使用模块。
author: ZJ
07-3-19
1.模块
1)建立Module Implementation。
2)创建Handlers。
6)在Axis2上部署这个模块。
现在我们在我们的例子程序中增加一个日志模块。这个模块包含一个handle,用来记录所有传递给它的信息。Axis2使用". mar" (Module Archive)来部署模块。下图给出了需要被打包为".mar"文档的文件结构。
步骤一:日志模块类
throws AxisFault;//Initialize the module
throws AxisFault;//End of module processing
public void engageNotify(AxisDescription axisDescription) throws AxisFault;
|
这些方法可以用来控制模块的初始化和终止。通过参数AxisConfiguration,可提供给用户完整的配置层次。模块设计者可以使用它来很好的控制模块的所有可能的操作。就这个简单的日志服务的例子而言,我们可以空实现这些类。
LoggingModule.java
package userguide.loggingmodule;
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;
public class LoggingModule implements Module {
// initialize the module
public void engageNotify(AxisDescription axisDescription) throws AxisFault {}
// shutdown the module
public String[] getPolicyNamespaces() {
return null;
}
}
|
步骤二:LogHandler
Axis2中的模块可以包含一个或多个handlers用来在不同的阶段执行不同的SOAP头处理。创建一个handler,应该实现org.apache.axis2.engine.Handler。但是为简单起见,org.apache.axis2.handlers.AbstractHandler提供了一个对Handler接口的抽象的实现。针对本例日志模块,我们将创建一个handler包含以下方法:
package userguide.loggingmodule;
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;
import javax.xml.namespace.QName;
@SuppressWarnings("serial")
public class LogHandler extends AbstractHandler implements Handler {
private static final Log log = LogFactory.getLog(LogHandler.class);
private QName name;
public QName getName() {
return name;
}
public void invoke(MessageContext msgContext) throws AxisFault {
log.info(msgContext.getEnvelope().toString());
}
public void revoke(MessageContext msgContext) {
log.info(msgContext.getEnvelope().toString());
}
public void setName(QName name) {
this.name = name;
}
}
|
"module.xml"包含了每一个特定的模块的部署配置信息。它应该包含的细节有一个实现模块的类(本例中是"LoggingModule"和各种各样的将在不同阶段运行的handlers)。本例中配置日志模块的"module.xml"如下:
<module name="logging" class="userguide.loggingmodule.LoggingModule ">
<inflow>
<handler name="InFlowLogHandler" class="userguide.loggingmodule.LogHandler">
<order phase="loggingPhase" />
</handler>
</inflow>
<outflow>
<handler name="OutFlowLogHandler" class="userguide.loggingmodule.LogHandler">
<order phase="loggingPhase"/>
</handler>
</outflow>
<Outfaultflow>
<handler name="FaultOutFlowLogHandler"
class="userguide.loggingmodule.LogHandler">
<order phase="loggingPhase"/>
</handler>
</Outfaultflow>
<INfaultflow>
<handler name="FaultInFlowLogHandler"
class="userguide.loggingmodule.LogHandler">
<order phase="loggingPhase"/>
</handler>
</INfaultflow>
</module>
|
<handler name="InFlowLogHandler" class="userguide.loggingmodule.LogHandler">
<order phase="loggingPhase" />
</handler>
|
步骤四:修改"axis2.xml"
在这个handler中,阶段"loggingPhase"是由这个模块的设计者定义的。这不是一个预定义的handler阶段,因此该模块的设计者应该将它在"axis2.xml"中声明。只有这样,Axis2引擎才能知道将这个handler放置在哪些“流”中(InFlow,OutFlow,等)。下面的xml定义展示了需要将日志模块部署到Axis2引擎而对axis2.xml作的修改。(This is an extract of the phase section of the "axis2.xml".)
<!-- ================================================= -->
<!-- Phases -->
<!-- ================================================= -->
<phaSEOrder type="inflow">
<!-- System pre defined phases -->
<phase name="TransportIn"/>
<phase name="Predispatch"/>
<handler name="AddressingBaseddispatcher"
class="org.apache.axis2.engine.AddressingBaseddispatcher">
<order phase="dispatch"/>
</handler>
<handler name="RequestURIBaseddispatcher"
class="org.apache.axis2.engine.RequestURIBaseddispatcher">
<order phase="dispatch"/>
</handler>
<handler name="SOAPActionBaseddispatcher"
<order phase="dispatch"/>
</handler>
<handler name="SOAPMessageBodyBaseddispatcher"
<order phase="dispatch"/>
</handler>
<handler name="Instancedispatcher"
class="org.apache.axis2.engine.Instancedispatcher">
<order phase="Postdispatch"/>
</handler>
</phase>
<!-- System pre defined phases -->
<!-- After Postdispatch phase module author or or service author can add any phase he want -->
<phase name="OperationInPhase"/>
<phase name="loggingPhase"/>
</phaSEOrder>
<phaSEOrder type="outflow">
<!-- user can add his own phases to this area -->
<phase name="OperationOutPhase"/>
<phase name="loggingPhase"/>
<!--these phase will run irrespective of the service-->
<phase name="PolicyDetermination"/>
<phase name="MessageOut"/>
</phaSEOrder/>
<phaSEOrder type="INfaultflow">
<!-- user can add his own phases to this area -->
<phase name="OperationInFaultPhase"/>
<phase name="loggingPhase"/>
</phaSEOrder>
<phaSEOrder type="Outfaultflow">
<!-- user can add his own phases to this area -->
<phase name="OperationOutFaultPhase"/>
<phase name="loggingPhase"/>
<phase name="PolicyDetermination"/>
<phase name="MessageOut"/>
</phaSEOrder>
|
步骤五:修改"services.xml"
到目前为止,我们已经为这个日志模块创建了所需的类和配置文件。下一步就是在我们的services中使用这个模块。我们就在MyService中使用此模块作演示。因此,我们需要修改MyService的"services.xml",以使得该模块起作用。对"services.xml"的修改如下
<service name="MyServiceWithModule">
<description>
This is a sample Web Service with a logging module engaged.
</description>
<module ref="logging"/>
<parameter name="ServiceClass" locked="false">
userguide.example1.MyService
</parameter>
<operation name="echo">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
<operation name="ping">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</operation>
|
</service>我们在"services.xml"加入了一行"<module ref="logging"/>"。这行将告知Axis2引擎,这个日志模块可以被这个service使用。在这个模块中的handler将在各自的状态被执行(依据"module.xml"中的描述)。并将服务重新打包为MyServiceWithModule.aar。
步骤六:打包
步骤七:在Axis2上部署这个模块
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。