src/java.base/share/classes/java/util/spi/AbstractResourceBundleProvider.java
author mchung
Thu, 21 Dec 2017 15:18:49 -0800
changeset 48524 b6fc9a193661
parent 47216 71c04702a3d5
child 52427 3c6aa484536c
permissions -rw-r--r--
8193767: Improve javadoc in ResourceBundle working with modules Reviewed-by: alanb, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package java.util.spi;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    28
import jdk.internal.misc.JavaUtilResourceBundleAccess;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    29
import jdk.internal.misc.SharedSecrets;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    30
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.IOException;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    32
import java.io.InputStream;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.security.AccessController;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.security.PrivilegedAction;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Locale;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    37
import java.util.PropertyResourceBundle;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.ResourceBundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
/**
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    42
 * {@code AbstractResourceBundleProvider} is an abstract class that provides
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    43
 * the basic support for a provider implementation class for
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    44
 * {@link ResourceBundleProvider}.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 *
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    46
 * <p>
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    47
 * Resource bundles can be packaged in one or more
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    48
 * named modules, <em>service provider modules</em>.  The <em>consumer</em> of the
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    49
 * resource bundle is the one calling {@link ResourceBundle#getBundle(String)}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    50
 * In order for the consumer module to load a resource bundle
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    51
 * "{@code com.example.app.MyResources}" provided by another module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    52
 * it will use the {@linkplain java.util.ServiceLoader service loader}
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    53
 * mechanism.  A service interface named "{@code com.example.app.spi.MyResourcesProvider}"
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    54
 * must be defined and a <em>service provider module</em> will provide an
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    55
 * implementation class of "{@code com.example.app.spi.MyResourcesProvider}"
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    56
 * as follows:
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    57
 *
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    58
 * <blockquote><pre>
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    59
 * {@code import com.example.app.spi.MyResourcesProvider;
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    60
 * class MyResourcesProviderImpl extends AbstractResourceBundleProvider
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    61
 *     implements MyResourcesProvider
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    62
 * {
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    63
 *     public MyResourcesProviderImpl() {
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    64
 *         super("java.properties");
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    65
 *     }
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    66
 *     // this provider maps the resource bundle to per-language package
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    67
 *     protected String toBundleName(String baseName, Locale locale) {
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    68
 *         return "p." + locale.getLanguage() + "." + baseName;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    69
 *     }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    70
 *
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    71
 *     public ResourceBundle getBundle(String baseName, Locale locale) {
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    72
 *         // this module only provides bundles in French
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    73
 *         if (locale.equals(Locale.FRENCH)) {
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    74
 *              return super.getBundle(baseName, locale);
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    75
 *         }
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    76
 *         // otherwise return null
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    77
 *         return null;
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    78
 *     }
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    79
 * }}</pre></blockquote>
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    80
 *
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    81
 * Refer to {@link ResourceBundleProvider} for details.
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
    82
 *
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    83
 * @see <a href="../ResourceBundle.html#resource-bundle-modules">
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    84
 *      Resource Bundles and Named Modules</a>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
 * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42338
diff changeset
    86
 * @spec JPMS
48524
b6fc9a193661 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47216
diff changeset
    87
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
public abstract class AbstractResourceBundleProvider implements ResourceBundleProvider {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    90
    private static final JavaUtilResourceBundleAccess RB_ACCESS =
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    91
        SharedSecrets.getJavaUtilResourceBundleAccess();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
    92
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    private static final String FORMAT_CLASS = "java.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
    private static final String FORMAT_PROPERTIES = "java.properties";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    private final String[] formats;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
     * Constructs an {@code AbstractResourceBundleProvider} with the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
     * "java.properties" format. This constructor is equivalent to
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
     * {@code AbstractResourceBundleProvider("java.properties")}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    protected AbstractResourceBundleProvider() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        this(FORMAT_PROPERTIES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
     * Constructs an {@code AbstractResourceBundleProvider} with the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
     * {@code formats}. The {@link #getBundle(String, Locale)} method looks up
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
     * resource bundles for the given {@code formats}. {@code formats} must
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     * be "java.class" or "java.properties".
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
     * @param formats the formats to be used for loading resource bundles
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
     * @throws NullPointerException if the given {@code formats} is null
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
     * @throws IllegalArgumentException if the given {@code formats} is not
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
     *         "java.class" or "java.properties".
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
    protected AbstractResourceBundleProvider(String... formats) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        this.formats = formats.clone();  // defensive copy
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        if (this.formats.length == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            throw new IllegalArgumentException("empty formats");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        for (String f : this.formats) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            if (!FORMAT_CLASS.equals(f) && !FORMAT_PROPERTIES.equals(f)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                throw new IllegalArgumentException(f);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
     * Returns the bundle name for the given {@code baseName} and {@code
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   132
     * locale} that this provider provides.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   133
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   134
     * @apiNote
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   135
     * A resource bundle provider may package its resource bundles in the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   136
     * same package as the base name of the resource bundle if the package
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   137
     * is not split among other named modules.  If there are more than one
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   138
     * bundle providers providing the resource bundle of a given base name,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   139
     * the resource bundles can be packaged with per-language grouping
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   140
     * or per-region grouping to eliminate the split packages.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   141
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   142
     * <p>For example, if {@code baseName} is {@code "p.resources.Bundle"} then
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   143
     * the resource bundle name of {@code "p.resources.Bundle"} of
47027
84b6099d24e5 8179246: &nbsp;/&thinsp; are literally visible in javadoc
naoto
parents: 44545
diff changeset
   144
     * <code style="white-space:nowrap">Locale("ja", "", "XX")</code>
84b6099d24e5 8179246: &nbsp;/&thinsp; are literally visible in javadoc
naoto
parents: 44545
diff changeset
   145
     * and {@code Locale("en")} could be <code style="white-space:nowrap">
84b6099d24e5 8179246: &nbsp;/&thinsp; are literally visible in javadoc
naoto
parents: 44545
diff changeset
   146
     * "p.resources.ja.Bundle_ja_&thinsp;_XX"</code> and
84b6099d24e5 8179246: &nbsp;/&thinsp; are literally visible in javadoc
naoto
parents: 44545
diff changeset
   147
     * {@code "p.resources.Bundle_en"} respectively.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   148
     *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   149
     * <p> This method is called from the default implementation of the
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
     * {@link #getBundle(String, Locale)} method.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
     * @implNote The default implementation of this method is the same as the
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
     * implementation of
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
     * {@link java.util.ResourceBundle.Control#toBundleName(String, Locale)}.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
     * @param baseName the base name of the resource bundle, a fully qualified
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
     *                 class name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
     * @param locale   the locale for which a resource bundle should be loaded
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
     * @return the bundle name for the resource bundle
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
    protected String toBundleName(String baseName, Locale locale) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        return ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_DEFAULT)
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   163
            .toBundleName(baseName, locale);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
     * Returns a {@code ResourceBundle} for the given {@code baseName} and
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   168
     * {@code locale}.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
     * @implNote
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   171
     * The default implementation of this method calls the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   172
     * {@link #toBundleName(String, Locale) toBundleName} method to get the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   173
     * bundle name for the {@code baseName} and {@code locale} and finds the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   174
     * resource bundle of the bundle name local in the module of this provider.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   175
     * It will only search the formats specified when this provider was
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   176
     * constructed.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
     * @param baseName the base bundle name of the resource bundle, a fully
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
     *                 qualified class name.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
     * @param locale the locale for which the resource bundle should be instantiated
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   181
     * @return {@code ResourceBundle} of the given {@code baseName} and
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   182
     *         {@code locale}, or {@code null} if no resource bundle is found
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   183
     * @throws NullPointerException if {@code baseName} or {@code locale} is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   184
     *         {@code null}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
     * @throws UncheckedIOException if any IO exception occurred during resource
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
     *         bundle loading
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
    public ResourceBundle getBundle(String baseName, Locale locale) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
        Module module = this.getClass().getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        String bundleName = toBundleName(baseName, locale);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        ResourceBundle bundle = null;
38782
ff27bc5c278e 8158604: test/java/util/ResourceBundle/modules/appbasic missing @test
mchung
parents: 36511
diff changeset
   193
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        for (String format : formats) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
                if (FORMAT_CLASS.equals(format)) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   197
                    bundle = loadResourceBundle(module, bundleName);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
                } else if (FORMAT_PROPERTIES.equals(format)) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   199
                    bundle = loadPropertyResourceBundle(module, bundleName);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                if (bundle != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                    break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
            } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                throw new UncheckedIOException(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        return bundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
    }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   210
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   211
    /*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   212
     * Returns the ResourceBundle of .class format if found in the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   213
     * of this provider.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   214
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   215
    private static ResourceBundle loadResourceBundle(Module module, String bundleName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   216
    {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   217
        PrivilegedAction<Class<?>> pa = () -> Class.forName(module, bundleName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   218
        Class<?> c = AccessController.doPrivileged(pa, null, GET_CLASSLOADER_PERMISSION);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   219
        if (c != null && ResourceBundle.class.isAssignableFrom(c)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   220
            @SuppressWarnings("unchecked")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   221
            Class<ResourceBundle> bundleClass = (Class<ResourceBundle>) c;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   222
            return RB_ACCESS.newResourceBundle(bundleClass);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   223
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   224
        return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   225
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   226
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   227
    /*
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   228
     * Returns the ResourceBundle of .property format if found in the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   229
     * of this provider.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   230
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   231
    private static ResourceBundle loadPropertyResourceBundle(Module module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   232
                                                             String bundleName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   233
        throws IOException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   234
    {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   235
        String resourceName = toResourceName(bundleName, "properties");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   236
        if (resourceName == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   237
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   238
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   239
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   240
        PrivilegedAction<InputStream> pa = () -> {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   241
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   242
                return module.getResourceAsStream(resourceName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   243
            } catch (IOException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   244
                throw new UncheckedIOException(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   245
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   246
        };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   247
        try (InputStream stream = AccessController.doPrivileged(pa)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   248
            if (stream != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   249
                return new PropertyResourceBundle(stream);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   250
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   251
                return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   252
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   253
        } catch (UncheckedIOException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   254
            throw e.getCause();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   255
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   256
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   257
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   258
    private static String toResourceName(String bundleName, String suffix) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   259
        if (bundleName.contains("://")) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   260
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   261
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   262
        StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   263
        sb.append(bundleName.replace('.', '/')).append('.').append(suffix);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   264
        return sb.toString();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   265
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38782
diff changeset
   266
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
}