KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > transport > tcp > GetQuote


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.transport.tcp ;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.EngineConfiguration;
21 import org.apache.axis.SimpleTargetedChain;
22 import org.apache.axis.client.Call;
23 import org.apache.axis.client.Service;
24 import org.apache.axis.configuration.DefaultEngineConfigurationFactory;
25 import org.apache.axis.configuration.SimpleProvider;
26 import org.apache.axis.encoding.XMLType;
27 import org.apache.axis.utils.Options;
28
29 import javax.xml.namespace.QName JavaDoc;
30 import javax.xml.rpc.ParameterMode JavaDoc;
31 import java.net.URL JavaDoc;
32
33 /**
34  *
35  * @author Doug Davis (dug@us.ibm.com.com)
36  */

37 public class GetQuote {
38     public String JavaDoc symbol ;
39     
40     // helper function; does all the real work
41
public float getQuote (String JavaDoc args[]) throws Exception JavaDoc {
42         Call.addTransportPackage("samples.transport");
43         Call.setTransportForProtocol("tcp", TCPTransport.class);
44         
45         Options opts = new Options( args );
46         
47         args = opts.getRemainingArgs();
48         
49         if ( args == null ) {
50             System.err.println( "Usage: GetQuote <symbol>" );
51             System.exit(1);
52         }
53         
54         String JavaDoc namespace = "urn:xmltoday-delayed-quotes";
55         symbol = args[0] ;
56
57         EngineConfiguration defaultConfig =
58             (new DefaultEngineConfigurationFactory()).
59             getClientEngineConfig();
60         SimpleProvider config = new SimpleProvider(defaultConfig);
61         SimpleTargetedChain c = new SimpleTargetedChain(new TCPSender());
62         config.deployTransport("tcp", c);
63
64         Service service = new Service(config);
65         Call call = (Call)service.createCall();
66         
67         call.setTransport(new TCPTransport());
68         
69         call.setTargetEndpointAddress( new URL JavaDoc(opts.getURL()) );
70         call.setOperationName( new QName JavaDoc("urn:xmltoday-delayed-quotes", "getQuote") );
71         call.addParameter( "symbol", XMLType.XSD_STRING, ParameterMode.IN );
72         call.setReturnType( XMLType.XSD_FLOAT );
73         
74         // TESTING HACK BY ROBJ
75
if (symbol.equals("XXX_noaction")) {
76             symbol = "XXX";
77         }
78         
79         call.setUsername( opts.getUser() );
80         call.setPassword( opts.getPassword() );
81         
82         // useful option for profiling - perhaps we should remove before
83
// shipping?
84
String JavaDoc countOption = opts.isValueSet('c');
85         int count=1;
86         if ( countOption != null) {
87             count=Integer.valueOf(countOption).intValue();
88             System.out.println("Iterating " + count + " times");
89         }
90         
91         Float JavaDoc res = new Float JavaDoc(0.0F);
92         for (int i=0; i<count; i++) {
93             Object JavaDoc ret = call.invoke(new Object JavaDoc[] {symbol} );
94             if (ret instanceof String JavaDoc) {
95                 System.out.println("Received problem response from server: "+ret);
96                 throw new AxisFault("", (String JavaDoc)ret, null, null);
97             }
98             res = (Float JavaDoc) ret;
99         }
100         
101         return res.floatValue();
102     }
103     
104     public static void main(String JavaDoc args[]) {
105         try {
106             GetQuote gq = new GetQuote();
107             float val = gq.getQuote(args);
108             // args array gets side-effected
109
System.out.println(gq.symbol + ": " + val);
110         }
111         catch( Exception JavaDoc e ) {
112             e.printStackTrace();
113         }
114     }
115     
116     public GetQuote () {
117     };
118     
119 };
120
Popular Tags