]> git.basschouten.com Git - openhab-addons.git/blob
15e0ff8ee8ac793e5b30771ac6bc34af0674b242
[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 java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
16 import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
17 import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
18
19 import java.io.File;
20 import java.nio.file.Path;
21 import java.nio.file.WatchEvent;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.service.AbstractWatchService;
26
27 /**
28  * Watches a Ruby lib dir
29  *
30  * @author Cody Cutrer - Initial contribution
31  */
32 @NonNullByDefault
33 public class JRubyLibWatchService extends AbstractWatchService {
34     private JRubyDependencyTracker dependencyTracker;
35
36     JRubyLibWatchService(String path, JRubyDependencyTracker dependencyTracker) {
37         super(path);
38         this.dependencyTracker = dependencyTracker;
39     }
40
41     @Override
42     protected boolean watchSubDirectories() {
43         return true;
44     }
45
46     @Override
47     protected WatchEvent.Kind<?> @Nullable [] getWatchEventKinds(Path path) {
48         return new WatchEvent.Kind<?>[] { ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY };
49     }
50
51     @Override
52     protected void processWatchEvent(WatchEvent<?> watchEvent, WatchEvent.Kind<?> kind, Path path) {
53         File file = path.toFile();
54         if (!file.isHidden() && (kind.equals(ENTRY_DELETE)
55                 || (file.canRead() && (kind.equals(ENTRY_CREATE) || kind.equals(ENTRY_MODIFY))))) {
56             dependencyTracker.dependencyChanged(file.getPath());
57         }
58     }
59 }