Package groovy.lang

Examples of groovy.lang.Script


                }
            }
        };

        try {
            final Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
            Stream.of(scriptClass.getMethods()).forEach(m -> {
                final String name = m.getName();
                globalClosures.put(name, new MethodClosure(scriptObject, name));
            });

            final MetaClass oldMetaClass = scriptObject.getMetaClass();
            scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {
                @Override
                public Object invokeMethod(Object object, String name, Object args) {
                    if (args == null) {
                        return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
                    } else if (args instanceof Tuple) {
                        return invokeMethod(object, name, ((Tuple) args).toArray());
                    } else if (args instanceof Object[]) {
                        return invokeMethod(object, name, (Object[]) args);
                    } else {
                        return invokeMethod(object, name, new Object[]{args});
                    }
                }

                @Override
                public Object invokeMethod(Object object, String name, Object args[]) {
                    try {
                        return super.invokeMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, context);
                    }
                }

                @Override
                public Object invokeStaticMethod(Object object, String name, Object args[]) {
                    try {
                        return super.invokeStaticMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, context);
                    }
                }
            });
            return scriptObject.run();
        } catch (Exception e) {
            throw new ScriptException(e);
        }
    }
View Full Code Here


     * scriptPath - The path to the script that will be run
     * script - the name of the script that will be run (include .groovy exception)
     */
    public synchronized Message process(Message message) throws ActionProcessingException {
        try {
          Script execScript = getScript(message);
            // create a Java/Groovy binding for the message object
            //
            Binding binding = new Binding();

            // binds the message object to the script with a variable name of 'message'
            //
            binding.setVariable("message", message);
            binding.setVariable("config", configTree);
            binding.setVariable("payloadProxy", payloadProxy);
            binding.setVariable("logger", logger);

            execScript.setBinding(binding);

            Object returnVal = execScript.run();

            if(returnVal instanceof Message) {
                return (Message) returnVal;
            } else {
                return message;
View Full Code Here

    }
    String script = imports.toString() + StringUtil.join(commandInfo, ' ');
    try {
      Class clazz = compiler.parseClass(script);
      if (Script.class.isAssignableFrom(clazz)) {
        Script scriptClass = (Script) ReflectionUtil.newInstance(clazz, new Class[] { Binding.class }, ctx.getUI().domain);
        return scriptClass.run();
      } else {
        ctx.getUI().info("Class created: " + clazz);
      }
    } catch (Throwable e) {
      ctx.getUI().error("Error executing: " + script, e);
View Full Code Here

   
    if (file.isExists()) {
      try {
        Class clazz = compiler.parseClass(file.getInputStream());
        if (Script.class.isAssignableFrom(clazz)) {
          Script script = (Script) ReflectionUtil.newInstance(clazz, new Class[] { Binding.class }, ctx.getUI().getDomain());
          return script.run();
        } else {
          ctx.getUI().error("File is not a script: " + filename + ": " + clazz, null);
        }
      } catch (CompilationFailedException e) {
        ctx.getUI().error("Could not compile script", e);
View Full Code Here

   */
  public <T> T run (Class<T> type) {
    InputStream input = null;
    try {
      Class<Script> clazz = gcl.parseClass(mis.getInputStream());
      Script script = (Script) ReflectionUtil.newInstance(clazz, new Class[0]);
      script.setBinding(this);
      return (T) script.run();
    } catch (CompilationFailedException e) {
      throw ThrowableManagerRegistry.caught(e);
    } catch (IOException e) {
      throw ThrowableManagerRegistry.caught(e);
    } finally {
View Full Code Here

     * Process the input files.
     */
    private void processFiles() throws CompilationFailedException, IOException {
        GroovyShell groovy = new GroovyShell(conf);

        Script s;

        if (isScriptFile) {
            s = groovy.parse(huntForTheScriptFile(script));
        } else {
            s = groovy.parse(script, "main");
View Full Code Here

        parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this));
    }

    private void parseAndRunScript(GroovyShell shell, String txt, Object mavenPom, String scriptName, File scriptFile, AntBuilder builder) {
        try {
            final Script script;
            if (scriptFile != null) {
                script = shell.parse(scriptFile);
            } else {
                script = shell.parse(txt, scriptName);
            }
            script.setProperty("ant", builder);
            script.setProperty("project", getProject());
            script.setProperty("properties", new AntProjectPropertiesDelegate(project));
            script.setProperty("target", getOwningTarget());
            script.setProperty("task", this);
            script.setProperty("args", cmdline.getCommandline());
            if (mavenPom != null) {
                script.setProperty("pom", mavenPom);
            }
            script.run();
        }
        catch (final MissingMethodException mme) {
            // not a script, try running through run method but properties will not be available
            if (scriptFile != null) {
                try {
View Full Code Here

  @SuppressWarnings({ "rawtypes", "unchecked" })
  protected void resolveVariables(List arg) {
    List temp = new ArrayList();
    GroovyShell gs = new GroovyShell(getBinding());
    for(Object obj:arg) {
      Script scr = gs.parse("\""+(String)obj+"\"");
      try {
        temp.add(scr.run().toString());
      }
      catch(MissingPropertyException e) {
        throw new SqoopException(ShellError.SHELL_0004, e.getMessage(), e);
      }
    }
View Full Code Here

        contexts.set((LinkedList<Map<String, Object>>) data.get("contexts"));
    }

    public Object build(Class viewClass) {
        if (Script.class.isAssignableFrom(viewClass)) {
            Script script = InvokerHelper.createScript(viewClass, this);
            return build(script);
        } else {
            throw new RuntimeException("Only scripts can be executed via build(Class)");
        }
    }
View Full Code Here

            // then simply return that class
            if (!Script.class.isAssignableFrom(scriptClass)) {
                return scriptClass;
            } else {
                // it's a script
                Script scriptObject = (Script) scriptClass.newInstance();
                scriptObject.setBinding(binding);

                // create a Map of MethodClosures from this new script object
                Method[] methods = scriptClass.getMethods();
                Map<String, Closure> closures = new HashMap<String, Closure>();
                for (Method m : methods) {
                    String name = m.getName();
                    closures.put(name, new MethodClosure(scriptObject, name));
                }

                // save all current closures into global closures map
                globalClosures.putAll(closures);

                MetaClass oldMetaClass = scriptObject.getMetaClass();

                /*
                * We override the MetaClass of this script object so that we can
                * forward calls to global closures (of previous or future "eval" calls)
                * This gives the illusion of working on the same "global" scope.
                */
                scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {
                    @Override
                    public Object invokeMethod(Object object, String name, Object args) {
                        if (args == null) {
                            return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
                        }
                        if (args instanceof Tuple) {
                            return invokeMethod(object, name, ((Tuple) args).toArray());
                        }
                        if (args instanceof Object[]) {
                            return invokeMethod(object, name, (Object[]) args);
                        } else {
                            return invokeMethod(object, name, new Object[]{args});
                        }
                    }

                    @Override
                    public Object invokeMethod(Object object, String name, Object[] args) {
                        try {
                            return super.invokeMethod(object, name, args);
                        } catch (MissingMethodException mme) {
                            return callGlobal(name, args, ctx);
                        }
                    }

                    @Override
                    public Object invokeStaticMethod(Object object, String name, Object[] args) {
                        try {
                            return super.invokeStaticMethod(object, name, args);
                        } catch (MissingMethodException mme) {
                            return callGlobal(name, args, ctx);
                        }
                    }
                });

                return scriptObject.run();
            }
        } catch (Exception e) {
            throw new ScriptException(e);
        } finally {
            // Fix for GROOVY-3669: Can't use several times the same JSR-223 ScriptContext for different groovy script
View Full Code Here

TOP

Related Classes of groovy.lang.Script

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.