[jsscripting] Refactor dependency tracking (#13756)
Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
parent
4163775e0b
commit
ae677dcd9d
@ -12,13 +12,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.automation.jsscripting.internal;
|
package org.openhab.automation.jsscripting.internal;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.script.ScriptEngine;
|
import javax.script.ScriptEngine;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
|
import org.openhab.automation.jsscripting.internal.fs.watch.JSDependencyTracker;
|
||||||
|
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
|
||||||
import org.openhab.core.automation.module.script.ScriptEngineFactory;
|
import org.openhab.core.automation.module.script.ScriptEngineFactory;
|
||||||
import org.openhab.core.config.core.ConfigurableService;
|
import org.openhab.core.config.core.ConfigurableService;
|
||||||
import org.osgi.framework.Constants;
|
import org.osgi.framework.Constants;
|
||||||
@ -39,6 +40,7 @@ import org.osgi.service.component.annotations.Reference;
|
|||||||
public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
||||||
private static final String CFG_INJECTION_ENABLED = "injectionEnabled";
|
private static final String CFG_INJECTION_ENABLED = "injectionEnabled";
|
||||||
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
|
private static final String INJECTION_CODE = "Object.assign(this, require('openhab'));";
|
||||||
|
private final JSDependencyTracker jsDependencyTracker;
|
||||||
private boolean injectionEnabled = true;
|
private boolean injectionEnabled = true;
|
||||||
|
|
||||||
public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021";
|
public static final String MIME_TYPE = "application/javascript;version=ECMAScript-2021";
|
||||||
@ -46,14 +48,14 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
|||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
public GraalJSScriptEngineFactory(final @Reference JSScriptServiceUtil jsScriptServiceUtil,
|
public GraalJSScriptEngineFactory(final @Reference JSScriptServiceUtil jsScriptServiceUtil,
|
||||||
Map<String, Object> config) {
|
final @Reference JSDependencyTracker jsDependencyTracker, Map<String, Object> config) {
|
||||||
|
this.jsDependencyTracker = jsDependencyTracker;
|
||||||
this.jsScriptServiceUtil = jsScriptServiceUtil;
|
this.jsScriptServiceUtil = jsScriptServiceUtil;
|
||||||
modified(config);
|
modified(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getScriptTypes() {
|
public List<String> getScriptTypes() {
|
||||||
List<String> scriptTypes = new ArrayList<>();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
|
* Whilst we run in parallel with Nashorn, we use a custom mime-type to avoid
|
||||||
@ -66,9 +68,7 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
|||||||
// scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
|
// scriptTypes.addAll(graalJSEngineFactory.getMimeTypes());
|
||||||
// scriptTypes.addAll(graalJSEngineFactory.getExtensions());
|
// scriptTypes.addAll(graalJSEngineFactory.getExtensions());
|
||||||
|
|
||||||
scriptTypes.add(MIME_TYPE);
|
return List.of(MIME_TYPE);
|
||||||
|
|
||||||
return Collections.unmodifiableList(scriptTypes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,6 +82,11 @@ public final class GraalJSScriptEngineFactory implements ScriptEngineFactory {
|
|||||||
new OpenhabGraalJSScriptEngine(injectionEnabled ? INJECTION_CODE : null, jsScriptServiceUtil));
|
new OpenhabGraalJSScriptEngine(injectionEnabled ? INJECTION_CODE : null, jsScriptServiceUtil));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable ScriptDependencyTracker getDependencyTracker() {
|
||||||
|
return jsDependencyTracker;
|
||||||
|
}
|
||||||
|
|
||||||
@Modified
|
@Modified
|
||||||
protected void modified(Map<String, ?> config) {
|
protected void modified(Map<String, ?> config) {
|
||||||
Object injectionEnabled = config.get(CFG_INJECTION_ENABLED);
|
Object injectionEnabled = config.get(CFG_INJECTION_ENABLED);
|
||||||
|
|||||||
@ -14,8 +14,16 @@ package org.openhab.automation.jsscripting.internal.fs.watch;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.openhab.core.OpenHAB;
|
import org.openhab.core.OpenHAB;
|
||||||
import org.openhab.core.automation.module.script.rulesupport.loader.DependencyTracker;
|
import org.openhab.core.automation.module.script.ScriptDependencyTracker;
|
||||||
|
import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptDependencyTracker;
|
||||||
|
import org.osgi.service.component.annotations.Activate;
|
||||||
|
import org.osgi.service.component.annotations.Component;
|
||||||
|
import org.osgi.service.component.annotations.Deactivate;
|
||||||
|
import org.osgi.service.component.annotations.Reference;
|
||||||
|
import org.osgi.service.component.annotations.ReferenceCardinality;
|
||||||
|
import org.osgi.service.component.annotations.ReferencePolicy;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -24,7 +32,9 @@ import org.slf4j.LoggerFactory;
|
|||||||
*
|
*
|
||||||
* @author Jonathan Gilbert - Initial contribution
|
* @author Jonathan Gilbert - Initial contribution
|
||||||
*/
|
*/
|
||||||
public class JSDependencyTracker extends DependencyTracker {
|
@Component(service = JSDependencyTracker.class)
|
||||||
|
@NonNullByDefault
|
||||||
|
public class JSDependencyTracker extends AbstractScriptDependencyTracker {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(JSDependencyTracker.class);
|
private final Logger logger = LoggerFactory.getLogger(JSDependencyTracker.class);
|
||||||
|
|
||||||
@ -35,6 +45,7 @@ public class JSDependencyTracker extends DependencyTracker {
|
|||||||
super(LIB_PATH);
|
super(LIB_PATH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Activate
|
||||||
public void activate() {
|
public void activate() {
|
||||||
File directory = new File(LIB_PATH);
|
File directory = new File(LIB_PATH);
|
||||||
if (!directory.exists()) {
|
if (!directory.exists()) {
|
||||||
@ -47,4 +58,18 @@ public class JSDependencyTracker extends DependencyTracker {
|
|||||||
|
|
||||||
super.activate();
|
super.activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deactivate
|
||||||
|
public void deactivate() {
|
||||||
|
super.deactivate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, unbind = "removeChangeTracker")
|
||||||
|
public void addChangeTracker(ScriptDependencyTracker.Listener listener) {
|
||||||
|
super.addChangeTracker(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeChangeTracker(ScriptDependencyTracker.Listener listener) {
|
||||||
|
super.removeChangeTracker(listener);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,57 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2010-2022 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.jsscripting.internal.fs.watch;
|
|
||||||
|
|
||||||
import org.openhab.core.automation.module.script.ScriptEngineManager;
|
|
||||||
import org.openhab.core.service.ReadyService;
|
|
||||||
import org.osgi.service.component.annotations.Activate;
|
|
||||||
import org.osgi.service.component.annotations.Component;
|
|
||||||
import org.osgi.service.component.annotations.Deactivate;
|
|
||||||
import org.osgi.service.component.annotations.Reference;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Monitors <openHAB-conf>/automation/js for Javascript files & libraries.
|
|
||||||
*
|
|
||||||
* This class is required to ensure that the *order* of set up is correct. Specifically, the dependency tracker must
|
|
||||||
* be activated after the script file watcher. This is because AbstractWatchService only allows a single service to
|
|
||||||
* watch a single directory, and given that the watchers are nested and the last registration wins, the one we want to
|
|
||||||
* monitor the libraries must be registered last.
|
|
||||||
*
|
|
||||||
* @author Jonathan Gilbert - Initial contribution
|
|
||||||
*/
|
|
||||||
@Component(immediate = true, service = JSFileWatcher.class)
|
|
||||||
public class JSFileWatcher {
|
|
||||||
|
|
||||||
private final JSScriptFileWatcher jsScriptFileWatcher;
|
|
||||||
private final JSDependencyTracker jsDependencyTracker;
|
|
||||||
|
|
||||||
@Activate
|
|
||||||
public JSFileWatcher(final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService) {
|
|
||||||
jsDependencyTracker = new JSDependencyTracker();
|
|
||||||
jsScriptFileWatcher = new JSScriptFileWatcher(manager, readyService, jsDependencyTracker);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Activate
|
|
||||||
public void activate() {
|
|
||||||
jsScriptFileWatcher.activate();
|
|
||||||
jsDependencyTracker.activate();
|
|
||||||
jsDependencyTracker.addChangeTracker(jsScriptFileWatcher);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deactivate
|
|
||||||
void deactivate() {
|
|
||||||
jsDependencyTracker.removeChangeTracker(jsScriptFileWatcher);
|
|
||||||
jsDependencyTracker.deactivate();
|
|
||||||
jsScriptFileWatcher.deactivate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -20,24 +20,30 @@ import java.util.Optional;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
|
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.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.ScriptFileReference;
|
||||||
import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
|
|
||||||
import org.openhab.core.service.ReadyService;
|
import org.openhab.core.service.ReadyService;
|
||||||
|
import org.osgi.service.component.annotations.Activate;
|
||||||
|
import org.osgi.service.component.annotations.Component;
|
||||||
|
import org.osgi.service.component.annotations.Reference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
|
* Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
|
||||||
*
|
*
|
||||||
* @author Jonathan Gilbert - Initial contribution
|
* @author Jonathan Gilbert - Initial contribution
|
||||||
*/
|
*/
|
||||||
public class JSScriptFileWatcher extends ScriptFileWatcher {
|
@Component(immediate = true, service = ScriptDependencyTracker.Listener.class)
|
||||||
|
public class JSScriptFileWatcher extends AbstractScriptFileWatcher {
|
||||||
private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
|
private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
|
||||||
|
|
||||||
private final String ignorePath;
|
private final String ignorePath;
|
||||||
|
|
||||||
public JSScriptFileWatcher(final ScriptEngineManager manager, final ReadyService readyService,
|
@Activate
|
||||||
JSDependencyTracker dependencyTracker) {
|
public JSScriptFileWatcher(final @Reference ScriptEngineManager manager,
|
||||||
super(manager, dependencyTracker, readyService, FILE_DIRECTORY);
|
final @Reference ReadyService readyService) {
|
||||||
|
super(manager, readyService, FILE_DIRECTORY);
|
||||||
|
|
||||||
ignorePath = pathToWatch + File.separator + "node_modules";
|
ignorePath = pathToWatch + File.separator + "node_modules";
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user