KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > stock > 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.stock ;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.client.Call;
21 import org.apache.axis.client.Service;
22 import org.apache.axis.encoding.XMLType;
23 import org.apache.axis.utils.Options;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import javax.xml.rpc.ParameterMode JavaDoc;
27 import java.net.URL JavaDoc;
28
29 /**
30  *
31  * @author Doug Davis (dug@us.ibm.com.com)
32  */

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