File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\jna\RegistryKey.java.html

1 /*
2 Copyright (c) 2007 Thomas Boerkel, All Rights Reserved
3
4 Disclaimer:
5 ===========
6 This code is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This code is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15 */
16 package hudson.util.jna;
17
18 import com.sun.jna.ptr.IntByReference;
19
20 import java.io.UnsupportedEncodingException;
21 import java.util.Collection;
22 import java.util.TreeMap;
23 import java.util.TreeSet;
24
25 /**
26 * Represents a Win32 registry key.
27 *
28 * @author Kohsuke Kawaguchi
29 */
30 public class RegistryKey {
31 /**
32 * 32bit Windows key value.
33 */
34 private int handle;
35
36 private final RegistryKey root;
37 private final String path;
38
39 /**
40 * Constructor for the root key.
41 */
42 private RegistryKey(int handle) {
43 this.handle = handle;
44 root = this;
45 path = "";
46 }
47
48 private RegistryKey(RegistryKey ancestor, String path,int handle) {
49 this.handle = handle;
50 this.root = ancestor.root;
51 this.path = combine(ancestor.path,path);
52 }
53
54 private static String combine(String a, String b) {
55 if(a.length()==0) return b;
56 if(b.length()==0) return a;
57 return a+'\\'+b;
58 }
59
60 /**
61 * Converts a Windows buffer to a Java String.
62 *
63 * @param buf buffer
64 * @throws java.io.UnsupportedEncodingException on error
65 * @return String
66 */
67 private static String convertBufferToString(byte[] buf) {
68 try {
69 return new String(buf, 0, buf.length - 2, "UTF-16LE");
70 } catch (UnsupportedEncodingException e) {
71 throw new AssertionError(e); // impossible
72 }
73 }
74
75 /**
76 * Converts a Windows buffer to an int.
77 *
78 * @param buf buffer
79 * @return int
80 */
81 private static int convertBufferToInt(byte[] buf) {
82 return (((int) (buf[0] & 0xff)) + (((int) (buf[1] & 0xff)) << 8) + (((int) (buf[2] & 0xff)) << 16) + (((int) (buf[3] & 0xff)) << 24));
83 }
84
85 public String getStringValue(String valueName) {
86 return convertBufferToString(getValue(valueName));
87 }
88
89 /**
90 * Read an int value.
91 */
92 public int getIntValue(String valueName) {
93 return convertBufferToInt(getValue(valueName));
94 }
95
96 private byte[] getValue(String valueName) {
97 IntByReference pType, lpcbData;
98 byte[] lpData = new byte[1];
99
100 pType = new IntByReference();
101 lpcbData = new IntByReference();
102
103 OUTER:
104 while(true) {
105 int r = Advapi32.INSTANCE.RegQueryValueEx(handle, valueName, null, pType, lpData, lpcbData);
106 switch (r) {
107 case WINERROR.ERROR_MORE_DATA:
108 lpData = new byte[lpcbData.getValue()];
109 continue OUTER;
110
111 case WINERROR.ERROR_SUCCESS:
112 return lpData;
113 }
114 throw new JnaException(r);
115 }
116 }
117
118 public void deleteValue(String valueName) {
119 check(Advapi32.INSTANCE.RegDeleteValue(handle, valueName));
120 }
121
122 private void check(int r) {
123 if (r != WINERROR.ERROR_SUCCESS)
124 throw new JnaException(r);
125 }
126
127 /**
128 * Writes a String value.
129 */
130 public void setValue(String name, String value) {
131 try {
132 byte[] bytes = value.getBytes("UTF-16LE");
133 int newLength = bytes.length+2; // for 0 padding
134 byte[] with0 = new byte[newLength];
135 System.arraycopy(bytes, 0, with0, 0, newLength);
136 check(Advapi32.INSTANCE.RegSetValueEx(handle, name, 0, WINNT.REG_SZ, with0, with0.length));
137 } catch (UnsupportedEncodingException e) {
138 throw new AssertionError(e);
139 }
140 }
141
142 /**
143 * Writes a DWORD value.
144 */
145 public void setValue(String name, int value) {
146 byte[] data = new byte[4];
147 data[0] = (byte) (value & 0xff);
148 data[1] = (byte) ((value >> 8) & 0xff);
149 data[2] = (byte) ((value >> 16) & 0xff);
150 data[3] = (byte) ((value >> 24) & 0xff);
151
152 check(Advapi32.INSTANCE.RegSetValueEx(handle, name, 0, WINNT.REG_DWORD, data, data.length));
153 }
154
155 /**
156 * Does a specified value exist?
157 */
158 public boolean valueExists(String name) {
159 IntByReference pType, lpcbData;
160 byte[] lpData = new byte[1];
161
162 pType = new IntByReference();
163 lpcbData = new IntByReference();
164
165 OUTER:
166 while(true) {
167 int r = Advapi32.INSTANCE.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData);
168 switch(r) {
169 case WINERROR.ERROR_MORE_DATA:
170 lpData = new byte[lpcbData.getValue()];
171 continue OUTER;
172 case WINERROR.ERROR_FILE_NOT_FOUND:
173 return false;
174 case WINERROR.ERROR_SUCCESS:
175 return true;
176 default:
177 throw new JnaException(r);
178 }
179 }
180 }
181
182 /**
183 * Deletes this key (and disposes the key.)
184 */
185 public void delete() {
186 check(Advapi32.INSTANCE.RegDeleteKey(handle, path));
187 dispose();
188 }
189
190 /**
191 * Get all sub keys of a key.
192 *
193 * @return array with all sub key names
194 */
195 public Collection<String> getSubKeys() {
196 WINBASE.FILETIME lpftLastWriteTime;
197 TreeSet<String> subKeys = new TreeSet<String>();
198 char[] lpName = new char[256];
199 IntByReference lpcName = new IntByReference(256);
200 lpftLastWriteTime = new WINBASE.FILETIME();
201 int dwIndex = 0;
202
203 while (Advapi32.INSTANCE.RegEnumKeyEx(handle, dwIndex, lpName, lpcName, null,
204 null, null, lpftLastWriteTime) == WINERROR.ERROR_SUCCESS) {
205 subKeys.add(new String(lpName, 0, lpcName.getValue()));
206 lpcName.setValue(256);
207 dwIndex++;
208 }
209
210 return subKeys;
211 }
212
213 public RegistryKey open(String subKeyName) {
214 return open(subKeyName,0xF003F/*KEY_ALL_ACCESS*/);
215 }
216
217 public RegistryKey openReadonly(String subKeyName) {
218 return open(subKeyName,0x20019/*KEY_READ*/);
219 }
220
221 public RegistryKey open(String subKeyName, int access) {
222 IntByReference pHandle = new IntByReference();
223 check(Advapi32.INSTANCE.RegOpenKeyEx(handle, subKeyName, 0, access, pHandle));
224 return new RegistryKey(this,subKeyName,pHandle.getValue());
225 }
226
227 /**
228 * Get all values under a key.
229 *
230 * @return TreeMap with name and value pairs
231 */
232 public TreeMap<String, Object> getValues() {
233 int dwIndex, result;
234 char[] lpValueName;
235 byte[] lpData;
236 IntByReference lpcchValueName, lpType, lpcbData;
237 String name;
238 TreeMap<String, Object> values = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
239
240 lpValueName = new char[16384];
241 lpcchValueName = new IntByReference(16384);
242 lpType = new IntByReference();
243 lpData = new byte[1];
244 lpcbData = new IntByReference();
245 lpcbData.setValue(0);
246
247 dwIndex = 0;
248
249 OUTER:
250 while (true) {
251 result = Advapi32.INSTANCE.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null,
252 lpType, lpData, lpcbData);
253 switch (result) {
254 case WINERROR.ERROR_NO_MORE_ITEMS:
255 return values;
256
257 case WINERROR.ERROR_MORE_DATA:
258 lpData = new byte[lpcbData.getValue()];
259 lpcchValueName = new IntByReference(16384);
260 continue OUTER;
261
262 case WINERROR.ERROR_SUCCESS:
263 name = new String(lpValueName, 0, lpcchValueName.getValue());
264
265 switch (lpType.getValue()) {
266 case WINNT.REG_SZ:
267 values.put(name, convertBufferToString(lpData));
268 break;
269 case WINNT.REG_DWORD:
270 values.put(name, convertBufferToInt(lpData));
271 break;
272 default:
273 break; // not supported yet
274 }
275 break;
276
277 default:
278 check(result);
279 }
280 dwIndex++;
281 lpcbData.setValue(0);
282 }
283 }
284
285
286 @Override
287 protected void finalize() throws Throwable {
288 super.finalize();
289 dispose();
290 }
291
292 public void dispose() {
293 if(handle!=0)
294 Advapi32.INSTANCE.RegCloseKey(handle);
295 handle = 0;
296 }
297
298 //
299 // Root keys
300 //
301 public static final RegistryKey CLASSES_ROOT = new RegistryKey(0x80000000);
302 public static final RegistryKey CURRENT_USER = new RegistryKey(0x80000001);
303 public static final RegistryKey LOCAL_MACHINE = new RegistryKey(0x80000002);
304 public static final RegistryKey USERS = new RegistryKey(0x80000003);
305 }
306