📄 windowspreferences.java
字号:
/* * @(#)WindowsPreferences.java 1.17 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util.prefs;import java.util.Map;import java.util.TreeMap;import java.util.StringTokenizer;import java.io.ByteArrayOutputStream;import java.util.logging.Logger;/** * Windows registry based implementation of <tt>Preferences</tt>. * <tt>Preferences</tt>' <tt>systemRoot</tt> and <tt>userRoot</tt> are stored in * <tt>HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs</tt> and * <tt>HKEY_CURRENT_USER\Software\JavaSoft\Prefs</tt> correspondingly. * * @author Konstantin Kladko * @version 1.17, 01/23/03 * @see Preferences * @see PreferencesFactory * @since 1.4 */class WindowsPreferences extends AbstractPreferences{ /** * Logger for error messages */ private static Logger logger; /** * Windows registry path to <tt>Preferences</tt>'s root nodes. */ private static final byte[] WINDOWS_ROOT_PATH = stringToByteArray("Software\\JavaSoft\\Prefs"); /** * Windows handles to <tt>HKEY_CURRENT_USER</tt> and * <tt>HKEY_LOCAL_MACHINE</tt> hives. */ private static final int HKEY_CURRENT_USER = 0x80000001; private static final int HKEY_LOCAL_MACHINE = 0x80000002; /** * Mount point for <tt>Preferences</tt>' user root. */ private static final int USER_ROOT_NATIVE_HANDLE = HKEY_CURRENT_USER; /** * Mount point for <tt>Preferences</tt>' system root. */ private static final int SYSTEM_ROOT_NATIVE_HANDLE = HKEY_LOCAL_MACHINE; /** * Maximum byte-encoded path length for Windows native functions, * ending <tt>null</tt> character not included. */ private static final int MAX_WINDOWS_PATH_LENGTH = 256; /** * User root node. */ static final Preferences userRoot = new WindowsPreferences(USER_ROOT_NATIVE_HANDLE, WINDOWS_ROOT_PATH); /** * System root node. */ static final Preferences systemRoot = new WindowsPreferences(SYSTEM_ROOT_NATIVE_HANDLE, WINDOWS_ROOT_PATH); /* Windows error codes. */ private static final int ERROR_SUCCESS = 0; private static final int ERROR_FILE_NOT_FOUND = 2; private static final int ERROR_ACCESS_DENIED = 5; /* Constants used to interpret returns of native functions */ private static final int NATIVE_HANDLE = 0; private static final int ERROR_CODE = 1; private static final int SUBKEYS_NUMBER = 0; private static final int VALUES_NUMBER = 2; private static final int MAX_KEY_LENGTH = 3; private static final int MAX_VALUE_NAME_LENGTH = 4; private static final int DISPOSITION = 2; private static final int REG_CREATED_NEW_KEY = 1; private static final int REG_OPENED_EXISTING_KEY = 2; private static final int NULL_NATIVE_HANDLE = 0; /* Windows security masks */ private static final int DELETE = 0x10000; private static final int KEY_QUERY_VALUE = 1; private static final int KEY_SET_VALUE = 2; private static final int KEY_CREATE_SUB_KEY = 4; private static final int KEY_ENUMERATE_SUB_KEYS = 8; private static final int KEY_READ = 0x20019; private static final int KEY_WRITE = 0x20006; private static final int KEY_ALL_ACCESS = 0xf003f; /** * Initial time between registry access attempts, in ms. The time is doubled * after each failing attempt (except the first). */ private static int INIT_SLEEP_TIME = 50; /** * Maximum number of registry access attempts. */ private static int MAX_ATTEMPTS = 5; /** * BackingStore availability flag. */ private boolean isBackingStoreAvailable = true; /** * Java wrapper for Windows registry API RegOpenKey() */ private static native int[] WindowsRegOpenKey(int hKey, byte[] subKey, int securityMask); /** * Retries RegOpenKey() MAX_ATTEMPTS times before giving up. */ private static int[] WindowsRegOpenKey1(int hKey, byte[] subKey, int securityMask) { int[] result = WindowsRegOpenKey(hKey, subKey, securityMask); if (result[ERROR_CODE] == ERROR_SUCCESS) { return result; } else if (result[ERROR_CODE] == ERROR_FILE_NOT_FOUND) { logger().warning("Trying to recreate Windows registry node " + byteArrayToString(subKey) + " at root 0x" + Integer.toHexString(hKey) + "."); // Try recreation int handle = WindowsRegCreateKeyEx(hKey, subKey)[NATIVE_HANDLE]; WindowsRegCloseKey(handle); return WindowsRegOpenKey(hKey, subKey, securityMask); } else if (result[ERROR_CODE] != ERROR_ACCESS_DENIED) { long sleepTime = INIT_SLEEP_TIME; for (int i = 0; i < MAX_ATTEMPTS; i++) { try { Thread.sleep(sleepTime); } catch(InterruptedException e) { return result; } sleepTime *= 2; result = WindowsRegOpenKey(hKey, subKey, securityMask); if (result[ERROR_CODE] == ERROR_SUCCESS) { return result; } } } return result; } /** * Java wrapper for Windows registry API RegCloseKey() */ private static native int WindowsRegCloseKey(int hKey); /** * Java wrapper for Windows registry API RegCreateKeyEx() */ private static native int[] WindowsRegCreateKeyEx(int hKey, byte[] subKey); /** * Retries RegCreateKeyEx() MAX_ATTEMPTS times before giving up. */ private static int[] WindowsRegCreateKeyEx1(int hKey, byte[] subKey) { int[] result = WindowsRegCreateKeyEx(hKey, subKey); if (result[ERROR_CODE] == ERROR_SUCCESS) { return result; } else { long sleepTime = INIT_SLEEP_TIME; for (int i = 0; i < MAX_ATTEMPTS; i++) { try { Thread.sleep(sleepTime); } catch(InterruptedException e) { return result; } sleepTime *= 2; result = WindowsRegCreateKeyEx(hKey, subKey); if (result[ERROR_CODE] == ERROR_SUCCESS) { return result; } } } return result; } /** * Java wrapper for Windows registry API RegDeleteKey() */ private static native int WindowsRegDeleteKey(int hKey, byte[] subKey); /** * Java wrapper for Windows registry API RegFlushKey() */ private static native int WindowsRegFlushKey(int hKey); /** * Retries RegFlushKey() MAX_ATTEMPTS times before giving up. */ private static int WindowsRegFlushKey1(int hKey) { int result = WindowsRegFlushKey(hKey); if (result == ERROR_SUCCESS) { return result; } else { long sleepTime = INIT_SLEEP_TIME; for (int i = 0; i < MAX_ATTEMPTS; i++) { try { Thread.sleep(sleepTime); } catch(InterruptedException e) { return result; } sleepTime *= 2; result = WindowsRegFlushKey(hKey); if (result == ERROR_SUCCESS) { return result; } } } return result; } /** * Java wrapper for Windows registry API RegQueryValueEx() */ private static native byte[] WindowsRegQueryValueEx(int hKey, byte[] valueName); /** * Java wrapper for Windows registry API RegSetValueEx() */ private static native int WindowsRegSetValueEx(int hKey, byte[] valueName, byte[] value); /** * Retries RegSetValueEx() MAX_ATTEMPTS times before giving up. */ private static int WindowsRegSetValueEx1(int hKey, byte[] valueName, byte[] value) { int result = WindowsRegSetValueEx(hKey, valueName, value); if (result == ERROR_SUCCESS) { return result; } else { long sleepTime = INIT_SLEEP_TIME; for (int i = 0; i < MAX_ATTEMPTS; i++) { try { Thread.sleep(sleepTime); } catch(InterruptedException e) { return result; } sleepTime *= 2; result = WindowsRegSetValueEx(hKey, valueName, value); if (result == ERROR_SUCCESS) { return result; } } } return result; } /** * Java wrapper for Windows registry API RegDeleteValue() */ private static native int WindowsRegDeleteValue(int hKey, byte[] valueName); /** * Java wrapper for Windows registry API RegQueryInfoKey() */ private static native int[] WindowsRegQueryInfoKey(int hKey); /** * Retries RegQueryInfoKey() MAX_ATTEMPTS times before giving up. */ private static int[] WindowsRegQueryInfoKey1(int hKey) { int[] result = WindowsRegQueryInfoKey(hKey); if (result[ERROR_CODE] == ERROR_SUCCESS) { return result; } else { long sleepTime = INIT_SLEEP_TIME; for (int i = 0; i < MAX_ATTEMPTS; i++) { try { Thread.sleep(sleepTime); } catch(InterruptedException e) { return result; } sleepTime *= 2; result = WindowsRegQueryInfoKey(hKey); if (result[ERROR_CODE] == ERROR_SUCCESS) { return result; } } } return result; } /** * Java wrapper for Windows registry API RegEnumKeyEx() */ private static native byte[] WindowsRegEnumKeyEx(int hKey, int subKeyIndex, int maxKeyLength); /** * Retries RegEnumKeyEx() MAX_ATTEMPTS times before giving up. */ private static byte[] WindowsRegEnumKeyEx1(int hKey, int subKeyIndex, int maxKeyLength) { byte[] result = WindowsRegEnumKeyEx(hKey, subKeyIndex, maxKeyLength); if (result != null) { return result; } else { long sleepTime = INIT_SLEEP_TIME; for (int i = 0; i < MAX_ATTEMPTS; i++) { try { Thread.sleep(sleepTime); } catch(InterruptedException e) { return result; } sleepTime *= 2; result = WindowsRegEnumKeyEx(hKey, subKeyIndex, maxKeyLength); if (result != null) { return result; } } } return result; } /** * Java wrapper for Windows registry API RegEnumValue() */ private static native byte[] WindowsRegEnumValue(int hKey, int valueIndex, int maxValueNameLength); /** * Retries RegEnumValueEx() MAX_ATTEMPTS times before giving up. */ private static byte[] WindowsRegEnumValue1(int hKey, int valueIndex, int maxValueNameLength) { byte[] result = WindowsRegEnumValue(hKey, valueIndex, maxValueNameLength); if (result != null) { return result; } else { long sleepTime = INIT_SLEEP_TIME; for (int i = 0; i < MAX_ATTEMPTS; i++) { try { Thread.sleep(sleepTime); } catch(InterruptedException e) { return result; } sleepTime *= 2; result = WindowsRegEnumValue(hKey, valueIndex, maxValueNameLength); if (result != null) { return result; } } } return result; } /** * Constructs a <tt>WindowsPreferences</tt> node, creating underlying
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -