2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.javascript.internal;
15 import static java.nio.file.StandardWatchEventKinds.*;
18 import java.nio.file.Path;
19 import java.nio.file.WatchEvent;
20 import java.nio.file.WatchEvent.Kind;
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;
30 * The {@link TransformationScriptWatcher} watches the transformation directory for files. If a deleted/modified file is
31 * detected, the script is passed to the {@link JavaScriptEngineManager}.
33 * @author Thomas Kordelle - Initial contribution
34 * @author Thomas Kordelle - pre compiled scripts
37 public class TransformationScriptWatcher extends AbstractWatchService {
39 public static final String TRANSFORM_FOLDER = OpenHAB.getConfigFolder() + File.separator
40 + TransformationService.TRANSFORM_FOLDER_NAME;
42 private final JavaScriptEngineManager manager;
45 public TransformationScriptWatcher(final @Reference JavaScriptEngineManager manager) {
46 super(TRANSFORM_FOLDER);
47 this.manager = manager;
51 public void activate() {
56 protected boolean watchSubDirectories() {
61 protected Kind<?>[] getWatchEventKinds(Path directory) {
62 return new Kind<?>[] { ENTRY_DELETE, ENTRY_MODIFY };
65 @SuppressWarnings("unchecked")
67 protected void processWatchEvent(WatchEvent<?> event, Kind<?> kind, Path path) {
68 logger.debug("New watch event {} for path {}.", kind, path);
70 if (kind == OVERFLOW) {
74 final WatchEvent<Path> ev = (WatchEvent<Path>) event;
75 final Path filename = ev.context();
77 logger.debug("Reloading javascript file {}.", filename);
79 manager.removeFromCache(filename.toString());