/** * A simple test that shows SOAP working. * * (With soaprmibeta6) * NOTE: On my Celeron 400/66 I do about 1000 string encodes AND decodes * per 1.1 seconds. * I also do about one RPC call (client-server-client) per 4.8ms. (1000) * I also do about one RPC call (client-server-client) per 3.6ms. (10000) * I also do about one RPC call (client-server-client) per 3.3ms. (100000) * * (With soaprmibeta6) (re-use MethodInvoker!!!) * I do about one RPC call (client-server-client) per 2.335ms. (1000 tries) * I do about one RPC call (client-server-client) per 1.384ms. (10000 tries) * I do about one RPC call (client-server-client) per 1.221ms. (100000 tries) */ public class HelloWorld extends TestCase { SerializeContext sctx; DeserializeContext dctx; SoapServletDispatcher soapServletDispatcher = null; Port receiverPort = null; public HelloWorld(String name) { super(name); } public static Test suite() { return new TestSuite(HelloWorld.class); } protected void setUp() throws SoapException { sctx = Soap.getDefault().createSerializeContext(); sctx.setDefaultEncodingStyle(SoapEnc.getDefault()); sctx.setSoapStyle(SoapStyle.IBMSOAP); //MSSOAP dctx = Soap.getDefault().createDeserializeContext(); dctx.setDefaultEncodingStyle(SoapEnc.getDefault()); //System.out.println("defenc:" + SoapEnc.getDefault().toString()); }
/** * Tests transmit/receive of a String object. */ public void testString() throws SoapException, IOException { if (true) return; setUp(); //for (int i=0; i < 1000; ++i) { for (int i=0; i < 1; ++i) { testString("test123", null); } } public void testString(String value, String name) throws SoapException, IOException { StringWriter writer = new StringWriter(); sctx.setWriter(writer); String sname = sctx.writeStartTag( "testMethodString", "urn:test-ns", "foo", true, null); if(name == null) { sctx.writeString(value); } else { sctx.writeString(value, name); } sctx.writeEndTag(sname); sctx.close(); String result = writer.toString(); System.out.println("Write:'\n"+result+"\n'"); // setup deserializer StringReader reader = new StringReader(result); dctx.setReader(reader); try { XmlPullParser pp = dctx.getPullParser(); StartTag stag = dctx.getStartTag(); if(pp.next() != XmlPullParser.START_TAG) throw new DeserializeException("expected start tag"); pp.readStartTag(stag); } catch(XmlPullParserException ex) { throw new DeserializeException( "xml parsing exception when deserializing", ex); } // read back from stream String s = dctx.readString(); dctx.close(); //assertEquals(value, s); //System.out.println("Read back:'\n"+s+"\n'"); } public static void main(String[] args) throws Exception { HelloWorld helloWorld = new HelloWorld("na"); helloWorld.setUp(); System.out.println("Return value:" + helloWorld.testRPC("value", "name")); } /** * Tests calling an RPC. */ public void testRPC() throws SoapException, XmlMapException, StructException, NoSuchMethodException, IOException { setUp(); System.out.println("Return value:" + testRPC("value", "name")); } public String testRPC(String value, String name) throws SoapException, XmlMapException, StructException, NoSuchMethodException, IOException { if (false) return new String("not done yet"); // lets first set up mapping - in future just unmarshal xml file XmlJavaMapping xmlJavaMapping = new XmlJavaMapping(); Class klass = RPCTest.class; Method method = klass.getMethod("test", new Class[]{String.class}); XmlJavaOperationMap[] operationsMap = { new XmlJavaOperationMap("test", "testResponse", method) }; xmlJavaMapping.mapPortType( "urn:soaprmi-tests", "test-port-type", RPCTestIfc.class, operationsMap, false); // now examine mappings XmlJavaPortTypeMap portMap = xmlJavaMapping.queryPortType("urn:soaprmi-tests", "test-port-type"); //System.out.println("java interface type ="+portMap.getJavaType()); XmlJavaOperationMap oMap = portMap.queryMethodRequest("test"); //System.out.println("java method: "+oMap.getJavaName()); // Let's do a real RPC try { // Create the MethodDispatcher Object target = new Object(); Port port = new Port(); String requestName = "test"; // Done above... //Class klass = RPCTest.class; //Method method = klass.getMethod(requestName, // new Class[]{String.class}); XmlJavaOperationMap operationsMap2 = new XmlJavaOperationMap("test", "testResponse", method); //xmlJavaMapping.mapPortType( "urn:soaprmi-tests", "test-port-type", // RPCTestIfc.class, operationsMap, false); XmlJavaTypeMap xmlJavaTypeMap = xmlJavaMapping.mapType("http://schemas.xmlsoap.org/soap/encoding/", "http://schemas.xmlsoap.org/soap/envelope/", "Envelope", RPCTest.class, false, false, true); XmlJavaTypeMap typeMap = xmlJavaMapping.queryTypeMap( "http://schemas.xmlsoap.org/soap/encoding/", "http://schemas.xmlsoap.org/soap/envelope/", "Envelope"); //System.out.println("***typeMap:" + typeMap.toString()); //System.out.println("***typeMap encStyle:" + // typeMap.getEncodingStyle()); // Create Method Invoker Binding binding = new Binding(); binding.setName("test"); Endpoint endpoint = new Endpoint(); endpoint.setBinding(binding); //System.out.println("Point0"); //**MethodInvoker methodInvoker = MethodInvoker.makeMethodInvoker(port, //** endpoint, method, xmlJavaMapping); Object[] parameters = { new String("parameter") };
东西太多了,又不能重贴三次.呵~~ 下面是包, package test.soaprmi;import java.io.*; import java.util.*; import java.util.Arrays; import java.lang.reflect.Method;import junit.framework.*;import soaprmi.mapping.*; import soaprmi.port.*; import soaprmi.soap.*; import soaprmi.soaprpc.*; import soaprmi.soapenc.SoapEnc; import soaprmi.struct.*; import soaprmi.util.*; import xpp.*;还有一段: try { initSoap("http://localhost/login"); } catch(Exception e) { e.printStackTrace(); } int i=0; MethodInvoker methodInvoker = MethodInvoker.makeMethodInvoker(port, endpoint, method, xmlJavaMapping); long startTime = System.currentTimeMillis(); for (i=0; i < 1000; ++i) { StringWriter writer = new StringWriter(); methodInvoker.sendRequest(parameters, writer); //System.out.println("Point0.1"); String request = writer.toString(); //System.out.println("*****SOAP Request***********"); //System.out.println(request); //System.out.println("*****SOAP Request***********"); //dctx.setReader(new StringReader(request)); // Dispatch a request //System.out.println("Point1"); //System.out.println(getClass()+" new request"); StringReader sreader = new StringReader(request); StringWriter swriter = new StringWriter(); Hashtable hash = new Hashtable(); //hash.put("provides.path", "http://localhost/login"); soapServletDispatcher.dispatch(sreader, swriter, hash); //System.out.println(" response to send = "+swriter.toString()); // Read the response Object response = methodInvoker.receiveResponse(new StringReader(swriter.toString())); //System.out.println("Response:" + response); writer.close(); //dctx.close(); } long endTime = System.currentTimeMillis(); System.out.println("Count:" + i + ", time (ms):" + (endTime-startTime)); } catch(soaprmi.ServerException e) { e.printStackTrace(); } catch(soaprmi.RemoteException e2) { e2.printStackTrace(); } catch(xpp.XmlPullParserException e3) { e3.printStackTrace(); } return new String("blah"); } protected void initSoap(String location) throws Exception { soapServletDispatcher = new SoapServletDispatcher(location); SoapServices services = new SoapServices(); services.addDispatcher(soapServletDispatcher); RPCTest rpcTest = new RPCTest(); receiverPort = services.createPort( "test-service", // portName RPCTestIfc.class, // portType rpcTest // implementation of port type ); }}
* A simple test that shows SOAP working.
*
* (With soaprmibeta6)
* NOTE: On my Celeron 400/66 I do about 1000 string encodes AND decodes
* per 1.1 seconds.
* I also do about one RPC call (client-server-client) per 4.8ms. (1000)
* I also do about one RPC call (client-server-client) per 3.6ms. (10000)
* I also do about one RPC call (client-server-client) per 3.3ms. (100000)
*
* (With soaprmibeta6) (re-use MethodInvoker!!!)
* I do about one RPC call (client-server-client) per 2.335ms. (1000 tries)
* I do about one RPC call (client-server-client) per 1.384ms. (10000 tries)
* I do about one RPC call (client-server-client) per 1.221ms. (100000 tries)
*/
public class HelloWorld extends TestCase { SerializeContext sctx; DeserializeContext dctx; SoapServletDispatcher soapServletDispatcher = null;
Port receiverPort = null; public HelloWorld(String name) {
super(name); }
public static Test suite() {
return new TestSuite(HelloWorld.class);
} protected void setUp() throws SoapException {
sctx = Soap.getDefault().createSerializeContext();
sctx.setDefaultEncodingStyle(SoapEnc.getDefault());
sctx.setSoapStyle(SoapStyle.IBMSOAP); //MSSOAP
dctx = Soap.getDefault().createDeserializeContext();
dctx.setDefaultEncodingStyle(SoapEnc.getDefault());
//System.out.println("defenc:" +
SoapEnc.getDefault().toString());
}
/**
* Tests transmit/receive of a String object.
*/
public void testString() throws SoapException, IOException {
if (true)
return;
setUp();
//for (int i=0; i < 1000; ++i) {
for (int i=0; i < 1; ++i) {
testString("test123", null);
}
} public void testString(String value, String name)
throws SoapException, IOException {
StringWriter writer = new StringWriter();
sctx.setWriter(writer);
String sname = sctx.writeStartTag(
"testMethodString", "urn:test-ns", "foo", true, null);
if(name == null) {
sctx.writeString(value);
} else {
sctx.writeString(value, name);
}
sctx.writeEndTag(sname);
sctx.close();
String result = writer.toString(); System.out.println("Write:'\n"+result+"\n'"); // setup deserializer
StringReader reader = new StringReader(result); dctx.setReader(reader); try {
XmlPullParser pp = dctx.getPullParser();
StartTag stag = dctx.getStartTag();
if(pp.next() != XmlPullParser.START_TAG)
throw new DeserializeException("expected
start tag");
pp.readStartTag(stag);
} catch(XmlPullParserException ex) {
throw new DeserializeException(
"xml parsing exception when deserializing",
ex); }
// read back from stream
String s = dctx.readString();
dctx.close();
//assertEquals(value, s); //System.out.println("Read back:'\n"+s+"\n'");
} public static void main(String[] args) throws Exception {
HelloWorld helloWorld = new HelloWorld("na");
helloWorld.setUp();
System.out.println("Return value:" +
helloWorld.testRPC("value", "name"));
} /**
* Tests calling an RPC.
*/
public void testRPC() throws SoapException, XmlMapException,
StructException, NoSuchMethodException, IOException {
setUp();
System.out.println("Return value:" + testRPC("value",
"name")); } public String testRPC(String value, String name)
throws SoapException, XmlMapException, StructException,
NoSuchMethodException, IOException {
if (false)
return new String("not done yet"); // lets first set up mapping - in future just unmarshal xml
file XmlJavaMapping xmlJavaMapping = new XmlJavaMapping(); Class klass = RPCTest.class;
Method method = klass.getMethod("test", new
Class[]{String.class});
XmlJavaOperationMap[] operationsMap = {
new XmlJavaOperationMap("test", "testResponse",
method) }; xmlJavaMapping.mapPortType( "urn:soaprmi-tests",
"test-port-type",
RPCTestIfc.class, operationsMap, false); // now examine mappings
XmlJavaPortTypeMap portMap =
xmlJavaMapping.queryPortType("urn:soaprmi-tests",
"test-port-type");
//System.out.println("java interface type
="+portMap.getJavaType());
XmlJavaOperationMap oMap = portMap.queryMethodRequest("test");
//System.out.println("java method: "+oMap.getJavaName());
// Let's do a real RPC
try {
// Create the MethodDispatcher
Object target = new Object();
Port port = new Port();
String requestName = "test"; // Done above...
//Class klass = RPCTest.class;
//Method method = klass.getMethod(requestName,
// new Class[]{String.class});
XmlJavaOperationMap operationsMap2 =
new XmlJavaOperationMap("test",
"testResponse", method); //xmlJavaMapping.mapPortType( "urn:soaprmi-tests",
"test-port-type",
// RPCTestIfc.class, operationsMap, false);
XmlJavaTypeMap xmlJavaTypeMap =
xmlJavaMapping.mapType("http://schemas.xmlsoap.org/soap/encoding/",
"http://schemas.xmlsoap.org/soap/envelope/", "Envelope", RPCTest.class,
false, false, true); XmlJavaTypeMap typeMap = xmlJavaMapping.queryTypeMap(
"http://schemas.xmlsoap.org/soap/encoding/",
"http://schemas.xmlsoap.org/soap/envelope/",
"Envelope");
//System.out.println("***typeMap:" +
typeMap.toString());
//System.out.println("***typeMap encStyle:" +
// typeMap.getEncodingStyle()); // Create Method Invoker
Binding binding = new Binding();
binding.setName("test");
Endpoint endpoint = new Endpoint();
endpoint.setBinding(binding);
//System.out.println("Point0");
//**MethodInvoker methodInvoker =
MethodInvoker.makeMethodInvoker(port,
//** endpoint, method, xmlJavaMapping);
Object[] parameters = { new String("parameter") };
下面是包,
package test.soaprmi;import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.lang.reflect.Method;import junit.framework.*;import soaprmi.mapping.*;
import soaprmi.port.*;
import soaprmi.soap.*;
import soaprmi.soaprpc.*;
import soaprmi.soapenc.SoapEnc;
import soaprmi.struct.*;
import soaprmi.util.*;
import xpp.*;还有一段: try {
initSoap("http://localhost/login");
} catch(Exception e) {
e.printStackTrace();
}
int i=0;
MethodInvoker methodInvoker =
MethodInvoker.makeMethodInvoker(port,
endpoint,
method, xmlJavaMapping);
long startTime = System.currentTimeMillis();
for (i=0; i < 1000; ++i) {
StringWriter writer = new StringWriter();
methodInvoker.sendRequest(parameters, writer);
//System.out.println("Point0.1"); String request = writer.toString(); //System.out.println("*****SOAP
Request***********");
//System.out.println(request);
//System.out.println("*****SOAP
Request***********");
//dctx.setReader(new StringReader(request)); // Dispatch a request
//System.out.println("Point1"); //System.out.println(getClass()+" new
request"); StringReader sreader = new
StringReader(request);
StringWriter swriter = new StringWriter();
Hashtable hash = new Hashtable();
//hash.put("provides.path",
"http://localhost/login");
soapServletDispatcher.dispatch(sreader,
swriter, hash);
//System.out.println(" response to send =
"+swriter.toString()); // Read the response
Object response =
methodInvoker.receiveResponse(new StringReader(swriter.toString()));
//System.out.println("Response:" + response);
writer.close();
//dctx.close();
}
long endTime = System.currentTimeMillis();
System.out.println("Count:" + i + ", time (ms):" +
(endTime-startTime));
} catch(soaprmi.ServerException e) {
e.printStackTrace();
} catch(soaprmi.RemoteException e2) {
e2.printStackTrace();
} catch(xpp.XmlPullParserException e3) {
e3.printStackTrace();
}
return new String("blah");
} protected void initSoap(String location) throws Exception { soapServletDispatcher = new SoapServletDispatcher(location); SoapServices services = new SoapServices();
services.addDispatcher(soapServletDispatcher); RPCTest rpcTest = new RPCTest();
receiverPort = services.createPort(
"test-service", // portName
RPCTestIfc.class, // portType
rpcTest // implementation of port type
);
}}
hayy