]> git.basschouten.com Git - openhab-addons.git/blob
05d763910a34567a6d90b93d79d0f0692f1b7258
[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
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.OpenHAB;
19 import org.openhab.core.automation.module.script.ScriptDependencyTracker;
20 import org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptDependencyTracker;
21 import org.osgi.service.component.annotations.Activate;
22 import org.osgi.service.component.annotations.Component;
23 import org.osgi.service.component.annotations.Deactivate;
24 import org.osgi.service.component.annotations.Reference;
25 import org.osgi.service.component.annotations.ReferenceCardinality;
26 import org.osgi.service.component.annotations.ReferencePolicy;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Tracks JS module dependencies
32  *
33  * @author Jonathan Gilbert - Initial contribution
34  */
35 @Component(service = JSDependencyTracker.class)
36 @NonNullByDefault
37 public class JSDependencyTracker extends AbstractScriptDependencyTracker {
38
39     private final Logger logger = LoggerFactory.getLogger(JSDependencyTracker.class);
40
41     public static final String LIB_PATH = String.join(File.separator, OpenHAB.getConfigFolder(), "automation", "js",
42             "node_modules");
43
44     public JSDependencyTracker() {
45         super(LIB_PATH);
46     }
47
48     @Activate
49     public void activate() {
50         File directory = new File(LIB_PATH);
51         if (!directory.exists()) {
52             if (!directory.mkdirs()) {
53                 logger.warn("Failed to create watched directory: {}", LIB_PATH);
54             }
55         } else if (directory.isFile()) {
56             logger.warn("Trying to watch directory {}, however it is a file", LIB_PATH);
57         }
58
59         super.activate();
60     }
61
62     @Deactivate
63     public void deactivate() {
64         super.deactivate();
65     }
66
67     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, unbind = "removeChangeTracker")
68     public void addChangeTracker(ScriptDependencyTracker.Listener listener) {
69         super.addChangeTracker(listener);
70     }
71
72     public void removeChangeTracker(ScriptDependencyTracker.Listener listener) {
73         super.removeChangeTracker(listener);
74     }
75 }