Source code for /engineering/autohit-2003/src/autohit/call/Call_QUERY_TABLE.javaOriginal file Call_QUERY_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.  QUERY_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 in which to query.  REQUIRED
  37  * n= name of the table entry to query REQUIRED
  38  * </pre>
  39  * RETURNS: the value or an empty string if it isn't there.
  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_QUERY_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 "QUERY_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 
 108 		} catch (CallException e) {
 109 			// Trap the table errors.
 110 			if (e.numeric == CallException.CODE_CALL_PERSISTMISMATCH_FAULT) {
 111 				throw new CallException(
 112 					this.format(
 113 						"Persist object named "
 114 							+ name
 115 							+ " found, but it is not a TABLE."),
 116 					CallException.CODE_CALL_PERSISTMISMATCH_FAULT,
 117 					e);
 118 			} else if (
 119 				e.numeric == CallException.CODE_CALL_PERSISTNOTFOUND_FAULT) {
 120 				throw new CallException(
 121 					this.format("Table named " + name + " not found."),
 122 					CallException.CODE_CALL_PERSISTNOTFOUND_FAULT,
 123 					e);
 124 			}
 125 			throw e;
 126 
 127 		} catch (Exception e) {
 128 			throw new CallException(
 129 				this.format(
 130 					"Exception while getting from a table.  error="
 131 						+ e.getMessage()),
 132 				CallException.CODE_CALL_FAULT,
 133 				e);
 134 		}
 135 		return result;
 136 	}
 137 
 138 	/**
 139 	 * Execute using the passed universe, rather than the loaded.
 140 	 * @param uni a universe
 141 	 * @return the result or null if there is no result
 142 	 * @see autohit.universe.Universe
 143 	 */
 144 	public String call(Universe uni) throws CallException {
 145 		return this.call();
 146 	}
 147 }