use of groovy.lang.Script in project sulky by huxi.
the class GroovyInstanceTest method broken.
@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
@Test
public void broken() throws IOException, InterruptedException {
JUnitTools.copyResourceToFile("/Foo.groovy", fooFile, System.currentTimeMillis() - 2 * ONE_MINUTE);
GroovyInstance instance = new GroovyInstance();
instance.setGroovyFileName(fooFile.getAbsolutePath());
instance.setRefreshInterval(1);
Class instanceClass = instance.getInstanceClass();
assertNotNull(instanceClass);
assertEquals("Foo", instanceClass.getName());
Object object = instance.getInstance();
assertNotNull(object);
assertTrue(object instanceof Script);
Script script = (Script) object;
String result = (String) script.run();
assertEquals("Foo", result);
JUnitTools.copyResourceToFile("/Broken.b0rken", fooFile, System.currentTimeMillis() - ONE_MINUTE);
Thread.sleep(100);
assertNull(instance.getInstanceClass());
assertNull(instance.getInstance());
// error should be logged only once...
Thread.sleep(10);
assertNull(instance.getInstanceClass());
assertNull(instance.getInstance());
Thread.sleep(10);
assertNull(instance.getInstanceClass());
assertNull(instance.getInstance());
Thread.sleep(10);
assertNotNull(instance.getErrorCause());
assertNotNull(instance.getErrorMessage());
JUnitTools.copyResourceToFile("/Bar.groovy", fooFile, System.currentTimeMillis());
object = instance.getInstance();
assertTrue(object instanceof Script);
script = (Script) object;
result = (String) script.run();
assertEquals("Bar", result);
assertNull(instance.getErrorCause());
assertNull(instance.getErrorMessage());
}
use of groovy.lang.Script in project openremote by openremote.
the class RulesetDeployment method compileRulesGroovy.
protected boolean compileRulesGroovy(Ruleset ruleset, Assets assetsFacade, Users usersFacade, Notifications notificationFacade, HistoricDatapoints historicDatapointsFacade, PredictedDatapoints predictedDatapointsFacade) {
try {
// TODO Implement sandbox
// new DenyAll().register();
Script script = groovyShell.parse(ruleset.getRules());
Binding binding = new Binding();
RulesBuilder rulesBuilder = new RulesBuilder();
binding.setVariable("LOG", RulesEngine.RULES_LOG);
binding.setVariable("rules", rulesBuilder);
binding.setVariable("assets", assetsFacade);
binding.setVariable("users", usersFacade);
binding.setVariable("notifications", notificationFacade);
binding.setVariable("historicDatapoints", historicDatapointsFacade);
binding.setVariable("predictedDatapoints", predictedDatapointsFacade);
if (ruleset instanceof TenantRuleset) {
binding.setVariable("realm", ((TenantRuleset) ruleset).getRealm());
}
if (ruleset instanceof AssetRuleset) {
binding.setVariable("assetId", ((AssetRuleset) ruleset).getAssetId());
}
script.setBinding(binding);
script.run();
for (Rule rule : rulesBuilder.build()) {
RulesEngine.LOG.finer("Registering groovy rule: " + rule.getName());
rules.register(rule);
}
return true;
} catch (Exception e) {
setError(e);
return false;
}
}
use of groovy.lang.Script in project hale by halestudio.
the class GroovyUtil method getScript.
/**
* Get the compiled script.
*
* @param function the transformation function the script is associated to
* @param binding the binding to set on the script
* @param service the Groovy service
* @param functionCached if the script should be cached per function instead
* of per cell
* @return the script
* @throws TransformationException if getting the script parameter from the
* function fails
*/
@SuppressWarnings("unchecked")
public static Script getScript(AbstractTransformationFunction<?> function, Binding binding, GroovyService service, boolean functionCached) throws TransformationException {
/*
* The compiled script is stored in a ThreadLocal cache, so it needs to
* be compiled only once per transformation thread.
*/
String script = getScriptString(function);
String hash = DatatypeConverter.printHexBinary(SHA_512.digest(script.getBytes()));
if (PARSED_SCRIPTS.get() == null) {
PARSED_SCRIPTS.set(new HashMap<>());
}
Script groovyScript = PARSED_SCRIPTS.get().get(hash);
if (groovyScript == null) {
groovyScript = service.parseScript(script, null);
PARSED_SCRIPTS.get().put(hash, groovyScript);
}
groovyScript.setBinding(binding);
return groovyScript;
}
use of groovy.lang.Script in project hale by halestudio.
the class GroovyFilter method getScript.
/**
* Get a Groovy script instance and configure it with the given binding.
*
* @param service the Groovy service
* @param binding the script binding
* @return the Groovy script instance
*/
protected Script getScript(GroovyService service, Binding binding) {
/*
* The compiled script is stored in a ThreadLocal variable so it needs
* only to be created once per filter thread.
*/
Script groovyScript = localScript.get();
if (groovyScript == null) {
groovyScript = service.parseScript(script, null);
localScript.set(groovyScript);
}
groovyScript.setBinding(binding);
return groovyScript;
}
use of groovy.lang.Script in project hale by halestudio.
the class GroovyCreate method createInstances.
private Iterable<MutableInstance> createInstances(TypeDefinition type, TransformationLog log, Cell cell, int index) throws TransformationException {
InstanceBuilder builder = new InstanceBuilder(false);
Binding binding = GroovyUtil.createBinding(builder, cell, cell, log, getExecutionContext(), type);
binding.setProperty(BINDING_INDEX, index);
try {
GroovyService service = getExecutionContext().getService(GroovyService.class);
Script script = GroovyUtil.getScript(this, binding, service);
return GroovyUtil.evaluateAll(script, builder, type, service, log);
} catch (TransformationException e) {
throw e;
} catch (NoResultException e) {
log.info(log.createMessage("Skipping target instance because received NoResultException from script", null));
return Collections.emptyList();
} catch (Exception e) {
throw new TransformationException(e.getMessage(), e);
}
}
Aggregations