diff --git a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java index 5c005581f9e..c1e58a4f7a9 100644 --- a/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java +++ b/bundles/org.openhab.automation.groovyscripting/src/main/java/org/openhab/automation/groovyscripting/internal/GroovyScriptEngineFactory.java @@ -12,6 +12,7 @@ */ package org.openhab.automation.groovyscripting.internal; +import java.io.File; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -20,10 +21,13 @@ import javax.script.ScriptEngine; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.OpenHAB; import org.openhab.core.automation.module.script.AbstractScriptEngineFactory; import org.openhab.core.automation.module.script.ScriptEngineFactory; import org.osgi.service.component.annotations.Component; +import groovy.lang.GroovyClassLoader; + /** * This is an implementation of a {@link ScriptEngineFactory} for Groovy. * @@ -33,12 +37,21 @@ import org.osgi.service.component.annotations.Component; @NonNullByDefault public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory { + private static final String FILE_DIRECTORY = "automation" + File.separator + "groovy"; private final org.codehaus.groovy.jsr223.GroovyScriptEngineFactory factory = new org.codehaus.groovy.jsr223.GroovyScriptEngineFactory(); private final List scriptTypes = (List) Stream.of(factory.getExtensions(), factory.getMimeTypes()) .flatMap(List::stream) // .collect(Collectors.toUnmodifiableList()); + private final GroovyClassLoader gcl = new GroovyClassLoader(); + + public GroovyScriptEngineFactory() { + String scriptDir = OpenHAB.getConfigFolder() + File.separator + FILE_DIRECTORY; + logger.debug("Adding script directory {} to the GroovyScriptEngine class path.", scriptDir); + gcl.addClasspath(scriptDir); + } + @Override public List getScriptTypes() { return scriptTypes; @@ -46,6 +59,9 @@ public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory { @Override public @Nullable ScriptEngine createScriptEngine(String scriptType) { - return scriptTypes.contains(scriptType) ? factory.getScriptEngine() : null; + if (scriptTypes.contains(scriptType)) { + return new org.codehaus.groovy.jsr223.GroovyScriptEngineImpl(gcl); + } + return null; } }