KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > bidbuy > v3


1 package samples.bidbuy ;
2
3 import org.apache.axis.client.Call;
4 import org.apache.axis.client.Service;
5 import org.apache.axis.encoding.XMLType;
6 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
7 import org.apache.axis.encoding.ser.BeanSerializerFactory;
8
9 import javax.xml.namespace.QName JavaDoc;
10 import javax.xml.rpc.ParameterMode JavaDoc;
11 import java.math.BigDecimal JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.Calendar JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 public class v3 implements vInterface {
18   public void register(String JavaDoc registryURL, samples.bidbuy.Service s)
19                  throws Exception JavaDoc {
20     try {
21       Service service = new Service();
22       Call call = (Call) service.createCall();
23
24       call.setTargetEndpointAddress( new URL JavaDoc(registryURL) );
25       call.setOperationName( new QName JavaDoc("http://www.soapinterop.org/Register", "Register" ));
26       call.addParameter("ServiceName", XMLType.XSD_STRING, ParameterMode.IN);
27       call.addParameter("ServiceUrl", XMLType.XSD_STRING, ParameterMode.IN);
28       call.addParameter("ServiceType", XMLType.XSD_STRING, ParameterMode.IN);
29       call.addParameter("ServiceWSDL", XMLType.XSD_STRING, ParameterMode.IN);
30       
31       call.invoke( new Object JavaDoc[] { s.getServiceName(), s.getServiceUrl(),
32                                   s.getServiceType(), s.getServiceWsdl() } );
33     }
34     catch( Exception JavaDoc e ) {
35       e.printStackTrace();
36       throw e ;
37     }
38   }
39
40   public void unregister(String JavaDoc registryURL, String JavaDoc name) throws Exception JavaDoc {
41     try {
42       Service service = new Service();
43       Call call = (Call) service.createCall();
44
45       call.setTargetEndpointAddress( new URL JavaDoc(registryURL) );
46       call.setOperationName( new QName JavaDoc("http://www.soapinterop.org/Unregister", "Unregister" ));
47       call.addParameter( "ServiceName", XMLType.XSD_STRING, ParameterMode.IN);
48       call.invoke( new Object JavaDoc[] { name } );
49     }
50     catch( Exception JavaDoc e ) {
51       e.printStackTrace();
52       throw e ;
53     }
54   }
55
56   public Boolean JavaDoc ping(String JavaDoc serverURL) throws Exception JavaDoc {
57     try {
58       Service service = new Service();
59       Call call = (Call) service.createCall();
60
61       call.setTargetEndpointAddress( new URL JavaDoc(serverURL) );
62       call.setUseSOAPAction( true );
63       call.setSOAPActionURI( "http://www.soapinterop.org/Ping" );
64       call.setOperationName( new QName JavaDoc("http://www.soapinterop.org/Bid", "Ping" ));
65       call.invoke( (Object JavaDoc[]) null );
66       return( new Boolean JavaDoc(true) );
67     }
68     catch( Exception JavaDoc e ) {
69       e.printStackTrace();
70       throw e ;
71     }
72   }
73
74   public Vector JavaDoc lookupAsString(String JavaDoc registryURL) throws Exception JavaDoc
75   {
76     try {
77       Service service = new Service();
78       Call call = (Call) service.createCall();
79
80       call.setTargetEndpointAddress( new URL JavaDoc(registryURL) );
81       call.setUseSOAPAction( true );
82       call.setSOAPActionURI( "http://www.soapinterop.org/LookupAsString" );
83       call.setOperationName( new QName JavaDoc("http://www.soapinterop.org/Registry", "LookupAsString" ));
84       call.addParameter( "ServiceType", XMLType.XSD_STRING, ParameterMode.IN);
85       call.setReturnType( XMLType.XSD_DOUBLE );
86
87       String JavaDoc res= (String JavaDoc) call.invoke( new Object JavaDoc[] { "Bid" } );
88
89       if ( res == null ) return( null );
90       StringTokenizer JavaDoc lineParser = new StringTokenizer JavaDoc( res, "\n" );
91
92       Vector JavaDoc services = new Vector JavaDoc();
93       while ( lineParser.hasMoreTokens() ) {
94         String JavaDoc line = (String JavaDoc) lineParser.nextElement();
95         StringTokenizer JavaDoc wordParser = new StringTokenizer JavaDoc( line, "\t" );
96         samples.bidbuy.Service s = null ;
97
98         for ( int i = 0 ; wordParser.hasMoreTokens() && i < 4 ; i++ )
99           switch(i) {
100             case 0 : s = new samples.bidbuy.Service();
101                      if ( services == null ) services = new Vector JavaDoc();
102                      services.add( s );
103                      s.setServiceName( (String JavaDoc) wordParser.nextToken());
104                      break ;
105             case 1 : s.setServiceUrl( (String JavaDoc) wordParser.nextToken());
106                      break ;
107             case 2 : s.setServiceType( (String JavaDoc) wordParser.nextToken());
108                      break ;
109             case 3 : s.setServiceWsdl( (String JavaDoc) wordParser.nextToken());
110                      break ;
111           }
112       }
113       return( services );
114     }
115     catch( Exception JavaDoc e ) {
116       e.printStackTrace();
117       throw e ;
118     }
119   }
120
121   public double requestForQuote(String JavaDoc serverURL) throws Exception JavaDoc {
122     try {
123
124       Service service = new Service();
125       Call call = (Call) service.createCall();
126
127       call.setTargetEndpointAddress( new URL JavaDoc(serverURL) );
128       call.setOperationName(new QName JavaDoc("http://www.soapinterop.org/Bid", "RequestForQuote") );
129       call.setReturnType( XMLType.XSD_DOUBLE );
130       call.setUseSOAPAction( true );
131       call.setSOAPActionURI( "http://www.soapinterop.org/RequestForQuote" );
132       call.addParameter( "ProductName", XMLType.XSD_STRING, ParameterMode.IN);
133       call.addParameter( "Quantity", XMLType.XSD_INT, ParameterMode.IN);
134       Object JavaDoc r = call.invoke( new Object JavaDoc[] { "widget", new Integer JavaDoc(10) } );
135
136 /*
137       sd.addOutputParam("RequestForQuoteResult",
138                         SOAPTypeMappingRegistry.XSD_DOUBLE);
139       sd.addOutputParam("Result",
140                         SOAPTypeMappingRegistry.XSD_DOUBLE);
141       sd.addOutputParam("return",
142                         SOAPTypeMappingRegistry.XSD_DOUBLE);
143 */

144
145       // ??? if ( r instanceof Float ) r = ((Float)r).toString();
146
if ( r instanceof String JavaDoc ) r = new Double JavaDoc((String JavaDoc) r);
147       Double JavaDoc res = (Double JavaDoc) r ;
148       return( res.doubleValue() );
149     }
150     catch( Exception JavaDoc e ) {
151       e.printStackTrace();
152       throw e ;
153     }
154   }
155
156   public String JavaDoc simpleBuy(String JavaDoc serverURL, int quantity ) throws Exception JavaDoc {
157     try {
158       Service service = new Service();
159       Call call = (Call) service.createCall();
160
161       call.setTargetEndpointAddress( new URL JavaDoc(serverURL) );
162       call.setUseSOAPAction( true );
163       call.setSOAPActionURI( "http://www.soapinterop.org/SimpleBuy" );
164       call.setOperationName( new QName JavaDoc("http://www.soapinterop.org/Bid", "SimpleBuy") );
165       call.setReturnType( XMLType.XSD_STRING );
166       call.addParameter( "Address", XMLType.XSD_STRING, ParameterMode.IN );
167       call.addParameter( "ProductName", XMLType.XSD_STRING, ParameterMode.IN);
168       call.addParameter( "Quantity", XMLType.XSD_INT, ParameterMode.IN );
169       
170       String JavaDoc res = (String JavaDoc) call.invoke(new Object JavaDoc[] { "123 Main St.",
171                                                        "Widget",
172                                                        new Integer JavaDoc(quantity)});
173
174       /* sd.addOutputParam("SimpleBuyResult",
175                         SOAPTypeMappingRegistry.XSD_STRING);
176       sd.addOutputParam("Result",
177                         SOAPTypeMappingRegistry.XSD_STRING);
178       sd.addOutputParam("return",
179                         SOAPTypeMappingRegistry.XSD_STRING); */

180
181       return( res );
182     }
183     catch( Exception JavaDoc e ) {
184       e.printStackTrace();
185       throw e ;
186     }
187   }
188
189   public String JavaDoc buy(String JavaDoc serverURL, int quantity, int numItems, double price)
190       throws Exception JavaDoc
191   {
192     try {
193       int i ;
194
195       Service service = new Service();
196       Call call = (Call) service.createCall();
197
198       call.setTargetEndpointAddress( new URL JavaDoc(serverURL) );
199       call.setUseSOAPAction( true );
200       call.setSOAPActionURI( "http://www.soapinterop.org/Buy" );
201       call.setReturnType( XMLType.XSD_STRING );
202
203       /* sd.addOutputParam("BuyResult",
204                         SOAPTypeMappingRegistry.XSD_STRING);
205       sd.addOutputParam("Result",
206                         SOAPTypeMappingRegistry.XSD_STRING);
207       sd.addOutputParam("return",
208                         SOAPTypeMappingRegistry.XSD_STRING); */

209
210
211       // register the PurchaseOrder class
212
QName JavaDoc poqn = new QName JavaDoc("http://www.soapinterop.org/Bid", "PurchaseOrder");
213       Class JavaDoc cls = PurchaseOrder.class;
214       call.registerTypeMapping(cls, poqn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
215
216       // register the Address class
217
QName JavaDoc aqn = new QName JavaDoc("http://www.soapinterop.org/Bid", "Address");
218       cls = Address.class;
219       call.registerTypeMapping(cls, aqn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
220
221       // register the LineItem class
222
QName JavaDoc liqn = new QName JavaDoc("http://www.soapinterop.org/Bid", "LineItem");
223       cls = LineItem.class;
224       call.registerTypeMapping(cls, liqn, BeanSerializerFactory.class, BeanDeserializerFactory.class);
225
226       LineItem[] lineItems = new LineItem[numItems];
227       
228       
229       for ( i = 0 ; i < numItems ; i++ )
230         lineItems[i] = new LineItem("Widget"+i,quantity,new BigDecimal JavaDoc(price));
231
232       PurchaseOrder po = new PurchaseOrder( "PO1",
233                                              Calendar.getInstance(),
234                                              new Address("Mr Big",
235                                                          "40 Wildwood Lane",
236                                                          "Weston",
237                                                          "CT",
238                                                          "06883"),
239                                              new Address("Mr Big's Dad",
240                                                          "40 Wildwood Lane",
241                                                          "Weston",
242                                                          "CT",
243                                                          "06883"),
244                                              lineItems );
245
246       call.addParameter( "PO", poqn, ParameterMode.IN );
247       call.setOperationName( new QName JavaDoc("http://www.soapinterop.org/Bid", "Buy") );
248
249       String JavaDoc res = (String JavaDoc) call.invoke( new Object JavaDoc[] { po } );
250
251       return( res );
252     }
253     catch( Exception JavaDoc e ) {
254       e.printStackTrace();
255       throw e ;
256     }
257   }
258
259 }
260
261
Popular Tags