]> git.basschouten.com Git - openhab-addons.git/blob
c801c3f49aea1c1c5e702ebd917814ade80be2fd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 org.openhab.core.automation.module.script.ScriptEngineManager;
16 import org.openhab.core.service.ReadyService;
17 import org.osgi.service.component.annotations.Activate;
18 import org.osgi.service.component.annotations.Component;
19 import org.osgi.service.component.annotations.Deactivate;
20 import org.osgi.service.component.annotations.Reference;
21
22 /**
23  * Monitors <openHAB-conf>/automation/js for Javascript files & libraries.
24  *
25  * This class is required to ensure that the *order* of set up is correct. Specifically, the dependency tracker must
26  * be activated after the script file watcher. This is because AbstractWatchService only allows a single service to
27  * watch a single directory, and given that the watchers are nested and the last registration wins, the one we want to
28  * monitor the libraries must be registered last.
29  *
30  * @author Jonathan Gilbert - Initial contribution
31  */
32 @Component(immediate = true, service = JSFileWatcher.class)
33 public class JSFileWatcher {
34
35     private final JSScriptFileWatcher jsScriptFileWatcher;
36     private final JSDependencyTracker jsDependencyTracker;
37
38     @Activate
39     public JSFileWatcher(final @Reference ScriptEngineManager manager, final @Reference ReadyService readyService) {
40         jsDependencyTracker = new JSDependencyTracker();
41         jsScriptFileWatcher = new JSScriptFileWatcher(manager, readyService, jsDependencyTracker);
42     }
43
44     @Activate
45     public void activate() {
46         jsScriptFileWatcher.activate();
47         jsDependencyTracker.activate();
48         jsDependencyTracker.addChangeTracker(jsScriptFileWatcher);
49     }
50
51     @Deactivate
52     void deactivate() {
53         jsDependencyTracker.removeChangeTracker(jsScriptFileWatcher);
54         jsDependencyTracker.deactivate();
55         jsScriptFileWatcher.deactivate();
56     }
57 }