本文整理汇总了Java中groovy.lang.Script.run方法的典型用法代码示例。如果您正苦于以下问题:Java Script.run方法的具体用法?Java Script.run怎么用?Java Script.run使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类groovy.lang.Script
的用法示例。
在下文中一共展示了Script.run方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import groovy.lang.Script; //导入方法依赖的package包/类
public GroovyResult execute(GroovyResult result, final Script groovyScript, final Map<String, Object> variables)
{
if (variables != null) {
final Binding binding = groovyScript.getBinding();
for (final Map.Entry<String, Object> entry : variables.entrySet()) {
binding.setVariable(entry.getKey(), entry.getValue());
}
}
if (result == null) {
result = new GroovyResult();
}
Object res = null;
try {
res = groovyScript.run();
} catch (final Exception ex) {
log.info("Groovy-Execution-Exception: " + ex.getMessage(), ex);
return new GroovyResult(ex);
}
result.setResult(res);
return result;
}
示例2: runScript
import groovy.lang.Script; //导入方法依赖的package包/类
/**
* Returns the builder object for creating new output variable value.
*/
protected void runScript(String mapperScript, Slurper slurper, Builder builder)
throws ActivityException, TransformerException {
CompilerConfiguration compilerConfig = new CompilerConfiguration();
compilerConfig.setScriptBaseClass(CrossmapScript.class.getName());
Binding binding = new Binding();
binding.setVariable("runtimeContext", getRuntimeContext());
binding.setVariable(slurper.getName(), slurper.getInput());
binding.setVariable(builder.getName(), builder);
GroovyShell shell = new GroovyShell(getPackage().getCloudClassLoader(), binding, compilerConfig);
Script gScript = shell.parse(mapperScript);
// gScript.setProperty("out", getRuntimeContext().get);
gScript.run();
}
示例3: 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);
}
}
示例4: 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);
}
}
示例5: 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);
}
示例6: locate
import groovy.lang.Script; //导入方法依赖的package包/类
@Override
public GPathResult locate(GPathResult root, OfflineOptions options) throws Exception {
boolean domain = Type.of(root) == Type.DOMAIN;
Script script = (Script) (
domain ? DOMAIN_SCRIPT_CLASS.newInstance() : STANDALONE_OR_HOST_SCRIPT_CLASS.newInstance()
);
script.setProperty("root", root);
if (domain) {
String defaultSocketBindingGroup = options.defaultProfile + "-sockets";
if ("default".equals(options.defaultProfile)) {
defaultSocketBindingGroup = "standard-sockets";
}
script.setProperty("defaultSocketBindingGroup", defaultSocketBindingGroup);
}
return (GPathResult) script.run();
}
示例7: eval
import groovy.lang.Script; //导入方法依赖的package包/类
/**
* Evaluate an expression.
*/
public Object eval(String source, int lineNo, int columnNo, Object script) throws BSFException {
try {
Class scriptClass = evalScripts.get(script);
if (scriptClass == null) {
scriptClass = loader.parseClass(script.toString(), source);
evalScripts.put(script, scriptClass);
} else {
LOG.fine("eval() - Using cached script...");
}
//can't cache the script because the context may be different.
//but don't bother loading parsing the class again
Script s = InvokerHelper.createScript(scriptClass, context);
return s.run();
} catch (Exception e) {
throw new BSFException(BSFException.REASON_EXECUTION_ERROR, "exception from Groovy: " + e, e);
}
}
示例8: 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
}
示例9: executeScript
import groovy.lang.Script; //导入方法依赖的package包/类
protected void executeScript(Class scriptClass, Permission missingPermission) {
try {
Script script = InvokerHelper.createScript(scriptClass, new Binding());
script.run();
//InvokerHelper.runScript(scriptClass, null);
} catch (AccessControlException ace) {
if (missingPermission != null && missingPermission.implies(ace.getPermission())) {
return;
} else {
fail(ace.toString());
}
}
if (missingPermission != null) {
fail("Should catch an AccessControlException");
}
}
示例10: initWithScript
import groovy.lang.Script; //导入方法依赖的package包/类
public GroovyFieldDelegate initWithScript(Script groovyScript) {
this.groovyScript = groovyScript;
Object result = groovyScript.run();
@SuppressWarnings("rawtypes")
Map<?, ?> methodMap = (result instanceof Map) ? ((Map) result) : Collections.emptyMap();
gameStarted = getClosure(methodMap, "gameStarted");
ballLost = getClosure(methodMap, "ballLost");
gameEnded = getClosure(methodMap, "gameEnded");
tick = getClosure(methodMap, "tick");
processCollision = getClosure(methodMap, "processCollision");
flippersActivated = getClosure(methodMap, "flippersActivated");
allDropTargetsInGroupHit = getClosure(methodMap, "allDropTargetsInGroupHit");
allRolloversInGroupActivated = getClosure(methodMap, "allRolloversInGroupActivated");
ballInSensorRange = getClosure(methodMap, "ballInSensorRange");
isFieldActive = getClosure(methodMap, "isFieldActive");
return this;
}
示例11: initializeApi
import groovy.lang.Script; //导入方法依赖的package包/类
/**
* Initializes the API that the 'runtime' scripts will rely upon.
* IF this is not called by the application then this is called automatically
* the first time that a script is evaluated.
*/
public void initializeApi() {
if( initialized ) {
return;
}
for( Script script : api ) {
if( log.isDebugEnabled() ) {
log.debug("evaluating API script:" + script);
}
//script.setBinding(localBindings(bindings, source));
Object result = script.run();
if( log.isDebugEnabled() )
log.debug("result:" + result);
}
}
示例12: eval
import groovy.lang.Script; //导入方法依赖的package包/类
public Object eval( InputStream is, String s ) {
if( log.isDebugEnabled() ) {
log.debug( "evaluating:" + s );
}
Script script = compile(is, s);
int before = context.getVariables().size();
Object result = script.run();
if( log.isTraceEnabled() )
log.trace("result:" + result);
if( before != context.getVariables().size() ) {
log.warn("Binding count increased executing:" + s + " keys:" + context.getVariables().keySet());
}
return result;
}
示例13: run
import groovy.lang.Script; //导入方法依赖的package包/类
@Override
public void run(JobToRun job) {
RunnerFilterChain<JobStoryRunContext> chain = createFilterChain();
StoryFilterChainAdapter adapter = new StoryFilterChainAdapter(job, chain);
DslBuilderAndRun.setFilterChainCurrentThread(adapter);
try {
Class<?> clazz = Class.forName(job.getStoryClassName());
if (! (clazz.getSuperclass().getName().equals("groovy.lang.Script"))){
throw new RuntimeException(job.getStoryClassName() + " is not a groovy script");
}
Script script = (Script)clazz.newInstance();
script.run();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例14: 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();
}
}
示例15: testDefaultSleep
import groovy.lang.Script; //导入方法依赖的package包/类
@Test
public void testDefaultSleep() throws Exception {
final Script script = createScript("sleep(1000);", new HashMap<String, Object>());
final AtomicInteger counter = new AtomicInteger(0);
Fiber fiber = new Fiber(scheduler, new SuspendableRunnable() {
@Override
public void run() throws SuspendExecution, InterruptedException {
counter.incrementAndGet();
script.run();
counter.incrementAndGet();
}
});
fiber.start();
fiber.join();
Assert.assertEquals(counter.intValue(), 2);
}