src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java
author rriggs
Fri, 23 Feb 2018 16:00:44 -0500
changeset 48944 25aa8b9f1dae
parent 47312 d4f959806fe9
child 48947 b4dd09b5d6d8
permissions -rw-r--r--
8198645: Use System.lineSeparator() instead of getProperty("line.separator") Reviewed-by: lancea, xuelei, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     1
/*
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     4
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    10
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    15
 * accompanied this code).
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    16
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    20
 *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    23
 * questions.
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    24
 */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    25
package jdk.xml.internal;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    26
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    27
import java.io.File;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    28
import java.io.FileInputStream;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    29
import java.io.FileNotFoundException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    30
import java.io.IOException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    31
import java.io.InputStream;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    32
import java.net.URL;
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    33
import java.security.AccessController;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    34
import java.security.CodeSource;
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    35
import java.security.PrivilegedAction;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    36
import java.security.PrivilegedActionException;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    37
import java.security.PrivilegedExceptionAction;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    38
import java.text.MessageFormat;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    39
import java.util.Locale;
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    40
import java.util.MissingResourceException;
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    41
import java.util.Properties;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    42
import java.util.ResourceBundle;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    43
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    44
/**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    45
 * This class contains utility methods for reading resources in the JAXP packages
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    46
 */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    47
