KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > jaxrpc > 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.jaxrpc;
18
19 import org.apache.axis.encoding.XMLType;
20 import org.apache.axis.utils.Options;
21
22 import javax.xml.namespace.QName JavaDoc;
23 import javax.xml.rpc.Call JavaDoc;
24 import javax.xml.rpc.ParameterMode JavaDoc;
25 import javax.xml.rpc.Service JavaDoc;
26 import javax.xml.rpc.ServiceFactory JavaDoc;
27 import java.net.URL JavaDoc;
28
29 /**
30  * This version of the ever so popular GetQuote is a near-duplicate of
31  * the GetQuote1 method in samples/stock which shows how to use the AXIS
32  * client APIs with and without WSDL. This version is strictly JAX-RPC
33  * compliant. It uses no AXIS enhancements.
34  *
35  * This sample supports the use of the standard options too (-p ...)
36  *
37  * @author Russell Butek (butek@us.ibm.com)
38  */

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

46     public float getQuote1(String JavaDoc args[]) throws Exception JavaDoc {
47         Options opts = new Options(args);
48
49         args = opts.getRemainingArgs();
50
51         if (args == null) {
52             System.err.println("Usage: GetQuote <symbol>");
53             System.exit(1);
54         }
55
56         /* Define the service QName and port QName */
57         /*******************************************/
58         QName JavaDoc servQN = new QName JavaDoc("urn:xmltoday-delayed-quotes",
59                 "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 JavaDoc service = ServiceFactory.newInstance().createService(
65                 new URL JavaDoc("file:samples/stock/GetQuote.wsdl"), servQN);
66         Call JavaDoc call = service.createCall(portQN, "getQuote");
67
68         /* Strange - but allows the user to change just certain portions of */
69         /* the URL we're gonna use to invoke the service. Useful when you */
70         /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
71         /********************************************************************/
72         opts.setDefaultURL(call.getTargetEndpointAddress());
73         call.setTargetEndpointAddress(opts.getURL());
74
75         /* Define some service specific properties */
76         /*******************************************/
77         call.setProperty(Call.USERNAME_PROPERTY, opts.getUser());
78         call.setProperty(Call.PASSWORD_PROPERTY, opts.getPassword());
79
80         /* Get symbol and invoke the service */
81         /*************************************/
82         Object JavaDoc result = call.invoke(new Object JavaDoc[] {symbol = args[0]});
83
84         return ((Float JavaDoc) result).floatValue();
85     } // getQuote1
86

87     /**
88      * This will do everything manually (ie. no WSDL).
89      */

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

134     /**
135      * This method does the same thing that getQuote1 does, but in
136      * addition it reuses the Call object to make another call.
137      */

138     public float getQuote3(String JavaDoc args[]) throws Exception JavaDoc {
139         Options opts = new Options(args);
140
141         args = opts.getRemainingArgs();
142
143         if (args == null) {
144             System.err.println("Usage: GetQuote <symbol>");
145             System.exit(1);
146         }
147
148         /* Define the service QName and port QName */
149         /*******************************************/
150         QName JavaDoc servQN = new QName JavaDoc("urn:xmltoday-delayed-quotes",
151                 "GetQuoteService");
152         QName JavaDoc portQN = new QName JavaDoc("urn:xmltoday-delayed-quotes", "GetQuote");
153
154         /* Now use those QNames as pointers into the WSDL doc */
155         /******************************************************/
156         Service JavaDoc service = ServiceFactory.newInstance().createService(
157                 new URL JavaDoc("file:samples/stock/GetQuote.wsdl"), servQN);
158         Call JavaDoc call = service.createCall(portQN, "getQuote");
159
160         /* Strange - but allows the user to change just certain portions of */
161         /* the URL we're gonna use to invoke the service. Useful when you */
162         /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
163         /********************************************************************/
164         opts.setDefaultURL(call.getTargetEndpointAddress());
165         call.setTargetEndpointAddress(opts.getURL());
166
167         /* Define some service specific properties */
168         /*******************************************/
169         call.setProperty(Call.USERNAME_PROPERTY, opts.getUser());
170         call.setProperty(Call.PASSWORD_PROPERTY, opts.getPassword());
171
172         /* Get symbol and invoke the service */
173         /*************************************/
174         Object JavaDoc result = call.invoke(new Object JavaDoc[] {symbol = args[0]});
175
176         /* Reuse the Call object for a different call */
177         /**********************************************/
178         call.setOperationName(new QName JavaDoc("urn:xmltoday-delayed-quotes", "test"));
179         call.removeAllParameters();
180         call.setReturnType(XMLType.XSD_STRING);
181
182         System.out.println(call.invoke(new Object JavaDoc[]{}));
183         return ((Float JavaDoc) result).floatValue();
184     } // getQuote3
185

186     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
187         String JavaDoc save_args[] = new String JavaDoc[args.length];
188         float val;
189         GetQuote1 gq = new GetQuote1();
190
191         /* Call the getQuote() that uses the WDSL */
192         /******************************************/
193         System.out.println("Using WSDL");
194         System.arraycopy(args, 0, save_args, 0, args.length);
195         val = gq.getQuote1(args);
196         System.out.println(gq.symbol + ": " + val);
197
198         /* Call the getQuote() that does it all manually */
199         /*************************************************/
200         System.out.println("Manually");
201         System.arraycopy(save_args, 0, args, 0, args.length);
202         val = gq.getQuote2(args);
203         System.out.println(gq.symbol + ": " + val);
204
205         /* Call the getQuote() that uses Axis's generated WSDL */
206         /*******************************************************/
207         System.out.println("WSDL + Reuse Call");
208         System.arraycopy(save_args, 0, args, 0, args.length);
209         val = gq.getQuote3(args);
210         System.out.println(gq.symbol + ": " + val);
211     } // main
212
}
213
Popular Tags