Source code for /engineering/autohit-2003/src/autohit/call/Call_INSTANCE.javaOriginal file Call_INSTANCE.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  * SPECIAL CALL.  INSTANCE  Makes a module instance.  Each instance will
  29  * be a seperate entity, with it's own local variables.
  30  * <pre>
  31  * REQURIES: logger, core, uni
  32  * IGNORES: 
  33  * PARAMETERS (INPUT):
  34  * type= module type name.   It is a valid java classname.  REQUIRED
  35  * name= to call the instance.  if it already exists, nothing will happen.  REQUIRED
  36  * </pre>
  37  * RETURNS: empty string.
  38  *
  39  * @author Erich P. Gatejen
  40  * @version 1.0
  41  * <i>Version History</i>
  42  * <code>EPG - Initial - 21Jun03</code>
  43  */
  44 public class Call_INSTANCE extends Call {
  45 
  46 	/**
  47 	 * Implement this to handle load time initialization.  The 
  48 	 * four main fields will already be set--vmc, sc, log, and u.
  49 	 * You must implement this, but you don't have to do anything.
  50 	 * Remember that calls are cached per VM and reused as often
  51 	 * as possible.  There will be no thread-safety issues with the
  52 	 * VMCore or log, but the SystemContecxt and Universe may be shared.
  53 	 * @throws CallException
  54 	 */
  55 	public void load_chain() throws CallException {
  56 		// Nothing to do.
  57 	}
  58 
  59 	/**
  60 	 * Implement this to return the name of the CALL
  61 	 * @return name of the CALL
  62 	 */
  63 	public String name() {
  64 		return "INSTANCE";
  65 	}
  66 
  67 	/**
  68 	 * Execute it.
  69 	 * @return the result or null if there is no result
  70 	 */
  71 	public String call() throws CallException {
  72 
  73 		String type;
  74 		String name;
  75 		Module mod;
  76 
  77 		try {
  78 
  79 			// Get parameters
  80 			type = this.requiredString("type");
  81 			name = this.requiredString("name");
  82 
  83 			// Create it
  84 			this.debug(
  85 				"Creating an instance of type=" + type + " and name=" + name);
  86 			if (vmc.has(name)) {
  87 				this.debug("Instance of " + name + " exists and is usable.");
  88 			} else {
  89 
  90 				try {
  91 					Class t = Class.forName(type);
  92 					mod = (Module) t.newInstance();
  93 
  94 					mod.instance(vmc, u, log, sc);
  95 
  96 					// persist it
  97 					vmc.persist(name, mod);
  98 
  99 					this.debug("Instantiated a [" + name + "].");
 100 
 101 				} catch (ClassNotFoundException ef) {
 102 					throw new CallException(
 103 						this.format(
 104 							"MODULE does not exist.  error=" + ef.getMessage()),
 105 						CallException.CODE_CALL_MODULE_CANT_LOAD_FAULT,
 106 						ef);
 107 
 108 				} catch (CallException ex) {
 109 					throw new CallException(
 110 						this.format(
 111 							"Cannot complete instantiation of ["
 112 								+ name
 113 								+ "].  Not aborting, but state of CALL undefined.  Error="
 114 								+ ex.getMessage()),
 115 						CallException.CODE_CALL_ERROR,
 116 						ex);
 117 
 118 				} catch (Exception e) {
 119 					throw new CallException(
 120 						this.format(
 121 							"General instantiation fault.  error="
 122 								+ e.getMessage()),
 123 						CallException.CODE_CALL_MODULE_CANT_LOAD_FAULT,
 124 						e);
 125 				}
 126 			}
 127 
 128 		} catch (CallException ec) {
 129 			throw ec;
 130 		} catch (Exception e) {
 131 			throw new CallException(
 132 				this.format(
 133 					"Exception while creating instance.  error="
 134 						+ e.getMessage()),
 135 				CallException.CODE_CALL_UNRECOVERABLE_FAULT,
 136 				e);
 137 		}
 138 		return Constants.EMPTY_LEFT;
 139 	}
 140 
 141 	/**
 142 	 * Execute using the passed universe, rather than the loaded.
 143 	 * @param uni a universe
 144 	 * @return the result or null if there is no result
 145 	 * @see autohit.universe.Universe
 146 	 */
 147 	public String call(Universe uni) throws CallException {
 148 		String result;
 149 
 150 		// save the uni and make sure it is put back even if there is an exception
 151 		Universe bucket = u;
 152 		try {
 153 			u = uni;
 154 			result = this.call();
 155 		} catch (CallException e) {
 156 			throw e;
 157 		} finally {
 158 			u = bucket;
 159 		}
 160 		return result;
 161 	}
 162 }