public class SecuritySupport {
48944
25aa8b9f1dae 8198645: Use System.lineSeparator() instead of getProperty("line.separator")
rriggs
parents: 47312
diff changeset
    48
    public final static String NEWLINE = System.lineSeparator();
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    49
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    50
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    51
     * Cache for properties in java.home/conf/jaxp.properties
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    52
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    53
    static final Properties cacheProps = new Properties();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    54
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    55
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    56
     * Flag indicating whether java.home/conf/jaxp.properties has been read
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    57
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    58
    static volatile boolean firstTime = true;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    59
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    60
    private SecuritySupport() {}
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    61
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    62
    public static String getErrorMessage(Locale locale, String bundle, String key,
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    63
            Object[] arguments) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    64
        ResourceBundle rb;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    65
        if (locale != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    66
            rb = ResourceBundle.getBundle(bundle,locale);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    67
        } else {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    68
            rb = ResourceBundle.getBundle(bundle);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    69
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    70
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    71
        String msg = rb.getString(key);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    72
        if (arguments != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    73
            msg = MessageFormat.format(msg, arguments);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    74
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    75
        return msg;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    76
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    77
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    78
    /**
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    79
     * Reads a system property with privilege
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    80
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    81
     * @param propName the name of the property
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    82
     * @return the value of the property
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    83
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    84
    public static String getSystemProperty(final String propName) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    85
        return
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    86
        AccessController.doPrivileged(
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    87
                (PrivilegedAction<String>) () -> System.getProperty(propName));
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    88
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    89
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
    90
    /**
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    91
     * Reads a system property with privilege
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    92
     *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    93
     * @param propName the name of the property
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    94
     * @return the value of the property
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    95
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    96
    public static String getSystemProperty(final String propName, String defValue) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    97
        String value = getSystemProperty(propName);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    98
        if (value == null) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
    99
            return defValue;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   100
        }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   101
        return value;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   102
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   103
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   104
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   105
     * Reads a system property with specified type.
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   106
     *
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   107
     * @param <T> the type of the property value
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   108
     * @param type the type of the property value
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   109
     * @param propName the name of the property
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   110
     * @param defValue the default value
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   111
     * @return the value of the property, or the default value if no system
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   112
     * property is found
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   113
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   114
    public static <T> T getSystemProperty(Class<T> type, String propName, String defValue) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   115
        String value = getSystemProperty(propName);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   116
        if (value == null) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   117
            value = defValue;
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   118
        }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   119
        if (Integer.class.isAssignableFrom(type)) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   120
            return type.cast(Integer.parseInt(value));
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   121
        } else if (Boolean.class.isAssignableFrom(type)) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   122
            return type.cast(Boolean.parseBoolean(value));
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   123
        }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   124
        return type.cast(value);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   125
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   126
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   127
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   128
     * Reads JAXP system property in this order: system property,
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   129
     * $java.home/conf/jaxp.properties if the system property is not specified
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   130
     *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   131
     * @param <T> the type of the property value
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   132
     * @param type the type of the property value
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   133
     * @param propName the name of the property
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   134
     * @param defValue the default value
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   135
     * @return the value of the property, or the default value if no system
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   136
     * property is found
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   137
     */
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   138
    public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) {
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   139
        String value = getJAXPSystemProperty(propName);
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   140
        if (value == null) {
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   141
            value = defValue;
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   142
        }
42247
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   143
        if (Integer.class.isAssignableFrom(type)) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   144
            return type.cast(Integer.parseInt(value));
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   145
        } else if (Boolean.class.isAssignableFrom(type)) {
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   146
            return type.cast(Boolean.parseBoolean(value));
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   147
        }
52fafb950d5a 8158619: Very large CDATA section in XML document causes OOME
joehw
parents: 39907
diff changeset
   148
        return type.cast(value);
39907
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   149
    }
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   150
db51759e3695 8158084: Catalog API: JAXP XML Processor Support
joehw
parents: 33542
diff changeset
   151
    /**
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   152
     * Reads JAXP system property in this order: system property,
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   153
     * $java.home/conf/jaxp.properties if the system property is not specified
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   154
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   155
     * @param propName the name of the property
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   156
     * @return the value of the property
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   157
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   158
    public static String getJAXPSystemProperty(String propName) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   159
        String value = getSystemProperty(propName);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   160
        if (value == null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   161
            value = readJAXPProperty(propName);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   162
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   163
        return value;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   164
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   165
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   166
    /**
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   167
     * Reads the specified property from $java.home/conf/jaxp.properties
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   168
     *
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   169
     * @param propName the name of the property
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   170
     * @return the value of the property
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   171
     */
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   172
    public static String readJAXPProperty(String propName) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   173
        String value = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   174
        InputStream is = null;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   175
        try {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   176
            if (firstTime) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   177
                synchronized (cacheProps) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   178
                    if (firstTime) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   179
                        String configFile = getSystemProperty("java.home") + File.separator
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   180
                                + "conf" + File.separator + "jaxp.properties";
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   181
                        File f = new File(configFile);
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   182
                        if (isFileExists(f)) {
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   183
                            is = getFileInputStream(f);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   184
                            cacheProps.load(is);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   185
                        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   186
                        firstTime = false;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   187
                    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   188
                }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   189
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   190
            value = cacheProps.getProperty(propName);
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   191
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   192
        } catch (IOException ex) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   193
        } finally {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   194
            if (is != null) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   195
                try {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   196
                    is.close();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   197
                } catch (IOException ex) {}
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   198
            }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   199
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   200
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   201
        return value;
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   202
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   203
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   204
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   205
     * Tests whether the file denoted by this abstract pathname is a directory.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   206
     * @param f the file to be tested
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   207
     * @return true if it is a directory, false otherwise
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   208
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   209
    public static boolean isDirectory(final File f) {
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   210
        return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   211
                -> f.isDirectory()));
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   212
    }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   213
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   214
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   215
     * Tests whether the file exists.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   216
     *
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   217
     * @param f the file to be tested
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   218
     * @return true if the file exists, false otherwise
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   219
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   220
    public static boolean isFileExists(final File f) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   221
        return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   222
                -> f.exists()));
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   223
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   224
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   225
    /**
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   226
     * Creates and returns a new FileInputStream from a file.
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   227
     * @param file the specified file
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   228
     * @return the FileInputStream
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   229
     * @throws FileNotFoundException if the file is not found
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   230
     */
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   231
    public static FileInputStream getFileInputStream(final File file)
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   232
            throws FileNotFoundException {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   233
        try {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   234
            return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) ()
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   235
                    -> new FileInputStream(file));
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   236
        } catch (PrivilegedActionException e) {
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   237
            throw (FileNotFoundException) e.getException();
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   238
        }
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   239
    }
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   240
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   241
    /**
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   242
     * Returns the resource as a stream.
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   243
     * @param name the resource name
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   244
     * @return the resource stream
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   245
     */
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   246
    public static InputStream getResourceAsStream(final String name) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   247
        return AccessController.doPrivileged((PrivilegedAction<InputStream>) () ->
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   248
                SecuritySupport.class.getResourceAsStream("/"+name));
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   249
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   250
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   251
    /**
46174
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   252
     * Gets a resource bundle using the specified base name, the default locale, and the caller's class loader.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   253
     * @param bundle the base name of the resource bundle, a fully qualified class name
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   254
     * @return a resource bundle for the given base name and the default locale
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   255
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   256
    public static ResourceBundle getResourceBundle(String bundle) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   257
        return getResourceBundle(bundle, Locale.getDefault());
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   258
    }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   259
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   260
    /**
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   261
     * Gets a resource bundle using the specified base name and locale, and the caller's class loader.
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   262
     * @param bundle the base name of the resource bundle, a fully qualified class name
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   263
     * @param locale the locale for which a resource bundle is desired
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   264
     * @return a resource bundle for the given base name and locale
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   265
     */
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   266
    public static ResourceBundle getResourceBundle(final String bundle, final Locale locale) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   267
        return AccessController.doPrivileged((PrivilegedAction<ResourceBundle>) () -> {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   268
            try {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   269
                return ResourceBundle.getBundle(bundle, locale);
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   270
            } catch (MissingResourceException e) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   271
                try {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   272
                    return ResourceBundle.getBundle(bundle, new Locale("en", "US"));
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   273
                } catch (MissingResourceException e2) {
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   274
                    throw new MissingResourceException(
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   275
                            "Could not load any resource bundle by " + bundle, bundle, "");
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   276
                }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   277
            }
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   278
        });
