author | aefimov |
Thu, 14 Nov 2019 23:16:40 +0000 | |
branch | aefimov-dns-client-branch |
changeset 59101 | 258033faefc9 |
parent 58870 | 35c438a6d45c |
permissions | -rw-r--r-- |
58870 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.dns.client.internal.util; |
|
27 |
||
28 |
import java.nio.file.Files; |
|
29 |
import java.nio.file.Path; |
|
30 |
import java.security.AccessController; |
|
31 |
import java.security.PrivilegedAction; |
|
32 |
import java.security.PrivilegedExceptionAction; |
|
33 |
import java.util.concurrent.atomic.AtomicLong; |
|
34 |
||
35 |
public class ReloadTracker { |
|
36 |
||
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
37 |
public static ReloadTracker newInstance(Path fileToTrack, long refreshTimeoutNanos) { |
58870 | 38 |
Path absolutePath = fileToTrack.toAbsolutePath(); |
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
39 |
return new ReloadTracker(absolutePath, refreshTimeoutNanos); |
58870 | 40 |
} |
41 |
||
42 |
public static class ReloadStatus { |
|
43 |
private final boolean isReloadNeeded; |
|
44 |
private final long lastModificationTimestamp; |
|
45 |
private final boolean fileExists; |
|
46 |
||
47 |
ReloadStatus(boolean fileExists, boolean isReloadNeeded, long lastModificationTimestamp) { |
|
48 |
this.isReloadNeeded = fileExists && isReloadNeeded; |
|
49 |
this.lastModificationTimestamp = lastModificationTimestamp; |
|
50 |
this.fileExists = fileExists; |
|
51 |
} |
|
52 |
||
53 |
public boolean isFileExists() { |
|
54 |
return fileExists; |
|
55 |
} |
|
56 |
||
57 |
public boolean isReloadNeeded() { |
|
58 |
return isReloadNeeded; |
|
59 |
} |
|
60 |
||
61 |
public long getLastModificationTimestamp() { |
|
62 |
return lastModificationTimestamp; |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
||
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
67 |
private ReloadTracker(Path filePath, long refreshTimeoutNanos) { |
58870 | 68 |
this.filePath = filePath; |
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
69 |
this.refreshTimeoutNanos = refreshTimeoutNanos; |
58870 | 70 |
} |
71 |
||
72 |
||
73 |
public ReloadStatus getReloadStatus() { |
|
74 |
boolean fileExists; |
|
75 |
var pa = (PrivilegedAction<Boolean>) () -> filePath.toFile().isFile(); |
|
76 |
fileExists = System.getSecurityManager() == null ? pa.run() : AccessController.doPrivileged(pa); |
|
77 |
||
78 |
long lastModificationTime = -1; |
|
79 |
long lastReload = lastRefreshed.get(); |
|
80 |
||
81 |
// Do not update lastModification time if file doesn't exist |
|
82 |
if (fileExists) { |
|
83 |
try { |
|
84 |
var pea = (PrivilegedExceptionAction<Long>) () -> Files.getLastModifiedTime(filePath).toMillis(); |
|
85 |
lastModificationTime = System.getSecurityManager() == null ? pea.run() |
|
86 |
: AccessController.doPrivileged(pea); |
|
87 |
} catch (Exception e) { |
|
88 |
// In case of Exception the tracked file will be reloaded, ie lastModificationTime == -1 |
|
89 |
} |
|
90 |
} |
|
91 |
return new ReloadStatus(fileExists, |
|
92 |
(lastModificationTime != lastSeenChanged.get()) || |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
93 |
(System.nanoTime() - lastReload > refreshTimeoutNanos), |
58870 | 94 |
lastModificationTime); |
95 |
} |
|
96 |
||
97 |
public void updateTimestamps(ReloadStatus rs) { |
|
98 |
lastSeenChanged.set(rs.lastModificationTimestamp); |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
99 |
lastRefreshed.set(System.nanoTime()); |
58870 | 100 |
} |
101 |
||
102 |
// Last timestamp when tracked file has been reloaded by consumer of this utility class |
|
103 |
private final AtomicLong lastRefreshed = new AtomicLong(-1); |
|
104 |
// Last processed FS last modified timestamp |
|
105 |
private final AtomicLong lastSeenChanged = new AtomicLong(-1); |
|
106 |
// Refresh timeout |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
107 |
private final long refreshTimeoutNanos; |
58870 | 108 |
// Path of the tracked file |
109 |
private final Path filePath; |
|
110 |
} |