二、Axis2中使用模块

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://zhangjunhd.blog.51cto.com/113473/25593
本文在分析Axis2 Guide的基础上调试成功并记录了如何在Axis2中使用模块。<o:p></o:p>
author: ZJ <st1:chsdate year="2007" month="3" day="19" islunardate="False" isrocdate="False" w:st="on">07-3-19</st1:chsdate>
<o:p> </o:p>
1.模块<o:p></o:p>
Axis2为模块提供一个延伸的支持。我们现在自定义一个模块并将其部署到我们先前创建的MyService。为一个给定的Web Service部署一个自定义的模块,其步骤如下:
1)建立Module Implementation<o:p></o:p>
2)创建Handlers<o:p></o:p>
3)修改"axis2.xml"<o:p></o:p>
4)修改"services.xml",使你的模块在部署期生效。<o:p></o:p>
5)将其打包为一个".mar"(Module Archive)<o:p></o:p>
6)在Axis2上部署这个模块。<o:p></o:p>
<o:p> </o:p>
2.为MyService增加一个日志模块<o:p></o:p>
现在我们在我们的例子程序中增加一个日志模块。这个模块包含一个handle,用来记录所有传递给它的信息。Axis2使用". mar" (Module Archive)来部署模块。下图给出了需要被打包为".mar"文档的文件结构。
<v:shapetype coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f" id="_x0000_t75"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></v:path><o:lock v:ext="edit" aspectratio="t"></o:lock></v:shapetype>
<o:p> </o:p>
步骤一:日志模块类<o:p></o:p>
日志模块是Axis2模块的实现类。Axis2模块应该实现"org.apache.axis2.modules.Module"接口中的如下方法。
public void init(ConfigurationContext configContext, AxisModule module)
 throws AxisFault;//Initialize the module
public void shutdown(AxisConfiguration axisSystem)
 throws AxisFault;//End of module processing
public void engageNotify(AxisDescription axisDescription) throws AxisFault;
这些方法可以用来控制模块的初始化和终止。通过参数AxisConfiguration,可提供给用户完整的配置层次。模块设计者可以使用它来很好的控制模块的所有可能的操作。就这个简单的日志服务的例子而言,我们可以空实现这些类。
LoggingModule.java<o:p></o:p>
package userguide.loggingmodule;
<o:p> </o:p>
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;<o:p></o:p>
import org.apache.axis2.description.AxisModule;<o:p></o:p>
import org.apache.axis2.modules.Module;<o:p></o:p>
<o:p> </o:p>
public class LoggingModule implements Module {<o:p></o:p>
    // initialize the module<o:p></o:p>
    public void init(ConfigurationContext configContext, AxisModule module) throws AxisFault {}<o:p></o:p>
    public void engageNotify(AxisDescription axisDescription) throws AxisFault {}
    // shutdown the module
    public void shutdown(ConfigurationContext configurationContext) throws AxisFault {}
    public String[] getPolicyNamespaces() {
           return null;     
    }
}
<o:p> </o:p>
步骤二:LogHandler<o:p></o:p>
Axis2中的模块可以包含一个或多个handlers用来在不同的阶段执行不同的SOAP头处理。创建一个handler,应该实现org.apache.axis2.engine.Handler。但是为简单起见,org.apache.axis2.handlers.AbstractHandler提供了一个对Handler接口的抽象的实现。针对本例日志模块,我们将创建一个handler包含以下方法:
1"public void invoke(MessageContext ctx);"//当控制权转到handler时,由Axis2引擎调用。
2"public void revoke(MessageContext ctx);"//handlersAxis2引擎撤销时调用。
package userguide.loggingmodule;
<o:p> </o:p>
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;<o:p></o:p>
@SuppressWarnings("serial")<o:p></o:p>
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;
    }
}
<o:p> </o:p>
步骤三:module.xml<o:p></o:p>
"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>
从这个文件中,我们可以看到"module.xml"定义了4个阶段:
1inflow-表示当一个消息到来时,这个handler链将运行。
2outflow-表示当一个消息发出时,这个handler链将运行。
3Outfaultflow-表示当有一个错误并且这个错误将发出时,这个handler链将运行。
4INfalutflow-表示当有一个错误并且这个错误将到来时,这个handler链将运行。
下面的标签设置描述了handler的名字,handler类和该handler将运行的阶段。
<handler name="InFlowLogHandler" class="userguide.loggingmodule.LogHandler">
<order phase="loggingPhase" />
</handler>
<o:p></o:p>
步骤四:修改"axis2.xml"<o:p></o:p>
在这个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 -->
<!-- ================================================= -->
<o:p> </o:p>
<phaseOrder type="inflow">
        <!--  System pre defined phases       -->
        <phase name="TransportIn"/>
        <phase name="PreDispatch"/>
        <phase name="Dispatch" class="org.apache.axis2.engine.DispatchPhase">
            <handler name="AddressingBasedDispatcher"
                     class="org.apache.axis2.engine.AddressingBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>
<o:p> </o:p>
            <handler name="RequestURIBasedDispatcher"
                     class="org.apache.axis2.engine.RequestURIBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>
<o:p> </o:p>
            <handler name="SOAPActionBasedDispatcher"
                     class="org.apache.axis2.engine.SOAPActionBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>
<o:p> </o:p>
            <handler name="SOAPMessageBodyBasedDispatcher"
                     class="org.apache.axis2.engine.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"/><o:p></o:p>
    </phaseOrder>
    <phaseOrder type="outflow">
        <!--      user can add his own phases to this area  -->
        <phase name="OperationOutPhase"/>
        <phase name="loggingPhase"/>
        <!--system predefined phase-->
        <!--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>
自定义的阶段"loggingPhase"在所有的流中都放置了,因此这个状态将会被所有的消息流调用。既然我们的模块与这个状态相联系,在这个模块中的LogHandler将会在这个状态被执行。
<o:p> </o:p>
步骤五:修改"services.xml"<o:p></o:p>
到目前为止,我们已经为这个日志模块创建了所需的类和配置文件。下一步就是在我们的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<o:p></o:p>
<o:p> </o:p>
步骤六:打包<o:p></o:p>
在部署这个模块之前,我们需要将这个模块打包为一个".mar"文件。使用jar命令可以完成,将其打包为"logging.mar"
<o:p> </o:p>
步骤七:在Axis2上部署这个模块<o:p></o:p>
为了在Axis2上部署模块,用户必须自己新建一个目录,取名为"modules",它的父目录为servlet容器中的"webapps/axis2/WEB-INF",再将".mar"文件复制到此目录下。本例中,为"logging.mar"
注:为了看到日志,用户必须将"log4j.properties"设置为log INFO。该配置文件在servlet容器下的"webapps\axis2\WEB-INF\classes"目录。将"log4j.rootCategory= ERROR, LOGFILE"替换为"log4j.rootCategory=INFO, ERROR, LOGFILE" <o:p> </o:p>

本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/25593

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值