Source code for /engineering/autohit-2003/src/autohit/call/Call_METHOD.javaOriginal file Call_METHOD.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 autohit.call.modules.Module;
  24 import autohit.common.Constants;
  25 import autohit.universe.Universe;
  26 
  27 /**
  28  * METHOD call.  Executes a method call into a Module instance.
  29  * <pre>
  30  * REQURIES: logger, core
  31  * IGNORES: uni
  32  * PARAMETERS (INPUT):
  33  * name= module instance name
  34  * method= method name
  35  * </pre>
  36  * RETURNS: defined by the method, but will always be a String.
  37  *
  38  * @author Erich P. Gatejen
  39  * @version 1.0
  40  * <i>Version History</i>
  41  * <code>EPG - Initial - 16Jun03 
  42  * 
  43  */
  44 public class Call_METHOD extends Call {
  45 
  46 	// Trim the size of result logging in debug mode
  47 	final static private int TRIM_SIZE = 500;
  48 
  49 	/**
  50 	 * Implement this to handle load time initialization.  The 
  51 	 * four main fields will already be set--vmc, sc, log, and u.
  52 	 * You must implement this, but you don't have to do anything.
  53 	 * Remember that calls are cached per VM and reused as often
  54 	 * as possible.  There will be no thread-safety issues with the
  55 	 * VMCore or log, but the SystemContecxt and Universe may be shared.
  56 	 * @throws CallException
  57 	 */
  58 	public void load_chain() throws CallException {
  59 		// Nothing to do.
  60 	}
  61 
  62 	/**
  63 	 * Implement this to return the name of the CALL
  64 	 * @return name of the CALL
  65 	 */
  66 	public String name() {
  67 		return "METHOD";
  68 	}
  69 
  70 	/**
  71 	 * Execute it.
  72 	 * @return the result or null if there is no result
  73 	 */
  74 	public String call() throws CallException {
  75 
  76 		String name;
  77 		String method;
  78 		String result = Constants.EMPTY_LEFT;
  79 
  80 		try {
  81 
  82 			name = this.requiredString("name");
  83 			method = this.requiredString("method");
  84 			result = this.do_call(name, method);
  85 
  86 		} catch (CallException e) {
  87 
  88 			throw e;
  89 
  90 		} catch (Exception ex) {
  91 			//any other is REAL bad
  92 			throw new CallException(
  93 				this.format("Serious fault.  error=" + ex.getMessage()),
  94 				CallException.CODE_CALL_UNRECOVERABLE_FAULT,
  95 				ex);
  96 		}
  97 		return result;
  98 	}
  99 
 100 	/**
 101 	 * This is a sub-call that assumes you know the module name and method.
 102 	 * This is used to cheat with <method>
 103 	 * @param name Name of module
 104 	 * @param method Name of method
 105 	 * @return the result or null if there is no result
 106 	 */
 107 	public String do_call(String name, String method) throws CallException {
 108 
 109 		String result = Constants.EMPTY_LEFT;
 110 
 111 		try {
 112 
 113 			//log.debug(
 114 			//	"call: METHOD.  Going to call method= "
 115 			//		+ method
 116 			//		+ " in module instance="
 117 			//		+ name);
 118 			if (vmc.has(name)) {
 119 				Module mod = (Module) vmc.get(name);
 120 				result = mod.execute(method);
 121 
 122 				// To cut back on excessive logging, we'll trim the result.
 123 				if (log.debugState()) {
 124 					String trimresult;
 125 					if (result.length() > TRIM_SIZE) {
 126 						trimresult = result.substring(0, TRIM_SIZE);
 127 					} else {
 128 						trimresult = result;
 129 					}
 130 					this.debug(
 131 						"DONE calling method= "
 132 							+ method
 133 							+ ".  Result="
 134 							+ trimresult);
 135 				}
 136 
 137 			} else {
 138 				// if it doesn't exist, that's a problem
 139 				throw new CallException(
 140 					this.format("Instance of " + name + " doesn't exist."),
 141 					CallException.CODE_CALL_FAULT);
 142 			}
 143 
 144 		} catch (CallException e) {
 145 
 146 			throw e;
 147 
 148 		} catch (Exception ex) {
 149 			//any other is REAL bad
 150 			throw new CallException(
 151 				this.format("Serious fault.  error=" + ex.getMessage()),
 152 				CallException.CODE_CALL_UNRECOVERABLE_FAULT,
 153 				ex);
 154 		}
 155 		return result;
 156 	}
 157 
 158 	/**
 159 	 * Execute using the passed universe, rather than the loaded.
 160 	 * @param uni a universe
 161 	 * @return the result or null if there is no result
 162 	 * @see autohit.universe.Universe
 163 	 */
 164 	public String call(Universe uni) throws CallException {
 165 		return this.call();
 166 	}
 167 }