groovy.lang.GroovyShell.getContext()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(13.6k)|赞(0)|评价(0)|浏览(171)

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

GroovyShell.getContext介绍

暂无

代码示例

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

/**
 * Initialize the engine.
 */
public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
  super.initialize(mgr, lang, declaredBeans);
  ClassLoader parent = mgr.getClassLoader();
  if (parent == null)
    parent = GroovyShell.class.getClassLoader();
  setLoader(mgr, parent);
  execScripts = new HashMap<Object, Class>();
  evalScripts = new HashMap<Object, Class>();
  context = shell.getContext();
  // create a shell
  // register the mgr with object name "bsf"
  context.setVariable("bsf", new BSFFunctions(mgr, this));
  int size = declaredBeans.size();
  for (int i = 0; i < size; i++) {
    declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
  }
}

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

private String eval(ShellSession session, String name, String def) {
 try {
  GroovyShell shell = getGroovyShell(session);
  Object ret = shell.getContext().getVariable(name);
  if (ret instanceof Closure) {
   log.log(Level.FINEST, "Invoking " + name + " closure");
   Closure c = (Closure)ret;
   ret = c.call();
  } else if (ret == null) {
   log.log(Level.FINEST, "No " + name + " will use empty");
   return def;
  }
  return String.valueOf(ret);
 }
 catch (Exception e) {
  log.log(Level.SEVERE, "Could not get a " + name + " message, will use empty", e);
  return def;
 }
}

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

public void open(CommandContext<? super Object> consumer) throws IOException, CommandException {
 this.consumer = (CommandContext<Object>)consumer;
 GroovyShell shell = GroovyCompiler.getGroovyShell(session);
 ShellBinding binding = (ShellBinding)shell.getContext();
 binding.setCurrent(new InvocationContextImpl<Object>(this.consumer));
 Object o;
 try {
  o = shell.evaluate(request);
 }
 finally {
  binding.setCurrent(null);
 }
 if (o != null) {
  try {
   consumer.provide(o);
  }
  catch (IOException e) {
   throw e;
  }
  catch (CommandException e) {
   throw e;
  }
  catch (Exception e) {
   throw new CommandException(ErrorKind.EVALUATION, "An error occured during the evalution of '" + request + "'", e);
  }
 }
}
public void close() throws IOException, CommandException {

代码示例来源:origin: com.github.corda.crash/crash.shell

private String eval(ShellSession session, String name, String def) {
 try {
  GroovyShell shell = getGroovyShell(session);
  Object ret = shell.getContext().getVariable(name);
  if (ret instanceof Closure) {
   log.log(Level.FINEST, "Invoking " + name + " closure");
   Closure c = (Closure)ret;
   ret = c.call();
  } else if (ret == null) {
   log.log(Level.FINEST, "No " + name + " will use empty");
   return def;
  }
  return String.valueOf(ret);
 }
 catch (Exception e) {
  log.log(Level.SEVERE, "Could not get a " + name + " message, will use empty", e);
  return def;
 }
}

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

/**
 * Displays the current binding used when instanciating the shell.
 */
private void displayBinding() {
  Binding context = shell.getContext();
  Map variables = context.getVariables();
  Set set = variables.keySet();
  if (set.isEmpty()) {
    out.println(MESSAGES.getMessage("command.binding.binding_empty"));
  }
  else {
    out.println(MESSAGES.getMessage("command.binding.available_variables"));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      Object key = iter.next();
      out.print("    ");
      out.print(key);
      out.print(" = ");
      out.println(variables.get(key));
    }
  }
}

代码示例来源:origin: org.crashub/crash.shell

private String eval(ShellSession session, String name, String def) {
 try {
  GroovyShell shell = getGroovyShell(session);
  Object ret = shell.getContext().getVariable(name);
  if (ret instanceof Closure) {
   log.log(Level.FINEST, "Invoking " + name + " closure");
   Closure c = (Closure)ret;
   ret = c.call();
  } else if (ret == null) {
   log.log(Level.FINEST, "No " + name + " will use empty");
   return def;
  }
  return String.valueOf(ret);
 }
 catch (Exception e) {
  log.log(Level.SEVERE, "Could not get a " + name + " message, will use empty", e);
  return def;
 }
}

代码示例来源:origin: com.thinkaurelius.groovy-shaded-asm/groovy-shaded-asm

/**
 * Displays the current binding used when instantiating the shell.
 */
private void displayBinding() {
  Binding context = shell.getContext();
  Map variables = context.getVariables();
  Set set = variables.keySet();
  if (set.isEmpty()) {
    out.println(MESSAGES.getMessage("command.binding.binding_empty"));
  }
  else {
    out.println(MESSAGES.getMessage("command.binding.available_variables"));
    for (Object key : set) {
      out.print("    ");
      out.print(key);
      out.print(" = ");
      out.println(variables.get(key));
    }
  }
}

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

/**
 * Displays the current binding used when instanciating the shell.
 */
