You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by di...@apache.org on 2007/02/26 15:49:40 UTC

svn commit: r511830 - in /webservices/axis2/trunk/java: ./ modules/ping/ modules/samples/ping/ modules/samples/ping/resources/ modules/samples/ping/resources/META-INF/ modules/samples/ping/src/ modules/samples/ping/src/samples/ modules/samples/ping/src...

Author: dims
Date: Mon Feb 26 06:49:39 2007
New Revision: 511830

URL: http://svn.apache.org/viewvc?view=rev&rev=511830
Log:
Fix for AXIS2-2244 - Sample to illustrate the pinging capability

Added:
    webservices/axis2/trunk/java/modules/samples/ping/
    webservices/axis2/trunk/java/modules/samples/ping/README.txt
    webservices/axis2/trunk/java/modules/samples/ping/build.xml
    webservices/axis2/trunk/java/modules/samples/ping/resources/
    webservices/axis2/trunk/java/modules/samples/ping/resources/META-INF/
    webservices/axis2/trunk/java/modules/samples/ping/resources/META-INF/services.xml
    webservices/axis2/trunk/java/modules/samples/ping/src/
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/client/
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/client/PingClient.java
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOnlyMessageReceiver.java
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOutMessageReceiver.java
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/service/
    webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/service/PingService.java
Modified:
    webservices/axis2/trunk/java/maven.xml
    webservices/axis2/trunk/java/modules/ping/   (props changed)

Modified: webservices/axis2/trunk/java/maven.xml
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/maven.xml?view=diff&rev=511830&r1=511829&r2=511830
==============================================================================
--- webservices/axis2/trunk/java/maven.xml (original)
+++ webservices/axis2/trunk/java/maven.xml Mon Feb 26 06:49:39 2007
@@ -739,6 +739,7 @@
                 <include name="version/**/*.*"/>
                 <include name="servicelifecycle/**/*.*"/>
                 <include name="pojo/**/*.*"/>
+		<include name="ping/**/*.*"/>
                 <!-- Docs related samples -->
                 <include name="userguide/**/*.*"/>
                 <include name="pojoguide/**/*.*"/>

Propchange: webservices/axis2/trunk/java/modules/ping/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Feb 26 06:49:39 2007
@@ -0,0 +1 @@
+target

Added: webservices/axis2/trunk/java/modules/samples/ping/README.txt
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/ping/README.txt?view=auto&rev=511830
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/ping/README.txt (added)
+++ webservices/axis2/trunk/java/modules/samples/ping/README.txt Mon Feb 26 06:49:39 2007
@@ -0,0 +1,34 @@
+Sample - Pinging the service deployed in Axis2
+==========================================
+
+Introduction
+============
+
+In this sample, we are deploying an AXIOM based service after writing
+a services.xml and creating an aar. We also test successful
+deployment using an AXIOM based client which send serveral ping requests.
+The client sends serveral ping requests, including service level requests.
+
+
+Pre-Requisites
+==============
+
+Apache Ant 1.6.2 or later
+
+Building the Service
+====================
+
+Type "ant generate.service" from Axis2_HOME/samples/pingingservices directory.
+Then deploy the
+Axis2_HOME/samples/pingingservices/build/PingService.aar
+
+
+Running the Client
+==================
+
+Type ant run.client in the Axis2_HOME/samples/pingingservices directory
+
+Help
+====
+Please contact axis-user list (axis-user@ws.apache.org) if you have
+any trouble running the sample.
\ No newline at end of file

