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.common.AutohitErrorCodes;
24 import autohit.common.AutohitProperties;
25 import autohit.common.Constants;
26 import autohit.common.Utils;
27 import autohit.common.channels.SimpleInjector;
28 import autohit.universe.Universe;
29 import autohit.vm.VM;
30 import autohit.vm.VMProcess;
31
32 /**
33 * RUN call. Runs a new script in a new VM. It will use the root logger to
34 * report the success or failure of the new VM startup. It will let the VM create its
35 * own logfile for actual execution. It only supports SimVM for now.
36 * <pre>
37 * REQURIES: logger, core
38 * IGNORES: uni
39 * PARAMETERS (INPUT):
40 * name= name of the script to run
41 * </pre>
42 * RETURNS: The PID of the new VM process as a String. It will be empty if the command failed.
43 *
44 * @author Erich P. Gatejen
45 * @version 1.0
46 * <i>Version History</i>
47 * <code>EPG - Initial - 1APR05</code>
48 *
49 */
50 public class Call_RUN extends Call {
51
52 // Trim the size of result logging in debug mode
53 final static private int TRIM_SIZE = 500;
54
55 /**
56 * Implement this to handle load time initialization. The
57 * four main fields will already be set--vmc, sc, log, and u.
58 * You must implement this, but you don't have to do anything.
59 * Remember that calls are cached per VM and reused as often
60 * as possible. There will be no thread-safety issues with the
61 * VMCore or log, but the SystemContecxt and Universe may be shared.
62 * @throws CallException
63 */
64 public void load_chain() throws CallException {
65 // Nothing to do.
66 }
67
68 /**
69 * Implement this to return the name of the CALL
70 * @return name of the CALL
71 */
72 public String name() {
73 return "RUN";
74 }
75
76 /**
77 * Execute it.
78 * @return the result or null if there is no result
79 */
80 public String call() throws CallException {
81
82 String result = Constants.EMPTY_LEFT;
83 VM myVM;
84 String name = null;
85
86 try {
87
88 // see if the parameter is passed
89 name = this.requiredString("name");
90
91 // build the process
92 VMProcess pcb = (sc.getKernel()).get();
93
94 // Get the VM instance
95 Class t = Class.forName("autohit.vm.SimVM");
96 myVM = (VM) t.newInstance();
97
98 // hook in the client logger
99 SimpleInjector clientInjector = new SimpleInjector();
100 clientInjector.setDefaultSenderID(AutohitProperties.SYSTEM_COMMANDRESPONSE_ID);
101 sc.getLogManager().addClient(clientInjector,Utils.norfIt(pcb.getPID()));
102
103 // Init it
104 myVM.init(clientInjector, name);
105 //myVM.init(sc.getRootLogger().sinjector, name);
106
107 // Set this as the parent vm Core
108 myVM.setParentCore(vmc);
109
110 // unleash it
111 pcb.execute(myVM);
112 result = Integer.toString(pcb.getPID());
113
114 } catch (CallException e) {
115 throw e;
116
117 } catch (ClassNotFoundException e) {
118 throw new CallException(
119 " failed because it could not load the class for the vm. VM specified = autohit.vm.SimVM",
120 AutohitErrorCodes.CODE_CALL_UNRECOVERABLE_FAULT);
121
122 } catch (Exception ex) {
123 //any other is REAL bad
124 throw new CallException(
125 this.format("Serious fault. error=" + ex.getMessage()),
126 CallException.CODE_CALL_UNRECOVERABLE_FAULT,
127 ex);
128 }
129 return result;
130 }
131
132 /**
133 * Execute using the passed universe, rather than the loaded.
134 * @param uni a universe
135 * @return the result or null if there is no result
136 * @see autohit.universe.Universe
137 */
138 public String call(Universe uni) throws CallException {
139 return this.call();
140 }
141 }
|