-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[groovyscripting] Fix default preset scope not applied (#17383)
This allows for removing many imports from scripts which results in less code. Fixes #17247 Signed-off-by: Wouter Born <github@maindrain.net>
- Loading branch information
Showing
6 changed files
with
322 additions
and
44 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...n/java/org/openhab/automation/groovyscripting/internal/CustomizableGroovyClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.groovyscripting.internal; | ||
|
||
import java.io.File; | ||
|
||
import org.codehaus.groovy.control.CompilerConfiguration; | ||
import org.codehaus.groovy.control.customizers.CompilationCustomizer; | ||
import org.openhab.core.OpenHAB; | ||
|
||
import groovy.lang.GroovyClassLoader; | ||
|
||
/** | ||
* Customizes the {@link GroovyClassLoader} so that {@link CompilationCustomizer}s can be added which allows for | ||
* importing additional classes via scopes. | ||
* | ||
* @author Wouter Born - Initial contribution | ||
*/ | ||
public class CustomizableGroovyClassLoader extends GroovyClassLoader { | ||
|
||
private static final String FILE_DIRECTORY = "automation" + File.separator + "groovy"; | ||
|
||
private CompilerConfiguration config; | ||
|
||
public CustomizableGroovyClassLoader() { | ||
this(CustomizableGroovyClassLoader.class.getClassLoader(), new CompilerConfiguration(), true); | ||
} | ||
|
||
public CustomizableGroovyClassLoader(ClassLoader parent, CompilerConfiguration config, | ||
boolean useConfigurationClasspath) { | ||
super(parent, config, useConfigurationClasspath); | ||
this.config = config; | ||
addClasspath(OpenHAB.getConfigFolder() + File.separator + FILE_DIRECTORY); | ||
} | ||
|
||
public void addCompilationCustomizers(CompilationCustomizer... customizers) { | ||
config.addCompilationCustomizers(customizers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...src/main/java/org/openhab/automation/groovyscripting/AbstractGroovyScriptingOSGiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.groovyscripting; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.openhab.core.automation.module.script.ScriptEngineContainer; | ||
import org.openhab.core.automation.module.script.ScriptEngineManager; | ||
import org.openhab.core.test.java.JavaOSGiTest; | ||
|
||
/** | ||
* Provides helper methods that can be reused for testing Groovy scripts. | ||
* | ||
* @author Wouter Born - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public abstract class AbstractGroovyScriptingOSGiTest extends JavaOSGiTest { | ||
|
||
protected @NonNullByDefault({}) ScriptEngine engine; | ||
|
||
private final String path = "OH-INF/automation/jsr223/"; | ||
|
||
@BeforeEach | ||
public void init() { | ||
ScriptEngineManager scriptManager = Objects.requireNonNull(getService(ScriptEngineManager.class), | ||
"Could not get ScriptEngineManager"); | ||
ScriptEngineContainer container = Objects.requireNonNull( | ||
scriptManager.createScriptEngine("groovy", "testGroovyEngine"), "Could not create Groovy ScriptEngine"); | ||
engine = container.getScriptEngine(); | ||
} | ||
|
||
protected void evalScript(String fileName) throws ScriptException, IOException { | ||
URL url = bundleContext.getBundle().getResource(path + fileName); | ||
engine.eval(new InputStreamReader(url.openStream())); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...pting.tests/src/main/java/org/openhab/automation/groovyscripting/ScriptScopeOSGiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) 2010-2024 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.automation.groovyscripting; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.script.ScriptException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* This tests the script modules using the Groovy scripting engine. | ||
* | ||
* @author Wouter Born - Initial contribution | ||
*/ | ||
public class ScriptScopeOSGiTest extends AbstractGroovyScriptingOSGiTest { | ||
|
||
@Test | ||
public void scopeWorking() throws ScriptException, IOException { | ||
evalScript("scope-working.groovy"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.