cd797a9bef0d21f939f4238f71dea2ac3e8be7ae
[bgpcep.git] / config-loader / config-loader-impl / src / main / java / org / opendaylight / bgpcep / config / loader / impl / SimpleConfigLoader.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.bgpcep.config.loader.impl;
9
10 import com.google.common.annotations.Beta;
11 import java.io.File;
12 import java.nio.file.WatchKey;
13 import java.nio.file.WatchService;
14 import javax.annotation.PostConstruct;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
20
21 @Beta
22 @Singleton
23 public final class SimpleConfigLoader extends AbstractWatchingConfigLoader implements AutoCloseable {
24     private final @NonNull WatchService watchService;
25     private final @NonNull File directory;
26
27     @Inject
28     public SimpleConfigLoader(final FileWatcher fileWatcher, final BindingRuntimeContext runtimeContext) {
29         updateModelContext(runtimeContext.getEffectiveModelContext());
30         this.watchService = fileWatcher.getWatchService();
31         this.directory = new File(fileWatcher.getPathFile());
32     }
33
34     @PostConstruct
35     public void init() {
36         start();
37     }
38
39     @Override
40     @PreDestroy
41     public void close() {
42         stop();
43     }
44
45     @Override
46     File directory() {
47         return directory;
48     }
49
50     @Override
51     WatchKey takeEvent() throws InterruptedException {
52         return watchService.take();
53     }
54 }