1 /*
2 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
32 import java.security.PrivilegedActionException;
33
34 import sun.util.logging.PlatformLogger;
35
36 /**
37 * Preferences implementation for Unix. Preferences are stored in the file
38 * system, with one directory per preferences node. All of the preferences
39 * at each node are stored in a single file. Atomic file system operations
40 * (e.g. File.renameTo) are used to ensure integrity. An in-memory cache of
41 * the "explored" portion of the tree is maintained for performance, and
42 * written back to the disk periodically. File-locking is used to ensure
43 * reasonable behavior when multiple VMs are running at the same time.
44 * (The file lock is obtained only for sync(), flush() and removeNode().)
45 *
46 * @author Josh Bloch
47 * @see Preferences
48 * @since 1.4
49 */
50 class FileSystemPreferences extends AbstractPreferences {
51
52 /**
53 * The code point U+0000, assigned to the null control character, is the
54 * only character encoded in Unicode and ISO/IEC 10646 that is always
55 * invalid in any XML 1.0 and 1.1 document.
56 */
57 private static final String CODE_POINT_U0000 = String.valueOf('\u0000');
58
59 static {
60 PrivilegedAction<Void> load = () -> {
61 System.loadLibrary("prefs");
62 return null;
63 };
64 AccessController.doPrivileged(load);
65 }
66
67 /**
68 * Sync interval in seconds.
69 */
70 private static final int SYNC_INTERVAL = Math.max(1,
71 AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
72 Integer.getInteger("java.util.prefs.syncInterval", 30)));
73
74 /**
75 * Returns logger for error messages. Backing store exceptions are logged at
76 * WARNING level.
77 */
78 private static PlatformLogger getLogger() {
515 tmpFile = new File(dir, "prefs.tmp");
516 AccessController.doPrivileged(new PrivilegedAction<Void>() {
517 public Void run() {
518 newNode = !dir.exists();
519 return null;
520 }
521 });
522 if (newNode) {
523 // These 2 things guarantee node will get wrtten at next flush/sync
524 prefsCache = new TreeMap<>();
525 nodeCreate = new NodeCreate();
526 changeLog.add(nodeCreate);
527 }
528 }
529
530 public boolean isUserNode() {
531 return isUserNode;
532 }
533
534 protected void putSpi(String key, String value) {
535 if (key.indexOf(CODE_POINT_U0000) != -1) {
536 throw new IllegalArgumentException("Key contains code point U+0000");
537 } else if (value.indexOf(CODE_POINT_U0000) != -1) {
538 throw new IllegalArgumentException("Value contains code point U+0000");
539 }
540 initCacheIfNecessary();
541 changeLog.add(new Put(key, value));
542 prefsCache.put(key, value);
543 }
544
545 protected String getSpi(String key) {
546 initCacheIfNecessary();
547 return prefsCache.get(key);
548 }
549
550 protected void removeSpi(String key) {
551 initCacheIfNecessary();
552 changeLog.add(new Remove(key));
553 prefsCache.remove(key);
554 }
555
556 /**
557 * Initialize prefsCache if it has yet to be initialized. When this method
558 * returns, prefsCache will be non-null. If the data was successfully
559 * read from the file, lastSyncTime will be updated. If prefsCache was
|
1 /*
2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
32 import java.security.PrivilegedActionException;
33
34 import sun.util.logging.PlatformLogger;
35
36 /**
37 * Preferences implementation for Unix. Preferences are stored in the file
38 * system, with one directory per preferences node. All of the preferences
39 * at each node are stored in a single file. Atomic file system operations
40 * (e.g. File.renameTo) are used to ensure integrity. An in-memory cache of
41 * the "explored" portion of the tree is maintained for performance, and
42 * written back to the disk periodically. File-locking is used to ensure
43 * reasonable behavior when multiple VMs are running at the same time.
44 * (The file lock is obtained only for sync(), flush() and removeNode().)
45 *
46 * @author Josh Bloch
47 * @see Preferences
48 * @since 1.4
49 */
50 class FileSystemPreferences extends AbstractPreferences {
51
52 static {
53 PrivilegedAction<Void> load = () -> {
54 System.loadLibrary("prefs");
55 return null;
56 };
57 AccessController.doPrivileged(load);
58 }
59
60 /**
61 * Sync interval in seconds.
62 */
63 private static final int SYNC_INTERVAL = Math.max(1,
64 AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
65 Integer.getInteger("java.util.prefs.syncInterval", 30)));
66
67 /**
68 * Returns logger for error messages. Backing store exceptions are logged at
69 * WARNING level.
70 */
71 private static PlatformLogger getLogger() {
508 tmpFile = new File(dir, "prefs.tmp");
509 AccessController.doPrivileged(new PrivilegedAction<Void>() {
510 public Void run() {
511 newNode = !dir.exists();
512 return null;
513 }
514 });
515 if (newNode) {
516 // These 2 things guarantee node will get wrtten at next flush/sync
517 prefsCache = new TreeMap<>();
518 nodeCreate = new NodeCreate();
519 changeLog.add(nodeCreate);
520 }
521 }
522
523 public boolean isUserNode() {
524 return isUserNode;
525 }
526
527 protected void putSpi(String key, String value) {
528 initCacheIfNecessary();
529 changeLog.add(new Put(key, value));
530 prefsCache.put(key, value);
531 }
532
533 protected String getSpi(String key) {
534 initCacheIfNecessary();
535 return prefsCache.get(key);
536 }
537
538 protected void removeSpi(String key) {
539 initCacheIfNecessary();
540 changeLog.add(new Remove(key));
541 prefsCache.remove(key);
542 }
543
544 /**
545 * Initialize prefsCache if it has yet to be initialized. When this method
546 * returns, prefsCache will be non-null. If the data was successfully
547 * read from the file, lastSyncTime will be updated. If prefsCache was
|