KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > bidbuy > TestClient


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package samples.bidbuy ;
18
19 import org.apache.axis.client.Call;
20 import org.apache.axis.client.Service;
21 import org.apache.axis.encoding.XMLType;
22 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
23 import org.apache.axis.encoding.ser.BeanSerializerFactory;
24 import org.apache.axis.utils.Options;
25
26 import javax.xml.namespace.QName JavaDoc;
27 import javax.xml.rpc.ParameterMode JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Calendar JavaDoc;
30
31
32 /**
33  * Test Client for the echo interop service. See the main entrypoint
34  * for more details on usage.
35  *
36  * @author Sam Ruby <rubys@us.ibm.com>
37  */

38 public class TestClient {
39
40     private static Service service = null ;
41     private static Call call = null;
42
43     /**
44      * Test an echo method. Declares success if the response returns
45      * true with an Object.equal comparison with the object to be sent.
46      * @param method name of the method to invoke
47      * @param toSend object of the correct type to be sent
48      */

49     private static void test(String JavaDoc method, Object JavaDoc toSend) {
50
51     }
52
53     /**
54      * Main entry point. Tests a variety of echo methods and reports
55      * on their results.
56      *
57      * Arguments are of the form:
58      * -h localhost -p 8080 -s /soap/servlet/rpcrouter
59      */

60     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
61         // set up the call object
62
Options opts = new Options(args);
63         service = new Service();
64         call = (Call) service.createCall();
65         call.setTargetEndpointAddress( new URL JavaDoc(opts.getURL()) );
66         call.setUseSOAPAction(true);
67         call.setSOAPActionURI("http://www.soapinterop.org/Bid");
68
69         // register the PurchaseOrder class
70
QName JavaDoc poqn = new QName JavaDoc("http://www.soapinterop.org/Bid",
71                                "PurchaseOrder");
72         Class JavaDoc cls = PurchaseOrder.class;
73         call.registerTypeMapping(cls, poqn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
74
75         // register the Address class
76
QName JavaDoc aqn = new QName JavaDoc("http://www.soapinterop.org/Bid", "Address");
77         cls = Address.class;
78         call.registerTypeMapping(cls, aqn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
79
80         // register the LineItem class
81
QName JavaDoc liqn = new QName JavaDoc("http://www.soapinterop.org/Bid", "LineItem");
82         cls = LineItem.class;
83         call.registerTypeMapping(cls, liqn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
84
85         try {
86             // Default return type based on what we expect
87
call.setOperationName( new QName JavaDoc("http://www.soapinterop.org/Bid", "Buy" ));
88             call.addParameter( "PO", poqn, ParameterMode.IN );
89             call.setReturnType( XMLType.XSD_STRING );
90
91             LineItem[] li = new LineItem[2];
92             li[0] = new LineItem("Tricorder", 1, "2500.95");
93             li[1] = new LineItem("Phasor", 3, "7250.95");
94
95             PurchaseOrder po = new PurchaseOrder(
96               "NCC-1701",
97               Calendar.getInstance(),
98               new Address("Sam Ruby", "Home", "Raleigh", "NC", "27676"),
99               new Address("Lou Gerstner", "Work", "Armonk", "NY", "15222"),
100               li
101             );
102
103             // issue the request
104
String JavaDoc receipt = (String JavaDoc) call.invoke( new Object JavaDoc[] {po} );
105
106             System.out.println(receipt);
107         } catch (Exception JavaDoc e) {
108            System.out.println("Buy failed: " + e);
109             throw e;
110         }
111     }
112
113 }
114
Popular Tags