001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.daytrader.client;
018    
019    import java.util.Collection;
020    import java.util.Iterator;
021    import java.util.Timer;
022    import java.util.TimerTask;
023    
024    import javax.naming.InitialContext;
025    import javax.rmi.PortableRemoteObject;
026    
027    import org.apache.geronimo.samples.daytrader.QuoteDataBean;
028    import org.apache.geronimo.samples.daytrader.ejb.Trade;
029    import org.apache.geronimo.samples.daytrader.ejb.TradeHome;
030    
031    public class TradeClient {
032    
033            public static final int DEFAULT_UPDATE_INTERVAL = 2;
034            public static final int DEFAULT_MAX_PER_SECOND = 10;
035    
036            // Various components
037            private TradeQuoteAuditStats auditStats;
038        private TradeClientGUI gui;
039    
040            private static TradeClient tradeClient;
041    
042            // EJB values
043            private InitialContext initial;
044            private Trade trade;
045            private boolean useENC = true;
046    
047            // Updater thread
048        private final Timer timer = new Timer();
049        private TimerTask updater;
050        private int updateInterval = DEFAULT_UPDATE_INTERVAL;
051    
052            public static void main(String[] args) {
053                    try     {
054                            TradeClient streamer  = new TradeClient();
055                            if (args.length > 0) {
056                                    if (args[0].equals("-noENC")) {
057                                            streamer.useENC = false;
058                                    }
059                                    else {
060                                            System.out.println("Usage TradeClient [-noENC]");
061                                            System.exit(1);
062                                    }
063                            }
064    
065                            tradeClient = streamer;
066                            streamer.startClient();
067            }
068                    catch (Exception e)     {
069                            System.err.println("Caught an unexpected exception!");
070                            e.printStackTrace();
071                    }
072            }
073    
074            public static TradeClient getTradeClient() {
075                    return tradeClient;
076            }
077    
078            private void startClient() throws Exception {
079                    auditStats = new TradeQuoteAuditStats();
080                    setupEJB();
081                    TradeClientMessageListener listener = new TradeClientMessageListener(this, useENC);
082                    listener.subscribe();
083                    resetStatsFromServer();
084                    gui = new TradeClientGUI(this);
085                    gui.show();
086        }
087    
088            public TradeQuoteAuditStats getAuditStats() {
089                    return auditStats;
090            }
091    
092            public void reset() throws Exception {
093                    resetStatsFromServer();
094            }
095    
096            public void resetStatsFromServer() throws Exception {
097                    auditStats.clearStats();
098                    Collection quotes = trade.getAllQuotes();
099    
100                    for (Iterator it = quotes.iterator(); it.hasNext(); ) {
101                            QuoteDataBean bean = (QuoteDataBean)it.next();
102                            auditStats.updateSymbol(bean.getSymbol(), bean.getCompanyName(), bean.getPrice(), bean.getOpen(), bean.getLow(), bean.getHigh(), bean.getVolume(), System.currentTimeMillis(), bean.getPrice(), bean.getVolume());
103                    }
104            }
105    
106            public void updateStatusMessage(String message) {
107                    gui.updateStatusMessage(message);
108            }
109    
110            public InitialContext getInitialContext() {
111                    return initial;
112            }
113    
114            public void setupEJB() throws Exception {
115                    initial = new InitialContext();
116                    Object objref;
117                    if (useENC) {
118                            objref = initial.lookup("java:comp/env/ejb/Trade");
119                    }
120                    else {
121                            objref = initial.lookup("ejb/TradeEJB");
122                    }
123                    TradeHome home = (TradeHome)PortableRemoteObject.narrow(objref, TradeHome.class);
124                    trade = home.create();
125            }
126    
127            public int getUpdateInterval() {
128                    return updateInterval;
129            }
130    
131            public void setUpdateInterval(int updateInterval) {
132                    this.updateInterval = updateInterval;
133            if (updater != null) {
134                updater.cancel();
135            }
136            updater = new TimerTask() {
137                public void run() {
138                    auditStats.fireTableDataChanged();
139                    System.out.println("Updating");
140                }
141            };
142            timer.scheduleAtFixedRate(updater, (long)updateInterval*1000, (long)updateInterval*1000);
143        }
144    
145            public void closeClient() {
146            System.exit(1);
147            }
148    
149    }