[scripting] Adjust to core changes (#14043)
* [scripting] Adjust to core changes Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
parent
bbc885725f
commit
6aa0dcbc70
|
@ -14,17 +14,17 @@ package org.openhab.automation.jrubyscripting.internal.watch;
|
|||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.automation.jrubyscripting.internal.JRubyScriptEngineFactory;
|
||||
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
|
||||
import org.openhab.core.automation.module.script.ScriptEngineFactory;
|
||||
import org.openhab.core.automation.module.script.ScriptEngineManager;
|
||||
import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
|
||||
import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
|
||||
import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
|
||||
import org.openhab.core.service.ReadyService;
|
||||
import org.openhab.core.service.StartLevelService;
|
||||
import org.osgi.framework.Constants;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
|
@ -37,7 +37,8 @@ import org.slf4j.LoggerFactory;
|
|||
*
|
||||
* @author Cody Cutrer - Initial contribution
|
||||
*/
|
||||
@Component(immediate = true, service = ScriptDependencyTracker.Listener.class)
|
||||
@Component(immediate = true, service = { ScriptFileWatcher.class, ScriptDependencyTracker.Listener.class })
|
||||
@NonNullByDefault
|
||||
public class JRubyScriptFileWatcher extends AbstractScriptFileWatcher {
|
||||
private final Logger logger = LoggerFactory.getLogger(JRubyScriptFileWatcher.class);
|
||||
|
||||
|
@ -47,32 +48,21 @@ public class JRubyScriptFileWatcher extends AbstractScriptFileWatcher {
|
|||
|
||||
@Activate
|
||||
public JRubyScriptFileWatcher(final @Reference ScriptEngineManager manager,
|
||||
final @Reference ReadyService readyService, final @Reference(target = "(" + Constants.SERVICE_PID
|
||||
final @Reference ReadyService readyService, final @Reference StartLevelService startLevelService,
|
||||
final @Reference(target = "(" + Constants.SERVICE_PID
|
||||
+ "=org.openhab.automation.jrubyscripting)") ScriptEngineFactory scriptEngineFactory) {
|
||||
super(manager, readyService, FILE_DIRECTORY);
|
||||
super(manager, readyService, startLevelService, FILE_DIRECTORY);
|
||||
|
||||
this.scriptEngineFactory = (JRubyScriptEngineFactory) scriptEngineFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void importFile(ScriptFileReference ref) {
|
||||
if (isIgnored(ref.getScriptFileURL().getFile())) {
|
||||
return;
|
||||
}
|
||||
super.importFile(ref);
|
||||
}
|
||||
protected Optional<String> getScriptType(Path scriptFilePath) {
|
||||
String path = scriptFilePath.toString();
|
||||
|
||||
@Override
|
||||
protected void processWatchEvent(@Nullable WatchEvent<?> event, WatchEvent.@Nullable Kind<?> kind,
|
||||
@Nullable Path path) {
|
||||
if (Objects.nonNull(path)) {
|
||||
if (!isIgnored(path.toString())) {
|
||||
super.processWatchEvent(event, kind, path);
|
||||
}
|
||||
if (scriptEngineFactory.isFileInGemHome(path) || scriptEngineFactory.isFileInLoadPath(path)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isIgnored(String path) {
|
||||
return scriptEngineFactory.isFileInGemHome(path) || scriptEngineFactory.isFileInLoadPath(path);
|
||||
return super.getScriptType(scriptFilePath);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,17 +14,15 @@ package org.openhab.automation.jsscripting.internal.fs.watch;
|
|||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
|
||||
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
|
||||
import org.openhab.core.automation.module.script.ScriptEngineManager;
|
||||
import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
|
||||
import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileReference;
|
||||
import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
|
||||
import org.openhab.core.service.ReadyService;
|
||||
import org.openhab.core.service.StartLevelService;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
|
@ -34,41 +32,26 @@ import org.osgi.service.component.annotations.Reference;
|
|||
*
|
||||
* @author Jonathan Gilbert - Initial contribution
|
||||
*/
|
||||
@Component(immediate = true, service = ScriptDependencyTracker.Listener.class)
|
||||
@Component(immediate = true, service = { ScriptFileWatcher.class, ScriptDependencyTracker.Listener.class })
|
||||
public class JSScriptFileWatcher extends AbstractScriptFileWatcher {
|
||||
private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
|
||||
|
||||
private final String ignorePath;
|
||||
|
||||
@Activate
|
||||
public JSScriptFileWatcher(final @Reference ScriptEngineManager manager,
|
||||
final @Reference ReadyService readyService) {
|
||||
super(manager, readyService, FILE_DIRECTORY);
|
||||
|
||||
ignorePath = pathToWatch + File.separator + "node_modules";
|
||||
public JSScriptFileWatcher(final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService,
|
||||
final @Reference StartLevelService startLevelService) {
|
||||
super(manager, readyService, startLevelService, FILE_DIRECTORY);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processWatchEvent(@Nullable WatchEvent<?> event, WatchEvent.@Nullable Kind<?> kind,
|
||||
@Nullable Path path) {
|
||||
if (Objects.nonNull(path)) {
|
||||
if (!path.startsWith(ignorePath)) {
|
||||
super.processWatchEvent(event, kind, path);
|
||||
}
|
||||
protected Optional<String> getScriptType(Path scriptFilePath) {
|
||||
if (!scriptFilePath.startsWith(pathToWatch + File.separator + "node_modules")
|
||||
&& "js".equals(super.getScriptType(scriptFilePath).orElse(null))) {
|
||||
return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean createAndLoad(ScriptFileReference ref) {
|
||||
return super.createAndLoad(new ScriptFileReference(ref.getScriptFileURL()) {
|
||||
@Override
|
||||
public Optional<String> getScriptType() {
|
||||
assert super.getScriptType().get().equalsIgnoreCase("js");
|
||||
return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean watchSubDirectories() {
|
||||
return false;
|
||||
|
|
Loading…
Reference in New Issue