001: /*
002: ** Java native interface to the Windows Registry API.
003: ** Copyright (c) 1997 by Timothy Gerard Endres
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** General Public License as published by the Free Software Foundation.
009: ** Version 2 of the license should be included with this distribution in
010: ** the file LICENSE, as well as License.html. If the license is not
011: ** included with this distribution, you may find a copy at the FSF web
012: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
013: ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
014: **
015: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
016: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
017: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
018: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
019: ** REDISTRIBUTION OF THIS SOFTWARE.
020: **
021: */
022:
023: package com.ice.jni.registry;
024:
025: import com.memoire.vainstall.VAGlobals;
026: import java.io.*;
027: import java.util.*;
028: import com.ice.text.HexNumberFormat;
029: import com.ice.util.HexDump;
030: import com.ice.util.StringUtilities;
031:
032: /**
033: * The Registry class provides is used to load the native
034: * library DLL, as well as a placeholder for the top level
035: * keys, error codes, and utility methods.
036: *
037: */
038:
039: public class Registry {
040: /**
041: * The following statics are the top level keys.
042: * Without these, there is no way to get "into"
043: * the registry, since the RegOpenSubkey() call
044: * requires an existing key which contains the
045: * subkey.
046: */
047: public static RegistryKey HKEY_CLASSES_ROOT;
048: public static RegistryKey HKEY_CURRENT_USER;
049: public static RegistryKey HKEY_LOCAL_MACHINE;
050: public static RegistryKey HKEY_USERS;
051: public static RegistryKey HKEY_PERFORMANCE_DATA;
052: public static RegistryKey HKEY_CURRENT_CONFIG;
053: public static RegistryKey HKEY_DYN_DATA;
054:
055: /**
056: * This is a key for ICE's testing purposes.
057: */
058: private static RegistryKey HKEY_ICE_TESTKEY = null;
059: /**
060: * These are predefined keys ($0-$9) used to make the
061: * testing program easier to use (less typing).
062: */
063: private static String[] preDefines;
064:
065: /**
066: * These are the Registry API error codes, which can
067: * be returned via the RegistryException.
068: */
069: public static final int ERROR_SUCCESS = 0;
070: public static final int ERROR_FILE_NOT_FOUND = 2;
071: public static final int ERROR_ACCESS_DENIED = 5;
072: public static final int ERROR_INVALID_HANDLE = 6;
073: public static final int ERROR_INVALID_PARAMETER = 87;
074: public static final int ERROR_CALL_NOT_IMPLEMENTED = 120;
075: public static final int ERROR_INSUFFICIENT_BUFFER = 122;
076: public static final int ERROR_LOCK_FAILED = 167;
077: public static final int ERROR_TRANSFER_TOO_LONG = 222;
078: public static final int ERROR_MORE_DATA = 234;
079: public static final int ERROR_NO_MORE_ITEMS = 259;
080: public static final int ERROR_BADDB = 1009;
081: public static final int ERROR_BADKEY = 1010;
082: public static final int ERROR_CANTOPEN = 1011;
083: public static final int ERROR_CANTREAD = 1012;
084: public static final int ERROR_CANTWRITE = 1013;
085: public static final int ERROR_REGISTRY_RECOVERED = 1014;
086: public static final int ERROR_REGISTRY_CORRUPT = 1015;
087: public static final int ERROR_REGISTRY_IO_FAILED = 1016;
088: public static final int ERROR_NOT_REGISTRY_FILE = 1017;
089: public static final int ERROR_KEY_DELETED = 1018;
090:
091: /**
092: * This is the last key used by the test program ($$).
093: */
094: private static String saveKey = null;
095: /**
096: * This is a Hashtable which maps nams to the top level keys.
097: */
098: private static Hashtable topLevelKeys = null;
099:
100: /**
101: * If true, debug the fv parameters and computation.
102: */
103: public boolean debugLevel;
104:
105: /**
106: * VAInstall change : add dllFile
107: * VAInstall: dllFile must be set by AbstractInstall
108: */
109: public static String dllFile = VAGlobals.JNI_DLL_FILE;
110: /**
111: * Loads the DLL needed for the native methods, creates the
112: * toplevel keys, fills the hashtable that maps various names
113: * to the toplevel keys.
114: * VAInstall: The dll File is not in the library path.
115: System.load() must be used
116: */
117: static {
118: //VAInstall Change : begin
119: if (dllFile == null)
120: System.err.println("ERROR temporary dll File is null");
121: try {
122: System.load(dllFile);
123: } catch (UnsatisfiedLinkError e) {
124: System.err.println("ERROR '" + dllFile + "' not found.\n\t"
125: + e.getMessage());
126: } catch (SecurityException e) {
127: System.err
128: .println("ERROR You do not have permission to load the DLL named '"
129: + dllFile + "'.\n\t" + e.getMessage());
130: }
131: //VAInstall Change : end
132: Registry.HKEY_CLASSES_ROOT = new RegistryKey(0x80000000,
133: "HKEY_CLASSES_ROOT");
134:
135: Registry.HKEY_CURRENT_USER = new RegistryKey(0x80000001,
136: "HKEY_CURRENT_USER");
137:
138: Registry.HKEY_LOCAL_MACHINE = new RegistryKey(0x80000002,
139: "HKEY_LOCAL_MACHINE");
140:
141: Registry.HKEY_USERS = new RegistryKey(0x80000003, "HKEY_USERS");
142:
143: Registry.HKEY_PERFORMANCE_DATA = new RegistryKey(0x80000004,
144: "HKEY_PERFORMANCE_DATA");
145:
146: Registry.HKEY_CURRENT_CONFIG = new RegistryKey(0x80000005,
147: "HKEY_CURRENT_CONFIG");
148:
149: Registry.HKEY_DYN_DATA = new RegistryKey(0x80000006,
150: "HKEY_DYN_DATA");
151:
152: Registry.topLevelKeys = new Hashtable(16);
153:
154: topLevelKeys.put("HKCR", Registry.HKEY_CLASSES_ROOT);
155: topLevelKeys.put("HKEY_CLASSES_ROOT",
156: Registry.HKEY_CLASSES_ROOT);
157:
158: topLevelKeys.put("HKCU", Registry.HKEY_CURRENT_USER);
159: topLevelKeys.put("HKEY_CURRENT_USER",
160: Registry.HKEY_CURRENT_USER);
161:
162: topLevelKeys.put("HKLM", Registry.HKEY_LOCAL_MACHINE);
163: topLevelKeys.put("HKEY_LOCAL_MACHINE",
164: Registry.HKEY_LOCAL_MACHINE);
165:
166: topLevelKeys.put("HKU", Registry.HKEY_USERS);
167: topLevelKeys.put("HKUS", Registry.HKEY_USERS);
168: topLevelKeys.put("HKEY_USERS", Registry.HKEY_USERS);
169:
170: topLevelKeys.put("HKPD", Registry.HKEY_PERFORMANCE_DATA);
171: topLevelKeys.put("HKEY_PERFORMANCE_DATA",
172: Registry.HKEY_PERFORMANCE_DATA);
173:
174: topLevelKeys.put("HKCC", Registry.HKEY_PERFORMANCE_DATA);
175: topLevelKeys.put("HKEY_CURRENT_CONFIG",
176: Registry.HKEY_PERFORMANCE_DATA);
177:
178: topLevelKeys.put("HKDD", Registry.HKEY_PERFORMANCE_DATA);
179: topLevelKeys.put("HKEY_DYN_DATA",
180: Registry.HKEY_PERFORMANCE_DATA);
181: }
182:
183: /**
184: * Get a top level key by name using the top level key Hashtable.
185: *
186: * @param keyName The name of the top level key.
187: * @return The top level RegistryKey, or null if unknown keyName.
188: *
189: * @see topLevelKeys
190: */
191:
192: public static RegistryKey getTopLevelKey(String keyName) {
193: return (RegistryKey) Registry.topLevelKeys.get(keyName);
194: }
195:
196: /**
197: * Open a subkey of a given top level key.
198: *
199: * @param topKey The top level key containing the subkey.
200: * @param keyName The subkey's name.
201: * @param access The access flag for the newly opened key.
202: * @return The newly opened RegistryKey.
203: *
204: * @see RegistryKey
205: */
206:
207: public static RegistryKey openSubkey(RegistryKey topKey,
208: String keyName, int access) {
209: RegistryKey subKey = null;
210:
211: try {
212: subKey = topKey.openSubKey(keyName, access);
213: } catch (NoSuchKeyException ex) {
214: subKey = null;
215: } catch (RegistryException ex) {
216: subKey = null;
217: }
218:
219: return subKey;
220: }
221:
222: /**
223: * Get the description of a Registry error code.
224: *
225: * @param errCode The error code from a RegistryException
226: * @return The description of the error code.
227: */
228:
229: public static String getErrorMessage(int errCode) {
230: switch (errCode) {
231: case ERROR_SUCCESS:
232: return "success";
233: case ERROR_FILE_NOT_FOUND:
234: return "key or value not found";
235: case ERROR_ACCESS_DENIED:
236: return "access denied";
237: case ERROR_INVALID_HANDLE:
238: return "invalid handle";
239: case ERROR_INVALID_PARAMETER:
240: return "invalid parameter";
241: case ERROR_CALL_NOT_IMPLEMENTED:
242: return "call not implemented";
243: case ERROR_INSUFFICIENT_BUFFER:
244: return "insufficient buffer";
245: case ERROR_LOCK_FAILED:
246: return "lock failed";
247: case ERROR_TRANSFER_TOO_LONG:
248: return "transfer was too long";
249: case ERROR_MORE_DATA:
250: return "more data buffer needed";
251: case ERROR_NO_MORE_ITEMS:
252: return "no more items";
253: case ERROR_BADDB:
254: return "bad database";
255: case ERROR_BADKEY:
256: return "bad key";
257: case ERROR_CANTOPEN:
258: return "can not open";
259: case ERROR_CANTREAD:
260: return "can not read";
261: case ERROR_CANTWRITE:
262: return "can not write";
263: case ERROR_REGISTRY_RECOVERED:
264: return "registry recovered";
265: case ERROR_REGISTRY_CORRUPT:
266: return "registry corrupt";
267: case ERROR_REGISTRY_IO_FAILED:
268: return "registry IO failed";
269: case ERROR_NOT_REGISTRY_FILE:
270: return "not a registry file";
271: case ERROR_KEY_DELETED:
272: return "key has been deleted";
273: }
274:
275: return "errCode=" + errCode;
276: }
277:
278: /**
279: * Export the textual definition for a registry key to a file.
280: * The resulting file can be re-loaded via RegEdit.
281: *
282: * @param pathName The pathname of the file into which to export.
283: * @param key The registry key definition to export.
284: * @param descend If true, descend and export all subkeys.
285: *
286: * @exception NoSuchKeyException Thrown by openSubKey().
287: * @exception RegistryException Any other registry API error.
288: */
289:
290: public static void exportRegistryKey(String pathName,
291: RegistryKey key, boolean descend)
292: throws java.io.IOException, NoSuchKeyException,
293: RegistryException {
294: PrintWriter out = new PrintWriter(new FileWriter(pathName));
295:
296: out.println("REGEDIT4");
297: out.println("");
298:
299: key.export(out, descend);
300:
301: out.flush();
302: out.close();
303: }
304:
305: /**
306: * The main() method is used to test the Registry package.
307: */
308:
309: public static void main(String argv[]) {
310: Registry.preDefines = new String[10];
311:
312: Registry.preDefines[0] = "HKLM\\System\\CurrentControlSet\\control";
313: Registry.preDefines[1] = "HKLM\\Software";
314: Registry.preDefines[2] = "HKLM\\Software\\Miscrosoft";
315: Registry.preDefines[3] = "HKLM\\Software\\Microsoft\\Windows"
316: + "\\CurrentVersion";
317: Registry.preDefines[4] = "HKLM\\Software\\Microsoft\\Windows"
318: + "\\CurrentVersion\\ProfileList";
319: Registry.preDefines[5] = "HKCU\\Software";
320: Registry.preDefines[6] = "HKCU\\Software\\Microsoft";
321: Registry.preDefines[7] = "HKCU\\AppEvents";
322: Registry.preDefines[8] = "HKCU\\AppEvents\\Schemes";
323: Registry.preDefines[9] = "HKCU\\AppEvents\\Schemes";
324:
325: try {
326: Registry.HKEY_ICE_TESTKEY = Registry.HKEY_CURRENT_USER
327: .openSubKey("Software\\ICE Engineering\\test");
328: } catch (NoSuchKeyException ex) {
329: } catch (RegistryException ex) {
330: }
331:
332: if (argv.length > 0) {
333: Registry.subMain(argv);
334: } else {
335: String inLine;
336: String saveLine = null;
337: BufferedReader input = new BufferedReader(
338: new InputStreamReader(System.in));
339:
340: for (;;) {
341: System.out.print("command: ");
342: System.out.flush();
343:
344: try {
345: inLine = input.readLine();
346: } catch (IOException ex) {
347: inLine = null;
348: }
349:
350: if (inLine == null || inLine.length() == 0)
351: break;
352:
353: if (inLine.equalsIgnoreCase("help")) {
354: Registry.usage(null);
355: continue;
356: }
357:
358: String[] subArgs;
359: if (inLine.equals("!!") && saveLine != null) {
360: subArgs = StringUtilities
361: .parseArgumentString(saveLine);
362: } else {
363: subArgs = StringUtilities
364: .parseArgumentString(inLine);
365: saveLine = inLine;
366: }
367:
368: Registry.subMain(subArgs);
369: }
370: }
371: }
372:
373: /**
374: * Print the usage/help information.
375: */
376:
377: public static void usage(String message) {
378: if (message != null)
379: System.err.println(message);
380:
381: System.err.println("keys regKey -- print the key names");
382: System.err.println("values regKey -- print the value names");
383: System.err.println("data regKey -- print the key's data");
384: System.err
385: .println("string regKey -- print REG_SZ key's string");
386: System.err
387: .println("setbin regKey valName binaryString -- set REG_BINARY");
388: System.err.println("setdw regKey valName int -- set REG_DWORD");
389: System.err
390: .println("setstr regKey valName string -- set REG_SZ");
391: System.err
392: .println("setmulti regKey valName semiColonString -- set REG_MULTI_SZ");
393: System.err
394: .println("delkey regKey subKey -- delete key 'subKey' of regKey");
395: System.err
396: .println("delval regKey valueName -- delete value 'valueName' of regKey");
397: System.err
398: .println("export regKey fileName -- export registry key to fileName");
399: System.err
400: .println("expand regKey valueName -- expand string value");
401:
402: System.err.println("");
403:
404: System.err.println("!! -- repeats last command");
405: System.err.println("$$ -- re-uses previous keyname");
406: System.err.println("Predefined Key Prefixes: (e.g. $0-9)");
407: for (int idx = 0; idx < Registry.preDefines.length; ++idx)
408: System.err.println(" $" + idx + "="
409: + Registry.preDefines[idx]);
410: }
411:
412: /**
413: * The actual main method, which is called for each command.
414: */
415:
416: public static void subMain(String argv[]) {
417: int index;
418: RegistryKey key;
419: RegistryKey subKey;
420: RegistryKey topKey = null;
421: boolean isRemote = false;
422: String topKeyName = null;
423: String hostName = null;
424:
425: if (argv.length < 1 || argv[0].equals("help")) {
426: Registry.usage(null);
427: return;
428: }
429:
430: if (argv.length < 2) {
431: Registry.usage(null);
432: return;
433: }
434:
435: String keyName = argv[1];
436:
437: if (Registry.saveKey != null && keyName.equals("$$")) {
438: keyName = Registry.saveKey;
439: } else if (keyName.equals("@@")) {
440: keyName = "HKCU\\Software\\ICE Engineering\\test";
441: } else {
442: char ch1 = keyName.charAt(0);
443: char ch2 = keyName.charAt(1);
444:
445: if (ch1 == '$' && ch2 >= '0' && ch2 <= '9') {
446: int pIdx = (ch2 - '0');
447: if (Registry.preDefines[pIdx] != null) {
448: if (keyName.length() < 3)
449: keyName = Registry.preDefines[pIdx];
450: else
451: keyName = Registry.preDefines[pIdx]
452: + keyName.substring(2);
453: } else {
454: System.err.println("Predefine '" + keyName
455: + "' not defined.");
456: return;
457: }
458: } else {
459: Registry.saveKey = argv[1];
460: }
461: }
462:
463: if (keyName.startsWith("\\\\")) {
464: isRemote = true;
465: index = keyName.indexOf('\\', 2);
466: hostName = keyName.substring(2, index);
467: keyName = keyName.substring(index + 1);
468: }
469:
470: index = keyName.indexOf('\\');
471:
472: if (index < 0) {
473: //
474: // "topLevelKeyname"
475: //
476: topKeyName = keyName;
477: keyName = null;
478: } else if (index < 4) {
479: //
480: // INVALID KEYNAME, topLevelName too short
481: //
482: System.err.println("Invalid key '" + keyName
483: + "', top level key name too short.");
484: return;
485: } else {
486: //
487: // "topLevelKeyname\subKey\subKey\..."
488: //
489: topKeyName = keyName.substring(0, index);
490:
491: if ((index + 1) >= keyName.length())
492: keyName = null;
493: else
494: keyName = keyName.substring(index + 1);
495: }
496:
497: topKey = Registry.getTopLevelKey(topKeyName);
498: if (topKey == null) {
499: System.err.println("ERROR, toplevel key '" + topKeyName
500: + "' not resolved!");
501: return;
502: }
503:
504: if (isRemote) {
505: System.err.println("REMOTE Key host='" + hostName + "'");
506:
507: RegistryKey remoteKey = null;
508:
509: try {
510: remoteKey = topKey.connectRegistry(hostName);
511: } catch (NoSuchKeyException ex) {
512: System.err.println("ERROR No such key connecting to '"
513: + hostName + "', " + ex.getMessage());
514: return;
515: } catch (RegistryException ex) {
516: System.err.println("ERROR errCode=" + ex.getErrorCode()
517: + "' connecting to '" + hostName + "', "
518: + ex.getMessage());
519: return;
520: }
521:
522: if (remoteKey != null) {
523: topKey = remoteKey;
524: }
525: }
526:
527: //
528: // P R O C E S S C O M M A N D S
529: //
530:
531: if (argv[0].equalsIgnoreCase("create")) {
532: Registry.createCommand(topKey, keyName);
533: } else if (argv[0].equalsIgnoreCase("setbin")) {
534: Registry
535: .setBinaryCommand(topKey, keyName, argv[2], argv[3]);
536: } else if (argv[0].equalsIgnoreCase("setdw")) {
537: Registry
538: .setBinaryCommand(topKey, keyName, argv[2], argv[3]);
539: } else if (argv[0].equalsIgnoreCase("setstr")) {
540: Registry
541: .setStringCommand(topKey, keyName, argv[2], argv[3]);
542: } else if (argv[0].equalsIgnoreCase("setmulti")) {
543: Registry.setMultiStringCommand(topKey, keyName, argv[2],
544: argv[3]);
545: } else if (argv[0].equalsIgnoreCase("keys")) {
546: Registry.listKeysCommand(topKey, keyName);
547: } else if (argv[0].equalsIgnoreCase("values")) {
548: Registry.listValuesCommand(topKey, keyName);
549: } else if (argv[0].equalsIgnoreCase("delkey")) {
550: Registry.deleteKeyCommand(topKey, keyName, argv[2]);
551: } else if (argv[0].equalsIgnoreCase("delval")) {
552: Registry.deleteValueCommand(topKey, keyName, argv[2]);
553: } else if (argv[0].equalsIgnoreCase("data")) {
554: Registry.getDataCommand(topKey, keyName, argv[2]);
555: } else if (argv[0].equalsIgnoreCase("string")) {
556: Registry.getStringCommand(topKey, keyName, argv[2]);
557: } else if (argv[0].equalsIgnoreCase("export")) {
558: Registry.exportKeyCommand(topKey, keyName, argv[2]);
559: } else if (argv[0].equalsIgnoreCase("expand")) {
560: Registry.expandStringCommand(topKey, keyName, argv[2]);
561: }
562: }
563:
564: private static void exportKeyCommand(RegistryKey topKey,
565: String keyName, String pathName) {
566: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
567: keyName, RegistryKey.ACCESS_READ);
568:
569: if (subKey == null)
570: return;
571:
572: try {
573: Registry.exportRegistryKey(pathName, subKey, true);
574: } catch (IOException ex) {
575: System.err.println("IO Exception: '" + ex.getMessage()
576: + "'");
577: } catch (NoSuchKeyException ex) {
578: System.err
579: .println("Error, encountered non-existent key during export.");
580: } catch (RegistryException ex) {
581: System.err.println("ERROR registry error="
582: + ex.getErrorCode() + ", " + ex.getMessage());
583: }
584: }
585:
586: private static void getDataCommand(RegistryKey topKey,
587: String keyName, String valueName) {
588: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
589: keyName, RegistryKey.ACCESS_READ);
590:
591: if (subKey == null)
592: return;
593:
594: RegistryValue data = null;
595:
596: try {
597: data = subKey.getValue(valueName);
598: } catch (NoSuchValueException ex) {
599: System.err.println("Value '" + valueName
600: + "' does not exist.");
601: return;
602: } catch (RegistryException ex) {
603: System.err.println("ERROR registry error="
604: + ex.getErrorCode() + ", " + ex.getMessage());
605: return;
606: }
607:
608: System.err.println("Value '" + valueName + "' is "
609: + data.toString());
610:
611: if (data instanceof RegStringValue) {
612: RegStringValue val = (RegStringValue) data;
613: System.err.println("REG_SZ '" + val.getData() + "'");
614: } else if (data instanceof RegMultiStringValue) {
615: RegMultiStringValue val = (RegMultiStringValue) data;
616: String[] args = val.getData();
617: for (int idx = 0; idx < args.length; ++idx)
618: System.err.println("REG_MULTI_SZ[" + idx + "] '"
619: + args[idx] + "'");
620: } else if (data instanceof RegDWordValue) {
621: RegDWordValue val = (RegDWordValue) data;
622:
623: HexNumberFormat xFmt = new HexNumberFormat("XXXXXXXX");
624:
625: System.err.println("REG_DWORD"
626: + ((RegistryValue.REG_DWORD_BIG_ENDIAN == val
627: .getType()) ? "_BIG_ENDIAN" : "") + " '"
628: + val.getData() + "' [x"
629: + xFmt.format(val.getData()) + "]");
630: } else {
631: RegBinaryValue val = (RegBinaryValue) data;
632: HexDump.dumpHexData(System.err, "REG_BINARY '"
633: + val.getName() + "'", val.getData(), val
634: .getLength());
635: }
636: }
637:
638: private static void getStringCommand(RegistryKey topKey,
639: String keyName, String valueName) {
640: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
641: keyName, RegistryKey.ACCESS_READ);
642:
643: if (subKey == null)
644: return;
645:
646: try {
647: String value = subKey.getStringValue(valueName);
648: System.err.println("String Value " + valueName + "='"
649: + value + "'");
650: } catch (RegistryException ex) {
651: System.err.println("ERROR getting value '" + valueName
652: + "', " + ex.getMessage());
653: return;
654: }
655: }
656:
657: private static void expandStringCommand(RegistryKey topKey,
658: String keyName, String valueName) {
659: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
660: keyName, RegistryKey.ACCESS_READ);
661:
662: if (subKey == null)
663: return;
664:
665: try {
666: String value = subKey.getStringValue(valueName);
667: System.err.println("String Value " + valueName + "='"
668: + value + "'");
669: value = RegistryKey.expandEnvStrings(value);
670: System.err.println("Expanded Value " + valueName + "='"
671: + value + "'");
672: } catch (RegistryException ex) {
673: System.err.println("ERROR getting value '" + valueName
674: + "', " + ex.getMessage());
675: return;
676: }
677: }
678:
679: private static void deleteKeyCommand(RegistryKey topKey,
680: String keyName, String deleteKeyName) {
681: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
682: keyName, RegistryKey.ACCESS_WRITE);
683:
684: if (subKey == null)
685: return;
686:
687: try {
688: subKey.deleteSubKey(deleteKeyName);
689: } catch (NoSuchKeyException ex) {
690: System.err.println("Key '" + keyName + "\\" + deleteKeyName
691: + "' does not exist.");
692: return;
693: } catch (RegistryException ex) {
694: System.err.println("ERROR deleting key '" + keyName + "', "
695: + ex.getMessage());
696: return;
697: }
698: }
699:
700: private static void deleteValueCommand(RegistryKey topKey,
701: String keyName, String valueName) {
702: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
703: keyName, RegistryKey.ACCESS_WRITE);
704:
705: if (subKey == null)
706: return;
707:
708: try {
709: subKey.deleteValue(valueName);
710: } catch (NoSuchValueException ex) {
711: System.err.println("Value '" + valueName
712: + "' does not exist.");
713: return;
714: } catch (RegistryException ex) {
715: System.err.println("ERROR deleting value '" + valueName
716: + "', " + ex.getMessage());
717: return;
718: }
719: }
720:
721: private static void
722: listKeysCommand( RegistryKey topKey, String keyName )
723: {
724: RegistryKey subKey =
725: Registry.openSubKeyVerbose
726: ( topKey, keyName, RegistryKey.ACCESS_READ );
727:
728: if ( subKey == null )
729: return;
730:
731: try {
732: Enumeration enum = subKey.keyElements();
733: for ( int kIdx = 0 ; enum.hasMoreElements() ; ++kIdx )
734: {
735: String keyStr = (String) enum.nextElement();
736: System.err.println
737: ( "Subkey[" + kIdx + "] = '" + keyStr + "'" );
738: }
739: }
740: catch ( RegistryException ex )
741: {
742: System.err.println
743: ( "ERROR getting key enumerator, "
744: + ex.getMessage() );
745: return;
746: }
747: }
748:
749: private static void
750: listValuesCommand( RegistryKey topKey, String keyName )
751: {
752: RegistryKey subKey =
753: Registry.openSubKeyVerbose
754: ( topKey, keyName, RegistryKey.ACCESS_READ );
755:
756: if ( subKey == null )
757: return;
758:
759: try {
760: Enumeration enum = subKey.valueElements();
761: for ( int kIdx = 0 ; enum.hasMoreElements() ; ++kIdx )
762: {
763: String name = (String) enum.nextElement();
764: System.err.println
765: ( "Value Name[" + kIdx + "] = '" + name + "'" );
766: }
767: }
768: catch ( RegistryException ex )
769: {
770: System.err.println
771: ( "ERROR getting value enumerator, "
772: + ex.getMessage() );
773: return;
774: }
775: }
776:
777: private static void setDWordCommand(RegistryKey topKey,
778: String keyName, String valueName, String data) {
779: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
780: keyName, RegistryKey.ACCESS_WRITE);
781:
782: if (subKey == null)
783: return;
784:
785: int anInt;
786: try {
787: anInt = Integer.parseInt(data);
788: } catch (NumberFormatException ex) {
789: System.err.println("ERROR bad int: '" + ex.getMessage()
790: + "'");
791: return;
792: }
793:
794: RegDWordValue val = new RegDWordValue(subKey, valueName);
795: val.setData(anInt);
796:
797: Registry.setValue(subKey, val);
798: }
799:
800: private static void setMultiStringCommand(RegistryKey topKey,
801: String keyName, String valueName, String data) {
802: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
803: keyName, RegistryKey.ACCESS_WRITE);
804:
805: if (subKey == null)
806: return;
807:
808: String[] strArray = StringUtilities.splitString(data, ";");
809:
810: RegMultiStringValue val = new RegMultiStringValue(subKey,
811: valueName, strArray);
812:
813: Registry.setValue(subKey, val);
814: }
815:
816: private static void setStringCommand(RegistryKey topKey,
817: String keyName, String valueName, String data) {
818: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
819: keyName, RegistryKey.ACCESS_WRITE);
820:
821: if (subKey == null)
822: return;
823:
824: RegStringValue val = new RegStringValue(subKey, valueName, data);
825:
826: Registry.setValue(subKey, val);
827: }
828:
829: private static void setBinaryCommand(RegistryKey topKey,
830: String keyName, String valueName, String data) {
831: RegistryKey subKey = Registry.openSubKeyVerbose(topKey,
832: keyName, RegistryKey.ACCESS_WRITE);
833:
834: if (subKey == null)
835: return;
836:
837: byte[] binData = data.getBytes();
838:
839: RegBinaryValue val = new RegBinaryValue(subKey, valueName,
840: binData);
841:
842: Registry.setValue(subKey, val);
843: }
844:
845: private static void createCommand(RegistryKey topKey, String keyName) {
846: RegistryKey subKey;
847:
848: try {
849: subKey = topKey.createSubKey(keyName, null,
850: RegistryKey.ACCESS_WRITE);
851: } catch (RegistryException ex) {
852: subKey = null;
853: System.err.println("ERROR creating subKey: "
854: + ex.getMessage());
855: }
856:
857: if (subKey != null) {
858: try {
859: subKey.flushKey();
860: subKey.closeKey();
861: } catch (RegistryException ex) {
862: subKey = null;
863: System.err.println("ERROR flushing and closing key: "
864: + ex.getMessage());
865: }
866: }
867:
868: if (subKey != null) {
869: System.err.println("SUCCEEDED "
870: + (subKey.wasCreated() ? "Creating"
871: : "Opening via create") + " Key '"
872: + keyName + "'");
873: } else {
874: System.err.println("FAILED Creating Key '" + keyName + "'");
875: }
876: }
877:
878: private static RegistryKey openSubKeyVerbose(RegistryKey topKey,
879: String keyName, int access) {
880: RegistryKey subKey = null;
881:
882: try {
883: subKey = topKey.openSubKey(keyName, access);
884: } catch (NoSuchKeyException ex) {
885: subKey = null;
886: System.err.println("Key '" + keyName + "' does not exist.");
887: } catch (RegistryException ex) {
888: subKey = null;
889: System.err.println("ERROR registry error="
890: + ex.getErrorCode() + ", " + ex.getMessage());
891: }
892:
893: return subKey;
894: }
895:
896: private static void setValue(RegistryKey subKey, RegistryValue value) {
897: try {
898: subKey.setValue(value);
899: subKey.flushKey();
900: } catch (RegistryException ex) {
901: System.err.println("ERROR setting MULTI_SZ value '"
902: + value.getName() + "', " + ex.getMessage());
903: }
904: }
905:
906: public static void setDllFile(String _dllFile) {
907: dllFile = _dllFile;
908: }
909:
910: }
|