jdk/src/java.base/share/classes/sun/security/util/Debug.java
author redestad
Tue, 03 May 2016 15:50:54 +0200
changeset 37781 71ed5645f17c
parent 37593 824750ada3d6
child 37796 256c45c4af5d
permissions -rw-r--r--
8155775: Re-examine naming of privileged methods to access System properties Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29122
052f0685cff4 8073955: Update java.security.debug help text to reflect recent enhancements for debugging
vinnie
parents: 26736
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.regex.Pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.regex.Matcher;
6122
16fa7ed7ff1b 6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents: 5506
diff changeset
    31
import java.util.Locale;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 32649
diff changeset
    32
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A utility class for debuging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class Debug {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private String prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static String args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    46
        args = GetPropertyAction.privilegedGetProperty("java.security.debug");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    48
        String args2 = GetPropertyAction
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
    49
                .privilegedGetProperty("java.security.auth.debug");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (args == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            args = args2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (args2 != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
               args = args + "," + args2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (args != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            args = marshal(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (args.equals("help")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                Help();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static void Help()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.err.println("all           turn on all debugging");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.err.println("access        print all checkPermission results");
13796
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    71
        System.err.println("certpath      PKIX CertPathBuilder and");
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    72
        System.err.println("              CertPathValidator debugging");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        System.err.println("combiner      SubjectDomainCombiner debugging");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        System.err.println("gssloginconfig");
13796
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    75
        System.err.println("              GSS LoginConfigImpl debugging");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.err.println("configfile    JAAS ConfigFile loading");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        System.err.println("configparser  JAAS ConfigFile parsing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        System.err.println("jar           jar verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.err.println("logincontext  login context results");
13796
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    80
        System.err.println("jca           JCA engine class debugging");
29122
052f0685cff4 8073955: Update java.security.debug help text to reflect recent enhancements for debugging
vinnie
parents: 26736
diff changeset
    81
        System.err.println("keystore      KeyStore debugging");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.err.println("policy        loading and granting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        System.err.println("provider      security provider debugging");
13796
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    84
        System.err.println("pkcs11        PKCS11 session manager debugging");
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    85
        System.err.println("pkcs11keystore");
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    86
        System.err.println("              PKCS11 KeyStore debugging");
29122
052f0685cff4 8073955: Update java.security.debug help text to reflect recent enhancements for debugging
vinnie
parents: 26736
diff changeset
    87
        System.err.println("pkcs12        PKCS12 KeyStore debugging");
13796
792402bbac8d 7196593: java.security.debug=help doesn't list certpath option
mullan
parents: 10788
diff changeset
    88
        System.err.println("sunpkcs11     SunPKCS11 provider debugging");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        System.err.println("scl           permissions SecureClassLoader assigns");
10788
680a3dbfcaba 7102686: Restructure timestamp code so that jars and modules can more easily share the same code
mullan
parents: 6122
diff changeset
    90
        System.err.println("ts            timestamping");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        System.err.println("The following can be used with access:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.err.println("stack         include stack trace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        System.err.println("domain        dump all domains in context");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        System.err.println("failure       before throwing exception, dump stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        System.err.println("              and domain that didn't have permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.err.println("The following can be used with stack and domain:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.err.println("permission=<classname>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.err.println("              only dump output if specified permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.err.println("              is being checked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        System.err.println("codebase=<URL>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.err.println("              only dump output if specified codebase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        System.err.println("              is being checked");
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   107
        System.err.println();
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   108
        System.err.println("The following can be used with provider:");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   109
        System.err.println();
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   110
        System.err.println("engine=<engines>");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   111
        System.err.println("              only dump output for the specified list");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   112
        System.err.println("              of JCA engines. Supported values:");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   113
        System.err.println("              Cipher, KeyAgreement, KeyGenerator,");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   114
        System.err.println("              KeyPairGenerator, KeyStore, Mac,");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   115
        System.err.println("              MessageDigest, SecureRandom, Signature.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.err.println();
29122
052f0685cff4 8073955: Update java.security.debug help text to reflect recent enhancements for debugging
vinnie
parents: 26736
diff changeset
   117
        System.err.println("The following can be used with certpath:");
052f0685cff4 8073955: Update java.security.debug help text to reflect recent enhancements for debugging
vinnie
parents: 26736
diff changeset
   118
        System.err.println();
052f0685cff4 8073955: Update java.security.debug help text to reflect recent enhancements for debugging
vinnie
parents: 26736
diff changeset
   119
        System.err.println("ocsp          dump the OCSP protocol exchanges");
052f0685cff4 8073955: Update java.security.debug help text to reflect recent enhancements for debugging
vinnie
parents: 26736
diff changeset
   120
        System.err.println();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.err.println("Note: Separate multiple options with a comma");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Get a Debug object corresponding to whether or not the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * option is set. Set the prefix to be the same as option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public static Debug getInstance(String option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return getInstance(option, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Get a Debug object corresponding to whether or not the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * option is set. Set the prefix to be prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public static Debug getInstance(String option, String prefix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (isOn(option)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            Debug d = new Debug();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            d.prefix = prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * True if the system property "security.debug" contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * string "option".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static boolean isOn(String option)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (args == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (args.indexOf("all") != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                return (args.indexOf(option) != -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * print a message to stderr that is prefixed with the prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * created from the call to getInstance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void println(String message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        System.err.println(prefix + ": "+message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * print a blank line to stderr that is prefixed with the prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void println()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        System.err.println(prefix + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * print a message to stderr that is prefixed with the prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public static void println(String prefix, String message)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        System.err.println(prefix + ": "+message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * return a hexadecimal printed representation of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * BigInteger object. the value is formatted to fit on lines of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * at least 75 characters, with embedded newlines. Words are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * separated for readability, with eight words (32 bytes) per line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public static String toHexString(BigInteger b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        String hexValue = b.toString(16);
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   203
        StringBuilder sb = new StringBuilder(hexValue.length()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (hexValue.startsWith("-")) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   206
            sb.append("   -");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            hexValue = hexValue.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   209
            sb.append("    ");     // four spaces
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if ((hexValue.length()%2) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            // add back the leading 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            hexValue = "0" + hexValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        while (i < hexValue.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // one byte at a time
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   218
            sb.append(hexValue.substring(i, i + 2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            i+=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (i!= hexValue.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if ((i%64) == 0) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   222
                    sb.append("\n    ");     // line after eight words
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                } else if (i%8 == 0) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   224
                    sb.append(" ");     // space between words
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   228
        return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * change a string into lower case except permission classes and URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private static String marshal(String args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (args != null) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   236
            StringBuilder target = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            StringBuffer source = new StringBuffer(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            // obtain the "permission=<classname>" options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            // the syntax of classname: IDENTIFIER.IDENTIFIER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            // the regular express to match a class name:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            String keyReg = "[Pp][Ee][Rr][Mm][Ii][Ss][Ss][Ii][Oo][Nn]=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            String keyStr = "permission=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            String reg = keyReg +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Pattern pattern = Pattern.compile(reg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            Matcher matcher = pattern.matcher(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            StringBuffer left = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            while (matcher.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                String matched = matcher.group();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                target.append(matched.replaceFirst(keyReg, keyStr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                target.append("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                // delete the matched sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                matcher.appendReplacement(left, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            matcher.appendTail(left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            source = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // obtain the "codebase=<URL>" options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // the syntax of URL is too flexible, and here assumes that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // URL contains no space, comma(','), and semicolon(';'). That
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // also means those characters also could be used as separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // after codebase option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // However, the assumption is incorrect in some special situation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // when the URL contains comma or semicolon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            keyReg = "[Cc][Oo][Dd][Ee][Bb][Aa][Ss][Ee]=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            keyStr = "codebase=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            reg = keyReg + "[^, ;]*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            pattern = Pattern.compile(reg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            matcher = pattern.matcher(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            left = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            while (matcher.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                String matched = matcher.group();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                target.append(matched.replaceFirst(keyReg, keyStr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                target.append("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // delete the matched sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                matcher.appendReplacement(left, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            matcher.appendTail(left);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            source = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // convert the rest to lower-case characters
6122
16fa7ed7ff1b 6867345: Turkish regional options cause NPE in sun.security.x509.AlgorithmId.algOID
xuelei
parents: 5506
diff changeset
   286
            target.append(source.toString().toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return target.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29122
diff changeset
   294
    private static final char[] hexDigits = "0123456789abcdef".toCharArray();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        StringBuilder sb = new StringBuilder(b.length * 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}