KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > stock > GetQuote1


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.stock ;
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.utils.Options;
23
24 import javax.xml.namespace.QName JavaDoc;
25 import javax.xml.rpc.ParameterMode JavaDoc;
26 import java.net.URL JavaDoc;
27
28 /**
29  * This version of the ever so popular GetQuote shows how to use the
30  * Axis client APIs with and without WSDL. The first flavor (getQuote1)
31  * will use WSDL to prefill all of the data about the remote service.
32  * The second one (getQuote2) will do it all manually. Either way the
33  * service is invoked it should produce the exact same request XML and
34  * of course same results.
35  *
36  * This sample supports the use of the standard options too (-p ...)
37  *
38  * @author Doug Davis (dug@us.ibm.com.com)
39  */

40 public class GetQuote1 {
41     public String JavaDoc symbol ;
42     
43     /**
44      * This will use the WSDL to prefill all of the info needed to make
45      * the call. All that's left is filling in the args to invoke().
46      */

47     public float getQuote1(String JavaDoc args[]) throws Exception JavaDoc {
48       Options opts = new Options( args );
49
50       args = opts.getRemainingArgs();
51
52       if ( args == null ) {
53         System.err.println( "Usage: GetQuote <symbol>" );
54         System.exit(1);
55       }
56
57       /* Define the service QName and port QName */
58       /*******************************************/
59       QName JavaDoc servQN = new QName JavaDoc("urn:xmltoday-delayed-quotes","GetQuoteService");
60       QName JavaDoc portQN = new QName JavaDoc("urn:xmltoday-delayed-quotes","GetQuote");
61
62       /* Now use those QNames as pointers into the WSDL doc */
63       /******************************************************/
64       Service service = new Service( new URL JavaDoc("file:GetQuote.wsdl"), servQN );
65       Call call = (Call) service.createCall( portQN, "getQuote" );
66
67       /* Strange - but allows the user to change just certain portions of */
68       /* the URL we're gonna use to invoke the service. Useful when you */
69       /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
70       /********************************************************************/
71       opts.setDefaultURL( call.getTargetEndpointAddress() );
72       call.setTargetEndpointAddress( new URL JavaDoc(opts.getURL()) );
73
74       /* Define some service specific properties */
75       /*******************************************/
76       call.setUsername( opts.getUser() );
77       call.setPassword( opts.getPassword() );
78
79       /* Get symbol and invoke the service */
80       /*************************************/
81         Object JavaDoc result = call.invoke( new Object JavaDoc[] { symbol = args[0] } );
82
83       return( ((Float JavaDoc) result).floatValue() );
84     }
85
86     /**
87      * This will do everything manually (ie. no WSDL).
88      */

89     public float getQuote2(String JavaDoc args[]) throws Exception JavaDoc {
90       Options opts = new Options( args );
91
92       args = opts.getRemainingArgs();
93
94       if ( args == null ) {
95         System.err.println( "Usage: GetQuote <symbol>" );
96         System.exit(1);
97       }
98
99       /* Create default/empty Service and Call object */
100       /************************************************/
101       Service service = new Service();
102       Call call = (Call) service.createCall();
103
104       /* Strange - but allows the user to change just certain portions of */
105       /* the URL we're gonna use to invoke the service. Useful when you */
106       /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
107       /********************************************************************/
108       opts.setDefaultURL( "http://localhost:8080/axis/servlet/AxisServlet" );
109
110       /* Set all of the stuff that would normally come from WSDL */
111       /***********************************************************/
112       call.setTargetEndpointAddress( new URL JavaDoc(opts.getURL()) );
113       call.setUseSOAPAction( true );
114       call.setSOAPActionURI( "getQuote" );
115       call.setEncodingStyle( "http://schemas.xmlsoap.org/soap/encoding/" );
116       call.setOperationName( new QName JavaDoc("urn:xmltoday-delayed-quotes", "getQuote") );
117       call.addParameter( "symbol", XMLType.XSD_STRING, ParameterMode.IN );
118       call.setReturnType( XMLType.XSD_FLOAT );
119
120       /* Define some service specific properties */
121       /*******************************************/
122       call.setUsername( opts.getUser() );
123       call.setPassword( opts.getPassword() );
124
125       /* Get symbol and invoke the service */
126       /*************************************/
127       Object JavaDoc result = call.invoke( new Object JavaDoc[] { symbol = args[0] } );
128
129       return( ((Float JavaDoc) result).floatValue() );
130     }
131
132     /**
133      * This will use the WSDL to prefill all of the info needed to make
134      * the call. All that's left is filling in the args to invoke().
135      */

136     public float getQuote3(String JavaDoc args[]) throws Exception JavaDoc {
137       Options opts = new Options( args );
138
139       args = opts.getRemainingArgs();
140
141       if ( args == null ) {
142         System.err.println( "Usage: GetQuote <symbol>" );
143         System.exit(1);
144       }
145
146       /* Define the service QName and port QName */
147       /*******************************************/
148       QName JavaDoc servQN = new QName JavaDoc("urn:xmltoday-delayed-quotes","GetQuoteService");
149       QName JavaDoc portQN = new QName JavaDoc("urn:xmltoday-delayed-quotes","GetQuote");
150
151       /* Now use those QNames as pointers into the WSDL doc */
152       /******************************************************/
153       Service service = new Service( new URL JavaDoc("file:GetQuote.wsdl"), servQN );
154       Call call = (Call) service.createCall( portQN, "getQuote" );
155
156       /* Strange - but allows the user to change just certain portions of */
157       /* the URL we're gonna use to invoke the service. Useful when you */
158       /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
159       /********************************************************************/
160       opts.setDefaultURL( call.getTargetEndpointAddress() );
161       call.setTargetEndpointAddress( new URL JavaDoc(opts.getURL()) );
162
163       /* Define some service specific properties */
164       /*******************************************/
165       call.setUsername( opts.getUser() );
166       call.setPassword( opts.getPassword() );
167
168       /* Get symbol and invoke the service */
169       /*************************************/
170       Object JavaDoc result = call.invoke( new Object JavaDoc[] { symbol = args[0] } );
171       result = call.invoke( new Object JavaDoc[] { symbol = args[0] } );
172
173       /* Reuse the call object to call the test method */
174       /*************************************************/
175       call.setOperation( portQN, "test" );
176       call.setReturnType( XMLType.XSD_STRING );
177
178       System.out.println( call.invoke(new Object JavaDoc[]{}) );
179
180       return( ((Float JavaDoc) result).floatValue() );
181     }
182
183     public static void main(String JavaDoc args[]) {
184       try {
185           String JavaDoc save_args[] = new String JavaDoc[args.length];
186           float val ;
187           GetQuote1 gq = new GetQuote1();
188
189           /* Call the getQuote() that uses the WDSL */
190           /******************************************/
191           System.out.println("Using WSDL");
192           System.arraycopy( args, 0, save_args, 0, args.length );
193           val = gq.getQuote1( args );
194           System.out.println( gq.symbol + ": " + val );
195
196           /* Call the getQuote() that does it all manually */
197           /*************************************************/
198           System.out.println("Manually");
199           System.arraycopy( save_args, 0, args, 0, args.length );
200           val = gq.getQuote2( args );
201           System.out.println( gq.symbol + ": " + val );
202
203           /* Call the getQuote() that uses Axis's generated WSDL */
204           /*******************************************************/
205           System.out.println("WSDL + Reuse Call");
206           System.arraycopy( save_args, 0, args, 0, args.length );
207           val = gq.getQuote3( args );
208           System.out.println( gq.symbol + ": " + val );
209       }
210       catch( Exception JavaDoc e ) {
211           e.printStackTrace();
212       }
213     }
214 };
215
Popular Tags