home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jhelp.z / StockServer.java < prev    next >
Text File  |  1997-07-30  |  7KB  |  224 lines

  1. /*
  2.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  5.  * modify and redistribute this software in source and binary code form,
  6.  * provided that i) this copyright notice and license appear on all copies of
  7.  * the software; and ii) Licensee does not utilize the software in a manner
  8.  * which is disparaging to Sun.
  9.  *
  10.  * This software is provided "AS IS," without a warranty of any kind. ALL
  11.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  12.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  13.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  14.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  15.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  16.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  17.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  18.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  19.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  20.  * POSSIBILITY OF SUCH DAMAGES.
  21.  *
  22.  * This software is not designed or intended for use in on-line control of
  23.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  24.  * the design, construction, operation or maintenance of any nuclear
  25.  * facility. Licensee represents and warrants that it will not use or
  26.  * redistribute the Software for such purposes.
  27.  */
  28.  
  29. package examples.stock;
  30.  
  31. import java.rmi.*;
  32. import java.rmi.server.*;
  33. import java.rmi.registry.LocateRegistry;
  34. import java.util.*;
  35.  
  36. public class StockServer extends UnicastRemoteObject
  37.     implements StockWatch, Runnable
  38. {
  39.     /** table that maps StockNotify objects to a vector of stocks */
  40.     private Hashtable notifyTable = new Hashtable();
  41.     /** table that maps stock names to stock update info */
  42.     private Hashtable stockTable = new Hashtable();
  43.     /** thread for notifying watchers of stock updates */
  44.     private Thread notifier = null;
  45.  
  46.     private static String name[] = { "Sun", "HP", "Microsoft", "DEC","Novell",
  47.                      "IBM","Apple","Netscape","Borland","SGI"};
  48.     /**
  49.      * Construct the stock server
  50.      * @exception RemoteException if remote object cannot be exported
  51.      */
  52.     public StockServer() throws RemoteException 
  53.     {
  54.     for (int i=0; i<name.length; i++) {
  55.         stockTable.put(name[i], new Stock(name[i]));
  56.     }
  57.     }
  58.  
  59.     /**
  60.      * Request notification of stock updates.
  61.      * @param stock the stock name
  62.      * @param obj the remote object to be notified
  63.      * @return the latest update of the stock
  64.      * @exception StockNotFoundException if stock is not known
  65.      */
  66.     public synchronized Stock watch (String stock, StockNotify obj)
  67.     throws StockNotFoundException
  68.     {
  69.     System.out.println("StockServer.watch: " + stock );
  70.     
  71.     if (!stockTable.containsKey(stock))
  72.         throw new StockNotFoundException(stock);
  73.     
  74.     Vector stocks = (Vector)notifyTable.get(obj);
  75.  
  76.     // register intereted party...
  77.     if (stocks == null) {
  78.         stocks = new Vector();
  79.         notifyTable.put(obj, stocks);
  80.     }
  81.  
  82.     // add stock to list
  83.     if (!stocks.contains(stock)) {
  84.         stocks.addElement(stock);
  85.     }
  86.     
  87.     // start thread to notify watchers...
  88.     if (notifier == null) {
  89.         notifier = new Thread(this, "StockNotifier");
  90.         notifier.start();
  91.     }
  92.     return (Stock)stockTable.get(stock);
  93.     }
  94.  
  95.     /**
  96.      * Cancel request for stock updates for a particular stock.
  97.      * @param stock the stock name
  98.      * @param obj the remote object canceling the notification
  99.      */
  100.     public void cancel(String stock, StockNotify obj)
  101.     {
  102.     Vector stocks = (Vector)notifyTable.get(obj);
  103.     stocks.removeElement(stock);
  104.     }
  105.  
  106.    /**
  107.      * Returns an array of stock update information for the stocks
  108.      * already registered by the remote object.
  109.      * @param obj the remote object
  110.      * @return the list of stocks, or null if obj is not watching any
  111.      *  stocks
  112.      * @exception RemoteException if some communication failure occurs
  113.      */
  114.     public Stock[] list(StockNotify obj)
  115.     {
  116.     Vector stocks = (Vector)notifyTable.get(obj);
  117.     Stock[] stockList = null;
  118.     
  119.     if (stocks != null) {
  120.         Enumeration enum = stocks.elements();
  121.         stockList = new Stock[stocks.size()];
  122.         int i=0;
  123.         // collect updates to the stocks this watcher is
  124.         // interested in
  125.         while (enum.hasMoreElements()) {
  126.         String stockname = (String)enum.nextElement();
  127.         stockList[i++] = (Stock)stockTable.get(stockname);
  128.         }
  129.     }
  130.     return stockList;
  131.     }
  132.  
  133.     /**
  134.      * Cancel all requests for stock updates for the remote object.
  135.      * @param obj the remote object canceling the request
  136.      * @exception RemoteException if some communication failure occurs
  137.      */
  138.     public synchronized void cancelAll(StockNotify obj)
  139.     {
  140.     notifyTable.remove(obj);
  141.     if (notifyTable.isEmpty()) {
  142.         Thread thread = notifier; // in case current thread is notifier
  143.         notifier = null;
  144.         thread.stop();
  145.     }
  146.     }
  147.  
  148.     /**
  149.      * Private method to generate random stock updates
  150.      */
  151.     private void generateUpdates() 
  152.     {
  153.     Enumeration enum = stockTable.elements();
  154.     while (enum.hasMoreElements()) {
  155.         Stock stock = (Stock)enum.nextElement();
  156.         stock.update();
  157.     }
  158.     }
  159.  
  160.     /**
  161.      * The run method (called from the notifier thread) sends out stock
  162.      * updates periodically to those remote objects that have
  163.      * registered interest in being notified.
  164.      */
  165.     public void run() 
  166.     {
  167.     while (true) {
  168.         try {
  169.         // wait for a few seconds between updates
  170.         Thread.currentThread().sleep(2000);
  171.         } catch (InterruptedException e) {
  172.         }
  173.  
  174.         Date date = new Date();
  175.         // update stocks in table
  176.         generateUpdates();
  177.         
  178.         // enumerate through each watcher...
  179.         Enumeration enum = notifyTable.keys();
  180.         while (enum.hasMoreElements()) {
  181.         StockNotify obj = (StockNotify)enum.nextElement();
  182.         Stock[] stockList = list(obj);
  183.         if (stockList != null) {
  184.             // send update
  185.             try {
  186.             System.out.println("StockServer.run: sending update " +
  187.                        date);
  188.             obj.update(date, stockList);
  189.             } catch (RemoteException e) {
  190.             // can't reach watcher; cancel notification request
  191.             System.out.println("StockServer.run: exception");
  192.             e.printStackTrace();
  193.             cancelAll(obj);
  194.             }
  195.         }
  196.         }
  197.     }
  198.     }
  199.  
  200.     /**
  201.      * Start up the stock server; also creates a registry so that the
  202.      * StockApplet can lookup the server.
  203.      */
  204.     public static void main(String args[]) 
  205.     {
  206.     // Create and install the security manager
  207.     System.setSecurityManager(new RMISecurityManager());
  208.  
  209.     try {
  210.         System.out.println("StockServer.main: creating registry");
  211.         LocateRegistry.createRegistry(2005);
  212.         System.out.println("StockServer.main: creating server");
  213.         StockServer server = new StockServer();
  214.         System.out.println("StockServer.main: binding server ");
  215.         Naming.rebind("//:2005/example.stock.StockServer", server);
  216.         System.out.println("StockServer.main: done");
  217.     } catch (Exception e) {
  218.         System.out.println("StockServer.main: an exception occurred: " +
  219.                    e.getMessage());
  220.         e.printStackTrace();
  221.     }
  222.     }
  223. }
  224.