Source code for /engineering/autohit-2003/src/autohit/call/Call_GET_TABLE.javaOriginal file Call_GET_TABLE.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.call;
  22 
  23 import java.util.Hashtable;
  24 
  25 import autohit.common.Constants;
  26 import autohit.universe.Universe;
  27 
  28 /**
  29  * CALL.  GET_TABLE  Gets a value from a TABLE in persist.  You can only 
  30  * get strings!  Trying to get anything else will cause an error and 
  31  * return nothing.
  32  * <pre>
  33  * REQURIES: logger, core
  34  * IGNORES: 
  35  * PARAMETERS (INPUT):
  36  * name= name of the table inwhich to insert.  REQUIRED
  37  * n= name of the table entry to get REQUIRED
  38  * </pre>
  39  * RETURNS: the value of the item
  40  *
  41  * @author Erich P. Gatejen
  42  * @version 1.0
  43  * <i>Version History</i>
  44  * <code>EPG - Initial - 28Jun03</code>
  45  */
  46 public class Call_GET_TABLE extends Call {
  47 
  48 	/**
  49 	 * Implement this to handle load time initialization.  The 
  50 	 * four main fields will already be set--vmc, sc, log, and u.
  51 	 * You must implement this, but you don't have to do anything.
  52 	 * Remember that calls are cached per VM and reused as often
  53 	 * as possible.  There will be no thread-safety issues with the
  54 	 * VMCore or log, but the SystemContecxt and Universe may be shared.
  55 	 * @throws CallException
  56 	 */
  57 	public void load_chain() throws CallException {
  58 		// Nothing to do.
  59 	}
  60 
  61 	/**
  62 	 * Implement this to return the name of the CALL
  63 	 * @return name of the CALL
  64 	 */
  65 	public String name() {
  66 		return "GET_TABLE";
  67 	}
  68 
  69 	/**
  70 	 * Execute it.
  71 	 * @return the result or null if there is no result
  72 	 */
  73 	public String call() throws CallException {
  74 
  75 		String name = Constants.UNKNOWN;
  76 		String n;
  77 		String result = Constants.EMPTY_LEFT;
  78 		Hashtable table;
  79 
  80 		try {
  81 
  82 			// Get the params and the table.  Exceptions will handle problems
  83 			name = (String) this.requiredString("name");
  84 			n = (String) this.requiredString("n");
  85 			table = (Hashtable) this.requiredPersist(name, Hashtable.class);
  86 
  87 			// See if the value is in the table.  if it isn't,
  88 			// the call will return EMPTY_LEFT, as it was set at the
  89 			// start of this method.
  90 			if (table.containsKey(n)) {
  91 				Object thingie = table.get(n);
  92 
  93 				// make sure it is a string
  94 				if (thingie instanceof String) {
  95 					result = (String) thingie;
  96 
  97 				} else {
  98 					throw new CallException(
  99 						this.format(
 100 							"Bad call.  Value of "
 101 								+ n
 102 								+ " is not a string in table "
 103 								+ name),
 104 						CallException.CODE_CALL_FAULT);
 105 				}
 106 
 107 			} else {
 108 				throw new CallException(
 109 					this.format(
 110 						"Bad call.  Entry named "
 111 							+ n
 112 							+ " does not exist in table "
 113 							+ name),
 114 					CallException.CODE_CALL_UNRECOVERABLE_FAULT);
 115 			}
 116 
 117 		} catch (CallException e) {
 118 			// Trap the table errors.
 119 			if (e.numeric == CallException.CODE_CALL_PERSISTMISMATCH_FAULT) {
 120 				throw new CallException(
 121 					this.format(
 122 						"Persist object named "
 123 							+ name
 124 							+ " found, but it is not a TABLE."),
 125 					CallException.CODE_CALL_PERSISTMISMATCH_FAULT,
 126 					e);
 127 			} else if (
 128 				e.numeric == CallException.CODE_CALL_PERSISTNOTFOUND_FAULT) {
 129 				throw new CallException(
 130 					this.format("Table named " + name + " not found."),
 131 					CallException.CODE_CALL_PERSISTNOTFOUND_FAULT,
 132 					e);
 133 			}
 134 			throw e;
 135 
 136 		} catch (Exception e) {
 137 			throw new CallException(
 138 				this.format(
 139 					"Exception while getting from a table.  error="
 140 						+ e.getMessage()),
 141 				CallException.CODE_CALL_FAULT,
 142 				e);
 143 		}
 144 		return result;
 145 	}
 146 
 147 	/**
 148 	 * Execute using the passed universe, rather than the loaded.
 149 	 * @param uni a universe
 150 	 * @return the result or null if there is no result
 151 	 * @see autohit.universe.Universe
 152 	 */
 153 	public String call(Universe uni) throws CallException {
 154 		return this.call();
 155 	}
 156 }