View Javadoc

1   /*
2    $Id: Script.java,v 1.17 2004/12/14 06:25:17 spullara Exp $
3   
4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5   
6    Redistribution and use of this software and associated documentation
7    ("Software"), with or without modification, are permitted provided
8    that the following conditions are met:
9   
10   1. Redistributions of source code must retain copyright
11      statements and notices.  Redistributions must also contain a
12      copy of this document.
13  
14   2. Redistributions in binary form must reproduce the
15      above copyright notice, this list of conditions and the
16      following disclaimer in the documentation and/or other
17      materials provided with the distribution.
18  
19   3. The name "groovy" must not be used to endorse or promote
20      products derived from this Software without prior written
21      permission of The Codehaus.  For written permission,
22      please contact info@codehaus.org.
23  
24   4. Products derived from this Software may not be called "groovy"
25      nor may "groovy" appear in their names without prior written
26      permission of The Codehaus. "groovy" is a registered
27      trademark of The Codehaus.
28  
29   5. Due credit should be given to The Codehaus -
30      http://groovy.codehaus.org/
31  
32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43   OF THE POSSIBILITY OF SUCH DAMAGE.
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.InvokerHelper;
51  
52  import java.io.File;
53  import java.io.IOException;
54  
55  /***
56   * This object represents a Groovy script
57   *
58   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
59   * @author Guillaume Laforge
60   * @version $Revision: 1.17 $
61   */
62  public abstract class Script extends GroovyObjectSupport {
63      private Binding binding = new Binding();
64  
65      protected Script() {
66      }
67  
68      protected Script(Binding binding) {
69          this.binding = binding;
70      }
71  
72      public Binding getBinding() {
73          return binding;
74      }
75  
76      public void setBinding(Binding binding) {
77          this.binding = binding;
78      }
79  
80      public Object getProperty(String property) {
81          //System.out.println("Script.getProperty for: " + property + " with binding: " + binding.getVariables());
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          //System.out.println("Script.setProperty for: " + property + " with newValue: " + newValue);
91          binding.setVariable(property, newValue);
92          //System.out.println("binding are now: " + binding.getVariables());
93      }
94  
95      /***
96       * Invoke a method (or closure in the binding) defined.
97       *
98       * @param name method to call
99       * @param args arguments to pass to the method
100      * @return value
101      */
102     public Object invokeMethod(String name, Object args) {
103         try {
104             return super.invokeMethod(name, args);
105         }
106                 // if the method was not found in the current scope (the script's methods)
107                 // let's try to see if there's a method closure with the same name in the binding
108         catch (MissingMethodException mme) {
109             Object boundClosure = binding.getVariable(name);
110             if (boundClosure != null && boundClosure instanceof Closure) {
111                 return ((Closure) boundClosure).call(args);
112             } else {
113                 throw mme;
114             }
115         }
116     }
117 
118     /***
119      * The main instance method of a script which has variables in scope
120      * as defined by the current {@link Binding} instance.
121      *
122      * @return
123      */
124     public abstract Object run();
125 
126     // println helper methods
127 
128     /***
129      * Prints a newline to the current 'out' variable which should be a PrintWriter
130      * or at least have a println() method defined on it.
131      * If there is no 'out' property then print to standard out.
132      */
133     public void println() {
134         Object object;
135 
136         try {
137             object = getProperty("out");
138         } catch (MissingPropertyException e) {
139             System.out.println();
140             return;
141         }
142 
143         InvokerHelper.invokeMethod(object, "println", ArgumentListExpression.EMPTY_ARRAY);
144     }
145 
146     /***
147      * Prints the value to the current 'out' variable which should be a PrintWriter
148      * or at least have a print() method defined on it.
149      * If there is no 'out' property then print to standard out.
150      */
151     public void print(Object value) {
152         Object object;
153 
154         try {
155             object = getProperty("out");
156         } catch (MissingPropertyException e) {
157             System.out.print(value);
158             return;
159         }
160 
161         InvokerHelper.invokeMethod(object, "print", new Object[]{value});
162     }
163 
164     /***
165      * Prints the value and a newline to the current 'out' variable which should be a PrintWriter
166      * or at least have a println() method defined on it.
167      * If there is no 'out' property then print to standard out.
168      */
169     public void println(Object value) {
170         Object object;
171 
172         try {
173             object = getProperty("out");
174         } catch (MissingPropertyException e) {
175             System.out.println(value);
176             return;
177         }
178 
179         InvokerHelper.invokeMethod(object, "println", new Object[]{value});
180     }
181 
182     /***
183      * A helper method to allow the dynamic evaluation of groovy expressions using this
184      * scripts binding as the variable scope
185      *
186      * @param expression is the Groovy script expression to evaluate
187      */
188     public Object evaluate(String expression) throws CompilationFailedException, IOException {
189         GroovyShell shell = new GroovyShell(binding);
190         return shell.evaluate(expression);
191     }
192 
193     /***
194      * A helper method to allow the dynamic evaluation of groovy expressions using this
195      * scripts binding as the variable scope
196      *
197      * @param file is the Groovy script to evaluate
198      */
199     public Object evaluate(File file) throws CompilationFailedException, IOException {
200         GroovyShell shell = new GroovyShell(binding);
201         return shell.evaluate(file);
202     }
203 
204     /***
205      * A helper method to allow scripts to be run taking command line arguments
206      */
207     public void run(File file, String[] arguments) throws CompilationFailedException, IOException {
208         GroovyShell shell = new GroovyShell(binding);
209         shell.run(file, arguments);
210     }
211 }