2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.groovyscripting.internal;
16 import java.util.List;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import javax.script.ScriptEngine;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.OpenHAB;
25 import org.openhab.core.automation.module.script.AbstractScriptEngineFactory;
26 import org.openhab.core.automation.module.script.ScriptEngineFactory;
27 import org.osgi.service.component.annotations.Component;
29 import groovy.lang.GroovyClassLoader;
32 * This is an implementation of a {@link ScriptEngineFactory} for Groovy.
34 * @author Wouter Born - Initial contribution
36 @Component(service = ScriptEngineFactory.class)
38 public class GroovyScriptEngineFactory extends AbstractScriptEngineFactory {
40 private static final String FILE_DIRECTORY = "automation" + File.separator + "groovy";
41 private final org.codehaus.groovy.jsr223.GroovyScriptEngineFactory factory = new org.codehaus.groovy.jsr223.GroovyScriptEngineFactory();
43 private final List<String> scriptTypes = (List<String>) Stream.of(factory.getExtensions(), factory.getMimeTypes())
44 .flatMap(List::stream) //
45 .collect(Collectors.toUnmodifiableList());
47 private final GroovyClassLoader gcl = new GroovyClassLoader(GroovyScriptEngineFactory.class.getClassLoader());
49 public GroovyScriptEngineFactory() {
50 String scriptDir = OpenHAB.getConfigFolder() + File.separator + FILE_DIRECTORY;
51 logger.debug("Adding script directory {} to the GroovyScriptEngine class path.", scriptDir);
52 gcl.addClasspath(scriptDir);
56 public List<String> getScriptTypes() {
61 public @Nullable ScriptEngine createScriptEngine(String scriptType) {
62 if (scriptTypes.contains(scriptType)) {
63 return new org.codehaus.groovy.jsr223.GroovyScriptEngineImpl(gcl);