Search in sources :

Example 1 with RestrictiveGroovyInterceptor

use of eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor in project hale by halestudio.

the class GroovySandboxTest method setUp.

/**
 * Sets up the Groovy shell and interceptor.
 */
@Before
public void setUp() {
    CompilerConfiguration cc = new CompilerConfiguration();
    // enable invoke dynamic support (simiar to in when scripts are created)
    cc.getOptimizationOptions().put(CompilerConfiguration.INVOKEDYNAMIC, true);
    cc.addCompilationCustomizers(new SandboxTransformer());
    shell = new GroovyShell(cc);
    interceptor = new RestrictiveGroovyInterceptor(Collections.<Class<?>>emptySet(), Collections.<Class<?>>emptySet(), Collections.<AllowedPrefix>emptyList());
}
Also used : SandboxTransformer(org.kohsuke.groovy.sandbox.SandboxTransformer) AllowedPrefix(eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor.AllowedPrefix) RestrictiveGroovyInterceptor(eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell) Before(org.junit.Before)

Example 2 with RestrictiveGroovyInterceptor

use of eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor in project hale by halestudio.

the class DefaultGroovyService method createInterceptorFromExtensions.

/**
 * @return the Groovy interceptor configured with the allowed classes from
 *         the extension point.
 */
public static RestrictiveGroovyInterceptor createInterceptorFromExtensions() {
    Set<Class<?>> additionalAllowedClasses = new HashSet<>();
    Set<Class<?>> additionalAllAllowedClasses = new HashSet<>();
    List<AllowedPrefix> additionalAllowedPackages = new ArrayList<>();
    for (IConfigurationElement conf : Platform.getExtensionRegistry().getConfigurationElementsFor(ID)) {
        if (conf.getName().equals("allow")) {
            boolean allowAll = Boolean.parseBoolean(conf.getAttribute("allowAll"));
            Class<?> allowedClass = ExtensionUtil.loadClass(conf, "class");
            if (allowAll)
                additionalAllAllowedClasses.add(allowedClass);
            else
                additionalAllowedClasses.add(allowedClass);
        }
        if (conf.getName().equals("allowPackage")) {
            boolean allowChildren = Boolean.parseBoolean(conf.getAttribute("allowChildren"));
            String packageName = conf.getAttribute("name");
            additionalAllowedPackages.add(new AllowedPrefix(packageName, allowChildren));
        }
    }
    return new RestrictiveGroovyInterceptor(additionalAllowedClasses, additionalAllAllowedClasses, additionalAllowedPackages);
}
Also used : AllowedPrefix(eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor.AllowedPrefix) RestrictiveGroovyInterceptor(eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor) ArrayList(java.util.ArrayList) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) HashSet(java.util.HashSet)

Aggregations

RestrictiveGroovyInterceptor (eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor)2 AllowedPrefix (eu.esdihumboldt.util.groovy.sandbox.internal.RestrictiveGroovyInterceptor.AllowedPrefix)2 GroovyShell (groovy.lang.GroovyShell)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 Before (org.junit.Before)1 SandboxTransformer (org.kohsuke.groovy.sandbox.SandboxTransformer)1