📄 windowspreferences.java
字号:
* Windows registry node and all its Windows parents, if they are not yet * created. * Logs a warning message, if Windows Registry is unavailable. */ private WindowsPreferences(WindowsPreferences parent, String name) { super(parent, name); int parentNativeHandle = parent.openKey(KEY_CREATE_SUB_KEY, KEY_READ); if (parentNativeHandle == NULL_NATIVE_HANDLE) { // if here, openKey failed and logged isBackingStoreAvailable = false; return; } int[] result = WindowsRegCreateKeyEx1(parentNativeHandle, toWindowsName(name)); if (result[ERROR_CODE] != ERROR_SUCCESS) { logger().warning("Could not create windows registry " + "node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegCreateKeyEx(...) returned error code " + result[ERROR_CODE] + "."); isBackingStoreAvailable = false; return; } newNode = (result[DISPOSITION] == REG_CREATED_NEW_KEY); closeKey(parentNativeHandle); closeKey(result[NATIVE_HANDLE]); } /** * Constructs a root node creating the underlying * Windows registry node and all of its parents, if they have not yet been * created. * Logs a warning message, if Windows Registry is unavailable. * @param rootNativeHandle Native handle to one of Windows top level keys. * @param rootDirectory Path to root directory, as a byte-encoded string. */ private WindowsPreferences(int rootNativeHandle, byte[] rootDirectory) { super(null,""); int[] result = WindowsRegCreateKeyEx1(rootNativeHandle, rootDirectory); if (result[ERROR_CODE] != ERROR_SUCCESS) { logger().warning("Could not open/create prefs root node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegCreateKeyEx(...) returned error code " + result[ERROR_CODE] + "."); isBackingStoreAvailable = false; return; } // Check if a new node newNode = (result[DISPOSITION] == REG_CREATED_NEW_KEY); closeKey(result[NATIVE_HANDLE]); } /** * Returns Windows absolute path of the current node as a byte array. * Java "/" separator is transformed into Windows "\". * @see Preferences#absolutePath() */ private byte[] windowsAbsolutePath() { ByteArrayOutputStream bstream = new ByteArrayOutputStream(); bstream.write(WINDOWS_ROOT_PATH, 0, WINDOWS_ROOT_PATH.length-1); StringTokenizer tokenizer = new StringTokenizer(absolutePath(),"/"); while (tokenizer.hasMoreTokens()) { bstream.write((byte)'\\'); String nextName = tokenizer.nextToken(); byte[] windowsNextName = toWindowsName(nextName); bstream.write(windowsNextName, 0, windowsNextName.length-1); } bstream.write(0); return bstream.toByteArray(); } /** * Opens current node's underlying Windows registry key using a * given security mask. * @param securityMask Windows security mask. * @return Windows registry key's handle. * @see #openKey(byte[], int) * @see #openKey(int, byte[], int) * @see #closeKey(int) */ private int openKey(int securityMask) { return openKey(securityMask, securityMask); } /** * Opens current node's underlying Windows registry key using a * given security mask. * @param mask1 Preferred Windows security mask. * @param mask2 Alternate Windows security mask. * @return Windows registry key's handle. * @see #openKey(byte[], int) * @see #openKey(int, byte[], int) * @see #closeKey(int) */ private int openKey(int mask1, int mask2) { return openKey(windowsAbsolutePath(), mask1, mask2); } /** * Opens Windows registry key at a given absolute path using a given * security mask. * @param windowsAbsolutePath Windows absolute path of the * key as a byte-encoded string. * @param mask1 Preferred Windows security mask. * @param mask2 Alternate Windows security mask. * @return Windows registry key's handle. * @see #openKey(int) * @see #openKey(int, byte[],int) * @see #closeKey(int) */ private int openKey(byte[] windowsAbsolutePath, int mask1, int mask2) { /* Check if key's path is short enough be opened at once otherwise use a path-splitting procedure */ if (windowsAbsolutePath.length <= MAX_WINDOWS_PATH_LENGTH + 1) { int[] result = WindowsRegOpenKey1(rootNativeHandle(), windowsAbsolutePath, mask1); if (result[ERROR_CODE] == ERROR_ACCESS_DENIED && mask2 != mask1) result = WindowsRegOpenKey1(rootNativeHandle(), windowsAbsolutePath, mask2); if (result[ERROR_CODE] != ERROR_SUCCESS) { logger().warning("Could not open windows " + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegOpenKey(...) returned error code " + result[ERROR_CODE] + "."); result[NATIVE_HANDLE] = NULL_NATIVE_HANDLE; if (result[ERROR_CODE] == ERROR_ACCESS_DENIED) { throw new SecurityException("Could not open windows " + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ": Access denied"); } } return result[NATIVE_HANDLE]; } else { return openKey(rootNativeHandle(), windowsAbsolutePath, mask1, mask2); } } /** * Opens Windows registry key at a given relative path * with respect to a given Windows registry key. * @param windowsAbsolutePath Windows relative path of the * key as a byte-encoded string. * @param nativeHandle handle to the base Windows key. * @param mask1 Preferred Windows security mask. * @param mask2 Alternate Windows security mask. * @return Windows registry key's handle. * @see #openKey(int) * @see #openKey(byte[],int) * @see #closeKey(int) */ private int openKey(int nativeHandle, byte[] windowsRelativePath, int mask1, int mask2) { /* If the path is short enough open at once. Otherwise split the path */ if (windowsRelativePath.length <= MAX_WINDOWS_PATH_LENGTH + 1 ) { int[] result = WindowsRegOpenKey1(nativeHandle, windowsRelativePath, mask1); if (result[ERROR_CODE] == ERROR_ACCESS_DENIED && mask2 != mask1) result = WindowsRegOpenKey1(nativeHandle, windowsRelativePath, mask2); if (result[ERROR_CODE] != ERROR_SUCCESS) { logger().warning("Could not open windows " + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(nativeHandle) + ". Windows RegOpenKey(...) returned error code " + result[ERROR_CODE] + "."); result[NATIVE_HANDLE] = NULL_NATIVE_HANDLE; } return result[NATIVE_HANDLE]; } else { int separatorPosition = -1; // Be greedy - open the longest possible path for (int i = MAX_WINDOWS_PATH_LENGTH; i > 0; i--) { if (windowsRelativePath[i] == ((byte)'\\')) { separatorPosition = i; break; } } // Split the path and do the recursion byte[] nextRelativeRoot = new byte[separatorPosition+1]; System.arraycopy(windowsRelativePath, 0, nextRelativeRoot,0, separatorPosition); nextRelativeRoot[separatorPosition] = 0; byte[] nextRelativePath = new byte[windowsRelativePath.length - separatorPosition - 1]; System.arraycopy(windowsRelativePath, separatorPosition+1, nextRelativePath, 0, nextRelativePath.length); int nextNativeHandle = openKey(nativeHandle, nextRelativeRoot, mask1, mask2); if (nextNativeHandle == NULL_NATIVE_HANDLE) { return NULL_NATIVE_HANDLE; } int result = openKey(nextNativeHandle, nextRelativePath, mask1,mask2); closeKey(nextNativeHandle); return result; } } /** * Closes Windows registry key. * Logs a warning if Windows registry is unavailable. * @param key's Windows registry handle. * @see #openKey(int) * @see #openKey(byte[],int) * @see #openKey(int, byte[],int) */ private void closeKey(int nativeHandle) { int result = WindowsRegCloseKey(nativeHandle); if (result != ERROR_SUCCESS) { logger().warning("Could not close windows " + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegCloseKey(...) returned error code " + result + "."); } } /** * Implements <tt>AbstractPreferences</tt> <tt>putSpi()</tt> method. * Puts name-value pair into the underlying Windows registry node. * Logs a warning, if Windows registry is unavailable. * @see #getSpi(String) */ protected void putSpi(String javaName, String value) { int nativeHandle = openKey(KEY_SET_VALUE); if (nativeHandle == NULL_NATIVE_HANDLE) { isBackingStoreAvailable = false; return; } int result = WindowsRegSetValueEx1(nativeHandle, toWindowsName(javaName), toWindowsValueString(value)); if (result != ERROR_SUCCESS) { logger().warning("Could not assign value to key " + byteArrayToString(toWindowsName(javaName))+ " at Windows registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegSetValueEx(...) returned error code " + result + "."); isBackingStoreAvailable = false; } closeKey(nativeHandle); } /** * Implements <tt>AbstractPreferences</tt> <tt>getSpi()</tt> method. * Gets a string value from the underlying Windows registry node. * Logs a warning, if Windows registry is unavailable. * @see #putSpi(String, String) */ protected String getSpi(String javaName) { int nativeHandle = openKey(KEY_QUERY_VALUE); if (nativeHandle == NULL_NATIVE_HANDLE) { return null; } Object resultObject = WindowsRegQueryValueEx(nativeHandle, toWindowsName(javaName)); if (resultObject == null) { closeKey(nativeHandle); return null; } closeKey(nativeHandle); return toJavaValueString((byte[]) resultObject); } /** * Implements <tt>AbstractPreferences</tt> <tt>removeSpi()</tt> method. * Deletes a string name-value pair from the underlying Windows registry * node, if this value still exists. * Logs a warning, if Windows registry is unavailable or key has already * been deleted. */ protected void removeSpi(String key) { int nativeHandle = openKey(KEY_SET_VALUE); if (nativeHandle == NULL_NATIVE_HANDLE) { return; } int result = WindowsRegDeleteValue(nativeHandle, toWindowsName(key)); if (result != ERROR_SUCCESS && result != ERROR_FILE_NOT_FOUND) { logger().warning("Could not delete windows registry " + "value " + byteArrayToString(windowsAbsolutePath())+ "\\" + toWindowsName(key) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegDeleteValue(...) returned error code " + result + "."); isBackingStoreAvailable = false; } closeKey(nativeHandle); } /** * Implements <tt>AbstractPreferences</tt> <tt>keysSpi()</tt> method. * Gets value names from the underlying Windows registry node. * Throws a BackingStoreException and logs a warning, if * Windows registry is unavailable. */ protected String[] keysSpi() throws BackingStoreException{ // Find out the number of values int nativeHandle = openKey(KEY_QUERY_VALUE); if (nativeHandle == NULL_NATIVE_HANDLE) { throw new BackingStoreException("Could not open windows" + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + "."); } int[] result = WindowsRegQueryInfoKey1(nativeHandle); if (result[ERROR_CODE] != ERROR_SUCCESS) { String info = "Could not query windows" + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegQueryInfoKeyEx(...) returned error code " + result[ERROR_CODE] + "."; logger().warning(info); throw new BackingStoreException(info); } int maxValueNameLength = result[MAX_VALUE_NAME_LENGTH]; int valuesNumber = result[VALUES_NUMBER]; if (valuesNumber == 0) { closeKey(nativeHandle); return new String[0]; } // Get the values String[] valueNames = new String[valuesNumber]; for (int i = 0; i < valuesNumber; i++) { byte[] windowsName = WindowsRegEnumValue1(nativeHandle, i, maxValueNameLength+1); if (windowsName == null) { String info = "Could not enumerate value #" + i + " of windows node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + "."; logger().warning(info); throw new BackingStoreException(info); } valueNames[i] = toJavaName(windowsName); } closeKey(nativeHandle); return valueNames; } /** * Implements <tt>AbstractPreferences</tt> <tt>childrenNamesSpi()</tt> method. * Calls Windows registry to retrive children of this node. * Throws a BackingStoreException and logs a warning message, * if Windows registry is not available. */ protected String[] childrenNamesSpi() throws BackingStoreException { // Open key int nativeHandle = openKey(KEY_ENUMERATE_SUB_KEYS| KEY_QUERY_VALUE); if (nativeHandle == NULL_NATIVE_HANDLE) { throw new BackingStoreException("Could not open windows" + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + "."); } // Get number of children int[] result = WindowsRegQueryInfoKey1(nativeHandle); if (result[ERROR_CODE] != ERROR_SUCCESS) { String info = "Could not query windows" + "registry node " + byteArrayToString(windowsAbsolutePath()) + " at root 0x" + Integer.toHexString(rootNativeHandle()) + ". Windows RegQueryInfoKeyEx(...) returned error code " + result[ERROR_CODE] + ".";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -