Source code for /engineering/autohit-2003/src/autohit/call/Call_DUMP2LOG.javaOriginal file Call_DUMP2LOG.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.io.BufferedWriter;
  24 import java.io.File;
  25 import java.io.FileWriter;
  26 
  27 import autohit.common.AutohitProperties;
  28 import autohit.common.Constants;
  29 import autohit.universe.Universe;
  30 import autohit.vm.VMLoader;
  31 
  32 /**
  33  * DUMP2LOG call.  Dumps a string to new, unique file in the /log directory.
  34  * <pre>
  35  * REQURIES: logger, core
  36  * IGNORES: uni
  37  * PARAMETERS (INPUT):
  38  	text= text entry
  39  * </pre>
  40  * RETURNS: name of file in /log.
  41  *
  42  * @author Erich P. Gatejen
  43  * @version 1.0
  44  * <i>Version History</i>
  45  * <code>EPG - Initial - 08Jul03</code>
  46  * 
  47  */
  48 public class Call_DUMP2LOG extends Call {
  49 
  50 	// total number of times to try and find a unique file.
  51 	public final static int MAX_TRIES = 5;
  52 
  53 	/**
  54 	 * Implement this to handle load time initialization.  The 
  55 	 * four main fields will already be set--vmc, sc, log, and u.
  56 	 * You must implement this, but you don't have to do anything.
  57 	 * Remember that calls are cached per VM and reused as often
  58 	 * as possible.  There will be no thread-safety issues with the
  59 	 * VMCore or log, but the SystemContecxt and Universe may be shared.
  60 	 * @throws CallException
  61 	 */
  62 	public void load_chain() throws CallException {
  63 		// Nothing to do.
  64 	}
  65 
  66 	/**
  67 	 * Implement this to return the name of the CALL
  68 	 * @return name of the CALL
  69 	 */
  70 	public String name() {
  71 		return "DUMP2LOG";
  72 	}
  73 
  74 	/**
  75 	 * Execute it.
  76 	 * @return the result or null if there is no result
  77 	 */
  78 	public String call() throws CallException {
  79 
  80 		String result = Constants.EMPTY_LEFT;
  81 
  82 		try {
  83 
  84 			// Find the table object.  Make sure it is a Hashtable
  85 			String text = (String) this.desiredString("text");
  86 			if (text == null) {
  87 				return result;
  88 			}
  89 
  90 			// Find a unique file name
  91 			VMLoader vl = sc.getLoader();
  92 			String p = vl.property(AutohitProperties.ROOT_PATH);
  93 			String path = p + AutohitProperties.vLOG_ROOT;
  94 			String proposed = Long.toString(System.currentTimeMillis()) + "0";
  95 			File f = null;
  96 			int i;
  97 			for (i = 0; i < MAX_TRIES; i++) {
  98 				f =
  99 					new File(
 100 						path
 101 							+ proposed
 102 							+ AutohitProperties.literal_FS_DUMP_EXTENSION);
 103 				if (!f.exists())
 104 					break;
 105 				proposed =
 106 					proposed.substring(0, proposed.length() - 1)
 107 						+ Integer.toString(i);
 108 			}
 109 
 110 			if (i < MAX_TRIES) {
 111 				result = proposed;
 112 				BufferedWriter out = new BufferedWriter(new FileWriter(f));
 113 				out.write(text);
 114 				out.close();
 115 				this.info(
 116 					"Succeeded with the dump.  Put in the file="
 117 						+ path
 118 						+ proposed
 119 						+ AutohitProperties.literal_FS_DUMP_EXTENSION);
 120 
 121 			} else {
 122 				throw new CallException(
 123 					this.format(
 124 						"Failed trying to create unique filename.  Last try="
 125 							+ path
 126 							+ proposed
 127 							+ AutohitProperties.literal_FS_DUMP_EXTENSION),
 128 					CallException.CODE_CALL_UNRECOVERABLE_FAULT);
 129 			}
 130 
 131 		} catch (Exception e) {
 132 			throw new CallException(
 133 				this.format(
 134 					"Exception while trying to dump.  error=" + e.getMessage()),
 135 				CallException.CODE_CALL_ERROR,
 136 				e);
 137 		}
 138 		return result;
 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 		return this.call();
 149 	}
 150 }