KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jnlp > sample > JreInstaller > WinRegistry


1 /*
2  * @(#)WinRegistry.java 1.5 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36 package jnlp.sample.JreInstaller;
37
38
39 /**
40  * A simple cover class for the Windows registry. The API is
41  * similar to the Windows C registry API.
42  */

43 public class WinRegistry
44 {
45     public static final int HKEY_CLASSES_ROOT = 0x80000000;
46     public static final int HKEY_CURRENT_USER = 0x80000001;
47     public static final int HKEY_LOCAL_MACHINE = 0x80000002;
48     public static final int HKEY_USERS = 0x80000003;
49     public static final int HKEY_PERFORMANCE_DATA = 0x80000004;
50     public static final int HKEY_CURRENT_CONFIG = 0x80000005;
51     public static final int HKEY_DYN_DATA = 0x80000006;
52
53     static final int KEY_QUERY_VALUE = 0x1;
54     static final int KEY_SET_VALUE = 0x2;
55     static final int KEY_CREATE_SUB_KEY = 0x4;
56     static final int KEY_ENUMERATE_SUB_KEYS = 0x8;
57     static final int KEY_NOTIFY = 0x10;
58     static final int KEY_CREATE_LINK = 0x20;
59     static final int KEY_READ = 0x20019;
60     static final int KEY_WRITE = 0x20006;
61
62     static final int KEY_EXECUTE = 0x20019;
63     static final int KEY_ALL_ACCESS = 0xf003f;
64     static final int REG_OPTION_RESERVED = 0x0;
65     static final int REG_OPTION_NON_VOLATILE = 0x0;
66     static final int REG_OPTION_VOLATILE = 0x1;
67     static final int REG_OPTION_CREATE_LINK = 0x2;
68     static final int REG_OPTION_BACKUP_RESTORE = 0x4;
69     static final int REG_OPTION_OPEN_LINK = 0x8;
70     static final int REG_LEGAL_OPTION = 0xf;
71     static final int REG_CREATED_NEW_KEY = 0x1;
72     static final int REG_OPENED_EXISTING_KEY = 0x2;
73     static final int REG_WHOLE_HIVE_VOLATILE = 0x1;
74     static final int REG_REFRESH_HIVE = 0x2;
75     static final int REG_NO_LAZY_FLUSH = 0x4;
76     static final int REG_NOTIFY_CHANGE_NAME = 0x1;
77     static final int REG_NOTIFY_CHANGE_ATTRIBUTES = 0x2;
78     static final int REG_NOTIFY_CHANGE_LAST_SET = 0x4;
79     static final int REG_NOTIFY_CHANGE_SECURITY = 0x8;
80     static final int REG_LEGAL_CHANGE_FILTER = 0xf;
81     static final int REG_NONE = 0x0;
82     static final int REG_SZ = 0x1;
83     static final int REG_EXPAND_SZ = 0x2;
84     static final int REG_BINARY = 0x3;
85     static final int REG_DWORD = 0x4;
86     static final int REG_DWORD_LITTLE_ENDIAN = 0x4;
87     static final int REG_DWORD_BIG_ENDIAN = 0x5;
88     static final int REG_LINK = 0x6;
89     static final int REG_MULTI_SZ = 0x7;
90     static final int REG_RESOURCE_LIST = 0x8;
91     static final int REG_FULL_RESOURCE_DESCRIPTOR = 0x9;
92     static final int REG_RESOURCE_REQUIREMENTS_LIST = 0xa;
93
94     /**
95      * Registry values are represented by an integer type code, e.g.
96      * REG_SZ for a string, and an array of bytes. This class combines
97      * the two and provides a method, getValue(), that converts the array
98      * of bytes into an appropriate java obejct.
99      * <p>
100      * KeyValue objects are created by native methods, see
101      */

102     static class KeyValue {
103     private final int type;
104     private final byte[] data;
105     private Object JavaDoc value = null;
106
107     KeyValue(int type, byte[] data) {
108         this.type = type;
109         this.data = data;
110     }
111     
112     public int getType() {
113         return type;
114     }
115     
116     public byte[] getData() {
117         return data;
118     }
119
120     public Object JavaDoc getValue() {
121         switch (type) {
122         case REG_SZ:
123         // fix for 4634008, 4506366
124
// make sure (data.length - 1) is a positive number
125
if (data.length - 1 < 0) return null;
126         return new String JavaDoc(data, 0, data.length - 1);
127
128         case REG_DWORD:
129         {
130             int n = 0;
131             for(int i = 0; (i < 4) && (i < data.length); i++) {
132             n += data[i] << (i * 8);
133             }
134             return new Integer JavaDoc(n);
135         }
136
137         default:
138         return getData();
139         }
140     }
141     }
142
143     static native void initIDs();
144     static native int sysOpenKey(int hKey, String JavaDoc subKey, int sam);
145     static native int sysCloseKey(int hKey);
146     static native KeyValue sysQueryKey(int hKey, String JavaDoc name);
147     static native int sysCreateKey(int hKey, String JavaDoc name, int sam);
148     static native boolean sysSetStringValue(int hKey, String JavaDoc name,
149                                             String JavaDoc value);
150     static native boolean sysDeleteKey(int hKey, String JavaDoc subKey);
151     static native boolean sysReboot();
152
153     /**
154      * [TBD] This should move to another class.
155      */

156     public static native String JavaDoc getWindowsDirectory();
157
158     static {
159     // Library should have already been loaded by Main
160
initIDs();
161     }
162
163     
164     public static Object JavaDoc get(int hKey, String JavaDoc path, String JavaDoc name) {
165     int key = sysOpenKey(hKey, path, KEY_READ);
166     if (key != 0) {
167         KeyValue kv = sysQueryKey(key, name);
168         sysCloseKey(key);
169         if (kv != null) {
170         return kv.getValue();
171         }
172     }
173     return null;
174     }
175
176     public static String JavaDoc getString(int hKey, String JavaDoc path, String JavaDoc name) {
177     Object JavaDoc rv = get(hKey, path, name);
178     return (rv instanceof String JavaDoc) ? (String JavaDoc)rv : null;
179     }
180
181     public static Integer JavaDoc getInteger(int hKey, String JavaDoc path, String JavaDoc name) {
182     Object JavaDoc rv = get(hKey, path, name);
183     if (rv instanceof Integer JavaDoc) {
184         return (Integer JavaDoc)rv;
185     } else if (rv instanceof byte[]){
186         byte[] b = (byte[])rv;
187         if (b.length == 4 && b[1] == 0 && b[2] == 0 && b[3] == 0) {
188         return new Integer JavaDoc(b[0]);
189         } else {
190         return null;
191         }
192     }
193     return null;
194     }
195
196     public static boolean setStringValue(int hKey, String JavaDoc path, String JavaDoc name,
197                                          String JavaDoc value) {
198         boolean retValue = false;
199         int key = sysCreateKey(hKey, path, KEY_WRITE);
200         if (key != 0) {
201             retValue = sysSetStringValue(key, name, value);
202             sysCloseKey(key);
203         }
204         return retValue;
205     }
206
207     /**
208      * Returns true if the subkey <code>path</code> exists in
209      * <code>hkey</code>.
210      */

211     public static boolean doesSubKeyExist(int hKey, String JavaDoc path) {
212     int key = sysOpenKey(hKey, path, KEY_READ);
213     if (key != 0) {
214             sysCloseKey(key);
215             return true;
216         }
217         return false;
218     }
219
220     public static boolean deleteKey(int hKey, String JavaDoc path) {
221         return sysDeleteKey(hKey, path);
222     }
223     
224     public static boolean doReboot() {
225     return sysReboot();
226     }
227 }
228
Popular Tags