Added: webservices/axis2/trunk/java/modules/samples/ping/build.xml
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/ping/build.xml?view=auto&rev=511830
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/ping/build.xml (added)
+++ webservices/axis2/trunk/java/modules/samples/ping/build.xml Mon Feb 26 06:49:39 2007
@@ -0,0 +1,55 @@
+<project basedir="." default="generate.service">
+
+    <property environment="env"/>
+    <property name="AXIS2_HOME" value="../../"/>
+
+    <property name="build.dir" value="build"/>
+
+    <path id="axis2.classpath">
+        <fileset dir="${AXIS2_HOME}/lib">
+            <include name="*.jar"/>
+        </fileset>
+    </path>
+	<path id="client.class.path">
+		<fileset dir="${AXIS2_HOME}/lib">
+			<include name="*.jar" />
+		</fileset>
+		<pathelement location="${build.dir}/classes" />
+	</path>
+
+    <target name="compile">
+        <mkdir dir="${build.dir}"/>
+        <mkdir dir="${build.dir}/classes"/>
+
+        <!--First let's compile the classes-->
+        <javac debug="on"
+               fork="true"
+               destdir="${build.dir}/classes"
+               srcdir="${basedir}/src"
+               classpathref="axis2.classpath">
+        </javac>
+    </target>
+
+    <target name="generate.service" depends="compile">
+        <!--aar them up -->
+        <copy toDir="${build.dir}/classes" failonerror="false">
+            <fileset dir="${basedir}/resources">
+                <include name="**/*.xml"/>
+                <include name="**/*.wsdl"/>
+            </fileset>
+        </copy>
+        <jar destfile="${build.dir}/SamplePingService.aar">
+            <fileset excludes="**/Test.class" dir="${build.dir}/classes"/>
+        </jar>
+    </target>
+
+    <target name="run.client" depends="compile">
+        <java classname="samples.ping.client.PingClient">
+            <classpath refid="client.class.path" />
+        </java>
+    </target>
+
+    <target name="clean">
+        <delete dir="${build.dir}"/>
+    </target>
+</project>

Added: webservices/axis2/trunk/java/modules/samples/ping/resources/META-INF/services.xml
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/ping/resources/META-INF/services.xml?view=auto&rev=511830
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/ping/resources/META-INF/services.xml (added)
+++ webservices/axis2/trunk/java/modules/samples/ping/resources/META-INF/services.xml Mon Feb 26 06:49:39 2007
@@ -0,0 +1,14 @@
+<service name="SamplePingService" scope="application">
+    <description>Ping Service</description>
+    <module ref="ping"/>
+
+    <operation name="getPrice">
+        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="samples.ping.receivers.RawXMLINOutMessageReceiver"/>
+    </operation>
+
+    <operation name="update">
+        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="samples.ping.receivers.RawXMLINOnlyMessageReceiver"/>
+    </operation>
+    
+    <parameter name="ServiceClass">samples.ping.service.PingService</parameter>
+</service>
\ No newline at end of file