5611d2529b49 8163121: BCEL: update to the latest 6.0 release
joehw
parents: 42247
diff changeset
   279
    }
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   280
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   281
    /**
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   282
     * Checks whether the file exists.
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   283
     * @param f the specified file
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   284
     * @return true if the file exists, false otherwise
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   285
     */
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   286
    public static boolean doesFileExist(final File f) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   287
        return (AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> f.exists()));
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   288
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   289
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   290
    /**
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   291
     * Checks the LastModified attribute of a file.
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   292
     * @param f the specified file
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   293
     * @return the LastModified attribute
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   294
     */
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   295
    static long getLastModified(final File f) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   296
        return (AccessController.doPrivileged((PrivilegedAction<Long>) () -> f.lastModified()));
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   297
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   298
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   299
    /**
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   300
     * Strip off path from an URI
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   301
     *
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   302
     * @param uri an URI with full path
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   303
     * @return the file name only
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   304
     */
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   305
    public static String sanitizePath(String uri) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   306
        if (uri == null) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   307
            return "";
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   308
        }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   309
        int i = uri.lastIndexOf("/");
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   310
        if (i > 0) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   311
            return uri.substring(i+1, uri.length());
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   312
        }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   313
        return "";
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   314
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   315
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   316
    /**
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   317
     * Check the protocol used in the systemId against allowed protocols
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   318
     *
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   319
     * @param systemId the Id of the URI
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   320
     * @param allowedProtocols a list of allowed protocols separated by comma
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   321
     * @param accessAny keyword to indicate allowing any protocol
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   322
     * @return the name of the protocol if rejected, null otherwise
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   323
     */
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   324
    public static String checkAccess(String systemId, String allowedProtocols,
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   325
            String accessAny) throws IOException {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   326
        if (systemId == null || (allowedProtocols != null &&
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   327
                allowedProtocols.equalsIgnoreCase(accessAny))) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   328
            return null;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   329
        }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   330
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   331
        String protocol;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   332
        if (!systemId.contains(":")) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   333
            protocol = "file";
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   334
        } else {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   335
            URL url = new URL(systemId);
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   336
            protocol = url.getProtocol();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   337
            if (protocol.equalsIgnoreCase("jar")) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   338
                String path = url.getPath();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   339
                protocol = path.substring(0, path.indexOf(":"));
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   340
            } else if (protocol.equalsIgnoreCase("jrt")) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   341
                // if the systemId is "jrt" then allow access if "file" allowed
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   342
                protocol = "file";
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   343
            }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   344
        }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   345
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   346
        if (isProtocolAllowed(protocol, allowedProtocols)) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   347
            //access allowed
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   348
            return null;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   349
        } else {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   350
            return protocol;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   351
        }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   352
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   353
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   354
    /**
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   355
     * Check if the protocol is in the allowed list of protocols. The check
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   356
     * is case-insensitive while ignoring whitespaces.
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   357
     *
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   358
     * @param protocol a protocol
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   359
     * @param allowedProtocols a list of allowed protocols
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   360
     * @return true if the protocol is in the list
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   361
     */
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   362
    private static boolean isProtocolAllowed(String protocol, String allowedProtocols) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   363
         if (allowedProtocols == null) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   364
             return false;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   365
         }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   366
         String temp[] = allowedProtocols.split(",");
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   367
         for (String t : temp) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   368
             t = t.trim();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   369
             if (t.equalsIgnoreCase(protocol)) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   370
                 return true;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   371
             }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   372
         }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   373
         return false;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   374
     }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   375
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   376
    public static ClassLoader getContextClassLoader() {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   377
        return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   378
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   379
            if (cl == null)
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   380
                cl = ClassLoader.getSystemClassLoader();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   381
            return cl;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   382
        });
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   383
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   384
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   385
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   386
    public static ClassLoader getSystemClassLoader() {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   387
        return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   388
            ClassLoader cl = null;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   389
            try {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   390
                cl = ClassLoader.getSystemClassLoader();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   391
            } catch (SecurityException ex) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   392
            }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   393
            return cl;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   394
        });
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   395
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   396
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   397
    public static ClassLoader getParentClassLoader(final ClassLoader cl) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   398
        return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   399
            ClassLoader parent = null;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   400
            try {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   401
                parent = cl.getParent();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   402
            } catch (SecurityException ex) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   403
            }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   404
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   405
            // eliminate loops in case of the boot
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   406
            // ClassLoader returning itself as a parent
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   407
            return (parent == cl) ? null : parent;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   408
        });
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   409
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   410
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   411
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   412
    // Used for debugging purposes
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   413
    public static String getClassSource(Class<?> cls) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   414
        return AccessController.doPrivileged((PrivilegedAction<String>) () -> {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   415
            CodeSource cs = cls.getProtectionDomain().getCodeSource();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   416
            if (cs != null) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   417
                URL loc = cs.getLocation();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   418
                return loc != null ? loc.toString() : "(no location)";
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   419
            } else {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   420
                return "(no code source)";
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   421
            }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   422
        });
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   423
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   424
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   425
    // ----------------  For SAX ----------------------
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   426
    /**
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   427
     * Returns the current thread's context class loader, or the system class loader
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   428
     * if the context class loader is null.
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   429
     * @return the current thread's context class loader, or the system class loader
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   430
     * @throws SecurityException
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   431
     */
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   432
    public static ClassLoader getClassLoader() throws SecurityException{
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   433
        return AccessController.doPrivileged((PrivilegedAction<ClassLoader>)() -> {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   434
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   435
            if (cl == null) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   436
                cl = ClassLoader.getSystemClassLoader();
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   437
            }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   438
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   439
            return cl;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   440
        });
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   441
    }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   442
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   443
    public static InputStream getResourceAsStream(final ClassLoader cl, final String name)
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   444
    {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   445
        return AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   446
            InputStream ris;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   447
            if (cl == null) {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   448
                ris = SecuritySupport.class.getResourceAsStream(name);
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   449
            } else {
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   450
                ris = cl.getResourceAsStream(name);
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   451
            }
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   452
            return ris;
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   453
        });
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   454
    }
33542
9f0eef87e8c1 8081248: Implement JEP 268: XML Catalog API
joehw
parents:
diff changeset
   455
}