src/java.prefs/unix/classes/java/util/prefs/FileSystemPreferences.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
35311
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 30043
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4230
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.prefs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.PrivilegedActionException;
4230
e208dda74f1c 6899607: Update java.util.prefs.FileSystemPreferences to use PlatformLogger
mchung
parents: 715
diff changeset
    33
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Preferences implementation for Unix.  Preferences are stored in the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * system, with one directory per preferences node.  All of the preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * at each node are stored in a single file.  Atomic file system operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (e.g. File.renameTo) are used to ensure integrity.  An in-memory cache of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the "explored" portion of the tree is maintained for performance, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * written back to the disk periodically.  File-locking is used to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * reasonable behavior when multiple VMs are running at the same time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * (The file lock is obtained only for sync(), flush() and removeNode().)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see     Preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class FileSystemPreferences extends AbstractPreferences {
22951
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    50
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    51
    static {
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    52
        PrivilegedAction<Void> load = () -> {
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    53
            System.loadLibrary("prefs");
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    54
            return null;
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    55
        };
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    56
        AccessController.doPrivileged(load);
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    57
    }
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Sync interval in seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int SYNC_INTERVAL = Math.max(1,
23582
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 22951
diff changeset
    63
        AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 22951
diff changeset
    64
             Integer.getInteger("java.util.prefs.syncInterval", 30)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Returns logger for error messages. Backing store exceptions are logged at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * WARNING level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
4230
e208dda74f1c 6899607: Update java.util.prefs.FileSystemPreferences to use PlatformLogger
mchung
parents: 715
diff changeset
    70
    private static PlatformLogger getLogger() {
e208dda74f1c 6899607: Update java.util.prefs.FileSystemPreferences to use PlatformLogger
mchung
parents: 715
diff changeset
    71
        return PlatformLogger.getLogger("java.util.prefs");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Directory for system preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static File systemRootDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Flag, indicating whether systemRoot  directory is writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static boolean isSystemRootWritable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Directory for user preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static File userRootDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Flag, indicating whether userRoot  directory is writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static boolean isUserRootWritable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * The user root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
38887
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
    97
    private static volatile Preferences userRoot;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
38887
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
    99
    static Preferences getUserRoot() {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   100
        Preferences root = userRoot;
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   101
        if (root == null) {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   102
            synchronized (FileSystemPreferences.class) {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   103
                root = userRoot;
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   104
                if (root == null) {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   105
                    setupUserRoot();
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   106
                    userRoot = root = new FileSystemPreferences(true);
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   107
                }
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   108
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
38887
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   110
        return root;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static void setupUserRoot() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   114
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   115
            public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                userRootDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                      new File(System.getProperty("java.util.prefs.userRoot",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                      System.getProperty("user.home")), ".java/.userPrefs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                // Attempt to create root dir if it does not yet exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (!userRootDir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    if (userRootDir.mkdirs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            chmod(userRootDir.getCanonicalPath(), USER_RWX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                            getLogger().warning("Could not change permissions" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                " on userRoot directory. ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        getLogger().info("Created user preferences directory.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        getLogger().warning("Couldn't create user preferences" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        " directory. User preferences are unusable.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                isUserRootWritable = userRootDir.canWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                String USER_NAME = System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                userLockFile = new File (userRootDir,".user.lock." + USER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                userRootModFile = new File (userRootDir,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                                               ".userRootModFile." + USER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                if (!userRootModFile.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    // create if does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    userRootModFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    // Only user can read/write userRootModFile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    int result = chmod(userRootModFile.getCanonicalPath(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                                               USER_READ_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    if (result !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        getLogger().warning("Problem creating userRoot " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            "mod file. Chmod failed on " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                             userRootModFile.getCanonicalPath() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                             " Unix error code " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    getLogger().warning(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                userRootModTime = userRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * The system root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
38887
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   164
    private static volatile Preferences systemRoot;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
38887
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   166
    static Preferences getSystemRoot() {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   167
        Preferences root = systemRoot;
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   168
        if (root == null) {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   169
            synchronized (FileSystemPreferences.class) {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   170
                root = systemRoot;
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   171
                if (root == null) {
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   172
                    setupSystemRoot();
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   173
                    systemRoot = root = new FileSystemPreferences(false);
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   174
                }
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   175
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
38887
bb8ffdf4e7aa 8139507: WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs
jlahoda
parents: 35311
diff changeset
   177
        return root;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static void setupSystemRoot() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   181
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   182
            public Void run() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   183
                String systemPrefsDirName =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                  System.getProperty("java.util.prefs.systemRoot","/etc/.java");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                systemRootDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                     new File(systemPrefsDirName, ".systemPrefs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                // Attempt to create root dir if it does not yet exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (!systemRootDir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    // system root does not exist in /etc/.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    // Switching  to java.home
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    systemRootDir =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                  new File(System.getProperty("java.home"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                                            ".systemPrefs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    if (!systemRootDir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        if (systemRootDir.mkdirs()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                            getLogger().info(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                "Created system preferences directory "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                + "in java.home.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                chmod(systemRootDir.getCanonicalPath(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                                          USER_RWX_ALL_RX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            getLogger().warning("Could not create "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                + "system preferences directory. System "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                + "preferences are unusable.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                isSystemRootWritable = systemRootDir.canWrite();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                systemLockFile = new File(systemRootDir, ".system.lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                systemRootModFile =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                               new File (systemRootDir,".systemRootModFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if (!systemRootModFile.exists() && isSystemRootWritable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    // create if does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    systemRootModFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    int result = chmod(systemRootModFile.getCanonicalPath(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                                          USER_RW_ALL_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    if (result !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        getLogger().warning("Chmod failed on " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                               systemRootModFile.getCanonicalPath() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                              " Unix error code " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                } catch (IOException e) { getLogger().warning(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                systemRootModTime = systemRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Unix user write/read permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private static final int USER_READ_WRITE = 0600;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static final int USER_RW_ALL_READ = 0644;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private static final int USER_RWX_ALL_RX = 0755;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private static final int USER_RWX = 0700;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * The lock file for the user tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    static File userLockFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * The lock file for the system tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    static File systemLockFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Unix lock handle for userRoot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Zero, if unlocked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private static int userRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Unix lock handle for systemRoot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Zero, if unlocked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private static int systemRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * The directory representing this preference node.  There is no guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * that this directory exits, as another VM can delete it at any time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * that it (the other VM) holds the file-lock.  While the root node cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * be deleted, it may not yet have been created, or the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * directory could have been deleted accidentally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private final File dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The file representing this preference node's preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * The file format is undocumented, and subject to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * from release to release, but I'm sure that you can figure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * it out if you try real hard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private final File prefsFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * A temporary file used for saving changes to preferences.  As part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * the sync operation, changes are first saved into this file, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * atomically renamed to prefsFile.  This results in an atomic state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * change from one valid set of preferences to another.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * the file-lock is held for the duration of this transformation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    private final File tmpFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * File, which keeps track of global modifications of userRoot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private static  File userRootModFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Flag, which indicated whether userRoot was modified by another VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private static boolean isUserRootModified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Keeps track of userRoot modification time. This time is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * zero after UNIX reboot, and is increased by 1 second each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * userRoot is modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private static long userRootModTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * File, which keeps track of global modifications of systemRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static File systemRootModFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Flag, which indicates whether systemRoot was modified by another VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private static boolean isSystemRootModified = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Keeps track of systemRoot modification time. This time is reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * zero after system reboot, and is increased by 1 second each time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * systemRoot is modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private static long systemRootModTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Locally cached preferences for this node (includes uncommitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * changes).  This map is initialized with from disk when the first get or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * put operation occurs on this node.  It is synchronized with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * corresponding disk file (prefsFile) by the sync operation.  The initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * value is read *without* acquiring the file-lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   339
    private Map<String, String> prefsCache = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * The last modification time of the file backing this node at the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * that prefCache was last synchronized (or initially read).  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * value is set *before* reading the file, so it's conservative; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * actual timestamp could be (slightly) higher.  A value of zero indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * that we were unable to initialize prefsCache from the disk, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * have not yet attempted to do so.  (If prefsCache is non-null, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * indicates the former; if it's null, the latter.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private long lastSyncTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    * Unix error code for locked file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private static final int EAGAIN = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    * Unix error code for denied access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private static final int EACCES = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /* Used to interpret results of native functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static final int LOCK_HANDLE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private static final int ERROR_CODE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * A list of all uncommitted preference changes.  The elements in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * list are of type PrefChange.  If this node is concurrently modified on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * disk by another VM, the two sets of changes are merged when this node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * is sync'ed by overwriting our prefsCache with the preference map last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * written out to disk (by the other VM), and then replaying this change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * log against that map.  The resulting map is then written back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * to the disk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   375
    final List<Change> changeLog = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Represents a change to a preference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private abstract class Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * Reapplies the change to prefsCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        abstract void replay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Represents a preference put.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private class Put extends Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        String key, value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        Put(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        void replay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            prefsCache.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Represents a preference remove.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private class Remove extends Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        Remove(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            this.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        void replay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            prefsCache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Represents the creation of this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private class NodeCreate extends Change {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * Performs no action, but the presence of this object in changeLog
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         * will force the node and its ancestors to be made permanent at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * next sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        void replay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * NodeCreate object for this node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    NodeCreate nodeCreate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Replay changeLog against prefsCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    private void replayChanges() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        for (int i = 0, n = changeLog.size(); i<n; i++)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   441
            changeLog.get(i).replay();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private static Timer syncTimer = new Timer(true); // Daemon Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        // Add periodic timer task to periodically sync cached prefs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        syncTimer.schedule(new TimerTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                syncWorld();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        }, SYNC_INTERVAL*1000, SYNC_INTERVAL*1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        // Add shutdown hook to flush cached prefs on normal termination
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   455
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   456
            public Void run() {
35311
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 30043
diff changeset
   457
                Runtime.getRuntime().addShutdownHook(
613162418a3d 8147545: Remove sun.misc.ManagedLocalsThread from java.prefs
bpb
parents: 30043
diff changeset
   458
                    new Thread(null, null, "Sync Timer Thread", 0, false) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        syncTimer.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        syncWorld();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    private static void syncWorld() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
         * Synchronization necessary because userRoot and systemRoot are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
         * lazily initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        Preferences userRt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        Preferences systemRt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        synchronized(FileSystemPreferences.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            userRt   = userRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            systemRt = systemRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (userRt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                userRt.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } catch(BackingStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            getLogger().warning("Couldn't flush user prefs: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (systemRt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                systemRt.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        } catch(BackingStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            getLogger().warning("Couldn't flush system prefs: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private final boolean isUserNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Special constructor for roots (both user and system).  This constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * will only be called twice, by the static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private FileSystemPreferences(boolean user) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        super(null, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        isUserNode = user;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        dir = (user ? userRootDir: systemRootDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        prefsFile = new File(dir, "prefs.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        tmpFile   = new File(dir, "prefs.tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Construct a new FileSystemPreferences instance with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * parent node and name.  This constructor, called from childSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * is used to make every node except for the two //roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private FileSystemPreferences(FileSystemPreferences parent, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        super(parent, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        isUserNode = parent.isUserNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        dir  = new File(parent.dir, dirName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        prefsFile = new File(dir, "prefs.xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        tmpFile  = new File(dir, "prefs.tmp");
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   521
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   522
            public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                newNode = !dir.exists();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (newNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            // These 2 things guarantee node will get wrtten at next flush/sync
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   529
            prefsCache = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            nodeCreate = new NodeCreate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            changeLog.add(nodeCreate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public boolean isUserNode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return isUserNode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    protected void putSpi(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        initCacheIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        changeLog.add(new Put(key, value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        prefsCache.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    protected String getSpi(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        initCacheIfNecessary();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   547
        return prefsCache.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    protected void removeSpi(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        initCacheIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        changeLog.add(new Remove(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        prefsCache.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Initialize prefsCache if it has yet to be initialized.  When this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * returns, prefsCache will be non-null.  If the data was successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * read from the file, lastSyncTime will be updated.  If prefsCache was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * null, but it was impossible to read the file (because it didn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * exist or for any other reason) prefsCache will be initialized to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * empty, modifiable Map, and lastSyncTime remain zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    private void initCacheIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (prefsCache != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            loadCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            // assert lastSyncTime == 0;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   572
            prefsCache = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Attempt to load prefsCache from the backing store.  If the attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * succeeds, lastSyncTime will be updated (the new value will typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * correspond to the data loaded into the map, but it may be less,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * if another VM is updating this node concurrently).  If the attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * fails, a BackingStoreException is thrown and both prefsCache and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * lastSyncTime are unaffected by the call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private void loadCache() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   586
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   587
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   588
                public Void run() throws BackingStoreException {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   589
                    Map<String, String> m = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    long newLastSyncTime = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        newLastSyncTime = prefsFile.lastModified();
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   593
                        try (FileInputStream fis = new FileInputStream(prefsFile)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   594
                            XmlSupport.importMap(fis, m);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   595
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        if (e instanceof InvalidPreferencesFormatException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                            getLogger().warning("Invalid preferences format in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                                        +  prefsFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                            prefsFile.renameTo( new File(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                                    prefsFile.getParentFile(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                                                  "IncorrectFormatPrefs.xml"));
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   603
                            m = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        } else if (e instanceof FileNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        getLogger().warning("Prefs file removed in background "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                                           + prefsFile.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                            throw new BackingStoreException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    // Attempt succeeded; update state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    prefsCache = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    lastSyncTime = newLastSyncTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Attempt to write back prefsCache to the backing store.  If the attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * succeeds, lastSyncTime will be updated (the new value will correspond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * exactly to the data thust written back, as we hold the file lock, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * prevents a concurrent write.  If the attempt fails, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * BackingStoreException is thrown and both the backing store (prefsFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * and lastSyncTime will be unaffected by this call.  This call will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * NEVER leave prefsFile in a corrupt state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    private void writeBackCache() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   633
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   634
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   635
                public Void run() throws BackingStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        if (!dir.exists() && !dir.mkdirs())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                            throw new BackingStoreException(dir +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                                             " create failed.");
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   640
                        try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   641
                            XmlSupport.exportMap(fos, prefsCache);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   642
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        if (!tmpFile.renameTo(prefsFile))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                            throw new BackingStoreException("Can't rename " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                            tmpFile + " to " + prefsFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        if (e instanceof BackingStoreException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            throw (BackingStoreException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        throw new BackingStoreException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    protected String[] keysSpi() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        initCacheIfNecessary();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   661
        return prefsCache.keySet().toArray(new String[prefsCache.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    protected String[] childrenNamesSpi() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   665
        return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   666
            new PrivilegedAction<String[]>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   667
                public String[] run() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   668
                    List<String> result = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    File[] dirContents = dir.listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    if (dirContents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        for (int i = 0; i < dirContents.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                            if (dirContents[i].isDirectory())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                result.add(nodeName(dirContents[i].getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    return result.toArray(EMPTY_STRING_ARRAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    private static final String[] EMPTY_STRING_ARRAY = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    protected AbstractPreferences childSpi(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return new FileSystemPreferences(this, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public void removeNode() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        synchronized (isUserNode()? userLockFile: systemLockFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            // to remove a node we need an exclusive lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            if (!lockFile(false))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                throw(new BackingStoreException("Couldn't get file lock."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                super.removeNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
           } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                unlockFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * Called with file lock held (in addition to node locks).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    protected void removeNodeSpi() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   704
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   705
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   706
                public Void run() throws BackingStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    if (changeLog.contains(nodeCreate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        changeLog.remove(nodeCreate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        nodeCreate = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    if (!dir.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    prefsFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    tmpFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    // dir should be empty now.  If it's not, empty it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    File[] junk = dir.listFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    if (junk.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        getLogger().warning(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                           "Found extraneous files when removing node: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                            + Arrays.asList(junk));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        for (int i=0; i<junk.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                            junk[i].delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    if (!dir.delete())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        throw new BackingStoreException("Couldn't delete dir: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                                                                         + dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public synchronized void sync() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        boolean userNode = isUserNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        boolean shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (userNode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            shared = false; /* use exclusive lock for user prefs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            /* if can write to system root, use exclusive lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
               otherwise use shared lock. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            shared = !isSystemRootWritable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        synchronized (isUserNode()? userLockFile:systemLockFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
           if (!lockFile(shared))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
               throw(new BackingStoreException("Couldn't get file lock."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
           final Long newModTime =
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   751
                AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   752
                    new PrivilegedAction<Long>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   753
               public Long run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                   long nmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                   if (isUserNode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                       nmt = userRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                       isUserRootModified = userRootModTime == nmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                       nmt = systemRootModFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                       isSystemRootModified = systemRootModTime == nmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                   }
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 23582
diff changeset
   762
                   return nmt;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
           });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
               super.sync();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   767
               AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   768
                   public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                   if (isUserNode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                       userRootModTime = newModTime.longValue() + 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                       userRootModFile.setLastModified(userRootModTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                   } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                       systemRootModTime = newModTime.longValue() + 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                       systemRootModFile.setLastModified(systemRootModTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                   return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
               });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
           } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                unlockFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    protected void syncSpi() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   787
            AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   788
                new PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   789
                public Void run() throws BackingStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    syncSpiPrivileged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            throw (BackingStoreException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    private void syncSpiPrivileged() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (isRemoved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            throw new IllegalStateException("Node has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        if (prefsCache == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            return;  // We've never been used, don't bother syncing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        long lastModifiedTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if ((isUserNode() ? isUserRootModified : isSystemRootModified)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            lastModifiedTime = prefsFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (lastModifiedTime  != lastSyncTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                // Prefs at this node were externally modified; read in node and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                // playback any local mods since last sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                loadCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                replayChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                lastSyncTime = lastModifiedTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        } else if (lastSyncTime != 0 && !dir.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            // This node was removed in the background.  Playback any changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            // against a virgin (empty) Map.
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   816
            prefsCache = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            replayChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        if (!changeLog.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            writeBackCache();  // Creates directory & file if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
           /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            * Attempt succeeded; it's barely possible that the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            * lastModified might fail (i.e., return 0), but this would not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            * be a disaster, as lastSyncTime is allowed to lag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            lastModifiedTime = prefsFile.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            /* If lastSyncTime did not change, or went back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
             * increment by 1 second. Since we hold the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
             * lastSyncTime always monotonically encreases in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
             * atomic sense.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            if (lastSyncTime <= lastModifiedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                lastSyncTime = lastModifiedTime + 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                prefsFile.setLastModified(lastSyncTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            changeLog.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public void flush() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        if (isRemoved())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    protected void flushSpi() throws BackingStoreException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Returns true if the specified character is appropriate for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Unix directory names.  A character is appropriate if it's a printable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * ASCII character (> 0x1f && < 0x7f) and unequal to slash ('/', 0x2f),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * dot ('.', 0x2e), or underscore ('_', 0x5f).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    private static boolean isDirChar(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        return ch > 0x1f && ch < 0x7f && ch != '/' && ch != '.' && ch != '_';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * Returns the directory name corresponding to the specified node name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * Generally, this is just the node name.  If the node name includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * inappropriate characters (as per isDirChar) it is translated to Base64.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * with the underscore  character ('_', 0x5f) prepended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    private static String dirName(String nodeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        for (int i=0, n=nodeName.length(); i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            if (!isDirChar(nodeName.charAt(i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                return "_" + Base64.byteArrayToAltBase64(byteArray(nodeName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        return nodeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Translate a string into a byte array by translating each character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * into two bytes, high-byte first ("big-endian").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    private static byte[] byteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        byte[] result = new byte[2*len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        for (int i=0, j=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            result[j++] = (byte) (c>>8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            result[j++] = (byte) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * Returns the node name corresponding to the specified directory name.
22951
5fd21112b2b6 8034043: Native methods for preferences API should not be in libjava
alanb
parents: 9035
diff changeset
   890
     * (Inverts the transformation of dirName(String).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    private static String nodeName(String dirName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        if (dirName.charAt(0) != '_')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            return dirName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        byte a[] = Base64.altBase64ToByteArray(dirName.substring(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        StringBuffer result = new StringBuffer(a.length/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        for (int i = 0; i < a.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            int highByte = a[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            int lowByte =  a[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            result.append((char) ((highByte << 8) | lowByte));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Try to acquire the appropriate file lock (user or system).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * the initial attempt fails, several more attempts are made using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * an exponential backoff strategy.  If all attempts fail, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @throws SecurityException if file access denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    private boolean lockFile(boolean shared) throws SecurityException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        boolean usernode = isUserNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        int[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        int errorCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        File lockFile = (usernode ? userLockFile : systemLockFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        long sleepTime = INIT_SLEEP_TIME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        for (int i = 0; i < MAX_ATTEMPTS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                  int perm = (usernode? USER_READ_WRITE: USER_RW_ALL_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                  result = lockFile0(lockFile.getCanonicalPath(), perm, shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                  errorCode = result[ERROR_CODE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                  if (result[LOCK_HANDLE] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                     if (usernode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                         userRootLockHandle = result[LOCK_HANDLE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                     } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                         systemRootLockHandle = result[LOCK_HANDLE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                     return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            } catch(IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
//                // If at first, you don't succeed...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                Thread.sleep(sleepTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            } catch(InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                checkLockFile0ErrorCode(errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            sleepTime *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        checkLockFile0ErrorCode(errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * Checks if unlockFile0() returned an error. Throws a SecurityException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * if access denied. Logs a warning otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    private void checkLockFile0ErrorCode (int errorCode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                                                      throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        if (errorCode == EACCES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            throw new SecurityException("Could not lock " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            (isUserNode()? "User prefs." : "System prefs.") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
             " Lock file access denied.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        if (errorCode != EAGAIN)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            getLogger().warning("Could not lock " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                             (isUserNode()? "User prefs. " : "System prefs.") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                             " Unix error code " + errorCode + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Locks file using UNIX file locking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @param fileName Absolute file name of the lock file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @return Returns a lock handle, used to unlock the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    private static native int[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            lockFile0(String fileName, int permission, boolean shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * Unlocks file previously locked by lockFile0().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @param lockHandle Handle to the file lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @return Returns zero if OK, UNIX error code if failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    private  static native int unlockFile0(int lockHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * Changes UNIX file permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    private static native int chmod(String fileName, int permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * Initial time between lock attempts, in ms.  The time is doubled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * after each failing attempt (except the first).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    private static int INIT_SLEEP_TIME = 50;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Maximum number of lock attempts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    private static int MAX_ATTEMPTS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   996
     * Release the appropriate file lock (user or system).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @throws SecurityException if file access denied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    private void unlockFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        int result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        boolean usernode = isUserNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        File lockFile = (usernode ? userLockFile : systemLockFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        int lockHandle = ( usernode ? userRootLockHandle:systemRootLockHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        if (lockHandle == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            getLogger().warning("Unlock: zero lockHandle for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                           (usernode ? "user":"system") + " preferences.)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        result = unlockFile0(lockHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            getLogger().warning("Could not drop file-lock on " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            (isUserNode() ? "user" : "system") + " preferences." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            " Unix error code " + result + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (result == EACCES)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                throw new SecurityException("Could not unlock" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                (isUserNode()? "User prefs." : "System prefs.") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                " Lock file access denied.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (isUserNode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            userRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            systemRootLockHandle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
}