]> git.basschouten.com Git - openhab-addons.git/blob
ba8d02d78908f5d86329f4eb2026d396ea5413a1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jsscripting.internal.fs.watch;
14
15 import java.io.File;
16 import java.nio.file.Path;
17 import java.util.Optional;
18
19 import org.openhab.automation.jsscripting.internal.GraalJSScriptEngineFactory;
20 import org.openhab.core.automation.module.script.ScriptDependencyTracker;
21 import org.openhab.core.automation.module.script.ScriptEngineManager;
22 import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher;
23 import org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher;
24 import org.openhab.core.service.ReadyService;
25 import org.openhab.core.service.StartLevelService;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
29
30 /**
31  * Monitors <openHAB-conf>/automation/js for Javascript files, but not libraries
32  *
33  * @author Jonathan Gilbert - Initial contribution
34  */
35 @Component(immediate = true, service = { ScriptFileWatcher.class, ScriptDependencyTracker.Listener.class })
36 public class JSScriptFileWatcher extends AbstractScriptFileWatcher {
37     private static final String FILE_DIRECTORY = "automation" + File.separator + "js";
38
39     @Activate
40     public JSScriptFileWatcher(final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService,
41             final @Reference StartLevelService startLevelService) {
42         super(manager, readyService, startLevelService, FILE_DIRECTORY);
43     }
44
45     @Override
46     protected Optional<String> getScriptType(Path scriptFilePath) {
47         if (!scriptFilePath.startsWith(pathToWatch + File.separator + "node_modules")
48                 && "js".equals(super.getScriptType(scriptFilePath).orElse(null))) {
49             return Optional.of(GraalJSScriptEngineFactory.MIME_TYPE);
50         } else {
51             return Optional.empty();
52         }
53     }
54
55     @Override
56     protected boolean watchSubDirectories() {
57         return false;
58     }
59 }