2 * Copyright (c) 2018 Orange and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.fd.honeycomb.transportpce.device.test;
18 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
19 import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
20 import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
23 import java.io.IOException;
24 import java.nio.file.FileSystems;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.nio.file.WatchEvent;
28 import java.nio.file.WatchKey;
29 import java.nio.file.WatchService;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
37 public class DirectoryWatchTest {
38 private static final Logger LOG = LoggerFactory.getLogger(DirectoryWatchTest.class);
39 private static final String DATA_JSON = "/var/lib/honeycomb/persist/config/data.json";
41 public static void main(String[] args) {
42 File data = new File(DATA_JSON);
43 String parent = data.getParent();
44 LOG.info("data.json directory : {}",parent);
46 WatchService watcher = FileSystems.getDefault().newWatchService();
47 Path dir = Paths.get(data.getParent());
48 dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
49 LOG.info("Watch Service registered for dir: {}", dir.getFileName());
54 } catch (InterruptedException ex) {
57 for (WatchEvent<?> event : key.pollEvents()) {
58 WatchEvent.Kind<?> kind = event.kind();
59 @SuppressWarnings("unchecked")
60 WatchEvent<Path> ev = (WatchEvent<Path>) event;
61 Path fileName = ev.context();
62 if (kind == ENTRY_MODIFY && fileName.toString().equals("data.json")) {
63 LOG.info("My source file has changed ...");
65 * merge honeycomb-config datastore to device oper
69 boolean valid = key.reset();
71 LOG.error("Key has been unregisterede");
75 } catch (IOException ex) {
76 LOG.error("WatchService Error",ex);