Source code for /engineering/autohit-2003/src/autohit/common/channels/Controller.javaOriginal file Controller.java
   1 /**
   2  * AUTOHIT 2003
   3  * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
   4  * 
   5  * This program is free software; you can redistribute it and/or modify 
   6  * it under the terms of the GNU General Public License as published by 
   7  * the Free Software Foundation; either version 2 of the License, or (at
   8  * your option) any later version.
   9  * This program is distributed in the hope that it will be useful, but
  10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12  * more details.
  13  * 
  14  * You should have received a copy of the GNU General Public License along
  15  * with this program; if not, write to the Free Software Foundation, Inc.,
  16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17  *
  18  * Additional license information can be found in the documentation.
  19  * @author Erich P Gatejen
  20  */
  21 package autohit.common.channels;
  22 
  23 import java.util.Hashtable;
  24 
  25 /**
  26  * Channel controller.  This controller assumes the 
  27  * hashtable provides enough thread safety.  I'll get fancy in the future.
  28  * <p>
  29  * Someone, somewhere needs to create an instance of this or the channel
  30  * system will not work.
  31  *
  32  * @author Erich P. Gatejen
  33  * @version 1.0
  34  * <i>Version History</i>
  35  * <code>EPG - Initial - 25Apr03</code>
  36  * 
  37  */
  38 public class Controller {
  39 
  40 	/**
  41 	 * Registry of channels.  There is only ONE per java VM
  42 	 */
  43 	static private Hashtable registry;
  44 
  45 	/**
  46 	 * Default constructor
  47 	 */
  48 	public Controller() {
  49 		// build only once--ever.
  50 		if (registry == null)
  51 			registry = new Hashtable();
  52 	}
  53 
  54 	/**
  55 	 * Register a channel
  56 	 * @param name String name
  57 	 * @param ch A channel
  58 	 */
  59 	public static void register(String name, Channel ch)
  60 		throws ChannelException {
  61 
  62 		// see if a controller instance exists
  63 		if (registry == null) {
  64 			throw new ChannelException(
  65 				"No one has created a controller",
  66 				ChannelException.CODE_CHANNEL_BAD_CONTROLLER_FAULT);
  67 		}
  68 
  69 		if (registry.containsKey(name)) {
  70 			throw new ChannelException(
  71 				"Channel " + name + "already registered",
  72 				ChannelException.CODE_CHANNEL_ALREADY_EXISTS_ERROR);
  73 		}
  74 		registry.put(name, ch);
  75 	}
  76 
  77 	/**
  78 	 * Remove a channel
  79 	 * @param name String name
  80 	 */
  81 	public static void remove(String name) throws ChannelException {
  82 
  83 		// see if a controller instance exists
  84 		if (registry == null) {
  85 			throw new ChannelException(
  86 				"No one has created a controller",
  87 				ChannelException.CODE_CHANNEL_BAD_CONTROLLER_FAULT);
  88 		}
  89 
  90 		if (!registry.containsKey(name)) {
  91 			throw new ChannelException(
  92 				"Channel " + name + " does not exist.",
  93 				ChannelException.CODE_CHANNEL_DOESNT_EXIST_ERROR);
  94 		}
  95 		registry.remove(name);
  96 	}
  97 
  98 	/**
  99 	 * Tune to a channel
 100 	 * @param name String name
 101 	 */
 102 	public static Channel tune(String name) throws ChannelException {
 103 
 104 		// see if a controller instance exists
 105 		if (registry == null) {
 106 			throw new ChannelException(
 107 				"No one has created a controller",
 108 				ChannelException.CODE_CHANNEL_BAD_CONTROLLER_FAULT);
 109 		}
 110 
 111 		if (!registry.containsKey(name)) {
 112 			throw new ChannelException(
 113 				"Channel " + name + " does not exist.",
 114 				ChannelException.CODE_CHANNEL_DOESNT_EXIST_ERROR);
 115 		}
 116 		return (Channel) registry.get(name);
 117 	}
 118 
 119 }