Tuesday, January 25, 2005

Axis

Apache Axis is an implementation of the SOAP ("Simple Object Access Protocol") submission to W3C. This project is a follow-on to the Apache SOAP project.

Java Service:
/*

* Cpg.java
*
*/

package web;

/**
*
* @author Zhibin Lu
*/
public class Cpg {

/** Creates a new instance of Cpg */
public Cpg() {
}

public String query(String in) {
return in;
}
}

Java Client:
package web;


import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

public class Client {
public static void main(String [] args) throws Exception {
String endpointURL = "http://localhost:8080/axis/services/CpgServ";
Service service = new Service();
Call call = (Call) service.createCall();

call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
call.setOperationName( new QName("CpgServ", "query") );
call.addParameter( "in", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING );

Object ret = call.invoke( new Object[] { "this is a test" } );
String arr = (String) ret;

System.out.println(arr);
}

}
Deploy:
deploy.xml


<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<service name="CpgServ" provider="java:RPC">
<parameter name="className" value="web.Cpg"/>
<parameter name="allowedMethods" value="query"/>
</service>

</deployment>

Copy the class file into Tomcat axis class directory and run
java org.apache.axis.client.AdminClient deploy.xml

The WSDL file is now located at: http://localhost:8080/axis/services/CpgServ?WSDL

Undeploy:
undeploy.xml

<undeployment xmlns="http://xml.apache.org/axis/wsdd/">
<service name="CpgServ"/>
</undeployment>

Run java org.apache.axis.client.AdminClient undeploy.xml
to undeploy it.

C#:
Install MS Web Services Enhancements (WSE)
Use C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\WseWsdl2.exe http://localhost:8080/axis/services/CpgServ?WSDL \temp\cpgProxy.cs to create C# proxy class.
The result file is cpgProxy.cs:

//------------------------------------------------------------------------------
// <autogenerated>
// This code was generated by a tool.
// Runtime Version: 1.1.4322.2032
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

//
// This source code was auto-generated by wsdl, Version=1.1.4322.2032.
//
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;


/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="CpgServSoapBinding", Namespace="http://localhost:8080/axis/services/CpgServ")]
public class CpgService : System.Web.Services.Protocols.SoapHttpClientProtocol {

/// <remarks/>
public CpgService() {
this.Url = "http://localhost:8080/axis/services/CpgServ";
}

/// <remarks/>
[System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="http://web", ResponseNamespace="http://localhost:8080/axis/services/CpgServ")]
[return: System.Xml.Serialization.SoapElementAttribute("queryReturn")]
public string query(string @in) {
object[] results = this.Invoke("query", new object[] {
@in});
return ((string)(results[0]));
}

/// <remarks/>
public System.IAsyncResult Beginquery(string @in, System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("query", new object[] {
@in}, callback, asyncState);
}

/// <remarks/>
public string Endquery(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((string)(results[0]));
}
}


Use MS Visual Studio or SharpDevelop to generate a Combine and include the csProxy.cs file. When compling, we need to add Microsoft.Web.Services into References.

The client flie:

using System;

namespace Client
{
class MainClass
{
public static void Main(string[] args)
{
CpgService serv = new CpgService();
string ret = serv.query("this is C# client.");
Console.WriteLine(ret);
}
}

}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home