You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-user@ws.apache.org by "Beil, Matthias" <Be...@ascom-ac.de> on 2007/05/02 16:43:49 UTC

StartUp solution

Hi,

 

I found one possible solution for the start up problem. There is now a
german :-( book about Axis2 on the market. It's called

 

Java Web Services mit Apache Axis2 ISBN 978-3-935042-81-9

 

Which explains on p. 212 the Axis2 ServiceLifeCycle interface. Based on
this interface I have written the following code (copied some code from
Muse ;-):

 

package com.ascom.ossj.wsn.producer.axis2;

 

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;

 

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.core.platform.axis2.AxisIsolationLayer;

import org.apache.muse.util.xml.XmlUtils;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.xml.sax.SAXException;

 

/**

 * Implements the {@link ServiceLifeCycle} interface for Axis2.

 *

 * @author Dipl.-Ing. Matthias Beil (bel)

 */

public class WsnOssjProducerServiceLifeCycle implements ServiceLifeCycle
{

 

   // ~ Static fields/initializers
---------------------------------------------

 

   private static final Log LOGGER =
LogFactory.getFactory().getInstance(WsnOssjProducerServiceLifeCycle.clas
s);

 

   // ~ Methods
----------------------------------------------------------------

 

   /**

    * Method startUp.

    *

    * @param configCtx

    * @param axisService

    */

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

 

      final AxisIsolationLayer layer = new AxisIsolationLayer();

      if (!layer.hasBeenInitialized()) {

 

         if (LOGGER.isDebugEnabled()) {

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

         }

 

         // get service path

         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) {

 

 
WsnOssjProducerServiceLifeCycle.LOGGER.error("MalformedURLException
caught for " + museDir, muex);

         }

 

         // set url to axis service

         axisService.setFileName(url);

 

         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/GetCurrentMess
ageRequest" + "</wsa:Action>";

 

         final EndpointReference epr = new
EndpointReference("http://localhost/wsn-ossj-producer/services/WsnOssjPo
rt");

 

         final MessageContext ctx = new MessageContext();

         ctx.setConfigurationContext(configCtx);

         ctx.setAxisService(axisService);

         ctx.setTo(epr);

 

         final SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();

         final SOAPEnvelope envelope = fac.getDefaultEnvelope();

         final SOAPHeader header = envelope.getHeader();

 

         header.addChild(this.convertToAxiom(this.getElement(wsaTo)));

 
header.addChild(this.convertToAxiom(this.getElement(wsaAction)));

 

         try {

            ctx.setEnvelope(envelope);

         }

         catch (final AxisFault fault) {

 

            LOGGER.error("AxisFault caught while setting the
envelope!");

         }

 

         MessageContext.setCurrentMessageContext(ctx);

         layer.initialize();

      }

   }

 

   /**

    * Method shutDown.

    *

    * @param configCtx

    * @param axisService

    */

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

 

      if (LOGGER.isDebugEnabled()) {

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

      }

   }

 

   /**

    * Convert DOM to Axiom. Muse uses the DOM API in the JDK, Axis2 uses
the

    * Axiom API, which is similar but... different.

    *

    * @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();

   }

 

   /**

    * Getter method for <code>element</code>.

    *

    * @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 looks partially like this

 

            <service name="WsnOssjPort"
class="com.ascom.ossj.wsn.producer.axis2.WsnOssjProducerServiceLifeCycle
">

 

                        <parameter locked="false"
name="ServiceClass">org.apache.muse.core.platform.axis2.AxisIsolationLay
er</parameter>

                        <operation name="handleRequest">

                                    <messageReceiver
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />

                                   <actionMapping>

 
http://docs.oasis-open.org/wsrf/rpw-2/GetResourceProperty/GetResourcePro
pertyRequest

                                   </actionMapping>

                                   <actionMapping>

 
http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/GetCurrentMessa
geRequest

                                   </actionMapping>

 
<actionMapping>http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/
SubscribeRequest</actionMapping>

                        </operation>

 

                        <module ref="rampart" />

 

                        <parameter name="InflowSecurity">

                                   <action>

 
<items>UsernameToken</items>

 
<passwordCallbackClass>com.ascom.ossj.wsn.producer.security.PWCBHandler<
/passwordCallbackClass>

                                   </action>

                        </parameter>

 

            </service>

 

With this configuration I have my resource started and can now put a
message in a JMS topic which then activates the publisher and the
message is directly send to the NotifcationConsumer. I don't need to
call a getCurrentMessage method anymore to initiate Muse.

 

I would like to know if this is a way to go or are there some
considerations which I have not thought about?

 

Mit freundlichen Gruessen / With kind regards

Matthias Beil


Re: StartUp solution

Posted by Daniel Jemiolo <da...@us.ibm.com>.
Hi,

At first glance, this looks very cool - looks like you might have solved 
it! Can you open a JIRA item and attach the code and a sample app that 
uses it? Just modify one of the current sample apps, so you don't have to 
send us your app (probably confidential). This will allow us to take a 
closer look and make sure nothing has broken before accepting the 
contribution.

Also, I am going to need you to sign Apache's contributor agreement (CLA). 
This is a straightforward process, I will send you more details 
separately.

Thanks,
Dan



"Beil, Matthias" <Be...@ascom-ac.de> wrote on 05/02/2007 10:43:49 AM:

> Hi,
> 
> 
> 
> I found one possible solution for the start up problem. There is now a
> german :-( book about Axis2 on the market. It's called
> 
> 
> 
> Java Web Services mit Apache Axis2 ISBN 978-3-935042-81-9
> 
> 
> 
> Which explains on p. 212 the Axis2 ServiceLifeCycle interface. Based on
> this interface I have written the following code (copied some code from
> Muse ;-):
> 
> 
> 
> package com.ascom.ossj.wsn.producer.axis2;
> 
> 
> 
> 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;
> 
> 
> 
> 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.core.platform.axis2.AxisIsolationLayer;
> 
> import org.apache.muse.util.xml.XmlUtils;
> 
> import org.w3c.dom.Document;
> 
> import org.w3c.dom.Element;
> 
> import org.xml.sax.SAXException;
> 
> 
> 
> /**
> 
>  * Implements the {@link ServiceLifeCycle} interface for Axis2.
> 
>  *
> 
>  * @author Dipl.-Ing. Matthias Beil (bel)
> 
>  */
> 
> public class WsnOssjProducerServiceLifeCycle implements ServiceLifeCycle
> {
> 
> 
> 
>    // ~ Static fields/initializers
> ---------------------------------------------
> 
> 
> 
>    private static final Log LOGGER =
> LogFactory.getFactory().getInstance(WsnOssjProducerServiceLifeCycle.clas
> s);
> 
> 
> 
>    // ~ Methods
> ----------------------------------------------------------------
> 
> 
> 
>    /**
> 
>     * Method startUp.
> 
>     *
> 
>     * @param configCtx
> 
>     * @param axisService
> 
>     */
> 
>    public void startUp(final ConfigurationContext configCtx, final
> AxisService axisService) {
> 
> 
> 
>       final AxisIsolationLayer layer = new AxisIsolationLayer();
> 
>       if (!layer.hasBeenInitialized()) {
> 
> 
> 
>          if (LOGGER.isDebugEnabled()) {
> 
>             LOGGER.info("Axis2 startup called for " +
> axisService.getName());
> 
>          }
> 
> 
> 
>          // get service path
> 
>          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) {
> 
> 
> 
> 
> WsnOssjProducerServiceLifeCycle.LOGGER.error("MalformedURLException
> caught for " + museDir, muex);
> 
>          }
> 
> 
> 
>          // set url to axis service
> 
>          axisService.setFileName(url);
> 
> 
> 
>          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/GetCurrentMess
> ageRequest" + "</wsa:Action>";
> 
> 
> 
>          final EndpointReference epr = new
> EndpointReference("http://localhost/wsn-ossj-producer/services/WsnOssjPo
> rt");
> 
> 
> 
>          final MessageContext ctx = new MessageContext();
> 
>          ctx.setConfigurationContext(configCtx);
> 
>          ctx.setAxisService(axisService);
> 
>          ctx.setTo(epr);
> 
> 
> 
>          final SOAPFactory fac = OMAbstractFactory.getSOAP12Factory();
> 
>          final SOAPEnvelope envelope = fac.getDefaultEnvelope();
> 
>          final SOAPHeader header = envelope.getHeader();
> 
> 
> 
>          header.addChild(this.convertToAxiom(this.getElement(wsaTo)));
> 
> 
> header.addChild(this.convertToAxiom(this.getElement(wsaAction)));
> 
> 
> 
>          try {
> 
>             ctx.setEnvelope(envelope);
> 
>          }
> 
>          catch (final AxisFault fault) {
> 
> 
> 
>             LOGGER.error("AxisFault caught while setting the
> envelope!");
> 
>          }
> 
> 
> 
>          MessageContext.setCurrentMessageContext(ctx);
> 
>          layer.initialize();
> 
>       }
> 
>    }
> 
> 
> 
>    /**
> 
>     * Method shutDown.
> 
>     *
> 
>     * @param configCtx
> 
>     * @param axisService
> 
>     */
> 
>    public void shutDown(final ConfigurationContext configCtx, final
> AxisService axisService) {
> 
> 
> 
>       if (LOGGER.isDebugEnabled()) {
> 
>          LOGGER.info("Axis2 shutdown called for " +
> axisService.getName());
> 
>       }
> 
>    }
> 
> 
> 
>    /**
> 
>     * Convert DOM to Axiom. Muse uses the DOM API in the JDK, Axis2 uses
> the
> 
>     * Axiom API, which is similar but... different.
> 
>     *
> 
>     * @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();
> 
>    }
> 
> 
> 
>    /**
> 
>     * Getter method for <code>element</code>.
> 
>     *
> 
>     * @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 looks partially like this
> 
> 
> 
>             <service name="WsnOssjPort"
> class="com.ascom.ossj.wsn.producer.axis2.WsnOssjProducerServiceLifeCycle
> ">
> 
> 
> 
>                         <parameter locked="false"
> name="ServiceClass">org.apache.muse.core.platform.axis2.AxisIsolationLay
> er</parameter>
> 
>                         <operation name="handleRequest">
> 
>                                     <messageReceiver
> class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
> 
>                                    <actionMapping>
> 
> 
> http://docs.oasis-open.org/wsrf/rpw-2/GetResourceProperty/GetResourcePro
> pertyRequest
> 
>                                    </actionMapping>
> 
>                                    <actionMapping>
> 
> 
> http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/GetCurrentMessa
> geRequest
> 
>                                    </actionMapping>
> 
> 
> <actionMapping>http://docs.oasis-open.org/wsn/bw-2/NotificationProducer/
> SubscribeRequest</actionMapping>
> 
>                         </operation>
> 
> 
> 
>                         <module ref="rampart" />
> 
> 
> 
>                         <parameter name="InflowSecurity">
> 
>                                    <action>
> 
> 
> <items>UsernameToken</items>
> 
> 
> <passwordCallbackClass>com.ascom.ossj.wsn.producer.security.PWCBHandler<
> /passwordCallbackClass>
> 
>                                    </action>
> 
>                         </parameter>
> 
> 
> 
>             </service>
> 
> 
> 
> With this configuration I have my resource started and can now put a
> message in a JMS topic which then activates the publisher and the
> message is directly send to the NotifcationConsumer. I don't need to
> call a getCurrentMessage method anymore to initiate Muse.
> 
> 
> 
> I would like to know if this is a way to go or are there some
> considerations which I have not thought about?
> 
> 
> 
> Mit freundlichen Gruessen / With kind regards
> 
> Matthias Beil
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: muse-user-unsubscribe@ws.apache.org
For additional commands, e-mail: muse-user-help@ws.apache.org