Debian Codegraph @

owner-examples/owner-examples-hotreload/src/main/java/org/aeonbits/owner/examples/HotReloadExample.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2012-2015, Luigi R. Viggiano
3 * All rights reserved.
4 *
5 * This software is distributable under the BSD license.
6 * See the terms of the BSD license in the documentation provided with this software.
7 */
8
9 package org.aeonbits.owner.examples;
10
11 import org.aeonbits.owner.Config;
12 import org.aeonbits.owner.Config.HotReload;
13 import org.aeonbits.owner.Config.Sources;
14 import org.aeonbits.owner.ConfigFactory;
15 import org.aeonbits.owner.Reloadable;
16 import org.aeonbits.owner.event.ReloadEvent;
17 import org.aeonbits.owner.event.ReloadListener;
18 import org.aeonbits.owner.util.Util;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.util.Date;
24 import java.util.Properties;
25
26 /**
27 * @author Luigi R. Viggiano
28 */
29 public class HotReloadExample {
30 private static final String CFG_FILE = "file:target/examples-generated-resources/HotReloadExample.properties";
31 private static File target;
32
33 static {
34 try {
35 target = Util.fileFromURI(CFG_FILE);
36 } catch (URISyntaxException e) {
37 e.printStackTrace();
38 }
39 }
40
41 @Sources(CFG_FILE)
42 @HotReload(1)
43 interface AutoReloadConfig extends Config, Reloadable {
44 @DefaultValue("5")
45 Integer someValue();
46 }
47
48 public static void main(String[] args) throws IOException, InterruptedException {
49 System.out.printf("\n\n HOT RELOAD EXAMPLE \n\n");
50
51 Util.save(target, new Properties() {{
52 setProperty("someValue", "10");
53 }});
54
55 AutoReloadConfig cfg = ConfigFactory.create(AutoReloadConfig.class);
56
57 cfg.addReloadListener(new ReloadListener() {
58 public void reloadPerformed(ReloadEvent event) {
59 System.out.print("\rReload intercepted at " + new Date() + " \n");
60 }
61 });
62
63 System.out.println("The program is running. ");
64
65 System.out.println("Now you can change the file located at: \n\n\t" + target.getAbsolutePath() +
66 "\n\n ...and see the changes reflected below\n\n");
67 int someValue = 0;
68 while (someValue >= 0) {
69 someValue = cfg.someValue();
70 System.out.print("\rsomeValue is: " + someValue + "\t\t\t\t");
71 Thread.sleep(500);
72 }
73 }
74 }