23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package jdk.dns.client.internal.util; |
26 package jdk.dns.client.internal.util; |
27 |
27 |
28 import java.io.IOException; |
|
29 import java.nio.file.Files; |
28 import java.nio.file.Files; |
30 import java.nio.file.Path; |
29 import java.nio.file.Path; |
31 import java.security.AccessController; |
30 import java.security.AccessController; |
32 import java.security.PrivilegedAction; |
31 import java.security.PrivilegedAction; |
33 import java.security.PrivilegedActionException; |
|
34 import java.security.PrivilegedExceptionAction; |
32 import java.security.PrivilegedExceptionAction; |
35 import java.util.concurrent.atomic.AtomicLong; |
33 import java.util.concurrent.atomic.AtomicLong; |
36 |
34 |
37 public class ReloadTracker { |
35 public class ReloadTracker { |
38 |
36 |
39 public static ReloadTracker newInstance(Path fileToTrack, long refreshTimeoutMillis) { |
37 public static ReloadTracker newInstance(Path fileToTrack, long refreshTimeoutNanos) { |
40 Path absolutePath = fileToTrack.toAbsolutePath(); |
38 Path absolutePath = fileToTrack.toAbsolutePath(); |
41 return new ReloadTracker(absolutePath, refreshTimeoutMillis); |
39 return new ReloadTracker(absolutePath, refreshTimeoutNanos); |
42 } |
40 } |
43 |
41 |
44 public static class ReloadStatus { |
42 public static class ReloadStatus { |
45 private final boolean isReloadNeeded; |
43 private final boolean isReloadNeeded; |
46 private final long lastModificationTimestamp; |
44 private final long lastModificationTimestamp; |
64 return lastModificationTimestamp; |
62 return lastModificationTimestamp; |
65 } |
63 } |
66 } |
64 } |
67 |
65 |
68 |
66 |
69 private ReloadTracker(Path filePath, long refreshTimeoutMillis) { |
67 private ReloadTracker(Path filePath, long refreshTimeoutNanos) { |
70 this.filePath = filePath; |
68 this.filePath = filePath; |
71 this.refreshTimeoutMillis = refreshTimeoutMillis; |
69 this.refreshTimeoutNanos = refreshTimeoutNanos; |
72 } |
70 } |
73 |
71 |
74 |
72 |
75 public ReloadStatus getReloadStatus() { |
73 public ReloadStatus getReloadStatus() { |
76 boolean fileExists; |
74 boolean fileExists; |
90 // In case of Exception the tracked file will be reloaded, ie lastModificationTime == -1 |
88 // In case of Exception the tracked file will be reloaded, ie lastModificationTime == -1 |
91 } |
89 } |
92 } |
90 } |
93 return new ReloadStatus(fileExists, |
91 return new ReloadStatus(fileExists, |
94 (lastModificationTime != lastSeenChanged.get()) || |
92 (lastModificationTime != lastSeenChanged.get()) || |
95 (System.currentTimeMillis() - lastReload > refreshTimeoutMillis), |
93 (System.nanoTime() - lastReload > refreshTimeoutNanos), |
96 lastModificationTime); |
94 lastModificationTime); |
97 } |
95 } |
98 |
96 |
99 public void updateTimestamps(ReloadStatus rs) { |
97 public void updateTimestamps(ReloadStatus rs) { |
100 lastSeenChanged.set(rs.lastModificationTimestamp); |
98 lastSeenChanged.set(rs.lastModificationTimestamp); |
101 lastRefreshed.set(System.currentTimeMillis()); |
99 lastRefreshed.set(System.nanoTime()); |
102 } |
100 } |
103 |
101 |
104 // Last timestamp when tracked file has been reloaded by consumer of this utility class |
102 // Last timestamp when tracked file has been reloaded by consumer of this utility class |
105 private final AtomicLong lastRefreshed = new AtomicLong(-1); |
103 private final AtomicLong lastRefreshed = new AtomicLong(-1); |
106 // Last processed FS last modified timestamp |
104 // Last processed FS last modified timestamp |
107 private final AtomicLong lastSeenChanged = new AtomicLong(-1); |
105 private final AtomicLong lastSeenChanged = new AtomicLong(-1); |
108 // Refresh timeout |
106 // Refresh timeout |
109 private final long refreshTimeoutMillis; |
107 private final long refreshTimeoutNanos; |
110 // Path of the tracked file |
108 // Path of the tracked file |
111 private final Path filePath; |
109 private final Path filePath; |
112 } |
110 } |