本文在分析Axis2 Guide的基础上调试成功并记录了如何在Axis2中使用模块。<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

author: ZJ <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />07-3-19

 

1.模块

Axis2为模块提供一个延伸的支持。我们现在自定义一个模块并将其部署到我们先前创建的MyService。为一个给定的Web Service部署一个自定义的模块,其步骤如下:
1建立Module Implementation

2)创建Handlers

3)修改"axis2.xml"

4)修改"services.xml",使你的模块在部署期生效。

5)将其打包为一个".mar"(Module Archive)

6)在Axis2上部署这个模块。

 

2.为MyService增加一个日志模块

现在我们在我们的例子程序中增加一个日志模块。这个模块包含一个handle,用来记录所有传递给它的信息。Axis2使用". mar" (Module Archive)来部署模块。下图给出了需要被打包为".mar"文档的文件结构。
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

 

步骤一:日志模块类

日志模块是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

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 init(ConfigurationContext configContext, AxisModule module) throws AxisFault {}

    public void engageNotify(AxisDescription axisDescription) throws AxisFault {}
    // shutdown the module
    public void shutdown(ConfigurationContext configurationContext) throws AxisFault {}
    public String[] getPolicyNamespaces() {
           return null;     
    }
}

 

步骤二:LogHandler

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;

 

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

"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>

 

步骤四:修改"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"/>
        <phase name="Dispatch" class="org.apache.axis2.engine.DispatchPhase">
            <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"
                     class="org.apache.axis2.engine.SOAPActionBasedDispatcher">
                <order phase="Dispatch"/>
            </handler>

 

            <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"/>

    </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将会在这个状态被执行。

 

步骤五:修改"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

 

步骤六:打包

在部署这个模块之前,我们需要将这个模块打包为一个".mar"文件。使用jar命令可以完成,将其打包为"logging.mar"

 

步骤七:在Axis2上部署这个模块

为了在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"