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.jaxrpc;
018:
019: import org.apache.axis.encoding.XMLType;
020: import org.apache.axis.utils.Options;
021:
022: import javax.xml.namespace.QName;
023: import javax.xml.rpc.Call;
024: import javax.xml.rpc.ParameterMode;
025: import javax.xml.rpc.Service;
026: import javax.xml.rpc.ServiceFactory;
027: import java.net.URL;
028:
029: /**
030: * This version of the ever so popular GetQuote is a near-duplicate of
031: * the GetQuote1 method in samples/stock which shows how to use the AXIS
032: * client APIs with and without WSDL. This version is strictly JAX-RPC
033: * compliant. It uses no AXIS enhancements.
034: *
035: * This sample supports the use of the standard options too (-p ...)
036: *
037: * @author Russell Butek (butek@us.ibm.com)
038: */
039: public class GetQuote1 {
040: public String symbol;
041:
042: /**
043: * This will use the WSDL to prefill all of the info needed to make
044: * the call. All that's left is filling in the args to invoke().
045: */
046: public float getQuote1(String args[]) throws Exception {
047: Options opts = new Options(args);
048:
049: args = opts.getRemainingArgs();
050:
051: if (args == null) {
052: System.err.println("Usage: GetQuote <symbol>");
053: System.exit(1);
054: }
055:
056: /* Define the service QName and port QName */
057: /*******************************************/
058: QName servQN = new QName("urn:xmltoday-delayed-quotes",
059: "GetQuoteService");
060: QName portQN = new QName("urn:xmltoday-delayed-quotes",
061: "GetQuote");
062:
063: /* Now use those QNames as pointers into the WSDL doc */
064: /******************************************************/
065: Service service = ServiceFactory.newInstance().createService(
066: new URL("file:samples/stock/GetQuote.wsdl"), servQN);
067: Call call = service.createCall(portQN, "getQuote");
068:
069: /* Strange - but allows the user to change just certain portions of */
070: /* the URL we're gonna use to invoke the service. Useful when you */
071: /* want to run it thru tcpmon (ie. put -p81 on the cmd line). */
072: /********************************************************************/
073: opts.setDefaultURL(call.getTargetEndpointAddress());
074: call.setTargetEndpointAddress(opts.getURL());
075:
076: /* Define some service specific properties */
077: /*******************************************/
078: call.setProperty(Call.USERNAME_PROPERTY, opts.getUser());
079: call.setProperty(Call.PASSWORD_PROPERTY, opts.getPassword());
080:
081: /* Get symbol and invoke the service */
082: /*************************************/
083: Object result = call.invoke(new Object[] { symbol = args[0] });
084:
085: return ((Float) result).floatValue();
086: } // getQuote1
087:
088: /**
089: * This will do everything manually (ie. no WSDL).
090: */
091: public float getQuote2(String args[]) throws Exception {
092: Options opts = new Options(args);
093:
094: args = opts.getRemainingArgs();
095:
096: if (args == null) {
097: System.err.println("Usage: GetQuote <symbol>");
098: System.exit(1);
099: }
100:
101: /* Create default/empty Service and Call object */
102: /************************************************/
103: Service service = ServiceFactory.newInstance().createService(
104: null);
105: 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(opts.getURL());
117: call.setProperty(Call.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
118: call.setProperty(Call.SOAPACTION_URI_PROPERTY, "getQuote");
119: call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY,
120: "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.setProperty(Call.USERNAME_PROPERTY, opts.getUser());
130: call.setProperty(Call.PASSWORD_PROPERTY, 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: } // getQuote2
138:
139: /**
140: * This method does the same thing that getQuote1 does, but in
141: * addition it reuses the Call object to make another call.
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 = ServiceFactory.newInstance().createService(
163: new URL("file:samples/stock/GetQuote.wsdl"), servQN);
164: 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(opts.getURL());
172:
173: /* Define some service specific properties */
174: /*******************************************/
175: call.setProperty(Call.USERNAME_PROPERTY, opts.getUser());
176: call.setProperty(Call.PASSWORD_PROPERTY, opts.getPassword());
177:
178: /* Get symbol and invoke the service */
179: /*************************************/
180: Object result = call.invoke(new Object[] { symbol = args[0] });
181:
182: /* Reuse the Call object for a different call */
183: /**********************************************/
184: call.setOperationName(new QName("urn:xmltoday-delayed-quotes",
185: "test"));
186: call.removeAllParameters();
187: call.setReturnType(XMLType.XSD_STRING);
188:
189: System.out.println(call.invoke(new Object[] {}));
190: return ((Float) result).floatValue();
191: } // getQuote3
192:
193: public static void main(String args[]) throws Exception {
194: String save_args[] = new String[args.length];
195: float val;
196: GetQuote1 gq = new GetQuote1();
197:
198: /* Call the getQuote() that uses the WDSL */
199: /******************************************/
200: System.out.println("Using WSDL");
201: System.arraycopy(args, 0, save_args, 0, args.length);
202: val = gq.getQuote1(args);
203: System.out.println(gq.symbol + ": " + val);
204:
205: /* Call the getQuote() that does it all manually */
206: /*************************************************/
207: System.out.println("Manually");
208: System.arraycopy(save_args, 0, args, 0, args.length);
209: val = gq.getQuote2(args);
210: System.out.println(gq.symbol + ": " + val);
211:
212: /* Call the getQuote() that uses Axis's generated WSDL */
213: /*******************************************************/
214: System.out.println("WSDL + Reuse Call");
215: System.arraycopy(save_args, 0, args, 0, args.length);
216: val = gq.getQuote3(args);
217: System.out.println(gq.symbol + ": " + val);
218: } // main
219: }
|