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.transport.tcp;
018:
019: import org.apache.axis.AxisFault;
020: import org.apache.axis.EngineConfiguration;
021: import org.apache.axis.SimpleTargetedChain;
022: import org.apache.axis.client.Call;
023: import org.apache.axis.client.Service;
024: import org.apache.axis.configuration.DefaultEngineConfigurationFactory;
025: import org.apache.axis.configuration.SimpleProvider;
026: import org.apache.axis.encoding.XMLType;
027: import org.apache.axis.utils.Options;
028:
029: import javax.xml.namespace.QName;
030: import javax.xml.rpc.ParameterMode;
031: import java.net.URL;
032:
033: /**
034: *
035: * @author Doug Davis (dug@us.ibm.com.com)
036: */
037: public class GetQuote {
038: public String symbol;
039:
040: // helper function; does all the real work
041: public float getQuote(String args[]) throws Exception {
042: Call.addTransportPackage("samples.transport");
043: Call.setTransportForProtocol("tcp", TCPTransport.class);
044:
045: Options opts = new Options(args);
046:
047: args = opts.getRemainingArgs();
048:
049: if (args == null) {
050: System.err.println("Usage: GetQuote <symbol>");
051: System.exit(1);
052: }
053:
054: String namespace = "urn:xmltoday-delayed-quotes";
055: symbol = args[0];
056:
057: EngineConfiguration defaultConfig = (new DefaultEngineConfigurationFactory())
058: .getClientEngineConfig();
059: SimpleProvider config = new SimpleProvider(defaultConfig);
060: SimpleTargetedChain c = new SimpleTargetedChain(new TCPSender());
061: config.deployTransport("tcp", c);
062:
063: Service service = new Service(config);
064: Call call = (Call) service.createCall();
065:
066: call.setTransport(new TCPTransport());
067:
068: call.setTargetEndpointAddress(new URL(opts.getURL()));
069: call.setOperationName(new QName("urn:xmltoday-delayed-quotes",
070: "getQuote"));
071: call.addParameter("symbol", XMLType.XSD_STRING,
072: ParameterMode.IN);
073: call.setReturnType(XMLType.XSD_FLOAT);
074:
075: // TESTING HACK BY ROBJ
076: if (symbol.equals("XXX_noaction")) {
077: symbol = "XXX";
078: }
079:
080: call.setUsername(opts.getUser());
081: call.setPassword(opts.getPassword());
082:
083: // useful option for profiling - perhaps we should remove before
084: // shipping?
085: String countOption = opts.isValueSet('c');
086: int count = 1;
087: if (countOption != null) {
088: count = Integer.valueOf(countOption).intValue();
089: System.out.println("Iterating " + count + " times");
090: }
091:
092: Float res = new Float(0.0F);
093: for (int i = 0; i < count; i++) {
094: Object ret = call.invoke(new Object[] { symbol });
095: if (ret instanceof String) {
096: System.out
097: .println("Received problem response from server: "
098: + ret);
099: throw new AxisFault("", (String) ret, null, null);
100: }
101: res = (Float) ret;
102: }
103:
104: return res.floatValue();
105: }
106:
107: public static void main(String args[]) {
108: try {
109: GetQuote gq = new GetQuote();
110: float val = gq.getQuote(args);
111: // args array gets side-effected
112: System.out.println(gq.symbol + ": " + val);
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116: }
117:
118: public GetQuote() {
119: };
120:
121: };
|