-
-
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.
Signed-off-by: Jonathan Gilbert <[email protected]>
- Loading branch information
Showing
4 changed files
with
144 additions
and
12 deletions.
There are no files selected for viewing
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
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
61 changes: 61 additions & 0 deletions
61
...c/main/java/org/openhab/automation/jsscripting/internal/fs/watch/JSDependencyTracker.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,61 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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 java.io.File; | ||
|
||
import org.openhab.core.OpenHAB; | ||
import org.openhab.core.automation.module.script.rulesupport.loader.DependencyTracker; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Tracks JS module dependencies | ||
* | ||
* @author Jonathan Gilbert - Initial contribution | ||
*/ | ||
@Component(immediate = true, service = JSDependencyTracker.class) | ||
public class JSDependencyTracker extends DependencyTracker { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(JSDependencyTracker.class); | ||
|
||
public static final String LIB_PATH = String.join(File.separator, OpenHAB.getConfigFolder(), "automation", "js", | ||
"node_modules"); | ||
|
||
public JSDependencyTracker() { | ||
super(LIB_PATH); | ||
} | ||
|
||
@Activate | ||
public void activate() { | ||
|
||
File directory = new File(LIB_PATH); | ||
if (!directory.exists()) { | ||
if (!directory.mkdirs()) { | ||
logger.warn("Failed to create watched directory: {}", LIB_PATH); | ||
} | ||
} else if (directory.isFile()) { | ||
logger.warn("Trying to watch directory {}, however it is a file", LIB_PATH); | ||
} | ||
|
||
super.activate(); | ||
} | ||
|
||
@Deactivate | ||
public void deactivate() { | ||
super.deactivate(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...c/main/java/org/openhab/automation/jsscripting/internal/fs/watch/JSScriptFileWatcher.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,65 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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 java.io.File; | ||
import java.util.Optional; | ||
|
||
import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory; | ||
import org.openhab.core.automation.module.script.ScriptEngineManager; | ||
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.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 | ||
* | ||
* @author Jonathan Gilbert - Initial contribution | ||
*/ | ||
@Component(immediate = true) | ||
public class JSScriptFileWatcher extends ScriptFileWatcher { | ||
private static final String FILE_DIRECTORY = "automation" + File.separator + "js"; | ||
|
||
@Activate | ||
public JSScriptFileWatcher(final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService, | ||
final @Reference JSDependencyTracker jsDependencyTracker) { | ||
super(manager, jsDependencyTracker, readyService, FILE_DIRECTORY); | ||
} | ||
|
||
@Activate | ||
@Override | ||
public void activate() { | ||
super.activate(); | ||
} | ||
|
||
@Deactivate | ||
@Override | ||
public void deactivate() { | ||
super.deactivate(); | ||
} | ||
|
||
@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); | ||
} | ||
}); | ||
} | ||
} |