changeset 4512:f604183d865f

7077172: KerberosTime does not take into account system clock adjustement Reviewed-by: valeriep
author weijun
date Fri, 18 Nov 2011 16:13:39 +0800
parents 0890350cf539
children 29e65f427dc7
files src/share/classes/sun/security/krb5/internal/KerberosTime.java
diffstat 1 files changed, 18 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/security/krb5/internal/KerberosTime.java	Tue Nov 22 11:29:03 2011 -0800
+++ b/src/share/classes/sun/security/krb5/internal/KerberosTime.java	Fri Nov 18 16:13:39 2011 +0800
@@ -68,8 +68,8 @@
     private int  microSeconds; // the last three digits of the microsecond value
 
     // The time when this class is loaded. Used in setNow()
-    private static final long initMilli = System.currentTimeMillis();
-    private static final long initMicro = System.nanoTime() / 1000;
+    private static long initMilli = System.currentTimeMillis();
+    private static long initMicro = System.nanoTime() / 1000;
 
     private static long syncTime;
     private static boolean DEBUG = Krb5.DEBUG;
@@ -212,9 +212,22 @@
     }
 
     public void setNow() {
-        long microElapsed = System.nanoTime() / 1000 - initMicro;
-        setTime(initMilli + microElapsed/1000);
-        microSeconds = (int)(microElapsed % 1000);
+        long newMilli = System.currentTimeMillis();
+        long newMicro = System.nanoTime() / 1000;
+        long microElapsed = newMicro - initMicro;
+        long calcMilli = initMilli + microElapsed/1000;
+        if (calcMilli - newMilli > 100 || newMilli - calcMilli > 100) {
+            if (DEBUG) {
+                System.out.println("System time adjusted");
+            }
+            initMilli = newMilli;
+            initMicro = newMicro;
+            setTime(newMilli);
+            microSeconds = 0;
+        } else {
+            setTime(calcMilli);
+            microSeconds = (int)(microElapsed % 1000);
+        }
     }
 
     public int getMicroSeconds() {