本文整理汇总了Java中groovy.lang.Script.setBinding方法的典型用法代码示例。如果您正苦于以下问题:Java Script.setBinding方法的具体用法?Java Script.setBinding怎么用?Java Script.setBinding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类groovy.lang.Script
的用法示例。
在下文中一共展示了Script.setBinding方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import groovy.lang.Script; //导入方法依赖的package包/类
@Override
public Object run(String dsl, Binding binding) {
CompilerConfiguration compilerConfiguration = prepareCompilerConfiguration();
ClassLoader classLoader = prepareClassLoader(AbstractDSLLauncher.class.getClassLoader());
GroovyCodeSource groovyCodeSource = prepareGroovyCodeSource(dsl);
// Groovy shell
GroovyShell shell = new GroovyShell(
classLoader,
new Binding(),
compilerConfiguration
);
// Groovy script
Script groovyScript = shell.parse(groovyCodeSource);
// Binding
groovyScript.setBinding(binding);
// Runs the script
return run(groovyScript);
}
示例2: processScenario
import groovy.lang.Script; //导入方法依赖的package包/类
/**
* Post process every stored request just before it get saved to disk
*
* @param sampler recorded http-request (sampler)
* @param tree HashTree (XML like data structure) that represents exact recorded sampler
*/
public void processScenario(HTTPSamplerBase sampler, HashTree tree, Arguments userVariables, JMeterRecorder recorder)
{
Binding binding = new Binding();
binding.setVariable(ScriptBindingConstants.LOGGER, LOG);
binding.setVariable(ScriptBindingConstants.SAMPLER, sampler);
binding.setVariable(ScriptBindingConstants.TREE, tree);
binding.setVariable(ScriptBindingConstants.CONTEXT, recorder.getContext());
binding.setVariable(ScriptBindingConstants.JSFLIGHT, JMeterJSFlightBridge.getInstance());
binding.setVariable(ScriptBindingConstants.USER_VARIABLES, userVariables);
binding.setVariable(ScriptBindingConstants.CLASSLOADER, classLoader);
Script compiledProcessScript = ScriptEngine.getScript(getScenarioProcessorScript());
if (compiledProcessScript == null)
{
return;
}
compiledProcessScript.setBinding(binding);
LOG.info("Run compiled script");
try {
compiledProcessScript.run();
} catch (Throwable throwable) {
LOG.error(throwable.getMessage(), throwable);
}
}
示例3: evaluate
import groovy.lang.Script; //导入方法依赖的package包/类
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating Groovy expression: {1}", expression);
try {
final ObjectCache<String, Script> scriptCache = threadScriptCache.get();
Script script = scriptCache.get(expression);
if (script == null) {
script = GROOVY_SHELL.parse(expression);
scriptCache.put(expression, script);
}
final Binding binding = new Binding();
for (final Entry<String, ?> entry : values.entrySet()) {
binding.setVariable(entry.getKey(), entry.getValue());
}
script.setBinding(binding);
return script.run();
} catch (final Exception ex) {
throw new ExpressionEvaluationException("Evaluating script with Groovy failed.", ex);
}
}
示例4: build
import groovy.lang.Script; //导入方法依赖的package包/类
public Object build(Script script) {
// this used to be synchronized, but we also used to remove the
// metaclass. Since adding the metaclass is now a side effect, we
// don't need to ensure the meta-class won't be observed and don't
// need to hide the side effect.
MetaClass scriptMetaClass = script.getMetaClass();
script.setMetaClass(new FactoryInterceptorMetaClass(scriptMetaClass, this));
script.setBinding(this);
Object oldScriptName = getProxyBuilder().getVariables().get(SCRIPT_CLASS_NAME);
try {
getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, script.getClass().getName());
return script.run();
} finally {
if(oldScriptName != null) {
getProxyBuilder().setVariable(SCRIPT_CLASS_NAME, oldScriptName);
} else {
getProxyBuilder().getVariables().remove(SCRIPT_CLASS_NAME);
}
}
}
示例5: invoke
import groovy.lang.Script; //导入方法依赖的package包/类
@Override
public SwampScriptInvokationResult invoke(SwampBinding binding) {
Validate.notNull(binding, "Binding cannot be null.");
GroovyInvokationResult result = new GroovyInvokationResult();
try {
Script invokableScript = compiledScript.getClass().newInstance();
invokableScript.setBinding((GroovyBinding) binding);
Object resultObject = invokableScript.run();
result.setVariables(new HashMap<>(binding.getVariableMap()));
result.getVariables().put(SCRIPT_RESULT, resultObject);
} catch (Exception e) {
throw new IllegalStateException(e);
}
return result;
}
示例6: testGroovyShell
import groovy.lang.Script; //导入方法依赖的package包/类
@Test
public void testGroovyShell() throws Exception {
GroovyShell groovyShell = new GroovyShell();
final String s = "x > 2 && y > 1";
Script script = groovyShell.parse(s);
Binding binding = new Binding();
binding.setProperty("x",5);
binding.setProperty("y",3);
script.setBinding(binding);
Object result = script.run();
Assert.assertEquals(true, result);
log.debug("evaluating [{}] with (x,y)=({},{}) => {}\n", s,
script.getBinding().getProperty("x"), script.getBinding().getProperty("y"), result);
binding.setProperty("y",0);
result = script.run();
Assert.assertEquals(false, result);
log.debug("evaluating [{}] with (x,y)=({},{}) => {}\n", s,
binding.getProperty("x"), binding.getProperty("y"), result);
}
示例7: testGroovyShell_goodBindingFollowedByBadBinding_Exception
import groovy.lang.Script; //导入方法依赖的package包/类
@Test(expected = groovy.lang.MissingPropertyException.class)
public void testGroovyShell_goodBindingFollowedByBadBinding_Exception() throws Exception {
GroovyShell groovyShell = new GroovyShell();
final String s = "x > 2 && y > 1";
Script script = groovyShell.parse(s);
Binding binding = new Binding();
binding.setProperty("x", 5);
binding.setProperty("y", 3);
script.setBinding(binding);
Object result = script.run();
Assert.assertEquals(true, result);
Assert.assertTrue(binding.hasVariable("x"));
binding = new Binding();
binding.setProperty("x1", 5);
binding.setProperty("y1", 3);
script.setBinding(binding);
Assert.assertFalse(binding.hasVariable("x"));
script.run(); // throws exception because no bindings for x, y
}
示例8: runScript
import groovy.lang.Script; //导入方法依赖的package包/类
private void runScript(String script) throws Exception {
Vertx vertx = Vertx.vertx();
try {
GroovyShell gcl = new GroovyShell();
Script s = gcl.parse(new File(script));
Binding binding = new Binding();
binding.setProperty("test", this);
binding.setProperty("vertx", vertx);
s.setBinding(binding);
s.run();
} finally {
CountDownLatch latch = new CountDownLatch(1);
vertx.close(v -> {
latch.countDown();
});
latch.await();
}
}
示例9: runReport
import groovy.lang.Script; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void runReport(Map<String, Object> extra) {
try
{
rows = new ArrayList<Map<String, Object>>();
Script script = groovyShell.parse(groovyScript);
script.setBinding(new Binding());
script.getBinding().setVariable("rows", rows);
script.getBinding().setVariable("columns", getColumns());
script.getBinding().setVariable("page", getPage());
script.getBinding().setVariable("pageLimit", getPageLimit());
script.getBinding().setVariable("paramConfig", getParameterConfig());
script.getBinding().setVariable("param", new HashMap<String, ParamConfig>() {{
for (ParamConfig paramConfig : getParameterConfig()) {
put(paramConfig.getId(), paramConfig);
}
}});
script.getBinding().setVariable("extra", extra);
script.run();
} catch (Exception ex)
{
ex.printStackTrace();
getErrors().add(ex.getMessage());
}
}
示例10: doLoad
import groovy.lang.Script; //导入方法依赖的package包/类
@Override
protected void doLoad(Reader reader, String fileName) {
Script script = shell.parse(reader, fileName);
script.setBinding(binding);
script.run();
// Add the last script as the first.
scripts.add(0, script);
}
示例11: executeScript
import groovy.lang.Script; //导入方法依赖的package包/类
@Override
public Object executeScript(String groovyScript, Map<String, Object> parameters) throws WorkflowException {
try {
Script script = scriptCache.get(groovyScript);
script.setBinding(fromMap(parameters));
return script.run();
} catch (ExecutionException ex) {
throw new WorkflowException("Error executing groovy script", ex);
}
}
示例12: execute
import groovy.lang.Script; //导入方法依赖的package包/类
private static final Object execute(String methodBody, Map<String, Object> bindings) throws Exception {
Binding binding = new Binding();
// Bind parameters
if (bindings != null && bindings.size() > 0) {
Set<String> keys = bindings.keySet();
for (String key : keys) {
binding.setVariable(key, bindings.get(key));
}
}
String cacheKey = new StringBuilder(CACHE_KEY_PREFIX_MATCHER_CONDITION).append(Str.AT)
.append(methodBody.hashCode()).toString();
Script parsedScript = (Script) cache.get(cacheKey);
if (parsedScript == null) {
parsedScript = shell.parse(methodBody);
Script cachedParsedScript = (Script) cache.putIfAbsent(cacheKey, parsedScript);
if (cachedParsedScript != null)
parsedScript = cachedParsedScript;
}
parsedScript.setBinding(binding);
return parsedScript.run();
}
示例13: executeGPath
import groovy.lang.Script; //导入方法依赖的package包/类
private void executeGPath() throws ActivityException {
String transform = (String) getAttributeValue(RULE);
if (StringHelper.isEmpty(transform)) {
logger.info("No transform defined for activity: " + getActivityName());
return;
}
GroovyShell shell = new GroovyShell(getClass().getClassLoader());
Script script = shell.parse(transform);
Binding binding = new Binding();
Variable inputVar = getMainProcessDefinition().getVariable(inputDocument);
if (inputVar == null)
throw new ActivityException("Input document variable not found: " + inputDocument);
String inputVarName = inputVar.getName();
Object inputVarValue = getGPathParamValue(inputVarName, inputVar.getType());
binding.setVariable(inputVarName, inputVarValue);
Variable outputVar = getMainProcessDefinition().getVariable(getOutputDocuments()[0]);
String outputVarName = outputVar.getName();
Object outputVarValue = getGPathParamValue(outputVarName, outputVar.getType());
binding.setVariable(outputVarName, outputVarValue);
script.setBinding(binding);
script.run();
Object groovyVarValue = binding.getVariable(outputVarName);
setGPathParamValue(outputVarName, outputVar.getType(), groovyVarValue);
}
示例14: execute
import groovy.lang.Script; //导入方法依赖的package包/类
@Override
public ScriptExecutionResult execute(GameScript<Script> script, ScriptBindings bindings, boolean returnResult) throws Exception {
Script groovyScript = script.getScript();
groovyScript.setBinding(new Binding(bindings));
groovyScript.run();
return returnResult ? new ScriptExecutionResult(groovyScript.getBinding().getVariables()) : null;
}
示例15: run
import groovy.lang.Script; //导入方法依赖的package包/类
public Action run(Binding binding) {
Script script = getScript();
if (script != null) {
script.setBinding(binding);
return (Action) script.run();
}
return null;
}