private void displayBinding() {
  Binding context = shell.getContext();
  Map variables = context.getVariables();
  Set set = variables.keySet();
  if (set.isEmpty()) {
    out.println(MESSAGES.getMessage("command.binding.binding_empty"));
  }
  else {
    out.println(MESSAGES.getMessage("command.binding.available_variables"));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      Object key = iter.next();
      out.print("    ");
      out.print(key);
      out.print(" = ");
      out.println(variables.get(key));
    }
  }
}

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

/**
 * Displays the current binding used when instanciating the shell.
 */
private void displayBinding() {
  Binding context = shell.getContext();
  Map variables = context.getVariables();
  Set set = variables.keySet();
  if (set.isEmpty()) {
    out.println(MESSAGES.getMessage("command.binding.binding_empty"));
  }
  else {
    out.println(MESSAGES.getMessage("command.binding.available_variables"));
    Iterator iter = set.iterator();
    while (iter.hasNext()) {
      Object key = iter.next();
      out.print("    ");
      out.print(key);
      out.print(" = ");
      out.println(variables.get(key));
    }
  }
}

代码示例来源:origin: jenkinsci/workflow-cps-plugin

@SuppressWarnings("unchecked")
private Object readResolve() {
  execution = CpsFlowExecution.PROGRAM_STATE_SERIALIZATION.get();
  setupTransients();
  assert execution!=null;
  if (scripts != null) { // compatibility: the field will be null in old programs
    GroovyShell shell = execution.getShell();
    assert shell.getContext().getVariables().isEmpty();
    if (!scripts.isEmpty()) {
      // Take the canonical bindings from the main script and relink that object with that of the shell and all other loaded scripts which kept the same bindings.
      shell.getContext().getVariables().putAll(scripts.get(0).getBinding().getVariables());
      for (Script script : scripts) {
        if (script.getBinding().getVariables().equals(shell.getContext().getVariables())) {
          script.setBinding(shell.getContext());
        }
      }
    }
  }
  return this;
}

代码示例来源:origin: org.crsh/crsh.shell.core

private String eval(String name, String def) {
 ClassLoader previous = setCRaSHLoader();
 try {
  GroovyShell shell = getGroovyShell();
  Object ret = shell.getContext().getVariable(name);
  if (ret instanceof Closure) {
   log.log(Level.FINEST, "Invoking " + name + " closure");
   Closure c = (Closure)ret;
   ret = c.call();
  } else if (ret == null) {
   log.log(Level.FINEST, "No " + name + " will use empty");
   return def;
  }
  return String.valueOf(ret);
 }
 catch (Exception e) {
  log.log(Level.SEVERE, "Could not get a " + name + " message, will use empty", e);
  return def;
 }
 finally {
  setPreviousLoader(previous);
 }
}

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

/**
   * Initialize the engine.
   */
  public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
    super.initialize(mgr, lang, declaredBeans);
    ClassLoader parent = mgr.getClassLoader();
    if (parent == null)
      parent = GroovyShell.class.getClassLoader();
    final ClassLoader finalParent = parent;
    this.loader =
        (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
          public Object run() {
            CompilerConfiguration configuration = new CompilerConfiguration();
            configuration.setClasspath(mgr.getClassPath());
            return new GroovyClassLoader(finalParent, configuration);
          }
        });
    execScripts = new HashMap();
    evalScripts = new HashMap();
    context = shell.getContext();
    // create a shell
    // register the mgr with object name "bsf"
    context.setVariable("bsf", new BSFFunctions(mgr, this));
    int size = declaredBeans.size();
    for (int i = 0; i < size; i++) {
      declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
    }
  }
}

代码示例来源:origin: com.thinkaurelius.groovy-shaded-asm/groovy-shaded-asm

/**
   * Initialize the engine.
   */
  public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
    super.initialize(mgr, lang, declaredBeans);
    ClassLoader parent = mgr.getClassLoader();
    if (parent == null)
      parent = GroovyShell.class.getClassLoader();
    final ClassLoader finalParent = parent;
    this.loader =
        (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
          public Object run() {
            CompilerConfiguration configuration = new CompilerConfiguration();
            configuration.setClasspath(mgr.getClassPath());
            return new GroovyClassLoader(finalParent, configuration);
          }
        });
    execScripts = new HashMap();
    evalScripts = new HashMap();
    context = shell.getContext();
    // create a shell
    // register the mgr with object name "bsf"
    context.setVariable("bsf", new BSFFunctions(mgr, this));
    int size = declaredBeans.size();
    for (int i = 0; i < size; i++) {
      declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
    }
  }
}

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

