src/jdk.jpackage/windows/classes/jdk/jpackage/internal/WindowsRegistry.java
branchJDK-8200758-branch
changeset 57151 38d0b67617e3
parent 57106 ea870b9ce89a
child 57391 970f28090a06
equal deleted inserted replaced
57150:fa68c2ab636d 57151:38d0b67617e3
    36 
    36 
    37 import static jdk.jpackage.internal.IOUtils.exec;
    37 import static jdk.jpackage.internal.IOUtils.exec;
    38 
    38 
    39 final class WindowsRegistry {
    39 final class WindowsRegistry {
    40 
    40 
       
    41     // Currently we only support HKEY_LOCAL_MACHINE. Native implementation will
       
    42     // require support for additinal HKEY if needed.
       
    43     private static final int HKEY_LOCAL_MACHINE = 1;
       
    44 
       
    45     static {
       
    46         System.loadLibrary("jpackage");
       
    47     }
       
    48 
    41     private WindowsRegistry() {}
    49     private WindowsRegistry() {}
    42 
    50 
    43     /**
    51     /**
    44      * Reads the registry value for DisableRealtimeMonitoring.
    52      * Reads the registry value for DisableRealtimeMonitoring.
    45      * @return true if DisableRealtimeMonitoring is set to 0x1,
    53      * @return true if DisableRealtimeMonitoring is set to 0x1,
    46      *         false otherwise.
    54      *         false otherwise.
    47      */
    55      */
    48     static final boolean readDisableRealtimeMonitoring() {
    56     static final boolean readDisableRealtimeMonitoring() {
    49         boolean result = false;
    57         final String subKey = "Software\\Microsoft\\"
    50         final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\"
       
    51                   + "Windows Defender\\Real-Time Protection";
    58                   + "Windows Defender\\Real-Time Protection";
    52         final String subkey = "DisableRealtimeMonitoring";
    59         final String value = "DisableRealtimeMonitoring";
    53         String value = readRegistry(key, subkey);
    60         int result = readDwordValue(HKEY_LOCAL_MACHINE, subKey, value, 0);
    54 
    61         return (result == 1);
    55         if (!value.isEmpty()) {
       
    56             // This code could be written better but this works. It validates
       
    57             // that the result of readRegistry returned what we expect and then
       
    58             // checks for a 0x0 or 0x1. 0x0 means real time monitoring is
       
    59             // on, 0x1 means it is off. So this function returns true if
       
    60             // real-time-monitoring is disabled.
       
    61             int index = value.indexOf(subkey);
       
    62             value = value.substring(index + subkey.length());
       
    63             String reg = "REG_DWORD";
       
    64             index = value.indexOf(reg);
       
    65             value = value.substring(index + reg.length());
       
    66             String hex = "0x";
       
    67             index = value.indexOf(hex);
       
    68             value = value.substring(index + hex.length());
       
    69 
       
    70             if (value.equals("1")) {
       
    71                 result = true;
       
    72             }
       
    73         }
       
    74 
       
    75         return result;
       
    76     }
    62     }
    77 
    63 
    78     static final List<String> readExclusionsPaths() {
    64     static final List<String> readExclusionsPaths() {
    79         List<String> result = new ArrayList<String>();
    65         List<String> result = new ArrayList<>();
    80         final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\"
    66         final String subKey = "Software\\Microsoft\\"
    81                 + "Windows Defender\\Exclusions\\Paths";
    67                 + "Windows Defender\\Exclusions\\Paths";
    82         String value = readRegistry(key, "");
    68         long lKey = openRegistryKey(HKEY_LOCAL_MACHINE, subKey);
       
    69         if (lKey == 0) {
       
    70             return result;
       
    71         }
    83 
    72 
    84         if (!value.isEmpty()) {
    73         String valueName;
    85             final String reg = "REG_DWORD";
    74         int index = 0;
    86             final String hex = "0x0";
    75         do {
       
    76             valueName = enumRegistryValue(lKey, index);
       
    77             if (valueName != null) {
       
    78                 result.add(valueName);
       
    79                 index++;
       
    80             }
       
    81         } while (valueName != null);
    87 
    82 
    88             int index = value.indexOf(key);
    83         closeRegistryKey(lKey);
    89             if (index == 0) {
       
    90                 value = value.substring(index + key.length());
       
    91 
       
    92                 while (value.length() > 0) {
       
    93                     index = value.indexOf(reg);
       
    94                     String name = value.substring(0, index);
       
    95                     value = value.substring(index + reg.length());
       
    96                     index = value.indexOf(hex);
       
    97                     value = value.substring(index + hex.length());
       
    98 
       
    99                     if (index > 0) {
       
   100                         name = name.trim();
       
   101                         result.add(name);
       
   102                     }
       
   103                 }
       
   104             }
       
   105         }
       
   106 
    84 
   107         return result;
    85         return result;
   108     }
    86     }
   109 
    87 
   110     /**
    88     /**
   111      * @param key in the registry
    89      * Reads DWORD registry value.
   112      * @param subkey in the registry key
    90      *
   113      * @return registry value or null if not found
    91      * @param key one of HKEY predefine value
       
    92      * @param subKey registry sub key
       
    93      * @param value value to read
       
    94      * @param defaultValue default value in case if subKey or value not found
       
    95      *                     or any other errors occurred
       
    96      * @return value's data only if it was read successfully, otherwise
       
    97      *         defaultValue
   114      */
    98      */
   115     static final String readRegistry(String key, String subkey){
    99     private static native int readDwordValue(int key, String subKey,
   116         String result = "";
   100             String value, int defaultValue);
   117 
   101 
   118         try {
   102     /**
   119             List<String> buildOptions = new ArrayList<>();
   103      * Open registry key.
   120             buildOptions.add("reg");
   104      *
   121             buildOptions.add("query");
   105      * @param key one of HKEY predefine value
   122             buildOptions.add("\"" + key + "\"");
   106      * @param subKey registry sub key
       
   107      * @return native handle to open key
       
   108      */
       
   109     private static native long openRegistryKey(int key, String subKey);
   123 
   110 
   124             if (!subkey.isEmpty()) {
   111     /**
   125                 buildOptions.add("/v");
   112      * Enumerates the values for registry key.
   126                 buildOptions.add(subkey);
   113      *
   127             }
   114      * @param lKey native handle to open key returned by openRegistryKey
       
   115      * @param index index of value starting from 0. Increment until this
       
   116      *              function returns NULL which means no more values.
       
   117      * @return returns value or NULL if error or no more data
       
   118      */
       
   119     private static native String enumRegistryValue(long lKey, int index);
   128 
   120 
   129             try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
   121     /**
   130                     PrintStream ps = new PrintStream(baos)) {
   122      * Close registry key.
   131                 ProcessBuilder security = new ProcessBuilder(buildOptions);
   123      *
   132                 exec(security, false, false, ps);
   124      * @param lKey native handle to open key returned by openRegistryKey
   133                 BufferedReader bfReader = new BufferedReader(
   125      */
   134                         new InputStreamReader(
   126     private static native void closeRegistryKey(long lKey);
   135                         new ByteArrayInputStream(baos.toByteArray())));
       
   136                 String line = null;
       
   137 
   127 
   138                 while((line = bfReader.readLine()) != null){
   128     /**
   139                     result += line;
   129      * Compares two Windows paths regardless case and if paths are short or long.
   140                 }
   130      *
   141             }
   131      * @param path1 path to compare
   142             catch (IOException e) {
   132      * @param path2 path to compare
   143             }
   133      * @return true if paths point to same location
   144         }
   134      */
   145         catch (Exception e) {
   135     public static native boolean comparePaths(String path1, String path2);
   146         }
       
   147 
       
   148         return result;
       
   149     }
       
   150 }
   136 }