[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:
parent
78e214651f
commit
d75baa59af
|
@ -12,6 +12,7 @@
|
||||||
*/
|
*/
|
||||||
package org.openhab.automation.groovyscripting.internal;
|
package org.openhab.automation.groovyscripting.internal;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
@ -20,10 +21,13 @@ import javax.script.ScriptEngine;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
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.AbstractScriptEngineFactory;
|
||||||
import org.openhab.core.automation.module.script.ScriptEngineFactory;
|
import org.openhab.core.automation.module.script.ScriptEngineFactory;
|
||||||
import org.osgi.service.component.annotations.Component;
|
import org.osgi.service.component.annotations.Component;
|
||||||
|
|
||||||
|
import groovy.lang.GroovyClassLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an implementation of a {@link ScriptEngineFactory} for Groovy.
|
* This is an implementation of a {@link ScriptEngineFactory} for Groovy.
|
||||||
*
|
*
|
||||||
|
@ -33,12 +37,21 @@ import org.osgi.service.component.annotations.Component;
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory {
|
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 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())
|
private final List<String> scriptTypes = (List<String>) Stream.of(factory.getExtensions(), factory.getMimeTypes())
|
||||||
.flatMap(List::stream) //
|
.flatMap(List::stream) //
|
||||||
.collect(Collectors.toUnmodifiableList());
|
.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
|
@Override
|
||||||
public List<String> getScriptTypes() {
|
public List<String> getScriptTypes() {
|
||||||
return scriptTypes;
|
return scriptTypes;
|
||||||
|
@ -46,6 +59,9 @@ public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable ScriptEngine createScriptEngine(String scriptType) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue