1 package net.sf.tomp.xtcl.command;
2
3 import net.sf.tomp.xtcl.Context;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 /***
9 * Calls a Function. Passes parameters.
10 *
11 * @author tomp
12 */
13 public class Call extends TryBlock {
14
15 private static Log log = LogFactory.getLog(Call.class);
16
17 /*** Name of the function to be called. */
18 protected String name;
19
20 /*** Array of params passed to the function */
21 protected String[] passes;
22
23 /***
24 * set the function name
25 *
26 * @param p function name
27 */
28 public void setName(String p) {
29 name = p;
30 }
31
32 /***
33 * set the passed params
34 *
35 * @param r array of passed params
36 */
37 public void setPasses(String[] r) {
38 passes = r;
39 }
40
41 /***
42 * Calls a function
43 *
44 * @param context command is to be executed within Context
45 * @return call result
46 * @throws Exception if something happens
47 */
48 public int execute(Context context) throws Exception {
49 int retCode = 0;
50 Context prev = context.getPrevious();
51 Function function = (Function) context.get(name);
52
53 for (int i = 0; i < passes.length; i++) {
54 Object v = prev.get(passes[i]);
55
56 log.debug("realparref=" + passes[i] + " realparvalue=" + v);
57
58 if (v == null) {
59 if (context.isStrictPassing()) {
60 log
61 .info("strict passing failed on function="
62 + function.getName() + " cannot assign "
63 + function.getParam(i) + " := "
64 + passes[i]);
65 return done(context, 1);
66 }
67 } else {
68 context.put(function.getParam(i), v);
69 log.debug(", assigned to formalpar=" + function.getParam(i));
70 }
71 }
72
73 return done(context, function.execute(context));
74 }
75
76 /***
77 * returns textual representation: CALL func_name (param1, param2, ...)
78 *
79 * @return textual representation
80 */
81 public String toString() {
82 return "CALL " + name + " (" + listArray(passes) + ")";
83 }
84 }
85
86
87
88
89
90
91
92
93
94
95