Source code for /engineering/autohit-2003/src/autohit/common/channels/SimpleInjector.javaOriginal file SimpleInjector.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 autohit.common.AutohitProperties;
  24 
  25 /**
  26  * A very simple injector.  Post anything and everything.  You can set a default 
  27  * sender ID with setDefaultSenderID.  If it is set as anything but null, when an atom without
  28  * a sender id is posted, the sender id will be changed to this default.
  29  *
  30  * @author Erich P. Gatejen
  31  * @version 1.0
  32  * <i>Version History</i>
  33  * <code>EPG - Rewrite - 27Apr03<br>
  34  * EPG - Add default post - 24Jul03<br>
  35  * EPG - Set default sender ID - 23Sep03
  36  * </code> 
  37  * 
  38  */
  39 public class SimpleInjector implements Injector {
  40 
  41 	private Channel myChannel;
  42 	
  43 	/**
  44 	 * Default sender ID.  It starts as null.  You must set it.
  45 	 */	
  46 	public String defaultSenderID = null;
  47 
  48 	/**
  49 	 * Default contructor
  50 	 */
  51 	public SimpleInjector() {
  52 		myChannel = null;
  53 	}
  54 
  55 	/**
  56 	 * Post an item
  57 	 * @param id The new default sender ID.  If set to null, it will not try and set the sender ID.
  58 	 */
  59 	public void setDefaultSenderID(String id) {
  60 		defaultSenderID =  id;
  61 	}
  62 	
  63 	/**
  64 	 * Post an item
  65 	 * @param a An atom to post
  66 	 * @return a receipt
  67 	 */
  68 	public Receipt post(Atom a) throws ChannelException {
  69 		if (myChannel == null)
  70 			throw new ChannelException(
  71 				"Not registered to a channel.",
  72 				ChannelException.CODE_CHANNEL_DOESNT_EXIST_ERROR);
  73 		
  74 		if (a.senderID == null) {
  75 			a.senderID = defaultSenderID;
  76 		}
  77 		
  78 		return myChannel.inject(a);
  79 	}
  80 
  81 	/**
  82 	 * Set channel callback
  83 	 * @return a receipt
  84 	 */
  85 	public void setChannel(Channel c) throws ChannelException {
  86 		myChannel = c;
  87 	}
  88 
  89 	/**
  90 	 * Post a a default item.  Use this if you aren't sure what
  91 	 * kind of Atom this channal normally services.
  92 	 * @param numeric value
  93 	 * @param o object to post (often a string)
  94 	 * @return a receipt
  95 	 * @throws ChannelException
  96 	 */
  97 	public Receipt defaultPost(int numeric, Object o) throws ChannelException {
  98 		String thingthang;
  99 		Receipt result = null;
 100 
 101 		try {
 102 			Atom a = new Atom(Atom.TYPE_GENERIC, Atom.ROUTINE, numeric, o);
 103 			if (defaultSenderID == null) {
 104 				a.senderID = AutohitProperties.SYSTEM_GENERIC_ID;
 105 			} else {
 106 				a.senderID = defaultSenderID;
 107 			}
 108 			result = this.post(a);
 109 		} catch (Exception e) {
 110 			throw new ChannelException(
 111 				"Sender ["
 112 					+ AutohitProperties.SYSTEM_GENERIC_ID
 113 					+ "] could not defaultPost.",
 114 				ChannelException.CODE_CHANNEL_FAULT);
 115 		}
 116 		return result;
 117 	}
 118 
 119 	/**
 120 	 * Instantiate a default atom for this kind of injector.  It will be completely
 121 	 * bare.
 122 	 * @return default atom
 123 	 */
 124 	public Atom defaultAtom() {
 125 		return new Atom();
 126 	}
 127 
 128 }