jdk/src/macosx/classes/java/util/prefs/MacOSXPreferencesFile.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 14342 8435a30053c1
child 22951 5fd21112b2b6
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13367
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    26
package java.util.prefs;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    28
import java.util.HashMap;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    29
import java.util.HashSet;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    30
import java.util.Iterator;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    31
import java.util.Timer;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    32
import java.util.TimerTask;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
import java.lang.ref.WeakReference;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    34
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    37
  MacOSXPreferencesFile synchronization:
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    38
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    39
  Everything is synchronized on MacOSXPreferencesFile.class. This prevents:
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    40
  * simultaneous updates to cachedFiles or changedFiles
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    41
  * simultaneous creation of two objects for the same name+user+host triplet
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    42
  * simultaneous modifications to the same file
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    43
  * modifications during syncWorld/flushWorld
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    44
  * (in MacOSXPreferences.removeNodeSpi()) modification or sync during
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    45
    multi-step node removal process
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    46
  ... among other things.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    47
*/
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    48
/*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    49
  Timers. There are two timers that control synchronization of prefs data to
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    50
  and from disk.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    51
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    52
  * Sync timer periodically calls syncWorld() to force external disk changes
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    53
      (e.g. from another VM) into the memory cache. The sync timer runs even
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    54
      if there are no outstanding local changes. The sync timer syncs all live
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    55
      MacOSXPreferencesFile objects (the cachedFiles list).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    56
    The sync timer period is controlled by the java.util.prefs.syncInterval
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    57
      property (same as FileSystemPreferences). By default there is *no*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    58
      sync timer (unlike FileSystemPreferences); it is only enabled if the
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    59
      syncInterval property is set. The minimum interval is 5 seconds.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    60
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    61
  * Flush timer calls flushWorld() to force local changes to disk.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    62
      The flush timer is scheduled to fire some time after each pref change,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    63
      unless it's already scheduled to fire before that. syncWorld and
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    64
      flushWorld will cancel any outstanding flush timer as unnecessary.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    65
      The flush timer flushes all changed files (the changedFiles list).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    66
    The time between pref write and flush timer call is controlled by the
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    67
      java.util.prefs.flushDelay property (unlike FileSystemPreferences).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    68
      The default is 60 seconds and the minimum is 5 seconds.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    69
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    70
  The flush timer's behavior is required by the Java Preferences spec
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    71
  ("changes will eventually propagate to the persistent backing store with
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
  an implementation-dependent delay"). The sync timer is not required by
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
  the spec (multiple VMs are only required to not corrupt the prefs), but
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    74
  the periodic sync is implemented by FileSystemPreferences and may be
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    75
  useful to some programs. The sync timer is disabled by default because
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    76
  it's expensive and is usually not necessary.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    77
*/
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    78
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    79
class MacOSXPreferencesFile {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    80
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    81
    static {
12559
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12553
diff changeset
    82
        java.security.AccessController.doPrivileged(
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12553
diff changeset
    83
            new java.security.PrivilegedAction<Void>() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12553
diff changeset
    84
                public Void run() {
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12553
diff changeset
    85
                    System.loadLibrary("osx");
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12553
diff changeset
    86
                    return null;
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12553
diff changeset
    87
                }
9456ceada8b1 7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents: 12553
diff changeset
    88
            });
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    89
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    90
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    91
    private class FlushTask extends TimerTask {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    92
        public void run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    93
            MacOSXPreferencesFile.flushWorld();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    94
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    95
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    96
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    97
    private class SyncTask extends TimerTask {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    98
        public void run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    99
            MacOSXPreferencesFile.syncWorld();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   100
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   101
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   102
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   103
    // Maps string -> weak reference to MacOSXPreferencesFile
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   104
    private static HashMap<String, WeakReference<MacOSXPreferencesFile>>
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   105
            cachedFiles;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   106
    // Files that may have unflushed changes
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   107
    private static HashSet<MacOSXPreferencesFile> changedFiles;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   108
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   109
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   110
    // Timer and pending sync and flush tasks (which are both scheduled
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   111
    // on the same timer)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   112
    private static Timer timer = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   113
    private static FlushTask flushTimerTask = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   114
    private static long flushDelay = -1; // in seconds (min 5, default 60)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   115
    private static long syncInterval = -1; // (min 5, default negative == off)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   116
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   117
    private String appName;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   118
    private long user;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   119
    private long host;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   120
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   121
    String name() { return appName; }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   122
    long user() { return user; }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   123
    long host() { return host; }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   124
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   125
    // private constructor - use factory method getFile() instead
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   126
    private MacOSXPreferencesFile(String newName, long newUser, long newHost)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   127
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   128
        appName = newName;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   129
        user = newUser;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   130
        host = newHost;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   131
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   132
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   133
    // Factory method
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   134
    // Always returns the same object for the given name+user+host
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   135
    static synchronized MacOSXPreferencesFile
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   136
        getFile(String newName, boolean isUser)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   137
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   138
        MacOSXPreferencesFile result = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   139
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   140
        if (cachedFiles == null)
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   141
            cachedFiles = new HashMap<>();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   142
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   143
        String hashkey =
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   144
            newName + String.valueOf(isUser);
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   145
        WeakReference<MacOSXPreferencesFile> hashvalue = cachedFiles.get(hashkey);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   146
        if (hashvalue != null) {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   147
            result = hashvalue.get();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   148
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   149
        if (result == null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   150
            // Java user node == CF current user, any host
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   151
            // Java system node == CF any user, current host
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   152
            result = new MacOSXPreferencesFile(newName,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   153
                                         isUser ? cfCurrentUser : cfAnyUser,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   154
                                         isUser ? cfAnyHost : cfCurrentHost);
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   155
            cachedFiles.put(hashkey, new WeakReference<MacOSXPreferencesFile>(result));
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   156
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   157
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   158
        // Don't schedule this file for flushing until some nodes or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   159
        // keys are added to it.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   160
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   161
        // Do set up the sync timer if requested; sync timer affects reads
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   162
        // as well as writes.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   163
        initSyncTimerIfNeeded();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   164
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   165
        return result;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   166
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   167
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   168
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   169
    // Write all prefs changes to disk and clear all cached prefs values
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   170
    // (so the next read will read from disk).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   171
    static synchronized boolean syncWorld()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   172
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   173
        boolean ok = true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   174
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   175
        if (cachedFiles != null  &&  !cachedFiles.isEmpty()) {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   176
            Iterator<WeakReference<MacOSXPreferencesFile>> iter =
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   177
                    cachedFiles.values().iterator();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   178
            while (iter.hasNext()) {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   179
                WeakReference<MacOSXPreferencesFile> ref = iter.next();
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   180
                MacOSXPreferencesFile f = ref.get();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   181
                if (f != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   182
                    if (!f.synchronize()) ok = false;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   183
                } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   184
                    iter.remove();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   185
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   186
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   187
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   188
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   189
        // Kill any pending flush
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   190
        if (flushTimerTask != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   191
            flushTimerTask.cancel();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   192
            flushTimerTask = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   193
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   194
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   195
        // Clear changed file list. The changed files were guaranteed to
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   196
        // have been in the cached file list (because there was a strong
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   197
        // reference from changedFiles.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   198
        if (changedFiles != null) changedFiles.clear();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   199
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   200
        return ok;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   201
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   202
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   203
12553
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   204
    // Sync only current user preferences
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   205
    static synchronized boolean syncUser() {
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   206
        boolean ok = true;
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   207
        if (cachedFiles != null  &&  !cachedFiles.isEmpty()) {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   208
            Iterator<WeakReference<MacOSXPreferencesFile>> iter =
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   209
                    cachedFiles.values().iterator();
12553
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   210
            while (iter.hasNext()) {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   211
                WeakReference<MacOSXPreferencesFile> ref = iter.next();
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   212
                MacOSXPreferencesFile f = ref.get();
12553
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   213
                if (f != null && f.user == cfCurrentUser) {
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   214
                    if (!f.synchronize()) {
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   215
                        ok = false;
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   216
                    }
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   217
                } else {
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   218
                    iter.remove();
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   219
                }
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   220
            }
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   221
        }
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   222
        // Remove synchronized file from changed file list. The changed files were
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   223
        // guaranteed to have been in the cached file list (because there was a strong
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   224
        // reference from changedFiles.
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   225
        if (changedFiles != null) {
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   226
            Iterator<MacOSXPreferencesFile> iterChanged = changedFiles.iterator();
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   227
            while (iterChanged.hasNext()) {
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   228
                MacOSXPreferencesFile f = iterChanged.next();
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   229
                if (f != null && f.user == cfCurrentUser)
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   230
                    iterChanged.remove();
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   231
             }
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   232
        }
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   233
        return ok;
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   234
    }
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   235
13367
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   236
    //Flush only current user preferences
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   237
    static synchronized boolean flushUser() {
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   238
        boolean ok = true;
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   239
        if (changedFiles != null  &&  !changedFiles.isEmpty()) {
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   240
            Iterator<MacOSXPreferencesFile> iterator = changedFiles.iterator();
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   241
            while(iterator.hasNext()) {
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   242
                MacOSXPreferencesFile f = iterator.next();
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   243
                if (f.user == cfCurrentUser) {
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   244
                    if (!f.synchronize())
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   245
                        ok = false;
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   246
                    else
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   247
                        iterator.remove();
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   248
                }
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   249
            }
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   250
        }
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   251
        return ok;
0cc1e08af793 7184287: (prefs) BackingStoreException when calling flush on root node[macosx]
khazra
parents: 13248
diff changeset
   252
    }
12553
cf79383bbfaf 7118100: (prefs) Inconsistency when using system and user preference on OSX Lion
khazra
parents: 12047
diff changeset
   253
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   254
    // Write all prefs changes to disk, but do not clear all cached prefs
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   255
    // values. Also kills any scheduled flush task.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   256
    // There's no CFPreferencesFlush() (<rdar://problem/3049129>), so lots of cached prefs
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   257
    // are cleared anyway.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   258
    static synchronized boolean flushWorld()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   259
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   260
        boolean ok = true;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   261
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   262
        if (changedFiles != null  &&  !changedFiles.isEmpty()) {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   263
            for (MacOSXPreferencesFile f : changedFiles) {
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   264
                if (!f.synchronize())
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   265
                    ok = false;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   266
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   267
            changedFiles.clear();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   268
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   269
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   270
        if (flushTimerTask != null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   271
            flushTimerTask.cancel();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   272
            flushTimerTask = null;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   273
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   274
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   275
        return ok;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   276
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   277
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   278
    // Mark this prefs file as changed. The changes will be flushed in
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   279
    // at most flushDelay() seconds.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   280
    // Must be called when synchronized on MacOSXPreferencesFile.class
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   281
    private void markChanged()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   282
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   283
        // Add this file to the changed file list
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   284
        if (changedFiles == null)
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   285
            changedFiles = new HashSet<>();
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   286
        changedFiles.add(this);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   287
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   288
        // Schedule a new flush and a shutdown hook, if necessary
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   289
        if (flushTimerTask == null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   290
            flushTimerTask = new FlushTask();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   291
            timer().schedule(flushTimerTask, flushDelay() * 1000);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   292
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   293
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   294
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   295
    // Return the flush delay, initializing from a property if necessary.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   296
    private static synchronized long flushDelay()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   297
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   298
        if (flushDelay == -1) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   299
            try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   300
                // flush delay >= 5, default 60
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   301
                flushDelay = Math.max(5, Integer.parseInt(System.getProperty("java.util.prefs.flushDelay", "60")));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   302
            } catch (NumberFormatException e) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   303
                flushDelay = 60;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   304
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   305
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   306
        return flushDelay;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   307
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   308
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   309
    // Initialize and run the sync timer, if the sync timer property is set
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   310
    // and the sync timer hasn't already been started.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   311
    private static synchronized void initSyncTimerIfNeeded()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   312
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   313
        // syncInterval: -1 is uninitialized, other negative is off,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   314
        // positive is seconds between syncs (min 5).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   315
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   316
        if (syncInterval == -1) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   317
            try {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   318
                syncInterval = Integer.parseInt(System.getProperty("java.util.prefs.syncInterval", "-2"));
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   319
                if (syncInterval >= 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   320
                    // minimum of 5 seconds
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   321
                    syncInterval = Math.max(5, syncInterval);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   322
                } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   323
                    syncInterval = -2; // default off
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   324
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   325
            } catch (NumberFormatException e) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   326
                syncInterval = -2; // bad property value - default off
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   327
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   328
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   329
            if (syncInterval > 0) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   330
                timer().schedule(new TimerTask() {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   331
                    @Override
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   332
                    public void run() {
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   333
                        MacOSXPreferencesFile.syncWorld();}
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   334
                    }, syncInterval * 1000, syncInterval * 1000);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   335
            } else {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   336
                // syncInterval property not set. No sync timer ever.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   337
            }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   338
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   339
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   340
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   341
    // Return the timer used for flush and sync, creating it if necessary.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   342
    private static synchronized Timer timer()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   343
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   344
        if (timer == null) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   345
            timer = new Timer(true); // daemon
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   346
            Thread flushThread = new Thread() {
12695
06bbe7432ade 7164636: (prefs) Cleanup src/macosx/classes/java/util/prefs
khazra
parents: 12559
diff changeset
   347
                @Override
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   348
                public void run() {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   349
                    flushWorld();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   350
                }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   351
            };
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   352
            /* Set context class loader to null in order to avoid
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   353
             * keeping a strong reference to an application classloader.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   354
             */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   355
            flushThread.setContextClassLoader(null);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   356
            Runtime.getRuntime().addShutdownHook(flushThread);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   357
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   358
        return timer;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   359
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   360
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   361
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   362
    // Node manipulation
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   363
    boolean addNode(String path)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   364
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   365
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   366
            markChanged();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   367
            return addNode(path, appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   368
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   369
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   370
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   371
    void removeNode(String path)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   372
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   373
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   374
            markChanged();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   375
            removeNode(path, appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   376
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   377
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   378
13248
8f0767748f15 7160252: (prefs) NodeAddedEvent was not delivered when new node add when new Node
khazra
parents: 12695
diff changeset
   379
    boolean addChildToNode(String path, String child)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   380
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   381
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   382
            markChanged();
13248
8f0767748f15 7160252: (prefs) NodeAddedEvent was not delivered when new node add when new Node
khazra
parents: 12695
diff changeset
   383
            return addChildToNode(path, child+"/", appName, user, host);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   384
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   385
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   386
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   387
    void removeChildFromNode(String path, String child)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   388
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   389
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   390
            markChanged();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   391
            removeChildFromNode(path, child+"/", appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   392
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   393
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   394
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   395
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   396
    // Key manipulation
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   397
    void addKeyToNode(String path, String key, String value)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   398
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   399
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   400
            markChanged();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   401
            addKeyToNode(path, key, value, appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   402
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   403
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   404
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   405
    void removeKeyFromNode(String path, String key)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   406
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   407
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   408
            markChanged();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   409
            removeKeyFromNode(path, key, appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   410
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   411
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   412
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   413
    String getKeyFromNode(String path, String key)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   414
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   415
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   416
            return getKeyFromNode(path, key, appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   417
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   418
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   419
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   420
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   421
    // Enumerators
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   422
    String[] getChildrenForNode(String path)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   423
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   424
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   425
            return getChildrenForNode(path, appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   426
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   427
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   428
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   429
    String[] getKeysForNode(String path)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   430
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   431
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   432
            return getKeysForNode(path, appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   433
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   434
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   435
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   436
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   437
    // Synchronization
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   438
    boolean synchronize()
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   439
    {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   440
        synchronized(MacOSXPreferencesFile.class) {
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   441
            return synchronize(appName, user, host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   442
        }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   443
    }
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   444
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   445
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   446
    // CF functions
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   447
    // Must be called when synchronized on MacOSXPreferencesFile.class
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   448
    private static final native boolean
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   449
        addNode(String path, String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   450
    private static final native void
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   451
        removeNode(String path, String name, long user, long host);
13248
8f0767748f15 7160252: (prefs) NodeAddedEvent was not delivered when new node add when new Node
khazra
parents: 12695
diff changeset
   452
    private static final native boolean
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   453
        addChildToNode(String path, String child,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   454
                       String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   455
    private static final native void
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   456
        removeChildFromNode(String path, String child,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   457
                            String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   458
    private static final native void
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   459
        addKeyToNode(String path, String key, String value,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   460
                     String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   461
    private static final native void
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   462
        removeKeyFromNode(String path, String key,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   463
                          String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   464
    private static final native String
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   465
        getKeyFromNode(String path, String key,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   466
                       String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   467
    private static final native String[]
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   468
        getChildrenForNode(String path, String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   469
    private static final native String[]
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   470
        getKeysForNode(String path, String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   471
    private static final native boolean
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   472
        synchronize(String name, long user, long host);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   473
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   474
    // CFPreferences host and user values (CFStringRefs)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   475
    private static long cfCurrentUser = currentUser();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   476
    private static long cfAnyUser = anyUser();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   477
    private static long cfCurrentHost = currentHost();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   478
    private static long cfAnyHost = anyHost();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   479
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   480
    // CFPreferences constant accessors
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   481
    private static final native long currentUser();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   482
    private static final native long anyUser();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   483
    private static final native long currentHost();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   484
    private static final native long anyHost();
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   485
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
   486