KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > WinRegistry


1 package com.sslexplorer.agent.client;
2
3 import java.text.MessageFormat JavaDoc;
4
5 import ca.beq.util.win32.registry.RegistryException;
6 import ca.beq.util.win32.registry.RegistryKey;
7 import ca.beq.util.win32.registry.RegistryValue;
8 import ca.beq.util.win32.registry.RootKey;
9
10 import com.sslexplorer.agent.client.util.Utils;
11
12 /**
13  * Manipulate and examine the Windows registry, using either Microsoft classes
14  * or the jRegistry library.
15  *
16  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
17  */

18 public class WinRegistry {
19
20     // #ifdef DEBUG
21
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(WinRegistry.class);
22     // #endif
23

24     /**
25      * Set a value
26      *
27      * @param scope scope
28      * @param key key
29      * @param value value
30      * @param val value type
31      * @return set ok
32      */

33     static public boolean setRegistryValue(String JavaDoc scope, String JavaDoc key, String JavaDoc value, String JavaDoc val) {
34
35         // #ifdef DEBUG
36
log.info(MessageFormat.format(Messages.getString("WinRegistry.lookingUpKeyWithScope"), new Object JavaDoc[] { scope , key, value } ) ) ;//$NON-NLS-1$
37
// #endif
38
if (Utils.checkVersion("1.3") && System.getProperty("os.name") != null //$NON-NLS-1$ //$NON-NLS-2$
39
&& System.getProperty("os.name").startsWith("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$
40

41             try {
42                 RegistryKey regkey = new RegistryKey((scope.equalsIgnoreCase("user") ? RootKey.HKEY_CURRENT_USER //$NON-NLS-1$
43
: RootKey.HKEY_LOCAL_MACHINE), key);
44
45                 if (!regkey.exists()) {
46                     regkey.create();
47                 }
48
49                 RegistryValue v = new RegistryValue(value, val);
50                 regkey.setValue(v);
51                 return true;
52             } catch (RegistryException ex) {
53                 // #ifdef DEBUG
54
log.error(MessageFormat.format(Messages.getString("WinRegistry.failedToSetRegistryValue"), new Object JavaDoc[] { value, key, scope } ) , ex) ;//$NON-NLS-1$
55
// #endif
56
return false;
57             }
58
59         } else {
60             
61             if (System.getProperty("java.vendor").startsWith("Microsoft")) { //$NON-NLS-1$ //$NON-NLS-2$
62
try {
63                     Class JavaDoc clazz = Class.forName("com.ms.lang.RegKey"); //$NON-NLS-1$
64
int userRoot = clazz.getField(scope.equalsIgnoreCase("user") ? "USER_ROOT" : "LOCALMACHINE_ROOT").getInt(null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
65
int keyOpenAll = clazz.getField("KEYOPEN_ALL").getInt(null); //$NON-NLS-1$
66
// #ifdef DEBUG
67
log.info(Messages.getString("WinRegistry.lookingForRoot")); //$NON-NLS-1$
68
// #endif
69
Object JavaDoc rootKey = clazz.getMethod("getRootKey", new Class JavaDoc[] { int.class }).invoke(null, //$NON-NLS-1$
70
new Object JavaDoc[] { new Integer JavaDoc(userRoot) });
71                     // #ifdef DEBUG
72
log.info(MessageFormat.format(Messages.getString("WinRegistry.gettingRegistryKey"), new Object JavaDoc[] { key } )); //$NON-NLS-1$
73
// #endif
74
Object JavaDoc obj = clazz.getConstructor(new Class JavaDoc[] { clazz, String JavaDoc.class, int.class }).newInstance(
75                         new Object JavaDoc[] { rootKey, key, new Integer JavaDoc(keyOpenAll) });
76                     // #ifdef DEBUG
77
log.info(Messages.getString("WinRegistry.checkingRegistryValue")); //$NON-NLS-1$
78
// #endif
79

80                     clazz.getMethod("setValue", new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class }) //$NON-NLS-1$
81
.invoke(obj, new Object JavaDoc[] { value, val });
82                 } catch (Throwable JavaDoc t) {
83                     // #ifdef DEBUG
84
log.error(MessageFormat.format(Messages.getString("WinRegistry.failedToSetRegistryValue"), new Object JavaDoc[] { value, key, scope } ), t ) ;//$NON-NLS-1$
85
// #endif
86
return false;
87                 }
88             } else {
89                 // #ifdef DEBUG
90
log.info(MessageFormat.format(Messages.getString("WinRegistry.setNotSupported") //$NON-NLS-1$
91
, new Object JavaDoc[] { System.getProperty("java.version"), System.getProperty("java.vendor") } ) ); //$NON-NLS-1$ //$NON-NLS-2$
92
// #endif
93
return false;
94             }
95
96         }
97
98         return true;
99     }
100
101     /**
102      * Get a value
103      *
104      * @param scope scope
105      * @param key key
106      * @param value value
107      * @param defaultValue default value
108      * @return set ok
109      */

110     static public String JavaDoc getRegistryValue(String JavaDoc scope, String JavaDoc key, String JavaDoc value, String JavaDoc defaultValue) {
111
112         // #ifdef DEBUG
113
log.info(MessageFormat.format(Messages.getString("WinRegistry.lookingUpKeyWithScope"), new Object JavaDoc[] { scope , key, value } ) ) ;//$NON-NLS-1$
114
// #endif
115
if (Utils.checkVersion("1.3") && System.getProperty("os.name") != null //$NON-NLS-1$ //$NON-NLS-2$
116
&& System.getProperty("os.name").startsWith("Windows")) { //$NON-NLS-1$ //$NON-NLS-2$
117

118             try {
119                 RegistryKey regkey = new RegistryKey((scope.equalsIgnoreCase("user") ? RootKey.HKEY_CURRENT_USER //$NON-NLS-1$
120
: RootKey.HKEY_LOCAL_MACHINE), key);
121
122                 return regkey.getValue(value).getStringValue();
123             } catch (RegistryException ex) {
124                 // #ifdef DEBUG
125
log.error(MessageFormat.format(Messages.getString("WinRegistry.cannotAccessRegistryKey"), new Object JavaDoc[] { key, scope } ), ex); //$NON-NLS-1$
126
// #endif
127
return defaultValue;
128             }
129
130         } else {
131             if (System.getProperty("java.vendor").startsWith("Microsoft")) { //$NON-NLS-1$ //$NON-NLS-2$
132
try {
133                     Class JavaDoc clazz = Class.forName("com.ms.lang.RegKey"); //$NON-NLS-1$
134
int userRoot = clazz.getField(scope.equalsIgnoreCase("user") ? "USER_ROOT" : "LOCALMACHINE_ROOT").getInt(null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
135
int keyOpenAll = clazz.getField("KEYOPEN_READ").getInt(null); //$NON-NLS-1$
136
// #ifdef DEBUG
137
log.info(Messages.getString("WinRegistry.lookingForRoot")); //$NON-NLS-1$
138
// #endif
139
Object JavaDoc rootKey = clazz.getMethod("getRootKey", new Class JavaDoc[] { int.class }).invoke(null, //$NON-NLS-1$
140
new Object JavaDoc[] { new Integer JavaDoc(userRoot) });
141                     // #ifdef DEBUG
142
log.info(MessageFormat.format(Messages.getString("WinRegistry.gettingRegistryKey"), new Object JavaDoc[] { key } )); //$NON-NLS-1$
143
// #endif
144

145                     Object JavaDoc obj = clazz.getConstructor(new Class JavaDoc[] { clazz, String JavaDoc.class, int.class }).newInstance(
146                         new Object JavaDoc[] { rootKey, key, new Integer JavaDoc(keyOpenAll) });
147                     // #ifdef DEBUG
148
log.info(Messages.getString("WinRegistry.checkingRegistryValue")); //$NON-NLS-1$
149
// #endif
150

151                     return (String JavaDoc) (clazz.getMethod("getStringValue", new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class }).invoke(obj, //$NON-NLS-1$
152
new Object JavaDoc[] { value, "" })); //$NON-NLS-1$
153
} catch (Throwable JavaDoc t) {
154                     // #ifdef DEBUG
155
log.error(MessageFormat.format(Messages.getString("WinRegistry.cannotAccessRegistryKey"), new Object JavaDoc[] { key, scope } ), t); //$NON-NLS-1$
156
// #endif
157
return defaultValue;
158                 }
159
160             } else {
161                 // #ifdef DEBUG
162
log.info(MessageFormat.format(Messages.getString("WinRegistry.getNotSupported") //$NON-NLS-1$
163
, new Object JavaDoc[] { System.getProperty("java.version"), System.getProperty("java.vendor") } ) ); //$NON-NLS-1$ //$NON-NLS-2$
164
// #endif
165
}
166         }
167
168         return defaultValue;
169     }
170
171 }
Popular Tags