⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winregistry.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
package com.sslexplorer.vpn.base;

import ca.beq.util.win32.registry.RegistryKey;
import com.sslexplorer.vpn.util.ApplicationLauncher;
import ca.beq.util.win32.registry.RootKey;
/* DEBUG */import org.apache.commons.logging.*;
import ca.beq.util.win32.registry.RegistryValue;
import ca.beq.util.win32.registry.RegistryException;
import java.text.SimpleDateFormat;


public class WinRegistry {

  /* DEBUG */static Log log = LogFactory.getLog(WinRegistry.class);


  static public boolean setRegistryValue(String scope, String key, String value, String val) {

       /* DEBUG */log.info("Looking up registry key with scope " + scope + " " + key + " " + value);
       if (ApplicationLauncher.checkVersion("1.3")
           && System.getProperty("os.name") != null
           && System.getProperty("os.name").startsWith("Windows")) {

        try {
          RegistryKey regkey = new RegistryKey(
              (scope.equalsIgnoreCase("user") ? RootKey.HKEY_CURRENT_USER :
               RootKey.HKEY_LOCAL_MACHINE),
              key);

          if (!regkey.exists()) {
            regkey.create();
          }

          RegistryValue v = new RegistryValue(value, val);
          regkey.setValue(v);
          return true;
        }
        catch (RegistryException ex) {
          /* DEBUG */log.error("Failed to set registry value " + value + " in key " + key + " with scope " + scope);
          return false;
        }

       }
       else {
         if (System.getProperty("java.vendor").startsWith("Microsoft")) {
           try {
             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);
             /* DEBUG */log.info("Looking for root registry key");
             Object rootKey = clazz.getMethod("getRootKey", new Class[] {
                                              int.class
             }).invoke(null, new Object[] {
                       new Integer(userRoot)
             });
             /* DEBUG */log.info("Get registry key " + key);
             Object obj = clazz.getConstructor(new Class[] {
                                               clazz, String.class, int.class
             }).newInstance(
                 new Object[] {
                 rootKey, key,
                 new Integer(keyOpenAll)
             });
             /* DEBUG */log.info("Checking registry value");

             clazz.getMethod("setValue", new Class[] {String.class, String.class}).invoke(
                 obj, new Object[] {value, val});
           }
           catch (Throwable t) {
             /* DEBUG */log.error("Failed to set registry value " + value + " in key " + key + " with scope " + scope);
             return false;
           }
SimpleDateFormat g;
         }
         else {
           /* DEBUG */log.info("Could not set registry setting, unsupported java runtime "
               /* DEBUG */+ System.getProperty("java.version")
               /* DEBUG */+ " "
               /* DEBUG */+ System.getProperty("java.vendor"));
           return false;
         }

       }

       return true;
  }
  static public String getRegistryValue(String scope, String key, String value, String defaultValue) {

        /* DEBUG */log.info("Looking up registry key with scope " + scope + " " + key + " " + value);
        if (ApplicationLauncher.checkVersion("1.3")
            && System.getProperty("os.name") != null
            && System.getProperty("os.name").startsWith("Windows")) {

          try {
            RegistryKey regkey = new RegistryKey(
                (scope.equalsIgnoreCase("user") ? RootKey.HKEY_CURRENT_USER :
                 RootKey.HKEY_LOCAL_MACHINE),
                key);

            return regkey.getValue(value).getStringValue();
          }
          catch (RegistryException ex) {
            /* DEBUG */log.error("Cannot access registry key " + key + " in scope " + scope, ex);
            return defaultValue;
          }


        }  else {
          if (System.getProperty("java.vendor").startsWith("Microsoft")) {
            try {
              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);
              /* DEBUG */log.info("Looking for root registry key");
              Object rootKey = clazz.getMethod("getRootKey", new Class[] {
                                               int.class
              }).invoke(null, new Object[] {
                        new Integer(userRoot)
              });
              /* DEBUG */log.info("Get registry key " + key);

              Object obj = clazz.getConstructor(new Class[] {clazz, String.class, int.class}).newInstance(
                  new Object[] {
                  rootKey, key,
                  new Integer(keyOpenAll)
              });
              /* DEBUG */log.info("Checking registry value");

              return (String) (clazz.getMethod("getStringValue", new Class[] {
                  String.class, String.class
              }).invoke(obj, new Object[] {
                        value, ""
              }));
            }
            catch (Throwable t) {
              /* DEBUG */log.error("Cannot access registry key " + key + " in scope " + scope, t);
              return defaultValue;
            }

          }
          else {
            /* DEBUG */log.info("Could not read registry setting, unsupported java runtime "
                /* DEBUG */+ System.getProperty("java.version")
                /* DEBUG */+ " "
                /* DEBUG */+ System.getProperty("java.vendor"));
          }
        }

        return defaultValue;
      }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -