Add ServiceLifeCycle for Axis2
------------------------------

                 Key: MUSE-231
                 URL: https://issues.apache.org/jira/browse/MUSE-231
             Project: Muse
          Issue Type: New Feature
          Components: Deployment - Axis2
    Affects Versions: 2.2.0
         Environment: Windows XP
Muse 2.2.0
JDK 1.4.2
            Reporter: Dipl.-Ing. Matthias Beil
         Assigned To: Dan Jemiolo
            Priority: Minor
             Fix For: 2.3.0


Axis2 defines a ServiceLifeCycle which allows to initialise Axis2 components on 
startup. A possible implementations as follow:

package org.apache.muse.core.platform.axis2;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPHeader;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.ServiceLifeCycle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.soap.SoapFault;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.stream.XMLStreamException;


/**
 * Implements the [EMAIL PROTECTED] ServiceLifeCycle} interface for Axis2.
 *
 * @author Dipl.-Ing. Matthias Beil (bel)
 * @version 1.0
 * @since Muse 2.3.0 - May 2, 2007
 */

public class AxisServiceLifeCycle implements ServiceLifeCycle {

    /** LOGGER */
    private static final Log LOGGER = LogFactory.getFactory().getInstance(
            AxisServiceLifeCycle.class);

    /**
     * Start up method will be called by Axis2. For this to happen the
     * <code>service.xml</code> must be configured correctly.
     *
     * @param configCtx
     * @param axisService
     */

    public void startUp(final ConfigurationContext configCtx,
        final AxisService axisService) {

        if (LOGGER.isDebugEnabled()) {

            LOGGER.debug("Axis2 startup called for " + axisService.getName());
        }

        final AxisIsolationLayer layer = new AxisIsolationLayer();
        if (!layer.hasBeenInitialized()) {

            // get service path, for some reason this value is not set in
            // axisService
            final URL tmpURL = axisService.getClassLoader().getResource(
                    "META-INF/services.xml");
            final File serviceFile = new File(tmpURL.getFile());
            final File museDir = serviceFile.getParentFile().getParentFile();

            URL url = null;
            try {

                url = museDir.toURL();
            } catch (final MalformedURLException muex) {

                LOGGER.error("MalformedURLException caught for " + museDir,
                    muex);
                return;
            }
            axisService.setFileName(url);

            // initialise values needed by Muse
            final String wsaTo =
                "<wsa:To xmlns:wsa=\"http://www.w3.org/2005/08/addressing\";>" +
                "http://localhost/wsn-ossj-producer/services/WsnOssjPort"; +
                "</wsa:To>";
            final String wsaAction =
                "<wsa:Action 
xmlns:wsa=\"http://www.w3.org/2005/08/addressing\";>" +
                
"http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/GetCurrentMessageRequest";
 +
                "</wsa:Action>";
            final EndpointReference epr = new EndpointReference(
                    "http://localhost/wsn-ossj-producer/services/WsnOssjPort";);

            // create a new message context, needed by Muse
            final MessageContext ctx = new MessageContext();
            ctx.setConfigurationContext(configCtx);
            ctx.setAxisService(axisService);

            // TO is needed by Muse
            ctx.setTo(epr);

            // get SOAP envelope and create header messages
            final SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
            final SOAPEnvelope envelope = fac.getDefaultEnvelope();
            final SOAPHeader header = envelope.getHeader();

            Element elem = this.getElement(wsaTo);
            if (elem == null) {

                LOGGER.error("Error in converting to an W3C element: " + wsaTo);
                return;
            }
            header.addChild(this.convertToAxiom(elem));

            elem = this.getElement(wsaAction);
            if (elem == null) {

                LOGGER.error("Error in converting to an W3C element: " +
                    wsaAction);
                return;
            }
            header.addChild(this.convertToAxiom(elem));

            try {

                ctx.setEnvelope(envelope);
            } catch (final AxisFault fault) {

                LOGGER.error("AxisFault caught while assigning the envelope!",
                    fault);
                return;
            }

            // now persist this to the message context
            MessageContext.setCurrentMessageContext(ctx);

            // call Muse initialisation method
            layer.initialize();

            if (layer.hasBeenInitialized()) {

                if (LOGGER.isDebugEnabled()) {

                    LOGGER.debug("Muse has been initialized!");
                }
            } else {

                LOGGER.error("Muse was not initialized!");
            }
        }
    }

    /**
     * Shutdown method will be called by Axis2 when it terminates.
     *
     * @param configCtx
     * @param axisService
     */

    public void shutDown(final ConfigurationContext configCtx,
        final AxisService axisService) {

        if (LOGGER.isDebugEnabled()) {

            LOGGER.debug("Axis2 shutdown called for " + axisService.getName());
        }

        final AxisIsolationLayer layer = new AxisIsolationLayer();
        if (!layer.hasBeenShutdown()) {

            try {

                layer.shutdown();

                if (LOGGER.isDebugEnabled()) {

                    LOGGER.debug("Muse was shutdown!");
                }
            } catch (SoapFault fault) {

                LOGGER.error("SAOPFault caught shutting down Muse!", fault);
            }
        }
    }

    /**
     * Convert DOM to Axiom. Muse uses the DOM API in the JDK, Axis2 uses the
     * Axiom API, which is similar but... different.
     * <p>
     * Taken from AxisEnvironment class.
     *
     * @param xml
     * @return OMElement
     * @throws RuntimeException
     */

    private OMElement convertToAxiom(final Element xml) {

        final String xmlString = XmlUtils.toString(xml, false);
        final byte[] xmlBytes = xmlString.getBytes();

        StAXOMBuilder builder = null;
        try {

            builder = new StAXOMBuilder(new ByteArrayInputStream(xmlBytes));
        } catch (final XMLStreamException error) {

            throw new RuntimeException(error.getMessage(), error);
        }

        return builder.getDocumentElement();
    }

    /**
     * Return Element from String. There might be some better way. This is just
     * quick and dirty.
     *
     * @param txt
     * @return Element
     */

    private Element getElement(final String txt) {

        Element elem = null;

        if ((txt != null) && (txt.length() > 0)) {

            try {

                final Document doc = XmlUtils.createDocument(txt);
                elem = doc.getDocumentElement();
            } catch (final IOException ioex) {

                LOGGER.error("IOException caught while getting Element for " +
                    txt, ioex);
            } catch (final SAXException saxex) {

                LOGGER.error("SAXException caught while getting Element for " +
                    txt, saxex);
            }
        }

        return elem;
    }

}

The service.xml file must be adapted as follow:

<?xml version="1.0"?>
<serviceGroup>

        <service name="SubscriptionManager" 
class="org.apache.muse.core.platform.axis2.AxisServiceLifeCycle">
                <description>
                        This is the Apache Muse router service.
                </description>
                <parameter name="ServiceClass" locked="false">
                        org.apache.muse.core.platform.axis2.AxisIsolationLayer
                </parameter>
                <operation name="handleRequest">
                        <messageReceiver
                                
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
                        <actionMapping>
                                
http://docs.oasis-open.org/wsrf/rlw-2/ImmediateResourceTermination/DestroyRequest
                        </actionMapping>
                        <actionMapping>
                                
http://docs.oasis-open.org/wsrf/rlw-2/ScheduledResourceTermination/SetTerminationTimeRequest
                        </actionMapping>
                        <actionMapping>
                                
http://docs.oasis-open.org/wsrf/rpw-2/GetResourceProperty/GetResourcePropertyRequest
                        </actionMapping>
                        <actionMapping>
                                
http://docs.oasis-open.org/wsrf/rpw-2/GetResourcePropertyDocument/GetResourcePropertyDocumentRequest
                        </actionMapping>
                        <actionMapping>
                                
http://docs.oasis-open.org/wsrf/rpw-2/GetMultipleResourceProperties/GetMultipleResourcePropertiesRequest
                        </actionMapping>
                </operation>
        </service>

</serviceGroup>

This works well for the ws-producer. The producer is initialized and 
notifications are send automatically.

I tried it with the ws-consumer, but it seems not working. Didn't invest any 
future because I only needed the ws-producer side.



-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to