1 package com.sslexplorer.agent.client; 2 3 import java.text.MessageFormat ; 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 18 public class WinRegistry { 19 20 static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(WinRegistry.class); 22 24 33 static public boolean setRegistryValue(String scope, String key, String value, String val) { 34 35 log.info(MessageFormat.format(Messages.getString("WinRegistry.lookingUpKeyWithScope"), new Object [] { scope , key, value } ) ) ; if (Utils.checkVersion("1.3") && System.getProperty("os.name") != null && System.getProperty("os.name").startsWith("Windows")) { 41 try { 42 RegistryKey regkey = new RegistryKey((scope.equalsIgnoreCase("user") ? RootKey.HKEY_CURRENT_USER : 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 log.error(MessageFormat.format(Messages.getString("WinRegistry.failedToSetRegistryValue"), new Object [] { value, key, scope } ) , ex) ; return false; 57 } 58 59 } else { 60 61 if (System.getProperty("java.vendor").startsWith("Microsoft")) { try { 63 Class clazz = Class.forName("com.ms.lang.RegKey"); int userRoot = clazz.getField(scope.equalsIgnoreCase("user") ? "USER_ROOT" : "LOCALMACHINE_ROOT").getInt(null); int keyOpenAll = clazz.getField("KEYOPEN_ALL").getInt(null); log.info(Messages.getString("WinRegistry.lookingForRoot")); Object rootKey = clazz.getMethod("getRootKey", new Class [] { int.class }).invoke(null, new Object [] { new Integer (userRoot) }); 71 log.info(MessageFormat.format(Messages.getString("WinRegistry.gettingRegistryKey"), new Object [] { key } )); Object obj = clazz.getConstructor(new Class [] { clazz, String .class, int.class }).newInstance( 75 new Object [] { rootKey, key, new Integer (keyOpenAll) }); 76 log.info(Messages.getString("WinRegistry.checkingRegistryValue")); 80 clazz.getMethod("setValue", new Class [] { String .class, String .class }) .invoke(obj, new Object [] { value, val }); 82 } catch (Throwable t) { 83 log.error(MessageFormat.format(Messages.getString("WinRegistry.failedToSetRegistryValue"), new Object [] { value, key, scope } ), t ) ; return false; 87 } 88 } else { 89 log.info(MessageFormat.format(Messages.getString("WinRegistry.setNotSupported") , new Object [] { System.getProperty("java.version"), System.getProperty("java.vendor") } ) ); return false; 94 } 95 96 } 97 98 return true; 99 } 100 101 110 static public String getRegistryValue(String scope, String key, String value, String defaultValue) { 111 112 log.info(MessageFormat.format(Messages.getString("WinRegistry.lookingUpKeyWithScope"), new Object [] { scope , key, value } ) ) ; if (Utils.checkVersion("1.3") && System.getProperty("os.name") != null && System.getProperty("os.name").startsWith("Windows")) { 118 try { 119 RegistryKey regkey = new RegistryKey((scope.equalsIgnoreCase("user") ? RootKey.HKEY_CURRENT_USER : RootKey.HKEY_LOCAL_MACHINE), key); 121 122 return regkey.getValue(value).getStringValue(); 123 } catch (RegistryException ex) { 124 log.error(MessageFormat.format(Messages.getString("WinRegistry.cannotAccessRegistryKey"), new Object [] { key, scope } ), ex); return defaultValue; 128 } 129 130 } else { 131 if (System.getProperty("java.vendor").startsWith("Microsoft")) { try { 133 Class clazz = Class.forName("com.ms.lang.RegKey"); int userRoot = clazz.getField(scope.equalsIgnoreCase("user") ? "USER_ROOT" : "LOCALMACHINE_ROOT").getInt(null); int keyOpenAll = clazz.getField("KEYOPEN_READ").getInt(null); log.info(Messages.getString("WinRegistry.lookingForRoot")); Object rootKey = clazz.getMethod("getRootKey", new Class [] { int.class }).invoke(null, new Object [] { new Integer (userRoot) }); 141 log.info(MessageFormat.format(Messages.getString("WinRegistry.gettingRegistryKey"), new Object [] { key } )); 145 Object obj = clazz.getConstructor(new Class [] { clazz, String .class, int.class }).newInstance( 146 new Object [] { rootKey, key, new Integer (keyOpenAll) }); 147 log.info(Messages.getString("WinRegistry.checkingRegistryValue")); 151 return (String ) (clazz.getMethod("getStringValue", new Class [] { String .class, String .class }).invoke(obj, new Object [] { value, "" })); } catch (Throwable t) { 154 log.error(MessageFormat.format(Messages.getString("WinRegistry.cannotAccessRegistryKey"), new Object [] { key, scope } ), t); return defaultValue; 158 } 159 160 } else { 161 log.info(MessageFormat.format(Messages.getString("WinRegistry.getNotSupported") , new Object [] { System.getProperty("java.version"), System.getProperty("java.vendor") } ) ); } 166 } 167 168 return defaultValue; 169 } 170 171 } | Popular Tags |