Author: mturk Date: Mon Dec 14 11:06:41 2009 New Revision: 890273 URL: http://svn.apache.org/viewvc?rev=890273&view=rev Log: Make HKEY class instead enum
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/HKEY.java commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/HKEY.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/HKEY.java?rev=890273&r1=890272&r2=890273&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/HKEY.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/HKEY.java Mon Dec 14 11:06:41 2009 @@ -16,8 +16,12 @@ package org.apache.commons.runtime.platform.windows; +import java.io.Closeable; +import java.io.IOException; +import org.apache.commons.runtime.io.Status; + /** - * Registry key type enum. + * Registry key type. * <p> * An application can use handles to these keys as entry points to the * registry. These handles are valid for all implementations of the @@ -27,8 +31,9 @@ * keys. * </p> */ -public enum HKEY +public class HKEY implements Closeable { + /** Registry entries subordinate to this key define types (or classes) * of documents and the properties associated with those types. Shell * and COM applications use the information stored under this key. @@ -43,19 +48,7 @@ * This handle should not be used in a service or an application that * impersonates different users. */ - CLASSES_ROOT( 1), - - /** Contains information about the current hardware profile of the - * local computer system. - * <p> - * The information under {...@code HKEY_CURRENT_CONFIG} - * describes only the differences between the current hardware - * configuration and the standard configuration. Information about - * the standard hardware configuration is stored under the Software - * and System keys of {...@code HKEY_LOCAL_MACHINE}. - * </p> - */ - CURRENT_CONFIG( 2), + public static final HKEY CLASSES_ROOT = new HKEY(0x80000000L); /** Registry entries subordinate to this key define the preferences of * the current user. These preferences include the settings of @@ -64,7 +57,7 @@ * easier to establish the current user's settings; the key maps to the * current user's branch in {...@code HKEY_USERS}. */ - CURRENT_USER( 3), + public static final HKEY CURRENT_USER = new HKEY(0x80000001L); /** Registry entries subordinate to this key define the physical state * of the computer, including data about the bus type, system memory, @@ -75,33 +68,88 @@ * security information, software-related information (such as server * names and the location of the server), and other system information. */ - LOCAL_MACHINE( 4), + public static final HKEY LOCAL_MACHINE = new HKEY(0x80000002L); /** Registry entries subordinate to this key define the default user * configuration for new users on the local computer and the user * configuration for the current user. */ - USERS ( 5); + public static final HKEY USERS = new HKEY(0x80000003L); + public static final HKEY PERFORMANCE_DATA = new HKEY(0x80000004L); - private int value; - private HKEY(int v) + /** Contains information about the current hardware profile of the + * local computer system. + * <p> + * The information under {...@code HKEY_CURRENT_CONFIG} + * describes only the differences between the current hardware + * configuration and the standard configuration. Information about + * the standard hardware configuration is stored under the Software + * and System keys of {...@code HKEY_LOCAL_MACHINE}. + * </p> + */ + public static final HKEY CURRENT_CONFIG = new HKEY(0x80000005L); + + public static final HKEY DYN_DATA = new HKEY(0x80000006L); + + + private long hKey; + private HKEY(long v) { - value = v; + hKey = v; } - public int valueOf() + public long handle() { - return value; + return hKey; } - public static HKEY valueOf(int value) + private static native int close0(long key); + /** + * Called by the garbage collector when the object is destroyed. + * The class will free internal resources allocated by the Operating system. + * @see Object#finalize() + * @throws Throwable the {...@code Exception} raised by this method. + */ + @Override + protected final void finalize() + throws Throwable { - for (HKEY e : values()) { - if (e.value == value) - return e; + if (hKey != 0L) { + try { + close0(hKey); + } catch (Exception e) { + // Ignore + } finally { + hKey = 0L; + } + } + } + + /** + * Free the allocated resource by the Operating system. + * <p> + * Note that {...@code Object.finalize()} method will call + * this function. However if the native code can block for + * long time explicit {...@code close()} should be called. + * </p> + * @see java.io.Closeable#close() + * @throws IOException if an I/O error occurs. + */ + public final void close() + throws IOException + { + int s = Status.ENOTIMPL; + try { + s = close0(hKey); + } catch (Exception e) { + // Ignore + } finally { + hKey = 0L; + } + if (s != 0) { + throw new IOException(Status.describe(s)); } - throw new IllegalArgumentException("Invalid initializer: " + value); } } Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c?rev=890273&r1=890272&r2=890273&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/os/win32/registry.c Mon Dec 14 11:06:41 2009 @@ -398,3 +398,24 @@ x_free(k.name); return i; } + +ACR_JNI_PLATFORM_DECLARE(jint, HKEY, close0)(ACR_JNISTDARGS, + jlong key) +{ + int rc = ACR_EBADF; + HKEY hkey = J2P(key, HKEY); + + if (IS_VALID_HANDLE(hkey)) { + if (hkey == HKEY_CLASSES_ROOT || + hkey == HKEY_CURRENT_CONFIG || + hkey == HKEY_CURRENT_USER || + hkey == HKEY_LOCAL_MACHINE || + hkey == HKEY_USERS || + hkey == HKEY_DYN_DATA) + rc = 0; + else + rc = (int)RegCloseKey(hkey); + } + return rc; +} +