[groovyscripting] add conf/automation/groovy to the classpath (#12043)

* [groovyscripting] added conf/automation/groovy-path to the classpath

Signed-off-by: Andreas Böckler <andy@boeckler.org>
This commit is contained in:
boecko 2022-01-29 09:33:12 +01:00 committed by GitHub
parent 78e214651f
commit d75baa59af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<String> scriptTypes = (List<String>) 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<String> 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;
}
}