Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/util/Registry.java


1   /*
2   ** Java native interface to the Windows Registry API.
3   ** Copyright (c) 1997 by Timothy Gerard Endres
4   ** 
5   ** This program is free software.
6   ** 
7   ** You may redistribute it and/or modify it under the terms of the GNU
8   ** General Public License as published by the Free Software Foundation.
9   ** Version 2 of the license should be included with this distribution in
10  ** the file LICENSE, as well as License.html. If the license is not
11  ** included  with this distribution, you may find a copy at the FSF web
12  ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13  ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14  **
15  ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16  ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17  ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18  ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19  ** REDISTRIBUTION OF THIS SOFTWARE. 
20  **
21   * Taken from the original "com.ice.jni.registry.Registry" class.
22   * Modified: 23 March 2003 by Antonio Petrelli 
23  */
24  
25  package javatools.util;
26  
27  import java.io.*;
28  import java.util.*;
29  import com.ice.jni.registry.*;
30  import com.ice.text.HexNumberFormat;
31  import com.ice.util.HexDump;
32  import com.ice.util.StringUtilities;
33  
34  
35  /**
36   * The Registry class provides is used to load the native
37   * library DLL, as well as a placeholder for the top level
38   * keys, error codes, and utility methods.
39   *
40   */
41  
42  public class
43  Registry
44    {
45    /**
46     * The following statics are the top level keys.
47     * Without these, there is no way to get "into"
48     * the registry, since the RegOpenSubkey() call
49     * requires an existing key which contains the
50     * subkey.
51     */
52    public static RegistryKey    HKEY_CLASSES_ROOT;
53    public static RegistryKey    HKEY_CURRENT_USER;
54    public static RegistryKey    HKEY_LOCAL_MACHINE;
55    public static RegistryKey    HKEY_USERS;
56    public static RegistryKey    HKEY_PERFORMANCE_DATA;
57    public static RegistryKey    HKEY_CURRENT_CONFIG;
58    public static RegistryKey    HKEY_DYN_DATA;
59  
60    /**
61     * This is a key for ICE's testing purposes.
62     */
63    private static RegistryKey    HKEY_ICE_TESTKEY = null;
64    /**
65     * These are predefined keys ($0-$9) used to make the
66     * testing program easier to use (less typing).
67     */
68    private static String[]      preDefines;
69  
70    /**
71     * These are the Registry API error codes, which can
72     * be returned via the RegistryException.
73     */
74    public static final int      ERROR_SUCCESS = 0;
75    public static final int      ERROR_FILE_NOT_FOUND = 2;
76    public static final int      ERROR_ACCESS_DENIED = 5;
77    public static final int      ERROR_INVALID_HANDLE = 6;
78    public static final int      ERROR_INVALID_PARAMETER = 87;
79    public static final int      ERROR_CALL_NOT_IMPLEMENTED = 120;
80    public static final int      ERROR_INSUFFICIENT_BUFFER = 122;
81    public static final int      ERROR_LOCK_FAILED = 167;
82    public static final int      ERROR_TRANSFER_TOO_LONG = 222;
83    public static final int      ERROR_MORE_DATA = 234;
84    public static final int      ERROR_NO_MORE_ITEMS = 259;
85    public static final int      ERROR_BADDB = 1009;
86    public static final int      ERROR_BADKEY = 1010;
87    public static final int      ERROR_CANTOPEN = 1011;
88    public static final int      ERROR_CANTREAD = 1012;
89    public static final int      ERROR_CANTWRITE = 1013;
90    public static final int      ERROR_REGISTRY_RECOVERED = 1014;
91    public static final int      ERROR_REGISTRY_CORRUPT = 1015;
92    public static final int      ERROR_REGISTRY_IO_FAILED = 1016;
93    public static final int      ERROR_NOT_REGISTRY_FILE = 1017;
94    public static final int      ERROR_KEY_DELETED = 1018;
95  
96    /**
97     * This is the last key used by the test program ($$).
98     */
99    private static String      saveKey = null;
100   /**
101    * This is a Hashtable which maps nams to the top level keys.
102    */
103   private static Hashtable    topLevelKeys = null;
104 
105 
106   /**
107    * If true, debug the fv parameters and computation.
108    */
109   public boolean    debugLevel;
110 
111   /**
112    * Loads the DLL needed for the native methods, creates the
113    * toplevel keys, fills the hashtable that maps various names
114    * to the toplevel keys.
115    */
116 
117   static {
118     Registry.HKEY_CLASSES_ROOT =
119       new RegistryKey( 0x80000000, "HKEY_CLASSES_ROOT" );
120 
121     Registry.HKEY_CURRENT_USER =
122       new RegistryKey( 0x80000001, "HKEY_CURRENT_USER" );
123 
124     Registry.HKEY_LOCAL_MACHINE =
125       new RegistryKey( 0x80000002, "HKEY_LOCAL_MACHINE" );
126 
127     Registry.HKEY_USERS =
128       new RegistryKey( 0x80000003, "HKEY_USERS" );
129 
130     Registry.HKEY_PERFORMANCE_DATA =
131       new RegistryKey( 0x80000004, "HKEY_PERFORMANCE_DATA" );
132 
133     Registry.HKEY_CURRENT_CONFIG =
134       new RegistryKey( 0x80000005, "HKEY_CURRENT_CONFIG" );
135 
136     Registry.HKEY_DYN_DATA =
137       new RegistryKey( 0x80000006, "HKEY_DYN_DATA" );
138 
139 
140     Registry.topLevelKeys = new Hashtable( 16 );
141 
142     topLevelKeys.put( "HKCR",          Registry.HKEY_CLASSES_ROOT );
143     topLevelKeys.put( "HKEY_CLASSES_ROOT",    Registry.HKEY_CLASSES_ROOT );
144 
145     topLevelKeys.put( "HKCU",          Registry.HKEY_CURRENT_USER );
146     topLevelKeys.put( "HKEY_CURRENT_USER",    Registry.HKEY_CURRENT_USER );
147 
148     topLevelKeys.put( "HKLM",          Registry.HKEY_LOCAL_MACHINE );
149     topLevelKeys.put( "HKEY_LOCAL_MACHINE",    Registry.HKEY_LOCAL_MACHINE );
150 
151     topLevelKeys.put( "HKU",          Registry.HKEY_USERS );
152     topLevelKeys.put( "HKUS",          Registry.HKEY_USERS );
153     topLevelKeys.put( "HKEY_USERS",        Registry.HKEY_USERS );
154 
155     topLevelKeys.put( "HKPD",          Registry.HKEY_PERFORMANCE_DATA );
156     topLevelKeys.put( "HKEY_PERFORMANCE_DATA",  Registry.HKEY_PERFORMANCE_DATA );
157 
158     topLevelKeys.put( "HKCC",          Registry.HKEY_PERFORMANCE_DATA );
159     topLevelKeys.put( "HKEY_CURRENT_CONFIG",  Registry.HKEY_PERFORMANCE_DATA );
160 
161     topLevelKeys.put( "HKDD",          Registry.HKEY_PERFORMANCE_DATA );
162     topLevelKeys.put( "HKEY_DYN_DATA",      Registry.HKEY_PERFORMANCE_DATA );
163     }
164 
165   /**
166    * Get a top level key by name using the top level key Hashtable.
167    *
168    * @param keyName The name of the top level key.
169    * @return The top level RegistryKey, or null if unknown keyName.
170    *
171    * @see topLevelKeys
172    */
173 
174   public static RegistryKey
175   getTopLevelKey( String keyName )
176     {
177     return (RegistryKey)
178       Registry.topLevelKeys.get( keyName );
179     }
180 
181   /**
182    * Open a subkey of a given top level key.
183    *
184    * @param topKey The top level key containing the subkey.
185    * @param keyName The subkey's name.
186    * @param access The access flag for the newly opened key.
187    * @return The newly opened RegistryKey.
188    *
189    * @see RegistryKey
190    */
191 
192   public static RegistryKey
193   openSubkey( RegistryKey topKey, String keyName, int access )
194     {
195     RegistryKey    subKey = null;
196 
197     try { subKey = topKey.openSubKey( keyName, access ); }
198     catch ( NoSuchKeyException ex )
199       {
200       subKey = null;
201       }
202     catch ( RegistryException ex )
203       {
204       subKey = null;
205       }
206 
207     return subKey;
208     }
209 
210   /**
211    * Get the description of a Registry error code.
212    *
213    * @param errCode The error code from a RegistryException
214    * @return The description of the error code.
215    */
216 
217   public static String
218   getErrorMessage( int errCode )
219     {
220     switch ( errCode )
221       {
222       case ERROR_SUCCESS: return "success";
223       case ERROR_FILE_NOT_FOUND: return "key or value not found";
224       case ERROR_ACCESS_DENIED: return "access denied";
225       case ERROR_INVALID_HANDLE: return "invalid handle";
226       case ERROR_INVALID_PARAMETER: return "invalid parameter";
227       case ERROR_CALL_NOT_IMPLEMENTED: return "call not implemented";
228       case ERROR_INSUFFICIENT_BUFFER: return "insufficient buffer";
229       case ERROR_LOCK_FAILED: return "lock failed";
230       case ERROR_TRANSFER_TOO_LONG: return "transfer was too long";
231       case ERROR_MORE_DATA: return "more data buffer needed";
232       case ERROR_NO_MORE_ITEMS: return "no more items";
233       case ERROR_BADDB: return "bad database";
234       case ERROR_BADKEY: return "bad key";
235       case ERROR_CANTOPEN: return "can not open";
236       case ERROR_CANTREAD: return "can not read";
237       case ERROR_CANTWRITE: return "can not write";
238       case ERROR_REGISTRY_RECOVERED: return "registry recovered";
239       case ERROR_REGISTRY_CORRUPT: return "registry corrupt";
240       case ERROR_REGISTRY_IO_FAILED: return "registry IO failed";
241       case ERROR_NOT_REGISTRY_FILE: return "not a registry file";
242       case ERROR_KEY_DELETED: return "key has been deleted";
243       }
244 
245     return "errCode=" + errCode;
246     }
247 
248   /**
249    * Export the textual definition for a registry key to a file.
250    * The resulting file can be re-loaded via RegEdit.
251    *
252    * @param pathName The pathname of the file into which to export.
253    * @param key The registry key definition to export.
254    * @param descend If true, descend and export all subkeys.
255    *
256      * @exception  NoSuchKeyException  Thrown by openSubKey().
257      * @exception  RegistryException  Any other registry API error.
258    */
259 
260   public static void
261   exportRegistryKey( String pathName, RegistryKey key, boolean descend )
262     throws java.io.IOException, NoSuchKeyException, RegistryException
263     {
264     PrintWriter out =
265       new PrintWriter(
266         new FileWriter( pathName ) );
267 
268     out.println( "REGEDIT4" );
269     out.println( "" );
270 
271     key.export( out, descend );
272 
273     out.flush();
274     out.close();
275     }
276 
277   /**
278    * The main() method is used to test the Registry package.
279    */
280 
281   public static void
282   main( String argv[] )
283     {
284     Registry.preDefines = new String[10];
285 
286     Registry.preDefines[0] = "HKLM\\System\\CurrentControlSet\\control";
287     Registry.preDefines[1] = "HKLM\\Software";
288     Registry.preDefines[2] = "HKLM\\Software\\Miscrosoft";
289     Registry.preDefines[3] = "HKLM\\Software\\Microsoft\\Windows"
290                   + "\\CurrentVersion";
291     Registry.preDefines[4] = "HKLM\\Software\\Microsoft\\Windows"
292                   + "\\CurrentVersion\\ProfileList";
293     Registry.preDefines[5] = "HKCU\\Software";
294     Registry.preDefines[6] = "HKCU\\Software\\Microsoft";
295     Registry.preDefines[7] = "HKCU\\AppEvents";
296     Registry.preDefines[8] = "HKCU\\AppEvents\\Schemes";
297     Registry.preDefines[9] = "HKCU\\AppEvents\\Schemes";
298 
299     try {
300       Registry.HKEY_ICE_TESTKEY =
301         Registry.HKEY_CURRENT_USER.openSubKey
302           ( "Software\\ICE Engineering\\test" );
303       }
304     catch ( NoSuchKeyException ex )
305       {
306       }
307     catch ( RegistryException ex )
308       {
309       }
310 
311     if ( argv.length > 0 )
312       {
313       Registry.subMain( argv );
314       }
315     else
316       {
317       String inLine;
318       String saveLine = null;
319       BufferedReader input =
320         new BufferedReader
321           ( new InputStreamReader( System.in ) );
322 
323       for ( ; ; )
324         {
325         System.out.print( "command: " );
326         System.out.flush();
327 
328         try { inLine = input.readLine(); }
329           catch ( IOException ex )
330             { inLine = null; }
331         
332         if ( inLine == null || inLine.length() == 0 )
333           break;
334         
335         if ( inLine.equalsIgnoreCase( "help" ) )
336           {
337           Registry.usage( null );
338           continue;
339           }
340 
341         String[] subArgs;
342         if ( inLine.equals( "!!" ) && saveLine != null )
343           {
344           subArgs =
345             StringUtilities.parseArgumentString( saveLine );
346           }
347         else
348           {
349           subArgs =
350             StringUtilities.parseArgumentString( inLine );
351           saveLine = inLine;
352           }
353 
354         Registry.subMain( subArgs );
355         }
356       }
357     }
358 
359   /**
360    * Print the usage/help information.
361    */
362 
363   public static void
364   usage( String message )
365     {
366     if ( message != null )
367       System.err.println( message );
368 
369     System.err.println
370       ( "keys regKey -- print the key names" );
371     System.err.println
372       ( "values regKey -- print the value names" );
373     System.err.println
374       ( "data regKey -- print the key's data" );
375     System.err.println
376       ( "string regKey -- print REG_SZ key's string" );
377     System.err.println
378       ( "setbin regKey valName binaryString -- set REG_BINARY" );
379     System.err.println
380       ( "setdw regKey valName int -- set REG_DWORD" );
381     System.err.println
382       ( "setstr regKey valName string -- set REG_SZ" );
383     System.err.println
384       ( "setmulti regKey valName semiColonString -- set REG_MULTI_SZ" );
385     System.err.println
386       ( "delkey regKey subKey -- delete key 'subKey' of regKey" );
387     System.err.println
388       ( "delval regKey valueName -- delete value 'valueName' of regKey" );
389     System.err.println
390       ( "export regKey fileName -- export registry key to fileName" );
391     System.err.println
392       ( "expand regKey valueName -- expand string value" );
393     
394     System.err.println( "" );
395     
396     System.err.println
397       ( "!! -- repeats last command" );
398     System.err.println
399       ( "$$ -- re-uses previous keyname" );
400     System.err.println
401       ( "Predefined Key Prefixes: (e.g. $0-9)" );
402     for ( int idx = 0 ; idx < Registry.preDefines.length ; ++idx )
403       System.err.println
404         ( "   $" + idx + "=" + Registry.preDefines[idx] );
405     }
406 
407   /**
408    * The actual main method, which is called for each command.
409    */
410 
411   public static void
412   subMain( String argv[] )
413     {
414     int        index;
415     RegistryKey    key;
416     RegistryKey    subKey;
417     RegistryKey    topKey = null;
418     boolean      isRemote = false;
419     String      topKeyName = null;
420     String      hostName = null;
421 
422     if ( argv.length < 1 || argv[0].equals( "help" ) )
423       {
424       Registry.usage( null );
425       return;
426       }
427 
428     if ( argv.length < 2 )
429       {
430       Registry.usage( null );
431       return;
432       }
433 
434     String keyName = argv[1];
435     
436     if ( Registry.saveKey != null
437         && keyName.equals( "$$" ) )
438       {
439       keyName = Registry.saveKey;
440       }
441     else if ( keyName.equals( "@@" ) )
442       {
443       keyName = "HKCU\\Software\\ICE Engineering\\test";
444       }
445     else
446       {
447       char ch1 = keyName.charAt(0);
448       char ch2 = keyName.charAt(1);
449 
450       if ( ch1 == '$' && ch2 >= '0' && ch2 <= '9' )
451         {
452         int pIdx = (ch2 - '0');
453         if ( Registry.preDefines[ pIdx ] != null )
454           {
455           if ( keyName.length() < 3 )
456             keyName = Registry.preDefines[ pIdx ];
457           else
458             keyName =
459               Registry.preDefines[ pIdx ]
460               + keyName.substring( 2 );
461           }
462         else
463           {
464           System.err.println
465             ( "Predefine '" + keyName + "' not defined." );
466           return;
467           }
468         }
469       else
470         {
471         Registry.saveKey = argv[1];
472         }
473       }
474 
475     if ( keyName.startsWith( "\\\\" ) )
476       {
477       isRemote = true;
478       index = keyName.indexOf( '\\', 2 );
479       hostName = keyName.substring( 2, index );
480       keyName = keyName.substring( index + 1 );
481       }
482 
483     index = keyName.indexOf( '\\' );
484 
485     if ( index < 0 )
486       {
487       //
488       // "topLevelKeyname"
489       //
490       topKeyName = keyName;
491       keyName = null;
492       }
493     else if ( index < 4 )
494       {
495       //
496       // INVALID KEYNAME, topLevelName too short
497       //
498       System.err.println
499         ( "Invalid key '" + keyName
500           + "', top level key name too short." );
501       return;
502       }
503     else
504       {
505       //
506       // "topLevelKeyname\subKey\subKey\..."
507       //
508       topKeyName = keyName.substring( 0, index );
509 
510       if ( (index + 1) >= keyName.length() )
511         keyName = null;
512       else
513         keyName = keyName.substring( index + 1 );
514       }
515 
516     topKey = Registry.getTopLevelKey( topKeyName );
517     if ( topKey == null )
518       {
519       System.err.println
520         ( "ERROR, toplevel key '" + topKeyName
521           + "' not resolved!" );
522       return;
523       }
524 
525     if ( isRemote )
526       {
527       System.err.println
528         ( "REMOTE Key host='" + hostName + "'" );
529       
530       RegistryKey remoteKey = null;
531 
532       try {
533         remoteKey = topKey.connectRegistry( hostName );
534         }
535       catch ( NoSuchKeyException ex )
536         {
537         System.err.println
538           ( "ERROR No such key connecting to '"
539             + hostName + "', " + ex.getMessage() );
540         return;
541         }
542       catch ( RegistryException ex )
543         {
544         System.err.println
545           ( "ERROR errCode=" + ex.getErrorCode()
546             + "' connecting to '" + hostName
547             + "', " + ex.getMessage() );
548         return;
549         }
550 
551       if ( remoteKey != null )
552         {
553         topKey = remoteKey;
554         }
555       }
556 
557 
558     //
559     // P R O C E S S    C O M M A N D S
560     //
561 
562     if ( argv[0].equalsIgnoreCase( "create" ) )
563       {
564       Registry.createCommand( topKey, keyName );
565       }
566     else if ( argv[0].equalsIgnoreCase( "setbin" ) )
567       {
568       Registry.setBinaryCommand
569         ( topKey, keyName, argv[2], argv[3] );
570       }
571     else if ( argv[0].equalsIgnoreCase( "setdw" ) )
572       {
573       Registry.setBinaryCommand
574         ( topKey, keyName, argv[2], argv[3] );
575       }
576     else if ( argv[0].equalsIgnoreCase( "setstr" ) )
577       {
578       Registry.setStringCommand
579         ( topKey, keyName, argv[2], argv[3] );
580       }
581     else if ( argv[0].equalsIgnoreCase( "setmulti" ) )
582       {
583       Registry.setMultiStringCommand
584         ( topKey, keyName, argv[2], argv[3] );
585       }
586     else if ( argv[0].equalsIgnoreCase( "keys" ) )
587       {
588       Registry.listKeysCommand( topKey, keyName );
589       }
590     else if ( argv[0].equalsIgnoreCase( "values" ) )
591       {
592       Registry.listValuesCommand( topKey, keyName );
593       }
594     else if ( argv[0].equalsIgnoreCase( "delkey" ) )
595       {
596       Registry.deleteKeyCommand( topKey, keyName, argv[2] );
597       }
598     else if ( argv[0].equalsIgnoreCase( "delval" ) )
599       {
600       Registry.deleteValueCommand( topKey, keyName, argv[2] );
601       }
602     else if ( argv[0].equalsIgnoreCase( "data" ) )
603       {
604       Registry.getDataCommand( topKey, keyName, argv[2] );
605       }
606     else if ( argv[0].equalsIgnoreCase( "string" ) )
607       {
608       Registry.getStringCommand( topKey, keyName, argv[2] );
609       }
610     else if ( argv[0].equalsIgnoreCase( "export" ) )
611       {
612       Registry.exportKeyCommand( topKey, keyName, argv[2] );
613       }
614     else if ( argv[0].equalsIgnoreCase( "expand" ) )
615       {
616       Registry.expandStringCommand( topKey, keyName, argv[2] );
617       }
618     }
619 
620   private static void
621   exportKeyCommand(
622       RegistryKey topKey, String keyName, String pathName )
623     {
624     RegistryKey subKey =
625       Registry.openSubKeyVerbose
626         ( topKey, keyName, RegistryKey.ACCESS_READ );
627 
628     if ( subKey == null )
629       return;
630     
631     try { Registry.exportRegistryKey( pathName, subKey, true ); }
632     catch ( IOException ex )
633       {
634       System.err.println
635         ( "IO Exception: '" + ex.getMessage() + "'" );
636       }
637     catch ( NoSuchKeyException ex )
638       {
639       System.err.println
640         ( "Error, encountered non-existent key during export." );
641       }
642     catch ( RegistryException ex )
643       {
644       System.err.println
645         ( "ERROR registry error=" + ex.getErrorCode()
646           + ", " + ex.getMessage() );
647       }
648     }
649 
650   private static void
651   getDataCommand(
652       RegistryKey topKey, String keyName, String valueName )
653     {
654     RegistryKey subKey =
655       Registry.openSubKeyVerbose
656         ( topKey, keyName, RegistryKey.ACCESS_READ );
657 
658     if ( subKey == null )
659       return;
660     
661     RegistryValue  data = null;
662 
663     try { data = subKey.getValue( valueName ); }
664     catch ( NoSuchValueException ex )
665       {
666       System.err.println
667         ( "Value '" + valueName + "' does not exist." );
668       return;
669       }
670     catch ( RegistryException ex )
671       {
672       System.err.println
673         ( "ERROR registry error=" + ex.getErrorCode()
674           + ", " + ex.getMessage() );
675       return;
676       }
677 
678     System.err.println
679       ( "Value '" + valueName + "' is " + data.toString() );
680 
681     if ( data instanceof RegStringValue )
682       {
683       RegStringValue val = (RegStringValue) data;
684       System.err.println( "REG_SZ '" + val.getData() + "'" );
685       }
686     else if ( data instanceof RegMultiStringValue )
687       {
688       RegMultiStringValue val = (RegMultiStringValue) data;
689       String[] args = val.getData();
690       for ( int idx = 0 ; idx < args.length ; ++idx )
691         System.err.println
692           ( "REG_MULTI_SZ[" + idx + "] '"
693             + args[idx] + "'" );
694       }
695     else if ( data instanceof RegDWordValue )
696       {
697       RegDWordValue val = (RegDWordValue) data;
698 
699       HexNumberFormat xFmt =
700         new HexNumberFormat( "XXXXXXXX" );
701 
702       System.err.println(
703         "REG_DWORD"
704         + ( (RegistryValue.REG_DWORD_BIG_ENDIAN
705             == val.getType())
706               ? "_BIG_ENDIAN" : "" )
707         + " '" + val.getData() + "' [x"
708         + xFmt.format( val.getData() ) + "]" );
709       }
710     else
711       {
712       RegBinaryValue val = (RegBinaryValue) data;
713       HexDump.dumpHexData
714         ( System.err,
715           "REG_BINARY '" + val.getName() + "'",
716           val.getData(), val.getLength() );
717       }
718     }
719 
720   private static void
721   getStringCommand(
722       RegistryKey topKey, String keyName, String valueName )
723     {
724     RegistryKey subKey =
725       Registry.openSubKeyVerbose
726         ( topKey, keyName, RegistryKey.ACCESS_READ );
727 
728     if ( subKey == null )
729       return;
730     
731     try {
732       String value = subKey.getStringValue( valueName );
733       System.err.println
734         ( "String Value " + valueName + "='" + value + "'" );
735       }
736     catch ( RegistryException ex )
737       {
738       System.err.println
739         ( "ERROR getting value '" + valueName + "', "
740           + ex.getMessage() );
741       return;
742       }
743     }
744 
745   private static void
746   expandStringCommand(
747       RegistryKey topKey, String keyName, String valueName )
748     {
749     RegistryKey subKey =
750       Registry.openSubKeyVerbose
751         ( topKey, keyName, RegistryKey.ACCESS_READ );
752 
753     if ( subKey == null )
754       return;
755     
756     try {
757       String value = subKey.getStringValue( valueName );
758       System.err.println
759         ( "String Value " + valueName + "='" + value + "'" );
760       value = RegistryKey.expandEnvStrings( value );
761       System.err.println
762         ( "Expanded Value " + valueName + "='" + value + "'" );
763       }
764     catch ( RegistryException ex )
765       {
766       System.err.println
767         ( "ERROR getting value '" + valueName + "', "
768           + ex.getMessage() );
769       return;
770       }
771     }
772 
773   private static void
774   deleteKeyCommand(
775       RegistryKey topKey, String keyName, String deleteKeyName )
776     {
777     RegistryKey subKey =
778       Registry.openSubKeyVerbose
779         ( topKey, keyName, RegistryKey.ACCESS_WRITE );
780 
781     if ( subKey == null )
782       return;
783 
784     try { subKey.deleteSubKey( deleteKeyName ); }
785     catch ( NoSuchKeyException ex )
786       {
787       System.err.println
788         ( "Key '" + keyName + "\\"
789           + deleteKeyName + "' does not exist." );
790       return;
791       }
792     catch ( RegistryException ex )
793       {
794       System.err.println
795         ( "ERROR deleting key '" + keyName
796           + "', " + ex.getMessage() );
797       return;
798       }
799     }
800 
801   private static void
802   deleteValueCommand(
803       RegistryKey topKey, String keyName, String valueName )
804     {
805     RegistryKey subKey =
806       Registry.openSubKeyVerbose
807         ( topKey, keyName, RegistryKey.ACCESS_WRITE );
808 
809     if ( subKey == null )
810       return;
811 
812     try { subKey.deleteValue( valueName ); }
813     catch ( NoSuchValueException ex )
814       {
815       System.err.println
816         ( "Value '" + valueName + "' does not exist." );
817       return;
818       }
819     catch ( RegistryException ex )
820       {
821       System.err.println
822         ( "ERROR deleting value '" + valueName
823           + "', " + ex.getMessage() );
824       return;
825       }
826     }
827 
828   private static void
829   listKeysCommand( RegistryKey topKey, String keyName )
830     {
831     RegistryKey subKey =
832       Registry.openSubKeyVerbose
833         ( topKey, keyName, RegistryKey.ACCESS_READ );
834 
835     if ( subKey == null )
836       return;
837     
838     try {
839       Enumeration enum = subKey.keyElements();
840       for ( int kIdx = 0 ; enum.hasMoreElements() ; ++kIdx )
841         {
842         String keyStr = (String) enum.nextElement();
843         System.err.println
844           ( "Subkey[" + kIdx + "] = '" + keyStr + "'" );
845         }
846       }
847     catch ( RegistryException ex )
848       {
849       System.err.println
850         ( "ERROR getting key enumerator, "
851           + ex.getMessage() );
852       return;
853       }
854     }
855 
856   private static void
857   listValuesCommand( RegistryKey topKey, String keyName )
858     {
859     RegistryKey subKey =
860       Registry.openSubKeyVerbose
861         ( topKey, keyName, RegistryKey.ACCESS_READ );
862 
863     if ( subKey == null )
864       return;
865     
866     try {
867       Enumeration enum = subKey.valueElements();
868       for ( int kIdx = 0 ; enum.hasMoreElements() ; ++kIdx )
869         {
870         String name = (String) enum.nextElement();
871         System.err.println
872           ( "Value Name[" + kIdx + "] = '" + name + "'" );
873         }
874       }
875     catch ( RegistryException ex )
876       {
877       System.err.println
878         ( "ERROR getting value enumerator, "
879           + ex.getMessage() );
880       return;
881       }
882     }
883 
884   private static void
885   setDWordCommand(
886       RegistryKey topKey, String keyName,
887       String valueName, String data )
888     {
889     RegistryKey subKey =
890       Registry.openSubKeyVerbose
891         ( topKey, keyName, RegistryKey.ACCESS_WRITE );
892 
893     if ( subKey == null )
894       return;
895 
896     int anInt;
897     try { anInt = Integer.parseInt( data ); }
898     catch ( NumberFormatException ex )
899       {
900       System.err.println
901         ( "ERROR bad int: '" + ex.getMessage() + "'" );
902       return;
903       }
904 
905     RegDWordValue val = new RegDWordValue( subKey, valueName );
906     val.setData( anInt );
907     
908     Registry.setValue( subKey, val );
909     }
910   
911   private static void
912   setMultiStringCommand(
913       RegistryKey topKey, String keyName,
914       String valueName, String data )
915     {
916     RegistryKey subKey =
917       Registry.openSubKeyVerbose
918         ( topKey, keyName, RegistryKey.ACCESS_WRITE );
919 
920     if ( subKey == null )
921       return;
922 
923     String[] strArray =
924       StringUtilities.splitString( data, ";" );
925 
926     RegMultiStringValue val =
927       new RegMultiStringValue
928         ( subKey, valueName, strArray );
929 
930     Registry.setValue( subKey, val );
931     }
932 
933   private static void
934   setStringCommand(
935       RegistryKey topKey, String keyName,
936       String valueName, String data )
937     {
938     RegistryKey subKey =
939       Registry.openSubKeyVerbose
940         ( topKey, keyName, RegistryKey.ACCESS_WRITE );
941 
942     if ( subKey == null )
943       return;
944 
945     RegStringValue val =
946       new RegStringValue( subKey, valueName, data );
947 
948     Registry.setValue( subKey, val );
949     }
950 
951   private static void
952   setBinaryCommand(
953       RegistryKey topKey, String keyName,
954       String valueName, String data )
955     {
956     RegistryKey subKey =
957       Registry.openSubKeyVerbose
958         ( topKey, keyName, RegistryKey.ACCESS_WRITE );
959 
960     if ( subKey == null )
961       return;
962 
963     byte[] binData = data.getBytes();
964 
965     RegBinaryValue val =
966       new RegBinaryValue( subKey, valueName, binData );
967 
968     Registry.setValue( subKey, val );
969     }
970 
971   private static void
972   createCommand( RegistryKey topKey, String keyName )
973     {
974     RegistryKey    subKey;
975 
976     try {
977       subKey =
978         topKey.createSubKey
979           ( keyName, null, RegistryKey.ACCESS_WRITE );
980       }
981     catch ( RegistryException ex )
982       {
983       subKey = null;
984       System.err.println
985         ( "ERROR creating subKey: " + ex.getMessage() );
986       }
987 
988     if ( subKey != null )
989       {
990       try {
991         subKey.flushKey();
992         subKey.closeKey();
993         }
994       catch ( RegistryException ex )
995         {
996         subKey = null;
997         System.err.println
998           ( "ERROR flushing and closing key: "
999             + ex.getMessage() );
1000        }
1001      }
1002
1003    if ( subKey != null )
1004      {
1005      System.err.println
1006        ( "SUCCEEDED "
1007          + ( subKey.wasCreated()
1008            ? "Creating" : "Opening via create" )
1009          + " Key '" + keyName + "'" );
1010      }
1011    else
1012      {
1013      System.err.println
1014        ( "FAILED Creating Key '" + keyName + "'" );
1015      }
1016    }
1017
1018  private static RegistryKey
1019  openSubKeyVerbose( RegistryKey topKey, String keyName, int access )
1020    {
1021    RegistryKey    subKey = null;
1022
1023    try { subKey = topKey.openSubKey( keyName, access ); }
1024    catch ( NoSuchKeyException ex )
1025      {
1026      subKey = null;
1027      System.err.println
1028        ( "Key '" + keyName + "' does not exist." );
1029      }
1030    catch ( RegistryException ex )
1031      {
1032      subKey = null;
1033      System.err.println
1034        ( "ERROR registry error=" + ex.getErrorCode()
1035          + ", " + ex.getMessage() );
1036      }
1037
1038    return subKey;
1039    }
1040
1041  private static void
1042  setValue( RegistryKey subKey, RegistryValue value )
1043    {
1044    try {
1045      subKey.setValue( value );
1046      subKey.flushKey();
1047      }
1048    catch ( RegistryException ex )
1049      {
1050      System.err.println
1051        ( "ERROR setting MULTI_SZ value '"
1052          + value.getName() + "', " + ex.getMessage() );
1053      }
1054    }
1055
1056  }
1057
1058
1059
1060