/**
   * Initialize the engine.
   */
  public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
    super.initialize(mgr, lang, declaredBeans);
    ClassLoader parent = mgr.getClassLoader();
    if (parent == null)
      parent = GroovyShell.class.getClassLoader();
    final ClassLoader finalParent = parent;
    this.loader =
        (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
          public Object run() {
            CompilerConfiguration configuration = new CompilerConfiguration();
            configuration.setClasspath(mgr.getClassPath());
            return new GroovyClassLoader(finalParent, configuration);
          }
        });
    execScripts = new HashMap();
    evalScripts = new HashMap();
    context = shell.getContext();
    // create a shell
    // register the mgr with object name "bsf"
    context.setVariable("bsf", new BSFFunctions(mgr, this));
    int size = declaredBeans.size();
    for (int i = 0; i < size; i++) {
      declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
    }
  }
}

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

/**
   * Initialize the engine.
   */
  public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
    super.initialize(mgr, lang, declaredBeans);
    ClassLoader parent = mgr.getClassLoader();
    if (parent == null)
      parent = GroovyShell.class.getClassLoader();
    final ClassLoader finalParent = parent;
    this.loader =
        (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() {
          public Object run() {
            CompilerConfiguration configuration = new CompilerConfiguration();
            configuration.setClasspath(mgr.getClassPath());
            return new GroovyClassLoader(finalParent, configuration);
          }
        });
    execScripts = new HashMap();
    evalScripts = new HashMap();
    context = shell.getContext();
    // create a shell
    // register the mgr with object name "bsf"
    context.setVariable("bsf", new BSFFunctions(mgr, this));
    int size = declaredBeans.size();
    for (int i = 0; i < size; i++) {
      declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
    }
  }
}

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

/**
 * Initialize the engine.
 */
public void initialize(final BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
  super.initialize(mgr, lang, declaredBeans);
  ClassLoader parent = mgr.getClassLoader();
  if (parent == null)
    parent = GroovyShell.class.getClassLoader();
  setLoader(mgr, parent);
  execScripts = new HashMap<Object, Class>();
  evalScripts = new HashMap<Object, Class>();
  context = shell.getContext();
  // create a shell
  // register the mgr with object name "bsf"
  context.setVariable("bsf", new BSFFunctions(mgr, this));
  int size = declaredBeans.size();
  for (int i = 0; i < size; i++) {
    declareBean((BSFDeclaredBean) declaredBeans.elementAt(i));
  }
}

代码示例来源:origin: info.cukes/cucumber-groovy

@Override
public void loadGlue(Glue glue, List<String> gluePaths) {
  this.glue = glue;
  final Binding context = shell.getContext();
  for (String gluePath : gluePaths) {
    // Load sources
    for (Resource resource : resourceLoader.resources(gluePath, ".groovy")) {
      Script script = parse(resource);
      runIfScript(context, script);
    }
    // Load compiled scripts
    for (Class<? extends Script> glueClass : classFinder.getDescendants(Script.class, packageName(gluePath))) {
      try {
        Script script = glueClass.getConstructor(Binding.class).newInstance(context);
        runIfScript(context, script);
      } catch (Exception e) {
        throw new CucumberException(e);
      }
    }
  }
}

代码示例来源:origin: apache/ofbiz-framework

Binding binding = shell.getContext();
  context.putAll(binding.getVariables());
} catch (CompilationFailedException e) {

代码示例来源:origin: com.github.corda.crash/crash.shell

public void open(CommandContext<? super Object> consumer) throws IOException, CommandException {
 this.consumer = (CommandContext<Object>)consumer;
 GroovyShell shell = GroovyCompiler.getGroovyShell(session);
 ShellBinding binding = (ShellBinding)shell.getContext();
 binding.setCurrent(new InvocationContextImpl<Object>(this.consumer));
 Object o;
 try {
  o = shell.evaluate(request);
 }
 finally {
  binding.setCurrent(null);
 }
 if (o != null) {
  try {
   consumer.provide(o);
  }
  catch (IOException e) {
   throw e;
  }
  catch (CommandException e) {
   throw e;
  }
  catch (Exception e) {
   throw new CommandException(ErrorKind.EVALUATION, "An error occured during the evalution of '" + request + "'", e);
  }
 }
}
public void close() throws IOException, CommandException {

代码示例来源:origin: org.crashub/crash.shell

public void open(CommandContext<? super Object> consumer) throws IOException, CommandException {
 this.consumer = (CommandContext<Object>)consumer;
 GroovyShell shell = GroovyCompiler.getGroovyShell(session);
 ShellBinding binding = (ShellBinding)shell.getContext();
 binding.setCurrent(new InvocationContextImpl<Object>(this.consumer));
 Object o;
 try {
  o = shell.evaluate(request);
 }
 finally {
  binding.setCurrent(null);
 }
 if (o != null) {
  try {
   consumer.provide(o);
  }
  catch (IOException e) {
   throw e;
  }
  catch (CommandException e) {
   throw e;
  }
  catch (Exception e) {
   throw new CommandException(ErrorKind.EVALUATION, "An error occured during the evalution of '" + request + "'", e);
  }
 }
}
public void close() throws IOException, CommandException {

相关文章