/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* 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
* @see Preferences
* @see PreferencesFactory
* @since 1.4
*/
/**
* Logger for error messages
*/
/**
* 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.
*/
/**
* Mount point for <tt>Preferences</tt>' user root.
*/
/**
* Mount point for <tt>Preferences</tt>' system root.
*/
/**
* Maximum byte-encoded path length for Windows native functions,
* ending <tt>null</tt> character not included.
*/
/**
* User root node.
*/
/**
* System root node.
*/
/* Windows error codes. */
/* Constants used to interpret returns of native functions */
/* Windows security masks */
/**
* Initial time between registry access attempts, in ms. The time is doubled
* after each failing attempt (except the first).
*/
/**
* Maximum number of registry access attempts.
*/
/**
* BackingStore availability flag.
*/
private boolean isBackingStoreAvailable = true;
/**
* Java wrapper for Windows registry API RegOpenKey()
*/
int securityMask);
/**
* Retries RegOpenKey() MAX_ATTEMPTS times before giving up.
*/
int securityMask) {
return result;
// Try recreation
long sleepTime = INIT_SLEEP_TIME;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
try {
} catch(InterruptedException e) {
return result;
}
sleepTime *= 2;
return result;
}
}
}
return result;
}
/**
* Java wrapper for Windows registry API RegCloseKey()
*/
/**
* Java wrapper for Windows registry API RegCreateKeyEx()
*/
/**
* Retries RegCreateKeyEx() MAX_ATTEMPTS times before giving up.
*/
return result;
} else {
long sleepTime = INIT_SLEEP_TIME;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
try {
} catch(InterruptedException e) {
return result;
}
sleepTime *= 2;
return result;
}
}
}
return result;
}
/**
* Java wrapper for Windows registry API RegDeleteKey()
*/
/**
* Java wrapper for Windows registry API RegFlushKey()
*/
/**
* Retries RegFlushKey() MAX_ATTEMPTS times before giving up.
*/
if (result == ERROR_SUCCESS) {
return result;
} else {
long sleepTime = INIT_SLEEP_TIME;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
try {
} catch(InterruptedException e) {
return result;
}
sleepTime *= 2;
if (result == ERROR_SUCCESS) {
return result;
}
}
}
return result;
}
/**
* Java wrapper for Windows registry API RegQueryValueEx()
*/
byte[] valueName);
/**
* Java wrapper for Windows registry API RegSetValueEx()
*/
byte[] value);
/**
* Retries RegSetValueEx() MAX_ATTEMPTS times before giving up.
*/
byte[] value) {
if (result == ERROR_SUCCESS) {
return result;
} else {
long sleepTime = INIT_SLEEP_TIME;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
try {
} catch(InterruptedException e) {
return result;
}
sleepTime *= 2;
if (result == ERROR_SUCCESS) {
return result;
}
}
}
return result;
}
/**
* Java wrapper for Windows registry API RegDeleteValue()
*/
/**
* Java wrapper for Windows registry API RegQueryInfoKey()
*/
/**
* Retries RegQueryInfoKey() MAX_ATTEMPTS times before giving up.
*/
return result;
} else {
long sleepTime = INIT_SLEEP_TIME;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
try {
} catch(InterruptedException e) {
return result;
}
sleepTime *= 2;
return result;
}
}
}
return result;
}
/**
* Java wrapper for Windows registry API RegEnumKeyEx()
*/
int maxKeyLength);
/**
* Retries RegEnumKeyEx() MAX_ATTEMPTS times before giving up.
*/
int maxKeyLength) {
return result;
} else {
long sleepTime = INIT_SLEEP_TIME;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
try {
} catch(InterruptedException e) {
return result;
}
sleepTime *= 2;
return result;
}
}
}
return result;
}
/**
* Java wrapper for Windows registry API RegEnumValue()
*/
int maxValueNameLength);
/**
* Retries RegEnumValueEx() MAX_ATTEMPTS times before giving up.
*/
int maxValueNameLength) {
return result;
} else {
long sleepTime = INIT_SLEEP_TIME;
for (int i = 0; i < MAX_ATTEMPTS; i++) {
try {
} catch(InterruptedException e) {
return result;
}
sleepTime *= 2;
return result;
}
}
}
return result;
}
/**
* Constructs a <tt>WindowsPreferences</tt> node, creating underlying
* Windows registry node and all its Windows parents, if they are not yet
* created.
* Logs a warning message, if Windows Registry is unavailable.
*/
if (parentNativeHandle == NULL_NATIVE_HANDLE) {
// if here, openKey failed and logged
isBackingStoreAvailable = false;
return;
}
int[] result =
". Windows RegCreateKeyEx(...) returned error code " +
isBackingStoreAvailable = false;
return;
}
}
/**
* 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.
*/
super(null,"");
int[] result =
". Windows RegCreateKeyEx(...) returned error code " +
isBackingStoreAvailable = false;
return;
}
// Check if a new node
}
/**
* Returns Windows absolute path of the current node as a byte array.
* Java "/" separator is transformed into Windows "\".
* @see Preferences#absolutePath()
*/
private byte[] windowsAbsolutePath() {
while (tokenizer.hasMoreTokens()) {
}
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)
*/
}
/**
* 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)
*/
}
/**
* 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)
*/
/* Check if key's path is short enough be opened at once
otherwise use a path-splitting procedure */
". Windows RegOpenKey(...) returned error code " +
throw new SecurityException("Could not open windows "
": Access denied");
}
}
return result[NATIVE_HANDLE];
} else {
}
}
/**
* 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)
*/
/* If the path is short enough open at once. Otherwise split the path */
". Windows RegOpenKey(...) returned error code " +
}
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
separatorPosition - 1];
if (nextNativeHandle == NULL_NATIVE_HANDLE) {
return NULL_NATIVE_HANDLE;
}
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)
*/
if (result != ERROR_SUCCESS) {
}
}
/**
* 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)
*/
if (nativeHandle == NULL_NATIVE_HANDLE) {
isBackingStoreAvailable = false;
return;
}
if (result != ERROR_SUCCESS) {
isBackingStoreAvailable = false;
}
}
/**
* 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)
*/
if (nativeHandle == NULL_NATIVE_HANDLE) {
return null;
}
if (resultObject == null) {
return null;
}
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.
*/
if (nativeHandle == NULL_NATIVE_HANDLE) {
return;
}
int result =
". Windows RegDeleteValue(...) returned error code " +
result + ".");
isBackingStoreAvailable = false;
}
}
/**
* 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.
*/
// Find out the number of values
if (nativeHandle == NULL_NATIVE_HANDLE) {
throw new BackingStoreException("Could not open windows"
}
". Windows RegQueryInfoKeyEx(...) returned error code " +
throw new BackingStoreException(info);
}
if (valuesNumber == 0) {
return new String[0];
}
// Get the values
for (int i = 0; i < valuesNumber; i++) {
if (windowsName == null) {
"Could not enumerate value #" + i + " of windows node " +
throw new BackingStoreException(info);
}
}
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.
*/
// Open key
if (nativeHandle == NULL_NATIVE_HANDLE) {
throw new BackingStoreException("Could not open windows"
}
// Get number of children
". Windows RegQueryInfoKeyEx(...) returned error code " +
throw new BackingStoreException(info);
}
if (subKeysNumber == 0) {
return new String[0];
}
// Get children
for (int i = 0; i < subKeysNumber; i++) {
maxKeyLength+1);
if (windowsName == null) {
"Could not enumerate key #" + i + " of windows node " +
throw new BackingStoreException(info);
}
}
return children;
}
/**
* Implements <tt>Preferences</tt> <tt>flush()</tt> method.
* Flushes Windows registry changes to disk.
* Throws a BackingStoreException and logs a warning message if Windows
* registry is not available.
*/
if (isRemoved()) {
return;
}
if (!isBackingStoreAvailable) {
throw new BackingStoreException(
"flush(): Backing store not available.");
}
if (nativeHandle == NULL_NATIVE_HANDLE) {
throw new BackingStoreException("Could not open windows"
}
if (result != ERROR_SUCCESS) {
throw new BackingStoreException(info);
}
}
/**
* Implements <tt>Preferences</tt> <tt>sync()</tt> method.
* Flushes Windows registry changes to disk. Equivalent to flush().
* @see flush()
*/
if (isRemoved())
throw new IllegalStateException("Node has been removed");
flush();
}
/**
* Implements <tt>AbstractPreferences</tt> <tt>childSpi()</tt> method.
* Constructs a child node with a
* given name and creates its underlying Windows registry node,
* if it does not exist.
* Logs a warning message, if Windows Registry is unavailable.
*/
return new WindowsPreferences(this, name);
}
/**
* Implements <tt>AbstractPreferences</tt> <tt>removeNodeSpi()</tt> method.
* Deletes underlying Windows registry node.
* Throws a BackingStoreException and logs a warning, if Windows registry
* is not available.
*/
int parentNativeHandle =
if (parentNativeHandle == NULL_NATIVE_HANDLE) {
throw new BackingStoreException("Could not open parent windows"
}
int result =
if (result != ERROR_SUCCESS) {
". Windows RegDeleteKeyEx(...) returned error code " +
result + ".";
throw new BackingStoreException(info);
}
}
/**
* Converts value's or node's name from its byte array representation to
* java string. Two encodings, simple and altBase64 are used. See
* {@link #toWindowsName(String) toWindowsName()} for a detailed
* description of encoding conventions.
* @param windowsNameArray Null-terminated byte array.
*/
// check if Alt64
return toJavaAlt64Name(windowsName);
}
char ch;
// Decode from simple encoding
char next = ' ';
i++;
ch = '\\';
i++;
}
} else if (ch == '\\') {
ch = '/';
}
}
}
/**
* Converts value's or node's name from its Windows representation to java
* string, using altBase64 encoding. See
* {@link #toWindowsName(String) toWindowsName()} for a detailed
* description of encoding conventions.
*/
byte[] byteBuffer =
}
}
/**
* Converts value's or node's name to its Windows representation
* as a byte-encoded string.
* Two encodings, simple and altBase64 are used.
* <p>
* <i>Simple</i> encoding is used, if java string does not contain
* any characters less, than 0x0020, or greater, than 0x007f.
* Simple encoding adds "/" character to capital letters, i.e.
* "A" is encoded as "/A". Character '\' is encoded as '//',
* '/' is encoded as '\'.
* The constructed string is converted to byte array by truncating the
* highest byte and adding the terminating <tt>null</tt> character.
* <p>
* <i>altBase64</i> encoding is used, if java string does contain at least
* one character less, than 0x0020, or greater, than 0x007f.
* This encoding is marked by setting first two bytes of the
* Windows string to '/!'. The java name is then encoded using
* byteArrayToAltBase64() method from
* Base64 class.
*/
// If a non-trivial character encountered, use altBase64
return toWindowsAlt64Name(javaName);
}
if (ch == '\\') {
} else if (ch == '/') {
} else {
}
}
}
/**
* Converts value's or node's name to its Windows representation
* as a byte-encoded string, using altBase64 encoding. See
* {@link #toWindowsName(String) toWindowsName()} for a detailed
* description of encoding conventions.
*/
// Convert to byte pairs
int counter = 0;
}
return stringToByteArray(
}
/**
* Converts value string from its Windows representation
* to java string. See
* {@link #toWindowsValueString(String) toWindowsValueString()} for the
* description of the encoding algorithm.
*/
// Use modified native2ascii algorithm
char ch;
char next = ' ';
break;
} else {
i += 5;
}
} else
i++;
(next == '/')) {
ch = '\\';
i++;
}
} else if (ch == '\\') {
ch = '/';
}
}
}
/**
* Converts value string to it Windows representation.
* as a byte-encoded string.
* Encoding algorithm adds "/" character to capital letters, i.e.
* "A" is encoded as "/A". Character '\' is encoded as '//',
* '/' is encoded as '\'.
* Then encoding scheme similar to jdk's native2ascii converter is used
* to convert java string to a byte array of ASCII characters.
*/
// write \udddd
for (int j = 0; j < len; j++){
}
for (int j = 0; j < 4; j++){
}
} else if (ch == '\\') {
} else if (ch == '/') {
} else {
}
}
}
/**
* Returns native handle for the top Windows node for this node.
*/
private int rootNativeHandle() {
return (isUserNode()? USER_ROOT_NATIVE_HANDLE :
}
/**
* Returns this java string as a null-terminated byte array
*/
}
return result;
}
/**
* Converts a null-terminated byte array to java string
*/
}
}
/**
* Empty, never used implementation of AbstractPreferences.flushSpi().
*/
// assert false;
}
/**
* Empty, never used implementation of AbstractPreferences.flushSpi().
*/
// assert false;
}
}
return logger;
}
}