Java Source Code: com.discursive.jccook.collections.lazy.StockQuoteTransformer


   1: /* 
   2:  * ========================================================================
   3:  * 
   4:  * Copyright 2005 Tim O'Brien.
   5:  *
   6:  * Licensed under the Apache License, Version 2.0 (the "License");
   7:  * you may not use this file except in compliance with the License.
   8:  * You may obtain a copy of the License at
   9:  * 
  10:  *   http://www.apache.org/licenses/LICENSE-2.0
  11:  * 
  12:  * Unless required by applicable law or agreed to in writing, software
  13:  * distributed under the License is distributed on an "AS IS" BASIS,
  14:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15:  * See the License for the specific language governing permissions and
  16:  * limitations under the License.
  17:  * 
  18:  * ========================================================================
  19:  */
  20: package com.discursive.jccook.collections.lazy;
  21: 
  22: import java.net.URL;
  23: 
  24: import org.apache.commons.collections.Transformer;
  25: import org.apache.commons.httpclient.HttpClient;
  26: import org.apache.commons.httpclient.HttpURL;
  27: import org.apache.commons.httpclient.methods.GetMethod;
  28: 
  29:	  public class StockQuoteTransformer implements Transformer {
  30:
  31:    protected URL quoteURL;
  32:    protected long timeout;
  33:
  34:	      public Object transform(Object symbol) {
  35:        QuoteRetriever retriever = new QuoteRetriever( (String) symbol );
  36:        
  37:	          try {
  38:            Thread retrieveThread = new Thread( retriever );
  39:            retrieveThread.start();
  40:            retrieveThread.join( timeout );
  41:        } catch( InterruptedException ie ) {
  42:            System.out.println( "Quote request timed out.");
  43:        }
  44:        
  45:        return retriever.getResult();
  46:    }
  47:
  48:    public URL getQuoteURL() { return quoteURL; }
  49:    public void setQuoteURL(URL url) { quoteURL = url; }
  50:
  51:    public long getTimeout() { return timeout; }
  52:    public void setTimeout(long l) { timeout = l; }
  53:    
  54:	      public class QuoteRetriever implements Runnable {
  55:        private String symbol;
  56:        private Float result = new Float( Float.NaN );
  57:        
  58:	          public QuoteRetriever(String symbol) {
  59:            this.symbol = symbol;
  60:        }
  61:        
  62:	          public Float getResult() {
  63:            return result;
  64:        }
  65:        
  66:	          public void run() {
  67:            HttpClient client = new HttpClient();
  68:	              try {
  69:                HttpURL url = new HttpURL( quoteURL.toString() );
  70:                url.setQuery( "symbol", symbol );
  71:                
  72:                GetMethod getMethod = new GetMethod( url.toString() );
  73:                client.executeMethod( getMethod );
  74:                String response = getMethod.getResponseBodyAsString();
  75:                
  76:                result = new Float( response );
  77:            } catch( Exception e ) {
  78:                System.out.println( "Error retrieving quote" );
  79:            }
  80:        }
  81:        
  82:    }
  83:}