]> git.basschouten.com Git - openhab-addons.git/blob
6fd660c6a8f142cee8f5a0333cf1ab918ba99411
[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.transform.javascript.internal;
14
15 import static java.nio.file.StandardWatchEventKinds.*;
16
17 import java.io.File;
18 import java.nio.file.Path;
19 import java.nio.file.WatchEvent;
20 import java.nio.file.WatchEvent.Kind;
21
22 import org.openhab.core.OpenHAB;
23 import org.openhab.core.service.AbstractWatchService;
24 import org.openhab.core.transform.TransformationService;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Reference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link TransformationScriptWatcher} watches the transformation directory for files. If a deleted/modified file is
33  * detected, the script is passed to the {@link JavaScriptEngineManager}.
34  *
35  * @author Thomas Kordelle - Initial contribution
36  * @author Thomas Kordelle - pre compiled scripts
37  */
38 @Component
39 public class TransformationScriptWatcher extends AbstractWatchService {
40     public static final String TRANSFORM_FOLDER = OpenHAB.getConfigFolder() + File.separator
41             + TransformationService.TRANSFORM_FOLDER_NAME;
42
43     private final Logger logger = LoggerFactory.getLogger(TransformationScriptWatcher.class);
44
45     private final JavaScriptEngineManager manager;
46
47     @Activate
48     public TransformationScriptWatcher(final @Reference JavaScriptEngineManager manager) {
49         super(TRANSFORM_FOLDER);
50         this.manager = manager;
51     }
52
53     @Override
54     public void activate() {
55         super.activate();
56     }
57
58     @Override
59     protected boolean watchSubDirectories() {
60         return true;
61     }
62
63     @Override
64     protected Kind<?>[] getWatchEventKinds(Path directory) {
65         return new Kind<?>[] { ENTRY_DELETE, ENTRY_MODIFY };
66     }
67
68     @SuppressWarnings("unchecked")
69     @Override
70     protected void processWatchEvent(WatchEvent<?> event, Kind<?> kind, Path path) {
71         logger.debug("New watch event {} for path {}.", kind, path);
72
73         if (kind == OVERFLOW) {
74             return;
75         }
76
77         final WatchEvent<Path> ev = (WatchEvent<Path>) event;
78         final Path filename = ev.context();
79
80         logger.debug("Reloading javascript file {}.", filename);
81
82         manager.removeFromCache(filename.toString());
83     }
84 }