groovy.lang.Binding类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(817)

本文整理了Java中groovy.lang.Binding类的一些代码示例,展示了Binding类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Binding类的具体详情如下:
包路径:groovy.lang.Binding
类名称:Binding

Binding介绍

[英]Represents the variable bindings of a script which can be altered from outside the script object or created outside of a script and passed into it.
[中]表示脚本的变量绑定,可以从脚本对象外部进行更改,也可以在脚本外部创建并传递到脚本中。

代码示例

代码示例来源:origin: jenkinsci/jenkins

/**
 * Loads a set of given beans
 * @param resources The resources to load
 * @throws IOException
 */
public void loadBeans(Resource[] resources) throws IOException {
  Closure beans = new Closure(this){
    @Override
    public Object call(Object... args) {
      return beans((Closure)args[0]);
    }
  };
  Binding b = new Binding();
  b.setVariable("beans", beans);
  GroovyShell shell = classLoader != null ? new GroovyShell(classLoader,b) : new GroovyShell(b);
  for (Resource resource : resources) {
    shell.evaluate(new InputStreamReader(resource.getInputStream()));
  }
}

代码示例来源:origin: groovy/groovy-core

private void lazyInit() {
  if (initialized) return;
  initialized = true;
  HttpServletResponse response = (HttpServletResponse) super.getVariable("response");
  ServletOutput output = new ServletOutput(response);
  super.setVariable("out", output.getWriter());
  super.setVariable("sout", output.getOutputStream());
  MarkupBuilder builder = new MarkupBuilder(output.getWriter());
  builder.setExpandEmptyElements(true);
  super.setVariable("html", builder);
  try {
    Class jsonBuilderClass = this.getClass().getClassLoader().loadClass("groovy.json.StreamingJsonBuilder");
    Constructor writerConstructor = jsonBuilderClass.getConstructor(Writer.class);
    super.setVariable("json", writerConstructor.newInstance(output.getWriter()));
  } catch (Throwable t) {
    t.printStackTrace();
  }
  // bind forward method
  MethodClosure c = new MethodClosure(this, "forward");
  super.setVariable("forward", c);
  
  // bind include method
  c = new MethodClosure(this, "include");
  super.setVariable("include", c);
  
  // bind redirect method
  c = new MethodClosure(this, "redirect");
  super.setVariable("redirect", c);
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Sets the binding (the variables available in the scope of the BeanBuilder)
 * @param b The Binding instance
 */
public void setBinding(Binding b) {
  this.binding = b.getVariables();
}

代码示例来源:origin: jenkinsci/jenkins

protected int run() throws Exception {
  // this allows the caller to manipulate the JVM state, so require the execute script privilege.
  Jenkins.getActiveInstance().checkPermission(Jenkins.RUN_SCRIPTS);
  Binding binding = new Binding();
  binding.setProperty("out",new PrintWriter(stdout,true));
  binding.setProperty("stdin",stdin);
  binding.setProperty("stdout",stdout);
  binding.setProperty("stderr",stderr);
  binding.setProperty("channel",channel);
  if (channel != null) {
    String j = getClientEnvironmentVariable("JOB_NAME");
    if (j != null) {
      Item job = Jenkins.getActiveInstance().getItemByFullName(j);
      binding.setProperty("currentJob", job);
      String b = getClientEnvironmentVariable("BUILD_NUMBER");
      if (b != null && job instanceof AbstractProject) {
        Run r = ((AbstractProject) job).getBuildByNumber(Integer.parseInt(b));
        binding.setProperty("currentBuild", r);
      }
    }
  }
  GroovyShell groovy = new GroovyShell(Jenkins.getActiveInstance().getPluginManager().uberClassLoader, binding);
  groovy.run(loadScript(),"RemoteClass",remaining.toArray(new String[remaining.size()]));
  return 0;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Filter to run for the LegacySecurityRealm is the
 * ChainServletFilter legacy from /WEB-INF/security/SecurityFilters.groovy.
 */
@Override
public Filter createFilter(FilterConfig filterConfig) {
  Binding binding = new Binding();
  SecurityComponents sc = this.createSecurityComponents();
  binding.setVariable("securityComponents", sc);
  binding.setVariable("securityRealm",this);
  BeanBuilder builder = new BeanBuilder();
  builder.parse(filterConfig.getServletContext().getResourceAsStream("/WEB-INF/security/SecurityFilters.groovy"),binding);
  
  WebApplicationContext context = builder.createApplicationContext();
  
  return (Filter) context.getBean("legacy");
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object evaluate(ScriptSource script, @Nullable Map<String, Object> arguments) {
  GroovyShell groovyShell = new GroovyShell(
      this.classLoader, new Binding(arguments), this.compilerConfiguration);
  try {
    String filename = (script instanceof ResourceScriptSource ?
        ((ResourceScriptSource) script).getResource().getFilename() : null);
    if (filename != null) {
      return groovyShell.evaluate(script.getScriptAsString(), filename);
    }
    else {
      return groovyShell.evaluate(script.getScriptAsString());
    }
  }
  catch (IOException ex) {
    throw new ScriptCompilationException(script, "Cannot access Groovy script", ex);
  }
  catch (GroovyRuntimeException ex) {
    throw new ScriptCompilationException(script, ex);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public SecurityComponents createSecurityComponents() {
  Binding binding = new Binding();
  binding.setVariable("authenticator", new Authenticator());
  BeanBuilder builder = new BeanBuilder();
  builder.parse(Jenkins.getInstance().servletContext.getResourceAsStream("/WEB-INF/security/AbstractPasswordBasedSecurityRealm.groovy"),binding);
  WebApplicationContext context = builder.createApplicationContext();
  return new SecurityComponents(
      findBean(AuthenticationManager.class, context),
      new ImpersonatingUserDetailsService(this));
}

代码示例来源:origin: jenkinsci/jenkins

public String call() throws RuntimeException {
    // if we run locally, cl!=null. Otherwise the delegating classloader will be available as context classloader.
    if (cl==null)       cl = Thread.currentThread().getContextClassLoader();
    CompilerConfiguration cc = new CompilerConfiguration();
    cc.addCompilationCustomizers(new ImportCustomizer().addStarImports(
        "jenkins",
        "jenkins.model",
        "hudson",
        "hudson.model"));
    GroovyShell shell = new GroovyShell(cl,new Binding(),cc);
    StringWriter out = new StringWriter();
    PrintWriter pw = new PrintWriter(out);
    shell.setVariable("out", pw);
    try {
      Object output = shell.evaluate(script);
      if(output!=null)
      pw.println("Result: "+output);
    } catch (Throwable t) {
      Functions.printStackTrace(t, pw);
    }
    return out.toString();
  }
}

代码示例来源:origin: spring-projects/spring-framework

binding.setVariable("beans", beans);
  GroovyShell shell = new GroovyShell(getBeanClassLoader(), binding);
  shell.evaluate(encodedResource.getReader(), "beans");

代码示例来源:origin: groovy/groovy-core

/**
 * When a method is not found in the current script, checks that it's possible to call a method closure from the binding.
 *
 * @throws IOException
 * @throws CompilationFailedException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
public void testInvokeMethodFallsThroughToMethodClosureInBinding() throws IOException, CompilationFailedException, IllegalAccessException, InstantiationException {
  String text = "if (method() == 3) { println 'succeeded' }";
  GroovyCodeSource codeSource = new GroovyCodeSource(text, "groovy.script", "groovy.script");
  GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
  Class clazz = loader.parseClass(codeSource);
  Script script = ((Script) clazz.newInstance());
  Binding binding = new Binding();
  binding.setVariable("method", new MethodClosure(new Dummy(), "method"));
  script.setBinding(binding);
  script.run();
}

代码示例来源:origin: groovy/groovy-core

protected void assertScript(final String text, final String scriptName) throws Exception {
  log.info("About to execute script");
  log.info(text);
  GroovyCodeSource gcs = (GroovyCodeSource) AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
      return new GroovyCodeSource(text, scriptName, "/groovy/testSupport");
    }
  });
  Class groovyClass = loader.parseClass(gcs);
  Script script = InvokerHelper.createScript(groovyClass, new Binding());
  script.run();
}

代码示例来源:origin: groovy/groovy-core

public void run()
  {
    final long id = Thread.currentThread().getId();

    // run the script numIter times
    for (int i = 0; i < numIter; i++)
    {
      Builder builder = new Builder();

      Binding binding = new Binding();
      binding.setVariable("builder", builder);

      script = InvokerHelper.createScript(scriptClass, binding);

      script.run();
    }

    latch.countDown();
  }
}

代码示例来源:origin: spockframework/spock

private GroovyShell createShell() {
  CompilerConfiguration compilerSettings = new CompilerConfiguration();
  compilerSettings.setScriptBaseClass(DelegatingScript.class.getName());
  return new GroovyShell(getClass().getClassLoader(), new Binding(), compilerSettings);
 }
}

代码示例来源:origin: groovy/groovy-core

public void testCreateScriptWithScriptClass() {
    GroovyClassLoader classLoader = new GroovyClassLoader();
    String controlProperty = "text";
    String controlValue = "I am a script";
    String code = controlProperty + " = '" + controlValue + "'";
    GroovyCodeSource codeSource = new GroovyCodeSource(code, "testscript", "/groovy/shell");
    Class scriptClass = classLoader.parseClass(codeSource, false);
    Script script = InvokerHelper.createScript(scriptClass, new Binding(bindingVariables));
    assertEquals(bindingVariables, script.getBinding().getVariables());
    script.run();
    assertEquals(controlValue, script.getProperty(controlProperty));
  }
}

代码示例来源:origin: groovy/groovy-core

public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException {
    // @todo refactor this path
    File currentDir = new File("./src/test/groovy/bugs");
    String file = "bug1567_script.groovy";

    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);
    String[] test = null;
    Object result = shell.run(new File(currentDir, file), test);

    String[] roots = new String[]{currentDir.getAbsolutePath()};
    GroovyScriptEngine gse = new GroovyScriptEngine(roots);
    binding = new Binding();
    // a MME was ensued here stating no 't.start()' was available
    // in the script
    gse.run(file, binding);
  }
}

代码示例来源:origin: groovy/groovy-core

/**
 * Write the template document with the set binding applied to the writer.
 *
 * @see groovy.lang.Writable#writeTo(java.io.Writer)
 */
public Writer writeTo(Writer writer) {
  Binding binding;
  if (map == null)
    binding = new Binding();
  else
    binding = new Binding(map);
  Script scriptObject = InvokerHelper.createScript(script.getClass(), binding);
  PrintWriter pw = new PrintWriter(writer);
  scriptObject.setProperty("out", pw);
  scriptObject.run();
  pw.flush();
  return writer;
}

代码示例来源:origin: org.codehaus.groovy/groovy

/**
 * A helper constructor used in main(String[]) method calls
 *
 * @param args are the command line arguments from a main()
 */
public Binding(String[] args) {
  this();
  setVariable("args", args);
}

代码示例来源:origin: io.vertx/vertx-lang-groovy-gen

@Test
public void testReuseBindingInScript() throws Exception {
 Class clazz = assertScript("ReuseBindingVerticleScript");
 Script script = (Script) clazz.newInstance();
 Binding binding = new Binding();
 binding.setVariable("myobject", new Object());
 script.setBinding(binding);
 ScriptVerticle verticle = new ScriptVerticle(script);
 assertDeploy((vertx, onDeploy) ->
   vertx.deployVerticle(verticle, onDeploy));
 assertTrue(isStarted());
}

代码示例来源:origin: crashub/crash

Binding binding = new Binding(consumer.getSession());
binding.setProperty("args", args);

代码示例来源:origin: jenkinsci/jenkins

/**
 * Parses the bean definition groovy script.
 */
public void parse(InputStream script) {
  parse(script,new Binding());
}

相关文章