Search in sources :

Example 41 with Script

use of groovy.lang.Script in project sulky by huxi.

the class GroovyInstanceTest method refresh.

@Test
public void refresh() throws IOException, InterruptedException {
    JUnitTools.copyResourceToFile("/Foo.groovy", fooFile, System.currentTimeMillis() - 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("/Bar.groovy", fooFile, System.currentTimeMillis());
    Thread.sleep(100);
    object = instance.getInstance();
    assertTrue(object instanceof Script);
    script = (Script) object;
    result = (String) script.run();
    assertEquals("Bar", result);
}
Also used : Script(groovy.lang.Script) Test(org.junit.Test)

Example 42 with Script

use of groovy.lang.Script in project CzechIdMng by bcvsolutions.

the class DefaultGroovyScriptService method evaluate.

@Override
public Object evaluate(String script, Map<String, Object> variables, List<Class<?>> extraAllowedClasses) {
    Assert.notNull(script);
    Binding binding = new Binding(variables);
    Set<Class<?>> allowedVariableClass = resolveCustomAllowTypes(variables);
    if (extraAllowedClasses != null) {
        allowedVariableClass.addAll(extraAllowedClasses);
    }
    GroovySandboxFilter sandboxFilter = null;
    // 
    try {
        // if groovy filter exist add extraAllowedClasses, into this filter, otherwise create new
        if (!GroovyInterceptor.getApplicableInterceptors().isEmpty()) {
            // exists only one goovy filter
            sandboxFilter = (GroovySandboxFilter) GroovyInterceptor.getApplicableInterceptors().get(0);
            sandboxFilter.addCustomTypes(allowedVariableClass);
        } else {
            sandboxFilter = new GroovySandboxFilter(allowedVariableClass);
            sandboxFilter.register();
        }
        // Get script and fill it with variables
        Script scriptObj = scriptCache.getScript(script);
        // Scripts aren't thread safe
        synchronized (scriptObj) {
            scriptObj.setBinding(binding);
            return scriptObj.run();
        }
    } catch (SecurityException | IdmSecurityException ex) {
        LOG.error("SecurityException [{}]", ex.getLocalizedMessage());
        if (ex instanceof IdmSecurityException) {
            throw ex;
        }
        throw new IdmSecurityException(CoreResultCode.GROOVY_SCRIPT_SECURITY_VALIDATION, ImmutableMap.of("message", ex.getLocalizedMessage()), ex);
    } catch (Exception e) {
        LOG.error("Exception [{}]", e.getLocalizedMessage());
        if (e instanceof ResultCodeException) {
            throw e;
        }
        throw new ResultCodeException(CoreResultCode.GROOVY_SCRIPT_EXCEPTION, ImmutableMap.of("message", e.getLocalizedMessage() != null ? e.getLocalizedMessage() : e.toString()), e);
    } finally {
        // otherwise unregister all filter.
        if (sandboxFilter != null) {
            if (sandboxFilter.isCustomTypesLast()) {
                sandboxFilter.unregister();
            } else {
                sandboxFilter.removeLastCustomTypes();
            }
        }
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovySandboxFilter(eu.bcvsolutions.idm.core.security.domain.GroovySandboxFilter) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmSecurityException(eu.bcvsolutions.idm.core.security.exception.IdmSecurityException) IdmSecurityException(eu.bcvsolutions.idm.core.security.exception.IdmSecurityException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) IdmSecurityException(eu.bcvsolutions.idm.core.security.exception.IdmSecurityException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Aggregations

Script (groovy.lang.Script)42 Binding (groovy.lang.Binding)21 IOException (java.io.IOException)12 GroovyShell (groovy.lang.GroovyShell)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)9 MissingMethodException (groovy.lang.MissingMethodException)6 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)5 PrintWriter (java.io.PrintWriter)5 Closure (groovy.lang.Closure)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)4 MetaClass (groovy.lang.MetaClass)4 MissingPropertyException (groovy.lang.MissingPropertyException)4 File (java.io.File)4 UrlMapping (grails.web.mapping.UrlMapping)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 Tuple (groovy.lang.Tuple)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 CompiledScript (javax.script.CompiledScript)3 ScriptException (javax.script.ScriptException)3