Search in sources :

Example 46 with Script

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());
}
Also used : Script(groovy.lang.Script) Test(org.junit.Test)

Example 47 with Script

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;
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) Rule(org.jeasy.rules.api.Rule)

Example 48 with Script

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;
}
Also used : Script(groovy.lang.Script)

Example 49 with Script

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;
}
Also used : Script(groovy.lang.Script)

Example 50 with Script

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);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) GroovyService(eu.esdihumboldt.util.groovy.sandbox.GroovyService) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) InstanceBuilder(eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)

Aggregations

Script (groovy.lang.Script)123 Binding (groovy.lang.Binding)59 GroovyShell (groovy.lang.GroovyShell)23 IOException (java.io.IOException)21 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 File (java.io.File)12 Map (java.util.Map)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)8 Closure (groovy.lang.Closure)8 PrintWriter (java.io.PrintWriter)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 MissingMethodException (groovy.lang.MissingMethodException)7