]> git.basschouten.com Git - openhab-addons.git/blob
71b061902ac48c5eb4c3696c983634827a9ff8a5
[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.jrubyscripting.internal.watch;
14
15 import static org.openhab.core.service.WatchService.Kind.*;
16
17 import java.io.File;
18 import java.nio.file.Path;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.core.service.WatchService;
23 import org.openhab.core.service.WatchService.Kind;
24
25 /**
26  * Watches a Ruby lib dir
27  *
28  * @author Cody Cutrer - Initial contribution
29  * @author Jan N. Klug - Refactored to new WatchService
30  */
31 @NonNullByDefault
32 public class JRubyLibWatchService implements JRubyWatchService, WatchService.WatchEventListener {
33     private final JRubyDependencyTracker dependencyTracker;
34     private final WatchService watchService;
35     private final List<Path> paths;
36
37     JRubyLibWatchService(WatchService watchService, List<Path> paths, JRubyDependencyTracker dependencyTracker) {
38         this.watchService = watchService;
39         this.dependencyTracker = dependencyTracker;
40         this.paths = paths;
41     }
42
43     @Override
44     public void activate() {
45         watchService.registerListener(this, paths);
46     }
47
48     @Override
49     public void deactivate() {
50         watchService.unregisterListener(this);
51     }
52
53     @Override
54     public void processWatchEvent(Kind kind, Path path) {
55         File file = path.toFile();
56         if (!file.isHidden() && (kind == DELETE || (file.canRead() && (kind == CREATE || kind == MODIFY)))) {
57             dependencyTracker.dependencyChanged(file.getPath());
58         }
59     }
60 }