src/jdk.jpackage/windows/classes/jdk/jpackage/internal/WindowsRegistry.java
author herrick
Wed, 28 Aug 2019 11:30:40 -0400
branchJDK-8200758-branch
changeset 57909 c7de06ed4b54
parent 57391 970f28090a06
permissions -rw-r--r--
8229979: jpackage cleanup src files, help text, and javadoc Reviewed-by: asemenyuk, kcr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     1
/*
57106
ea870b9ce89a 8216492: Update copyright of all new jpackage fils to 2019
kcr
parents: 57067
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     4
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    10
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    15
 * accompanied this code).
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    16
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    20
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    23
 * questions.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    24
 */
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    25
57039
98d3963b0b7b 8214051: rename jpackager tool to jpackage
herrick
parents: 57038
diff changeset
    26
package jdk.jpackage.internal;
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    27
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    28
import java.io.BufferedReader;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    31
import java.io.IOException;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    32
import java.io.InputStreamReader;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    33
import java.io.PrintStream;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    34
import java.util.ArrayList;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    35
import java.util.List;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    36
57067
9c17d779808e 8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents: 57039
diff changeset
    37
final class WindowsRegistry {
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    38
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    39
    // Currently we only support HKEY_LOCAL_MACHINE. Native implementation will
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    40
    // require support for additinal HKEY if needed.
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    41
    private static final int HKEY_LOCAL_MACHINE = 1;
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    42
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    43
    static {
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    44
        System.loadLibrary("jpackage");
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    45
    }
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    46
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    47
    private WindowsRegistry() {}
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    48
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    49
    /**
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    50
     * Reads the registry value for DisableRealtimeMonitoring.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    51
     * @return true if DisableRealtimeMonitoring is set to 0x1,
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    52
     *         false otherwise.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    53
     */
57067
9c17d779808e 8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents: 57039
diff changeset
    54
    static final boolean readDisableRealtimeMonitoring() {
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    55
        final String subKey = "Software\\Microsoft\\"
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    56
                  + "Windows Defender\\Real-Time Protection";
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    57
        final String value = "DisableRealtimeMonitoring";
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    58
        int result = readDwordValue(HKEY_LOCAL_MACHINE, subKey, value, 0);
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    59
        return (result == 1);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    60
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    61
57067
9c17d779808e 8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents: 57039
diff changeset
    62
    static final List<String> readExclusionsPaths() {
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    63
        List<String> result = new ArrayList<>();
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    64
        final String subKey = "Software\\Microsoft\\"
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    65
                + "Windows Defender\\Exclusions\\Paths";
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    66
        long lKey = openRegistryKey(HKEY_LOCAL_MACHINE, subKey);
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    67
        if (lKey == 0) {
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    68
            return result;
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    69
        }
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    70
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    71
        String valueName;
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    72
        int index = 0;
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    73
        do {
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    74
            valueName = enumRegistryValue(lKey, index);
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    75
            if (valueName != null) {
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    76
                result.add(valueName);
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    77
                index++;
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    78
            }
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    79
        } while (valueName != null);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    80
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    81
        closeRegistryKey(lKey);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    82
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    83
        return result;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    84
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    85
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    86
    /**
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    87
     * Reads DWORD registry value.
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    88
     *
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    89
     * @param key one of HKEY predefine value
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    90
     * @param subKey registry sub key
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    91
     * @param value value to read
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    92
     * @param defaultValue default value in case if subKey or value not found
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    93
     *                     or any other errors occurred
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    94
     * @return value's data only if it was read successfully, otherwise
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    95
     *         defaultValue
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    96
     */
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    97
    private static native int readDwordValue(int key, String subKey,
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
    98
            String value, int defaultValue);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    99
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   100
    /**
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   101
     * Open registry key.
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   102
     *
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   103
     * @param key one of HKEY predefine value
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   104
     * @param subKey registry sub key
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   105
     * @return native handle to open key
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   106
     */
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   107
    private static native long openRegistryKey(int key, String subKey);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   108
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   109
    /**
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   110
     * Enumerates the values for registry key.
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   111
     *
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   112
     * @param lKey native handle to open key returned by openRegistryKey
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   113
     * @param index index of value starting from 0. Increment until this
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   114
     *              function returns NULL which means no more values.
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   115
     * @return returns value or NULL if error or no more data
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   116
     */
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   117
    private static native String enumRegistryValue(long lKey, int index);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   118
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   119
    /**
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   120
     * Close registry key.
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   121
     *
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   122
     * @param lKey native handle to open key returned by openRegistryKey
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   123
     */
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   124
    private static native void closeRegistryKey(long lKey);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   125
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   126
    /**
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57391
diff changeset
   127
     * Compares two Windows paths regardless case and if paths
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57391
diff changeset
   128
     * are short or long.
57151
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   129
     *
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   130
     * @param path1 path to compare
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   131
     * @param path2 path to compare
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   132
     * @return true if paths point to same location
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   133
     */
38d0b67617e3 8217317: Create jpackage native library for windows
herrick
parents: 57106
diff changeset
   134
    public static native boolean comparePaths(String path1, String path2);
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   135
}