src/jdk.dns.client/share/classes/jdk/dns/client/internal/util/ReloadTracker.java
branchaefimov-dns-client-branch
changeset 58870 35c438a6d45c
child 59101 258033faefc9
equal deleted inserted replaced
58869:cc66ac8c7646 58870:35c438a6d45c
       
     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.io.IOException;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.security.AccessController;
       
    32 import java.security.PrivilegedAction;
       
    33 import java.security.PrivilegedActionException;
       
    34 import java.security.PrivilegedExceptionAction;
       
    35 import java.util.concurrent.atomic.AtomicLong;
       
    36 
       
    37 public class ReloadTracker {
       
    38 
       
    39     public static ReloadTracker newInstance(Path fileToTrack, long refreshTimeoutMillis) {
       
    40         Path absolutePath = fileToTrack.toAbsolutePath();
       
    41         return new ReloadTracker(absolutePath, refreshTimeoutMillis);
       
    42     }
       
    43 
       
    44     public static class ReloadStatus {
       
    45         private final boolean isReloadNeeded;
       
    46         private final long lastModificationTimestamp;
       
    47         private final boolean fileExists;
       
    48 
       
    49         ReloadStatus(boolean fileExists, boolean isReloadNeeded, long lastModificationTimestamp) {
       
    50             this.isReloadNeeded = fileExists && isReloadNeeded;
       
    51             this.lastModificationTimestamp = lastModificationTimestamp;
       
    52             this.fileExists = fileExists;
       
    53         }
       
    54 
       
    55         public boolean isFileExists() {
       
    56             return fileExists;
       
    57         }
       
    58 
       
    59         public boolean isReloadNeeded() {
       
    60             return isReloadNeeded;
       
    61         }
       
    62 
       
    63         public long getLastModificationTimestamp() {
       
    64             return lastModificationTimestamp;
       
    65         }
       
    66     }
       
    67 
       
    68 
       
    69     private ReloadTracker(Path filePath, long refreshTimeoutMillis) {
       
    70         this.filePath = filePath;
       
    71         this.refreshTimeoutMillis = refreshTimeoutMillis;
       
    72     }
       
    73 
       
    74 
       
    75     public ReloadStatus getReloadStatus() {
       
    76         boolean fileExists;
       
    77         var pa = (PrivilegedAction<Boolean>) () -> filePath.toFile().isFile();
       
    78         fileExists = System.getSecurityManager() == null ? pa.run() : AccessController.doPrivileged(pa);
       
    79 
       
    80         long lastModificationTime = -1;
       
    81         long lastReload = lastRefreshed.get();
       
    82 
       
    83         // Do not update lastModification time if file doesn't exist
       
    84         if (fileExists) {
       
    85             try {
       
    86                 var pea = (PrivilegedExceptionAction<Long>) () -> Files.getLastModifiedTime(filePath).toMillis();
       
    87                 lastModificationTime = System.getSecurityManager() == null ? pea.run()
       
    88                         : AccessController.doPrivileged(pea);
       
    89             } catch (Exception e) {
       
    90                 // In case of Exception the tracked file will be reloaded, ie lastModificationTime == -1
       
    91             }
       
    92         }
       
    93         return new ReloadStatus(fileExists,
       
    94                 (lastModificationTime != lastSeenChanged.get()) ||
       
    95                         (System.currentTimeMillis() - lastReload > refreshTimeoutMillis),
       
    96                 lastModificationTime);
       
    97     }
       
    98 
       
    99     public void updateTimestamps(ReloadStatus rs) {
       
   100         lastSeenChanged.set(rs.lastModificationTimestamp);
       
   101         lastRefreshed.set(System.currentTimeMillis());
       
   102     }
       
   103 
       
   104     // Last timestamp when tracked file has been reloaded by consumer of this utility class
       
   105     private final AtomicLong lastRefreshed = new AtomicLong(-1);
       
   106     // Last processed FS last modified timestamp
       
   107     private final AtomicLong lastSeenChanged = new AtomicLong(-1);
       
   108     // Refresh timeout
       
   109     private final long refreshTimeoutMillis;
       
   110     // Path of the tracked file
       
   111     private final Path filePath;
       
   112 }