Added: webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/client/PingClient.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/client/PingClient.java?view=auto&rev=511830
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/client/PingClient.java (added)
+++ webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/client/PingClient.java Mon Feb 26 06:49:39 2007
@@ -0,0 +1,101 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*      http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package samples.ping.client;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axis2.Constants;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.addressing.EndpointReference;
+import org.apache.axis2.client.Options;
+import org.apache.axis2.client.ServiceClient;
+
+public class PingClient {
+    private static EndpointReference targetEPR =
+            new EndpointReference(
+                    "http://localhost:8080/axis2/services/SamplePingService");
+
+    private static final String PING_REQUEST = "pingRequest";
+    private static final String PING_OPERATION = "operation";
+    private static final String PING_SOAPACTION = "http://ws.apache.org/axis2/modules/ping";
+    private static final String PING_NAMESPACE = "http://ws.apache.org/axis2/modules/ping/xsd";
+
+    public PingClient() {
+
+    }
+
+    public static OMElement getPingRequest1(String [] operationList) {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMNamespace omNs = fac.createOMNamespace(PING_NAMESPACE, "tns");
+
+        OMElement pingRequest = fac.createOMElement(PING_REQUEST, omNs);
+
+        for (int i = 0; i < operationList.length; i++) {
+            OMElement operation = fac.createOMElement(PING_OPERATION, omNs);
+            operation.addChild(fac.createOMText(operationList[i]));
+            pingRequest.addChild(operation);
+        }
+        return pingRequest;
+    }
+
+    public static OMElement getPingRequest4() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMNamespace omNs = fac.createOMNamespace(PING_NAMESPACE, "tns");
+
+        return fac.createOMElement(PING_REQUEST, omNs);
+    }
+
+    public static void sendRequest(ServiceClient sender, OMElement request, String description) throws AxisFault {
+        System.out.println("Ping request - " + description);
+        System.out.println(request.toString());
+        OMElement result = sender.sendReceive(request);
+        System.out.println("Ping response ");
+        System.out.println(result.toString());
+        System.out.println();
+    }
+
+    public static void main(String[] args) {
+        try {
+            Options options = new Options();
+            options.setTo(targetEPR);
+            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
+            options.setAction(PING_SOAPACTION);
+
+            ServiceClient sender = new ServiceClient();
+            sender.setOptions(options);
+
+            OMElement pingRequestPayload;
+
+            pingRequestPayload = getPingRequest1(new String[]{"getPrice", "update"});
+            sendRequest(sender, pingRequestPayload, "two operations are specified");
+
+            pingRequestPayload = getPingRequest1(new String[]{"getPrice"});
+            sendRequest(sender, pingRequestPayload, "one operation is specified");
+
+            pingRequestPayload = getPingRequest1(new String[0]);
+            sendRequest(sender, pingRequestPayload, "Service level");
+
+            pingRequestPayload = getPingRequest4();
+            sendRequest(sender, pingRequestPayload,"Service level");
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

Added: webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOnlyMessageReceiver.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOnlyMessageReceiver.java?view=auto&rev=511830
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOnlyMessageReceiver.java (added)
+++ webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOnlyMessageReceiver.java Mon Feb 26 06:49:39 2007
@@ -0,0 +1,73 @@
+package samples.ping.receivers;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.receivers.AbstractInMessageReceiver;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisOperation;
+import org.apache.axis2.engine.MessageReceiver;
+import org.apache.axis2.engine.Pingable;
+import org.apache.axis2.i18n.Messages;
+
+import java.lang.reflect.Method;
+
+public class RawXMLINOnlyMessageReceiver extends AbstractInMessageReceiver
+        implements MessageReceiver, Pingable {
+
+    private Method findOperation(AxisOperation op, Class implClass) {
+        String methodName = op.getName().getLocalPart();
+        Method[] methods = implClass.getMethods();
+
+        for (int i = 0; i < methods.length; i++) {
+            if (methods[i].getName().equals(methodName) &&
+                    methods[i].getParameterTypes().length == 1 &&
+                    OMElement.class.getName().equals(
+                            methods[i].getParameterTypes()[0].getName()) &&
+                    "void".equals(methods[i].getReturnType().getName())) {
+                return methods[i];
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Invokes the bussiness logic invocation on the service implementation class
+     *
+     * @param msgContext the incoming message context
+     * @throws org.apache.axis2.AxisFault on invalid method (wrong signature)
+     */
+    public void invokeBusinessLogic(MessageContext msgContext) throws AxisFault {
+        try {
+
+            // get the implementation class for the Web Service
+            Object obj = getTheImplementationObject(msgContext);
+
+            // find the WebService method
+            Class implClass = obj.getClass();
+
+            AxisOperation op = msgContext.getAxisOperation();
+            Method method = findOperation(op, implClass);
+
+            if (method != null) {
+                method.invoke(
+                        obj, new Object[]{msgContext.getEnvelope().getBody().getFirstElement()});
+            } else {
+                throw new AxisFault(Messages.getMessage("methodDoesNotExistInOnly"));
+            }
+
+        } catch (Exception e) {
+            throw AxisFault.makeFault(e);
+        }
+    }
+
+    public int ping() throws AxisFault {
+        // get the implementation class for the Web Service
+        Object obj = getTheImplementationObject(MessageContext.getCurrentMessageContext());
+
+        if(obj instanceof Pingable){
+            return ((Pingable)obj).ping();
+        }
+        return PING_MR_LEVEL;
+    }
+}

Added: webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOutMessageReceiver.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOutMessageReceiver.java?view=auto&rev=511830
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOutMessageReceiver.java (added)
+++ webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/receivers/RawXMLINOutMessageReceiver.java Mon Feb 26 06:49:39 2007
@@ -0,0 +1,85 @@
+package samples.ping.receivers;
+
+import org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver;
+import org.apache.axis2.engine.MessageReceiver;
+import org.apache.axis2.description.AxisOperation;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.i18n.Messages;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axis2.engine.Pingable;
+
+import java.lang.reflect.Method;
+
+public class RawXMLINOutMessageReceiver extends AbstractInOutSyncMessageReceiver
+        implements MessageReceiver, Pingable {
+
+    private Method findOperation(AxisOperation op, Class implClass) {
+        String methodName = op.getName().getLocalPart();
+        Method[] methods = implClass.getMethods();
+
+        for (int i = 0; i < methods.length; i++) {
+            if (methods[i].getName().equals(methodName) &&
+                    methods[i].getParameterTypes().length == 1 &&
+                    OMElement.class.getName().equals(
+                            methods[i].getParameterTypes()[0].getName()) &&
+                    OMElement.class.getName().equals(methods[i].getReturnType().getName())) {
+                return methods[i];
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Invokes the bussiness logic invocation on the service implementation class
+     *
+     * @param msgContext    the incoming message context
+     * @param newmsgContext the response message context
+     * @throws org.apache.axis2.AxisFault on invalid method (wrong signature) or behaviour (return null)
+     */
+    public void invokeBusinessLogic(MessageContext msgContext, MessageContext newmsgContext)
+            throws AxisFault {
+        try {
+
+            // get the implementation class for the Web Service
+            Object obj = getTheImplementationObject(msgContext);
+
+            // find the WebService method
+            Class implClass = obj.getClass();
+
+            AxisOperation opDesc = msgContext.getAxisOperation();
+            Method method = findOperation(opDesc, implClass);
+
+            if (method != null) {
+                OMElement result = (OMElement) method.invoke(
+                        obj, new Object[]{msgContext.getEnvelope().getBody().getFirstElement()});
+                SOAPFactory fac = getSOAPFactory(msgContext);
+                SOAPEnvelope envelope = fac.getDefaultEnvelope();
+
+                if (result != null) {
+                    envelope.getBody().addChild(result);
+                }
+
+                newmsgContext.setEnvelope(envelope);
+
+            } else {
+                throw new AxisFault(Messages.getMessage("methodDoesNotExistInOut"));
+            }
+        } catch (Exception e) {
+            throw AxisFault.makeFault(e);
+        }
+    }
+
+    public int ping() throws AxisFault{
+        // get the implementation class for the Web Service
+        Object obj = getTheImplementationObject(MessageContext.getCurrentMessageContext());
+
+        if(obj instanceof Pingable){
+            return ((Pingable)obj).ping();
+        }
+        return PING_MR_LEVEL;
+    }
+}

Added: webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/service/PingService.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/service/PingService.java?view=auto&rev=511830
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/service/PingService.java (added)
+++ webservices/axis2/trunk/java/modules/samples/ping/src/samples/ping/service/PingService.java Mon Feb 26 06:49:39 2007
@@ -0,0 +1,86 @@
+/*
+* Copyright 2004,2005 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*      http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package samples.ping.service;
+
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.engine.Pingable;
+
+import org.apache.axis2.AxisFault;
+
+import java.util.HashMap;
+
+public class PingService implements Pingable {
+
+    private HashMap map = new HashMap();
+
+    public OMElement getPrice(OMElement element) throws XMLStreamException {
+        element.build();
+        element.detach();
+
+        OMElement symbolElement = element.getFirstElement();
+        String symbol = symbolElement.getText();
+
+        String returnText = "42";
+        Double price = (Double) map.get(symbol);
+        if (price != null) {
+            returnText = "" + price.doubleValue();
+        }
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMNamespace omNs =
+                fac.createOMNamespace("http://quickstart.samples/xsd", "ns");
+        OMElement method = fac.createOMElement("getPriceResponse", omNs);
+        OMElement value = fac.createOMElement("return", omNs);
+        value.addChild(fac.createOMText(value, returnText));
+        method.addChild(value);
+        return method;
+    }
+
+    public void update(OMElement element) throws XMLStreamException {
+        element.build();
+        element.detach();
+
+        OMElement symbolElement = element.getFirstElement();
+        String symbol = symbolElement.getText();
+
+        OMElement priceElement = (OMElement) symbolElement.getNextOMSibling();
+        String price = priceElement.getText();
+
+        map.put(symbol, new Double(price));
+    }
+
+    public int ping() throws AxisFault {
+        MessageContext msgContext = MessageContext.getCurrentMessageContext();
+        //operation name to can be accessed in this manner
+        String operationName = (String)msgContext.getProperty(OPERATION_TO_PING);
+
+        //Implementation of this method depends on the particular service
+        if(operationName != null && operationName.equals("getPrice")){
+            //For this sample
+            return PING_SUCCESSFUL;
+        }else if(operationName != null && operationName.equals("update")){
+            return PING_FAILD;
+        }
+        return PING_FAILD;
+    }
+}
+



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