1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package groovy.lang;
47
48 import org.codehaus.groovy.ast.expr.ArgumentListExpression;
49 import org.codehaus.groovy.control.CompilationFailedException;
50 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
51 import org.codehaus.groovy.runtime.InvokerHelper;
52
53 import java.io.File;
54 import java.io.IOException;
55
56 /***
57 * This object represents a Groovy script
58 *
59 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
60 * @author Guillaume Laforge
61 * @version $Revision: 4262 $
62 */
63 public abstract class Script extends GroovyObjectSupport {
64 private Binding binding = new Binding();
65
66 protected Script() {
67 }
68
69 protected Script(Binding binding) {
70 this.binding = binding;
71 }
72
73 public Binding getBinding() {
74 return binding;
75 }
76
77 public void setBinding(Binding binding) {
78 this.binding = binding;
79 }
80
81 public Object getProperty(String property) {
82 try {
83 return binding.getVariable(property);
84 } catch (MissingPropertyException e) {
85 return super.getProperty(property);
86 }
87 }
88
89 public void setProperty(String property, Object newValue) {
90 if ("binding".equals(property))
91 setBinding((Binding) newValue);
92 else
93 binding.setVariable(property, newValue);
94 }
95
96 /***
97 * Invoke a method (or closure in the binding) defined.
98 *
99 * @param name method to call
100 * @param args arguments to pass to the method
101 * @return value
102 */
103 public Object invokeMethod(String name, Object args) {
104 try {
105 return super.invokeMethod(name, args);
106 }
107
108
109 catch (MissingMethodException mme) {
110 try {
111 if (name.equals(mme.getMethod())) {
112 Object boundClosure = binding.getVariable(name);
113 if (boundClosure != null && boundClosure instanceof Closure) {
114 return ((Closure) boundClosure).call((Object[])args);
115 } else {
116 throw mme;
117 }
118 } else {
119 throw mme;
120 }
121 } catch (MissingPropertyException mpe) {
122 throw mme;
123 }
124 }
125 }
126
127 /***
128 * The main instance method of a script which has variables in scope
129 * as defined by the current {@link Binding} instance.
130 */
131 public abstract Object run();
132
133
134
135 /***
136 * Prints a newline to the current 'out' variable which should be a PrintWriter
137 * or at least have a println() method defined on it.
138 * If there is no 'out' property then print to standard out.
139 */
140 public void println() {
141 Object object;
142
143 try {
144 object = getProperty("out");
145 } catch (MissingPropertyException e) {
146 System.out.println();
147 return;
148 }
149
150 InvokerHelper.invokeMethod(object, "println", ArgumentListExpression.EMPTY_ARRAY);
151 }
152
153 /***
154 * Prints the value to the current 'out' variable which should be a PrintWriter
155 * or at least have a print() method defined on it.
156 * If there is no 'out' property then print to standard out.
157 */
158 public void print(Object value) {
159 Object object;
160
161 try {
162 object = getProperty("out");
163 } catch (MissingPropertyException e) {
164 DefaultGroovyMethods.print(System.out,value);
165 return;
166 }
167
168 InvokerHelper.invokeMethod(object, "print", new Object[]{value});
169 }
170
171 /***
172 * Prints the value and a newline to the current 'out' variable which should be a PrintWriter
173 * or at least have a println() method defined on it.
174 * If there is no 'out' property then print to standard out.
175 */
176 public void println(Object value) {
177 Object object;
178
179 try {
180 object = getProperty("out");
181 } catch (MissingPropertyException e) {
182 DefaultGroovyMethods.println(System.out,value);
183 return;
184 }
185
186 InvokerHelper.invokeMethod(object, "println", new Object[]{value});
187 }
188
189 /***
190 * A helper method to allow the dynamic evaluation of groovy expressions using this
191 * scripts binding as the variable scope
192 *
193 * @param expression is the Groovy script expression to evaluate
194 */
195 public Object evaluate(String expression) throws CompilationFailedException, IOException {
196 GroovyShell shell = new GroovyShell(binding);
197 return shell.evaluate(expression);
198 }
199
200 /***
201 * A helper method to allow the dynamic evaluation of groovy expressions using this
202 * scripts binding as the variable scope
203 *
204 * @param file is the Groovy script to evaluate
205 */
206 public Object evaluate(File file) throws CompilationFailedException, IOException {
207 GroovyShell shell = new GroovyShell(binding);
208 return shell.evaluate(file);
209 }
210
211 /***
212 * A helper method to allow scripts to be run taking command line arguments
213 */
214 public void run(File file, String[] arguments) throws CompilationFailedException, IOException {
215 GroovyShell shell = new GroovyShell(binding);
216 shell.run(file, arguments);
217 }
218 }