Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package groovy.lang;
 
 
 
 import java.io.*;
 import java.util.List;
 import java.util.Map;

Represents a groovy shell capable of running arbitrary groovy scripts

Author(s):
James Strachan
Guillaume Laforge
Version:
$Revision: 13745 $
 
 public class GroovyShell extends GroovyObjectSupport {
        
     public static final String[] EMPTY_ARGS = {};
 
     
     private Binding context;
     private int counter;
     private CompilerConfiguration config;
     private GroovyClassLoader loader;
 
     public static void main(String[] args) {
         GroovyMain.main(args);
     }
 
     public GroovyShell() {
         this(nullnew Binding());
     }
 
     public GroovyShell(Binding binding) {
         this(nullbinding);
     }
 
     public GroovyShell(CompilerConfiguration config) {
         this(new Binding(), config);
     }
 
     public GroovyShell(Binding bindingCompilerConfiguration config) {
         this(nullbindingconfig);
     }
 
     public GroovyShell(ClassLoader parentBinding binding) {
         this(parentbinding.);
     }
 
     public GroovyShell(ClassLoader parent) {
         this(parentnew Binding(), .);
     }
     
     public GroovyShell(ClassLoader parentBinding bindingfinal CompilerConfiguration config) {
         if (binding == null) {
             throw new IllegalArgumentException("Binding must not be null.");
         }
         if (config == null) {
             throw new IllegalArgumentException("Compiler configuration must not be null.");
         }
         final ClassLoader parentLoader = (parent!=null)?parent:GroovyShell.class.getClassLoader();
         this. = (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
             public Object run() {
                 return new GroovyClassLoader(parentLoader,config);
             }
         });
         this. = binding;        
         this. = config;
     }
     
     public void initializeBinding() {
         Map map = .getVariables();
         if (map.get("shell")==nullmap.put("shell",this);
     }
     
    public void resetLoadedClasses() {
        .clearCache();
    }

    
Creates a child shell using a new ClassLoader which uses the parent shell's class loader as its parent

Parameters:
shell is the parent shell used for the variable bindings and the parent class loader
    public GroovyShell(GroovyShell shell) {
        this(shell.loadershell.context);
    }
    public Binding getContext() {
        return ;
    }
    public GroovyClassLoader getClassLoader() {
        return ;
    }
    public Object getProperty(String property) {
        Object answer = getVariable(property);
        if (answer == null) {
            answer = super.getProperty(property);
        }
        return answer;
    }
    public void setProperty(String propertyObject newValue) {
        setVariable(propertynewValue);
        try {
            super.setProperty(propertynewValue);
        } catch (GroovyRuntimeException e) {
            // ignore, was probably a dynamic property
        }
    }

    
A helper method which runs the given script file with the given command line arguments

Parameters:
scriptFile the file of the script to run
list the command line arguments to pass in
    public Object run(File scriptFileList listthrows CompilationFailedExceptionIOException {
        String[] args = new String[list.size()];
        return run(scriptFile, (String[]) list.toArray(args));
    }

    
A helper method which runs the given cl script with the given command line arguments

Parameters:
scriptText is the text content of the script
fileName is the logical file name of the script (which is used to create the class name of the script)
list the command line arguments to pass in
    public Object run(String scriptTextString fileNameList listthrows CompilationFailedException {
        String[] args = new String[list.size()];
        list.toArray(args);
        return run(scriptTextfileNameargs);
    }

    
Runs the given script file name with the given command line arguments

Parameters:
scriptFile the file name of the script to run
args the command line arguments to pass in
    public Object run(final File scriptFileString[] argsthrows CompilationFailedExceptionIOException {
        String scriptName = scriptFile.getName();
        int p = scriptName.lastIndexOf(".");
        if (p++ >= 0) {
            if (scriptName.substring(p).equals("java")) {
                ..println("error: cannot compile file with .java extension: " + scriptName);
                throw new CompilationFailedException(0, null);
            }
        }
        // Get the current context classloader and save it on the stack
        final Thread thread = Thread.currentThread();
        //ClassLoader currentClassLoader = thread.getContextClassLoader();
        class DoSetContext implements PrivilegedAction {
            ClassLoader classLoader;
            public DoSetContext(ClassLoader loader) {
                 = loader;
            }
            public Object run() {
                thread.setContextClassLoader();
                return null;
            }
        }
        AccessController.doPrivileged(new DoSetContext());
        // Parse the script, generate the class, and invoke the main method.  This is a little looser than
        // if you are compiling the script because the JVM isn't executing the main method.
        Class scriptClass;
        try {
            scriptClass = (Class) AccessController.doPrivileged(new PrivilegedExceptionAction() {
                public Object run() throws CompilationFailedExceptionIOException {
                    return .parseClass(scriptFile);
                }
            });
        } catch (PrivilegedActionException pae) {
            Exception e = pae.getException();
            if (e instanceof CompilationFailedException) {
                throw (CompilationFailedExceptione;
            } else if (e instanceof IOException) {
                throw (IOExceptione;
            } else {
                throw (RuntimeExceptionpae.getException();
            }
        }
        return runScriptOrMainOrTestOrRunnable(scriptClassargs);
        // Set the context classloader back to what it was.
        //AccessController.doPrivileged(new DoSetContext(currentClassLoader));
    }

    
if (theClass is a Script) { run it like a script } else if (theClass has a main method) { run the main method } else if (theClass instanceof GroovyTestCase) { use the test runner to run it } else if (theClass implements Runnable) { if (theClass has a constructor with String[] params) instanciate theClass with this constructor and run else if (theClass has a no-args constructor) instanciate theClass with the no-args constructor and run }
    private Object runScriptOrMainOrTestOrRunnable(Class scriptClassString[] args) {
        if (scriptClass == null) {
            return null;
        }
        try {
            if (Script.class.isAssignableFrom(scriptClass)) {
                // treat it just like a script if it is one
                Script script  = null;
                try {
                    script = (ScriptscriptClass.newInstance();
                } catch (InstantiationException e) {
                    // ignore instaintiations errors,, try to do main
                } catch (IllegalAccessException e) {
                   // ignore instaintiations errors, try to do main
                }
                if (script != null) {
                    script.setBinding();
                    script.setProperty("args"args);
                    return script.run();
                }
            }
            // let's find a main method
            scriptClass.getMethod("main"new Class[]{String[].class});
            // if that main method exist, invoke it
            return InvokerHelper.invokeMethod(scriptClass"main"new Object[]{args});
        } catch (NoSuchMethodException e) {
            // if it implements Runnable, try to instantiate it
            if (Runnable.class.isAssignableFrom(scriptClass)) {
                return runRunnable(scriptClassargs);
            }
            // if it's a JUnit 3.8.x test, run it with an appropriate runner
            if (isJUnit3Test(scriptClass)) {
                return runJUnit3Test(scriptClass);
            }
            // if it's a JUnit 4.x test, run it with an appropriate runner
            if (isJUnit4Test(scriptClass)) {
                return runJUnit4Test(scriptClass);
            }
            // if it's a TestNG tst, run it with an appropriate runner
            if (isTestNgTest(scriptClass)) {
                return runTestNgTest(scriptClass);
            }
            throw new GroovyRuntimeException("This script or class could not be run.\n" +
                    "It should either: \n" +
                    "- have a main method, \n" +
                    "- be a JUnit test, TestNG test or extend GroovyTestCase, \n" +
                    "- or implement the Runnable interface.");
        }
    }
    private Object runRunnable(Class scriptClassString[] args) {
        Constructor constructor = null;
        Runnable runnable = null;
        Throwable reason = null;
        try {
            // first, fetch the constructor taking String[] as parameter
            constructor = scriptClass.getConstructor(new Class[]{(new String[]{}).getClass()});
            try {
                // instantiate a runnable and run it
                runnable = (Runnableconstructor.newInstance(new Object[]{args});
            } catch (Throwable t) {
                reason = t;
            }
        } catch (NoSuchMethodException e1) {
            try {
                // otherwise, find the default constructor
                constructor = scriptClass.getConstructor(new Class[]{});
                try {
                    // instantiate a runnable and run it
                    runnable = (Runnableconstructor.newInstance(new Object[]{});
                } catch (Throwable t) {
                    reason = t;
                }
            } catch (NoSuchMethodException nsme) {
                reason = nsme;
            }
        }
        if (constructor != null && runnable != null) {
            runnable.run();
        } else {
            throw new GroovyRuntimeException("This script or class was runnable but could not be run. "reason);
        }
        return null;
    }

    
Run the specified class extending TestCase as a unit test. This is done through reflection, to avoid adding a dependency to the JUnit framework. Otherwise, developers embedding Groovy and using GroovyShell to load/parse/compile groovy scripts and classes would have to add another dependency on their classpath.

Parameters:
scriptClass the class to be run as a unit test
    private Object runJUnit3Test(Class scriptClass) {
        try {
            Object testSuite = InvokerHelper.invokeConstructorOf("junit.framework.TestSuite",new Object[]{scriptClass});
            return InvokerHelper.invokeStaticMethod("junit.textui.TestRunner""run"new Object[]{testSuite});
        } catch (ClassNotFoundException e) {
            throw new GroovyRuntimeException("Failed to run the unit test. JUnit is not on the Classpath.");
        }
    }
    private Object runJUnit4Test(Class scriptClass) {
        try {
            return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.JUnit4Utils",
                    "realRunJUnit4Test"new Object[]{scriptClass});
        } catch (ClassNotFoundException e) {
            throw new GroovyRuntimeException("Failed to run the JUnit 4 test.");
        }
    }
    private Object runTestNgTest(Class scriptClass) {
        try {
            return InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.TestNgUtils",
                    "realRunTestNgTest"new Object[]{scriptClass});
        } catch (ClassNotFoundException e) {
            throw new GroovyRuntimeException("Failed to run the TestNG test.");
        }
    }

    
Utility method to check through reflection if the class appears to be a JUnit 3.8.x test, i.e.&nsbp;checks if it extends JUnit 3.8.x's TestCase.

Parameters:
scriptClass the class we want to check
Returns:
true if the class appears to be a test
    private boolean isJUnit3Test(Class scriptClass) {
        // check if the parsed class is a GroovyTestCase,
        // so that it is possible to run it as a JUnit test
        boolean isUnitTestCase = false;
        try {
            try {
                Class testCaseClass = this..loadClass("junit.framework.TestCase");
                // if scriptClass extends testCaseClass
                if (testCaseClass.isAssignableFrom(scriptClass)) {
                    isUnitTestCase = true;
                }
            } catch (ClassNotFoundException e) {
                // fall through
            }
        } catch (Throwable e) {
            // fall through
        }
        return isUnitTestCase;
    }

    
Utility method to check via reflection if the parsed class appears to be a JUnit4 test, i.e.&nsbp;checks whether it appears to be using the relevant JUnit 4 annotations.

Parameters:
scriptClass the class we want to check
Returns:
true if the class appears to be a test
    private boolean isJUnit4Test(Class scriptClass) {
        // if we are running under Java 1.4 don't bother trying to check
        char version = System.getProperty("java.version").charAt(2);
        if (version < '5') {
            return false;
        }
        // check if there are appropriate class or method annotations
        // that suggest we have a JUnit 4 test
        boolean isTest = false;
        try {
            if (InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.JUnit4Utils",
                    "realIsJUnit4Test"new Object[]{scriptClassthis.}) == .) {
                isTest = true;
            };
        } catch (ClassNotFoundException e) {
            throw new GroovyRuntimeException("Failed to invoke the JUnit 4 helper class.");
        }
        return isTest;
    }

    
Utility method to check via reflection if the parsed class appears to be a TestNG test, i.e.&nsbp;checks whether it appears to be using the relevant TestNG annotations.

Parameters:
scriptClass the class we want to check
Returns:
true if the class appears to be a test
    private boolean isTestNgTest(Class scriptClass) {
        char version = System.getProperty("java.version").charAt(2);
        if (version < '5') {
            return false;
        }
        // check if there are appropriate class or method annotations
        // that suggest we have a TestNG test
        boolean isTest = false;
        try {
            if (InvokerHelper.invokeStaticMethod("org.codehaus.groovy.vmplugin.v5.TestNgUtils",
                    "realIsTestNgTest"new Object[]{scriptClassthis.}) == .) {
                isTest = true;
            };
        } catch (ClassNotFoundException e) {
            throw new GroovyRuntimeException("Failed to invoke the TestNG helper class.");
        }
        return isTest;
    }

    
Runs the given script text with command line arguments

Parameters:
scriptText is the text content of the script
fileName is the logical file name of the script (which is used to create the class name of the script)
args the command line arguments to pass in
    public Object run(String scriptTextString fileNameString[] argsthrows CompilationFailedException {
        try {
            return run(new ByteArrayInputStream(scriptText.getBytes(.getSourceEncoding())), fileNameargs);
        } catch (UnsupportedEncodingException e) {
            throw new CompilationFailedException(0, nulle);
        }
    }

    
Runs the given script with command line arguments

Parameters:
in the stream reading the script
fileName is the logical file name of the script (which is used to create the class name of the script)
args the command line arguments to pass in
    public Object run(final InputStream infinal String fileNameString[] argsthrows CompilationFailedException {
        GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                return new GroovyCodeSource(infileName"/groovy/shell");
            }
        });
        Class scriptClass = parseClass(gcs);
        return runScriptOrMainOrTestOrRunnable(scriptClassargs);
    }
    public Object getVariable(String name) {
        return .getVariables().get(name);
    }
    public void setVariable(String nameObject value) {
        .setVariable(namevalue);
    }

    
Evaluates some script against the current Binding and returns the result

    public Object evaluate(GroovyCodeSource codeSourcethrows CompilationFailedException {
        Script script = parse(codeSource);
        return script.run();
    }

    
Evaluates some script against the current Binding and returns the result

Parameters:
scriptText the text of the script
fileName is the logical file name of the script (which is used to create the class name of the script)
    public Object evaluate(String scriptTextString fileNamethrows CompilationFailedException {
        try {
            return evaluate(new ByteArrayInputStream(scriptText.getBytes(.getSourceEncoding())), fileName);
        } catch (UnsupportedEncodingException e) {
            throw new CompilationFailedException(0, nulle);
        }
    }

    
Evaluates some script against the current Binding and returns the result. The .class file created from the script is given the supplied codeBase
    public Object evaluate(String scriptTextString fileNameString codeBasethrows CompilationFailedException {
        try {
            return evaluate(new GroovyCodeSource(new ByteArrayInputStream(scriptText.getBytes(.getSourceEncoding())), fileNamecodeBase));
        } catch (UnsupportedEncodingException e) {
            throw new CompilationFailedException(0, nulle);
        }
    }

    
Evaluates some script against the current Binding and returns the result

Parameters:
file is the file of the script (which is used to create the class name of the script)
    public Object evaluate(File filethrows CompilationFailedExceptionIOException {
        return evaluate(new GroovyCodeSource(file));
    }

    
Evaluates some script against the current Binding and returns the result

Parameters:
scriptText the text of the script
    public Object evaluate(String scriptTextthrows CompilationFailedException {
        try {
            return evaluate(new ByteArrayInputStream(scriptText.getBytes(.getSourceEncoding())), generateScriptName());
        } catch (UnsupportedEncodingException e) {
            throw new CompilationFailedException(0, nulle);
        }
    }

    
Evaluates some script against the current Binding and returns the result

Parameters:
in the stream reading the script
    public Object evaluate(InputStream inthrows CompilationFailedException {
        return evaluate(ingenerateScriptName());
    }

    
Evaluates some script against the current Binding and returns the result

Parameters:
in the stream reading the script
fileName is the logical file name of the script (which is used to create the class name of the script)
    public Object evaluate(InputStream inString fileNamethrows CompilationFailedException {
        Script script = null;
        try {
            script = parse(infileName);
            return script.run();
        } finally {
            if (script != null) {
                InvokerHelper.removeClass(script.getClass());
            }
        }
    }

    
Parses the given script and returns it ready to be run

Parameters:
in the stream reading the script
fileName is the logical file name of the script (which is used to create the class name of the script)
Returns:
the parsed script which is ready to be run via @link Script.run()
    public Script parse(final InputStream infinal String fileNamethrows CompilationFailedException {
        GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                return new GroovyCodeSource(infileName"/groovy/shell");
            }
        });
        return parse(gcs);
    }

    
Parses the groovy code contained in codeSource and returns a java class.
    private Class parseClass(final GroovyCodeSource codeSourcethrows CompilationFailedException {
        // Don't cache scripts
        return .parseClass(codeSourcefalse);
    }

    
Parses the given script and returns it ready to be run. When running in a secure environment (-Djava.security.manager) codeSource.getCodeSource() determines what policy grants should be given to the script.

Parameters:
codeSource
Returns:
ready to run script
    public Script parse(final GroovyCodeSource codeSourcethrows CompilationFailedException {
        return InvokerHelper.createScript(parseClass(codeSource), );
    }

    
Parses the given script and returns it ready to be run

Parameters:
file is the file of the script (which is used to create the class name of the script)
    public Script parse(File filethrows CompilationFailedExceptionIOException {
        return parse(new GroovyCodeSource(file));
    }

    
Parses the given script and returns it ready to be run

Parameters:
scriptText the text of the script
    public Script parse(String scriptTextthrows CompilationFailedException {
        try {
            return parse(new ByteArrayInputStream(scriptText.getBytes(.getSourceEncoding())), generateScriptName());
        } catch (UnsupportedEncodingException e) {
            throw new CompilationFailedException(0, nulle);
        }
    }
    public Script parse(String scriptTextString fileNamethrows CompilationFailedException {
        try {
            return parse(new ByteArrayInputStream(scriptText.getBytes(.getSourceEncoding())), fileName);
        } catch (UnsupportedEncodingException e) {
            throw new CompilationFailedException(0, nulle);
        }
    }

    
Parses the given script and returns it ready to be run

Parameters:
in the stream reading the script
    public Script parse(InputStream inthrows CompilationFailedException {
        return parse(ingenerateScriptName());
    }
    protected synchronized String generateScriptName() {
        return "Script" + (++) + ".groovy";
    }
New to GrepCode? Check out our FAQ X