Thursday, December 24, 2015

Creating Axis2 Module for Custom Logging and Integrating to WSO2 ESB

This article is about creating Axis2 module and Integrating it with WSO2 ESB.

1) Create a Maven Project using eclipse. And add the below to the dependencies in pom.xml.Below is the pom.xml created for this sample.

    4.0.0

    CustomAxis2ModuleProject
    CustomAxis2ModuleProject
    0.0.1-SNAPSHOT
    jar

    CustomAxis2ModuleProject
    http://maven.apache.org

    
        UTF-8
    

    
        
            junit
            junit
            3.8.1
            test
        
        
            org.apache.axis2.wso2
            axis2
            1.6.1.wso2v14
        
        
            org.apache.synapse
            synapse-core
            2.1.3-wso2v11
        
        
            commons-logging
            commons-logging
            1.1.1
        
    
    
        
            
                true
                daily
                ignore
            
            wso2-nexus
            http://maven.wso2.org/nexus/content/groups/wso2-public/
        
    
    
        
            
                true
                daily
                ignore
            
            wso2-nexus
            http://maven.wso2.org/nexus/content/groups/wso2-public/
        
    


2) Create the Axis2 Module Implementation.This can be done by creating a CustomLoggingModule class by implementing the Module class like below.
package com.custom.axis.loggers;

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.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;

public class CustomLoggingModule implements Module {

    private static final Log log = LogFactory.getLog(CustomLoggingModule.class);

    public void applyPolicy(Policy arg0, AxisDescription arg1) throws AxisFault {
        // TODO Auto-generated method stub

    }

    public boolean canSupportAssertion(Assertion arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    public void engageNotify(AxisDescription arg0) throws AxisFault {
        // TODO Auto-generated method stub

    }

    public void init(ConfigurationContext arg0, AxisModule arg1)
            throws AxisFault {
        if (log.isDebugEnabled()) {
            log.debug("Custom Logging Module Init Method Invoked");
        }
    }

    public void shutdown(ConfigurationContext arg0) throws AxisFault {
        // TODO Auto-generated method stub

    }

}


3) Create the Handlers. This can be done by creating a CustomLoggingHandler class by extending AbstractHandler class.
package com.custom.axis.loggers;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.AbstractHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/*
 * Custom Axis2 Handler for custom logging.
 *
 */

public class CustomLoggingHandler extends AbstractHandler {

    private static final Log log = LogFactory
            .getLog(CustomLoggingHandler.class);

    public InvocationResponse invoke(MessageContext msgContext)
            throws AxisFault {

        // Add your custom logging logic here

        return InvocationResponse.CONTINUE;
    }

}



4) Create a module.xml in src/main/resources/META-INF. The sample module.xml is below. Here only invoking the handler in the InFlow.

Note: Here due to auto formatting in the editor the <inFlow> is converted to <inflow>. Use <inFlow> when you try out the sample. 

    
        
            
        
     
 

5) The maven project structure will look like below.
 6) Build the project using Maven build.

7) Copy the jar created to  ESB_HOME/repository/deployment/server/axis2modules/ and rename with .mar extension.
8) Modify the axis2.xml to add the phase to the InFlow.
 9) To assign the module globally to all the service requests add the module to axis2.xml as below.
10) Now the custom module is ready to use.

References
[1] http://wso2.com/library/777/ 
[2] https://axis.apache.org/axis2/java/core/docs/modules.html
[3] https://axis.apache.org/axis2/java/core/docs/Axis2ArchitectureGuide.html#requirements


No comments:

Post a Comment