View Javadoc

    1   /*
    2    $Id: Script.java 4262 2006-11-25 13:27:06Z blackdrag $
    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.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                 // if the method was not found in the current scope (the script's methods)
    108                 // let's try to see if there's a method closure with the same name in the binding
    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     // println helper methods
    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 }