001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package samples.stock;
018:
019: import org.apache.axis.client.Call;
020: import org.apache.axis.client.Service;
021: import org.apache.axis.encoding.XMLType;
022: import org.apache.axis.utils.Options;
023:
024: import javax.xml.namespace.QName;
025: import javax.xml.rpc.ParameterMode;
026: import java.net.URL;
027:
028: /**
029: * This version of the ever so popular GetQuote shows how to use the
030: * Axis client APIs with and without WSDL. The first flavor (getQuote1)
031: * will use WSDL to prefill all of the data about the remote service.
032: * The second one (getQuote2) will do it all manually. Either way the
033: * service is invoked it should produce the exact same request XML and
034: * of course same results.
035: *
036: * This sample supports the use of the standard options too (-p ...)
037: *
038: * @author Doug Davis (dug@us.ibm.com.com)
039: */
040: public class GetQuote1 {
041: public String symbol;
042:
043: /**
044: * This will use the WSDL to prefill all of the info needed to make
045: * the call. All that's left is filling in the args to invoke().
046: */
047: public float getQuote1(String args[]) throws Exception {
048: Options opts = new Options(args);
049:
050: args = opts.getRemainingArgs();
051:
052: if (args == null) {
053: System.err.println("Usage: GetQuote <symbol>");
054: System.exit(1);
055: }
056:
057: /* Define the service QName and port QName */
058: /*******************************************/
059: QName servQN = new QName("urn:xmltoday-delayed-quotes",
060: "GetQuoteService");
061: QName portQN = new QName("urn:xmltoday-delayed-quotes",
062: "GetQuote");
063:
064: /* Now use those QNames as pointers into the WSDL doc */
065: /******************************************************/
066: Service service = new Service(new URL("file:GetQuote.wsdl"),
067: servQN);
068: Call call = (Call) service.createCall(portQN, "getQuote");
069:
070: /* Strange - but allows the user to change just certain portions of */
071: /* the URL we're gonna use to invoke the service. Useful when you */
072: /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
073: /********************************************************************/
074: opts.setDefaultURL(call.getTargetEndpointAddress());
075: call.setTargetEndpointAddress(new URL(opts.getURL()));
076:
077: /* Define some service specific properties */
078: /*******************************************/
079: call.setUsername(opts.getUser());
080: call.setPassword(opts.getPassword());
081:
082: /* Get symbol and invoke the service */
083: /*************************************/
084: Object result = call.invoke(new Object[] { symbol = args[0] });
085:
086: return (((Float) result).floatValue());
087: }
088:
089: /**
090: * This will do everything manually (ie. no WSDL).
091: */
092: public float getQuote2(String args[]) throws Exception {
093: Options opts = new Options(args);
094:
095: args = opts.getRemainingArgs();
096:
097: if (args == null) {
098: System.err.println("Usage: GetQuote <symbol>");
099: System.exit(1);
100: }
101:
102: /* Create default/empty Service and Call object */
103: /************************************************/
104: Service service = new Service();
105: Call call = (Call) service.createCall();
106:
107: /* Strange - but allows the user to change just certain portions of */
108: /* the URL we're gonna use to invoke the service. Useful when you */
109: /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
110: /********************************************************************/
111: opts
112: .setDefaultURL("http://localhost:8080/axis/servlet/AxisServlet");
113:
114: /* Set all of the stuff that would normally come from WSDL */
115: /***********************************************************/
116: call.setTargetEndpointAddress(new URL(opts.getURL()));
117: call.setUseSOAPAction(true);
118: call.setSOAPActionURI("getQuote");
119: call
120: .setEncodingStyle("http://schemas.xmlsoap.org/soap/encoding/");
121: call.setOperationName(new QName("urn:xmltoday-delayed-quotes",
122: "getQuote"));
123: call.addParameter("symbol", XMLType.XSD_STRING,
124: ParameterMode.IN);
125: call.setReturnType(XMLType.XSD_FLOAT);
126:
127: /* Define some service specific properties */
128: /*******************************************/
129: call.setUsername(opts.getUser());
130: call.setPassword(opts.getPassword());
131:
132: /* Get symbol and invoke the service */
133: /*************************************/
134: Object result = call.invoke(new Object[] { symbol = args[0] });
135:
136: return (((Float) result).floatValue());
137: }
138:
139: /**
140: * This will use the WSDL to prefill all of the info needed to make
141: * the call. All that's left is filling in the args to invoke().
142: */
143: public float getQuote3(String args[]) throws Exception {
144: Options opts = new Options(args);
145:
146: args = opts.getRemainingArgs();
147:
148: if (args == null) {
149: System.err.println("Usage: GetQuote <symbol>");
150: System.exit(1);
151: }
152:
153: /* Define the service QName and port QName */
154: /*******************************************/
155: QName servQN = new QName("urn:xmltoday-delayed-quotes",
156: "GetQuoteService");
157: QName portQN = new QName("urn:xmltoday-delayed-quotes",
158: "GetQuote");
159:
160: /* Now use those QNames as pointers into the WSDL doc */
161: /******************************************************/
162: Service service = new Service(new URL("file:GetQuote.wsdl"),
163: servQN);
164: Call call = (Call) service.createCall(portQN, "getQuote");
165:
166: /* Strange - but allows the user to change just certain portions of */
167: /* the URL we're gonna use to invoke the service. Useful when you */
168: /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
169: /********************************************************************/
170: opts.setDefaultURL(call.getTargetEndpointAddress());
171: call.setTargetEndpointAddress(new URL(opts.getURL()));
172:
173: /* Define some service specific properties */
174: /*******************************************/
175: call.setUsername(opts.getUser());
176: call.setPassword(opts.getPassword());
177:
178: /* Get symbol and invoke the service */
179: /*************************************/
180: Object result = call.invoke(new Object[] { symbol = args[0] });
181: result = call.invoke(new Object[] { symbol = args[0] });
182:
183: /* Reuse the call object to call the test method */
184: /*************************************************/
185: call.setOperation(portQN, "test");
186: call.setReturnType(XMLType.XSD_STRING);
187:
188: System.out.println(call.invoke(new Object[] {}));
189:
190: return (((Float) result).floatValue());
191: }
192:
193: public static void main(String args[]) {
194: try {
195: String save_args[] = new String[args.length];
196: float val;
197: GetQuote1 gq = new GetQuote1();
198:
199: /* Call the getQuote() that uses the WDSL */
200: /******************************************/
201: System.out.println("Using WSDL");
202: System.arraycopy(args, 0, save_args, 0, args.length);
203: val = gq.getQuote1(args);
204: System.out.println(gq.symbol + ": " + val);
205:
206: /* Call the getQuote() that does it all manually */
207: /*************************************************/
208: System.out.println("Manually");
209: System.arraycopy(save_args, 0, args, 0, args.length);
210: val = gq.getQuote2(args);
211: System.out.println(gq.symbol + ": " + val);
212:
213: /* Call the getQuote() that uses Axis's generated WSDL */
214: /*******************************************************/
215: System.out.println("WSDL + Reuse Call");
216: System.arraycopy(save_args, 0, args, 0, args.length);
217: val = gq.getQuote3(args);
218: System.out.println(gq.symbol + ": " + val);
219: } catch (Exception e) {
220: e.printStackTrace();
221: }
222: }
223: };
|