jdk/src/share/classes/java/util/ResourceBundle.java
author okutsu
Thu, 14 Apr 2011 15:59:47 +0900
changeset 9224 75c0420badef
parent 6660 7bc303389b4f
child 10419 12c063b39232
permissions -rw-r--r--
7028818: (lc) Lazily initialize locale extension 7029740: (lc) New Locale class implementation doesn't follow the Java coding conventions 7032820: (lc) sun.util.locale.InternalLocaleBuilder.CaseInsensitiveChar.equals problems 7033503: (lc) Restore optimization code for Locale class initialization 7033504: (lc) incompatible behavior change for ja_JP_JP and th_TH_TH locales Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.net.JarURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.util.concurrent.ConcurrentMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.jar.JarEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    59
import sun.util.locale.BaseLocale;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    60
import sun.util.locale.LocaleObjectCache;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    61
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    65
 * Resource bundles contain locale-specific objects.  When your program needs a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    66
 * locale-specific resource, a <code>String</code> for example, your program can
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    67
 * load it from the resource bundle that is appropriate for the current user's
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    68
 * locale. In this way, you can write program code that is largely independent
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    69
 * of the user's locale isolating most, if not all, of the locale-specific
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * information in resource bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * This allows you to write programs that can:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <UL type=SQUARE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <LI> be easily localized, or translated, into different languages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <LI> handle multiple locales at once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <LI> be easily modified later to support even more locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Resource bundles belong to families whose members share a common base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * name, but whose names also have additional components that identify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * their locales. For example, the base name of a family of resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * bundles might be "MyResources". The family should have a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * resource bundle which simply has the same name as its family -
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * "MyResources" - and will be used as the bundle of last resort if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * specific locale is not supported. The family can then provide as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * many locale-specific members as needed, for example a German one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * named "MyResources_de".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Each resource bundle in a family contains the same items, but the items have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * been translated for the locale represented by that resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * For example, both "MyResources" and "MyResources_de" may have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <code>String</code> that's used on a button for canceling operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * In "MyResources" the <code>String</code> may contain "Cancel" and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * "MyResources_de" it may contain "Abbrechen".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * If there are different resources for different countries, you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * can make specializations: for example, "MyResources_de_CH" contains objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * the German language (de) in Switzerland (CH). If you want to only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * modify some of the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * in the specialization, you can do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * When your program needs a locale-specific object, it loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * the <code>ResourceBundle</code> class using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * {@link #getBundle(java.lang.String, java.util.Locale) getBundle}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * ResourceBundle myResources =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *      ResourceBundle.getBundle("MyResources", currentLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * Resource bundles contain key/value pairs. The keys uniquely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * identify a locale-specific object in the bundle. Here's an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * example of a <code>ListResourceBundle</code> that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * two key/value pairs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * public class MyResources extends ListResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *     protected Object[][] getContents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *         return new Object[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *             {"OkKey", "OK"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *             {"CancelKey", "Cancel"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *             // END OF MATERIAL TO LOCALIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * Keys are always <code>String</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * In this example, the keys are "OkKey" and "CancelKey".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * In the above example, the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * are also <code>String</code>s--"OK" and "Cancel"--but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * they don't have to be. The values can be any type of object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * You retrieve an object from resource bundle using the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * getter method. Because "OkKey" and "CancelKey"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * are both strings, you would use <code>getString</code> to retrieve them:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * button1 = new Button(myResources.getString("OkKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * button2 = new Button(myResources.getString("CancelKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * The getter methods all require the key as an argument and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * the object if found. If the object is not found, the getter method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * throws a <code>MissingResourceException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * Besides <code>getString</code>, <code>ResourceBundle</code> also provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * a method for getting string arrays, <code>getStringArray</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * as well as a generic <code>getObject</code> method for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * type of object. When using <code>getObject</code>, you'll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * have to cast the result to the appropriate type. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * int[] myIntegers = (int[]) myResources.getObject("intList");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * The Java Platform provides two subclasses of <code>ResourceBundle</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <code>ListResourceBundle</code> and <code>PropertyResourceBundle</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * that provide a fairly simple way to create resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * As you saw briefly in a previous example, <code>ListResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * manages its resource as a list of key/value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * <code>PropertyResourceBundle</code> uses a properties file to manage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * its resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * If <code>ListResourceBundle</code> or <code>PropertyResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * do not suit your needs, you can write your own <code>ResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * subclass.  Your subclasses must override two methods: <code>handleGetObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * and <code>getKeys()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <h4>ResourceBundle.Control</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * The {@link ResourceBundle.Control} class provides information necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * to perform the bundle loading process by the <code>getBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * factory methods that take a <code>ResourceBundle.Control</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * instance. You can implement your own subclass in order to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * non-standard resource bundle formats, change the search strategy, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * define caching parameters. Refer to the descriptions of the class and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * {@link #getBundle(String, Locale, ClassLoader, Control) getBundle}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * factory method for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * <h4>Cache Management</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * Resource bundle instances created by the <code>getBundle</code> factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * methods are cached by default, and the factory methods return the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * resource bundle instance multiple times if it has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * cached. <code>getBundle</code> clients may clear the cache, manage the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * lifetime of cached resource bundle instances using time-to-live values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * or specify not to cache resource bundle instances. Refer to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * descriptions of the {@linkplain #getBundle(String, Locale, ClassLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * Control) <code>getBundle</code> factory method}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * #clearCache(ClassLoader) clearCache}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * Control#getTimeToLive(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * ResourceBundle.Control.getTimeToLive}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * Control#needsReload(String, Locale, String, ClassLoader, ResourceBundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * long) ResourceBundle.Control.needsReload} for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * <h4>Example</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * The following is a very simple example of a <code>ResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * subclass, <code>MyResources</code>, that manages two resources (for a larger number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * resources you would probably use a <code>Map</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * Notice that you don't need to supply a value if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * a "parent-level" <code>ResourceBundle</code> handles the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * key with the same value (as for the okKey below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * // default (English language, United States)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * public class MyResources extends ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *     public Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *         if (key.equals("okKey")) return "Ok";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *         if (key.equals("cancelKey")) return "Cancel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *         return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *     public Enumeration&lt;String&gt; getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *         return Collections.enumeration(keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *     // Overrides handleKeySet() so that the getKeys() implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *     // can rely on the keySet() value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *     protected Set&lt;String&gt; handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *         return new HashSet&lt;String&gt;(Arrays.asList("okKey", "cancelKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * // German language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * public class MyResources_de extends MyResources {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *     public Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *         // don't need okKey, since parent level handles it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *         if (key.equals("cancelKey")) return "Abbrechen";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *         return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *     protected Set&lt;String&gt; handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *         return new HashSet&lt;String&gt;(Arrays.asList("cancelKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * You do not have to restrict yourself to using a single family of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 * <code>ResourceBundle</code>s. For example, you could have a set of bundles for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * exception messages, <code>ExceptionResources</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * (<code>ExceptionResources_fr</code>, <code>ExceptionResources_de</code>, ...),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * and one for widgets, <code>WidgetResource</code> (<code>WidgetResources_fr</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 * <code>WidgetResources_de</code>, ...); breaking up the resources however you like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * @see ListResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * @see PropertyResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * @see MissingResourceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
public abstract class ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /** initial size of the bundle cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private static final int INITIAL_CACHE_SIZE = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /** constant indicating that no resource bundle exists */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static final ResourceBundle NONEXISTENT_BUNDLE = new ResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            public Enumeration<String> getKeys() { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            protected Object handleGetObject(String key) { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            public String toString() { return "NONEXISTENT_BUNDLE"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * The cache is a map from cache keys (with bundle base name, locale, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * class loader) to either a resource bundle or NONEXISTENT_BUNDLE wrapped by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * BundleReference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * The cache is a ConcurrentMap, allowing the cache to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * concurrently by multiple threads.  This will also allow the cache keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * to be reclaimed along with the ClassLoaders they reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * This variable would be better named "cache", but we keep the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * name for compatibility with some workarounds for bug 4212439.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private static final ConcurrentMap<CacheKey, BundleReference> cacheList
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
   292
        = new ConcurrentHashMap<>(INITIAL_CACHE_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Queue for reference objects referring to class loaders or bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private static final ReferenceQueue referenceQueue = new ReferenceQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * The parent bundle of this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * The parent bundle is searched by {@link #getObject getObject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * when this bundle does not contain a particular resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    protected ResourceBundle parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The locale for this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private Locale locale = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * The base bundle name for this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * The flag indicating this bundle has expired in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private volatile boolean expired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * The back link to the cache key. null if this bundle isn't in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * the cache (yet) or has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private volatile CacheKey cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * A Set of the keys contained only in this ResourceBundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private volatile Set<String> keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public ResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Gets a string for the given key from this resource bundle or one of its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Calling this method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <code>(String) {@link #getObject(java.lang.String) getObject}(key)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param key the key for the desired string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @exception NullPointerException if <code>key</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @exception MissingResourceException if no object for the given key can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @exception ClassCastException if the object found for the given key is not a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return the string for the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public final String getString(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return (String) getObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Gets a string array for the given key from this resource bundle or one of its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Calling this method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * <code>(String[]) {@link #getObject(java.lang.String) getObject}(key)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @param key the key for the desired string array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @exception NullPointerException if <code>key</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @exception MissingResourceException if no object for the given key can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @exception ClassCastException if the object found for the given key is not a string array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @return the string array for the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public final String[] getStringArray(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return (String[]) getObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Gets an object for the given key from this resource bundle or one of its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * This method first tries to obtain the object from this resource bundle using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * {@link #handleGetObject(java.lang.String) handleGetObject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * If not successful, and the parent resource bundle is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * it calls the parent's <code>getObject</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * If still not successful, it throws a MissingResourceException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param key the key for the desired object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @exception NullPointerException if <code>key</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @exception MissingResourceException if no object for the given key can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return the object for the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public final Object getObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        Object obj = handleGetObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                obj = parent.getObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (obj == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                throw new MissingResourceException("Can't find resource for bundle "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                                   +this.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                                   +", key "+key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                                                   this.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                                                   key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Returns the locale of this resource bundle. This method can be used after a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * call to getBundle() to determine whether the resource bundle returned really
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * corresponds to the requested locale or is a fallback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return the locale of this resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public Locale getLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Automatic determination of the ClassLoader to be used to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * resources on behalf of the client.  N.B. The client is getLoader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * caller's caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private static ClassLoader getLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        Class[] stack = getClassContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /* Magic number 2 identifies our caller's caller */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        Class c = stack[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        ClassLoader cl = (c == null) ? null : c.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (cl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // When the caller's loader is the boot class loader, cl is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // here. In that case, ClassLoader.getSystemClassLoader() may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            // return the same class loader that the application is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            // using. We therefore use a wrapper ClassLoader to create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // separate scope for bundles loaded on behalf of the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // runtime so that these bundles cannot be returned from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // cache to the application (5048280).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            cl = RBClassLoader.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private static native Class[] getClassContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * A wrapper of ClassLoader.getSystemClassLoader().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private static class RBClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        private static final RBClassLoader INSTANCE = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    new PrivilegedAction<RBClassLoader>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                        public RBClassLoader run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            return new RBClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        private static final ClassLoader loader = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        private RBClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        public Class<?> loadClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                return loader.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return Class.forName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        public URL getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                return loader.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return ClassLoader.getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        public InputStream getResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                return loader.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return ClassLoader.getSystemResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Sets the parent bundle of this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * The parent bundle is searched by {@link #getObject getObject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * when this bundle does not contain a particular resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param parent this bundle's parent bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    protected void setParent(ResourceBundle parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        assert parent != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Key used for cached resource bundles.  The key checks the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * name, the locale, and the class loader to determine if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * resource is a match to the requested one. The loader may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * null, but the base name and the locale must have a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private static final class CacheKey implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        // These three are the actual keys for lookup in Map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        private Locale locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        private LoaderReference loaderRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        // bundle format which is necessary for calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // Control.needsReload().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        private String format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        // These time values are in CacheKey so that NONEXISTENT_BUNDLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // doesn't need to be cloned for caching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        // The time when the bundle has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        private volatile long loadTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        // The time when the bundle expires in the cache, or either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        // Control.TTL_DONT_CACHE or Control.TTL_NO_EXPIRATION_CONTROL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        private volatile long expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // Placeholder for an error report by a Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        private Throwable cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // Hash code value cache to avoid recalculating the hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        // of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        private int hashCodeCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        CacheKey(String baseName, Locale locale, ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            this.name = baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            this.locale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                this.loaderRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                loaderRef = new LoaderReference(loader, referenceQueue, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            calculateHashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        CacheKey setName(String baseName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (!this.name.equals(baseName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                this.name = baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                calculateHashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        Locale getLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        CacheKey setLocale(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            if (!this.locale.equals(locale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                this.locale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                calculateHashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        ClassLoader getLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            return (loaderRef != null) ? loaderRef.get() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                final CacheKey otherEntry = (CacheKey)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                //quick check to see if they are not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                if (hashCodeCache != otherEntry.hashCodeCache) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                //are the names the same?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                if (!name.equals(otherEntry.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                // are the locales the same?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                if (!locale.equals(otherEntry.locale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                //are refs (both non-null) or (both null)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                if (loaderRef == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    return otherEntry.loaderRef == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                ClassLoader loader = loaderRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                return (otherEntry.loaderRef != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        // with a null reference we can no longer find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        // out which class loader was referenced; so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        // treat it as unequal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        && (loader != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        && (loader == otherEntry.loaderRef.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            } catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            return hashCodeCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        private void calculateHashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            hashCodeCache = name.hashCode() << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            hashCodeCache ^= locale.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            ClassLoader loader = getLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                hashCodeCache ^= loader.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                CacheKey clone = (CacheKey) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                if (loaderRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                    clone.loaderRef = new LoaderReference(loaderRef.get(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                                          referenceQueue, clone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                // Clear the reference to a Throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                clone.cause = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                //this should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        void setFormat(String format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        private void setCause(Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (this.cause == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                // Override the cause if the previous one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                // ClassNotFoundException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                if (this.cause instanceof ClassNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        private Throwable getCause() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            String l = locale.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (l.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if (locale.getVariant().length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    l = "__" + locale.getVariant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    l = "\"\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return "CacheKey[" + name + ", lc=" + l + ", ldr=" + getLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                + "(format=" + format + ")]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * The common interface to get a CacheKey in LoaderReference and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * BundleReference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    private static interface CacheKeyReference {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        public CacheKey getCacheKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * References to class loaders are weak references, so that they can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * garbage collected when nobody else is using them. The ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * class has no reason to keep class loaders alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    private static final class LoaderReference extends WeakReference<ClassLoader>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                                               implements CacheKeyReference {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        private CacheKey cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        LoaderReference(ClassLoader referent, ReferenceQueue q, CacheKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            super(referent, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            cacheKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        public CacheKey getCacheKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * References to bundles are soft references so that they can be garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * collected when they have no hard references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    private static final class BundleReference extends SoftReference<ResourceBundle>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                               implements CacheKeyReference {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        private CacheKey cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        BundleReference(ResourceBundle referent, ReferenceQueue q, CacheKey key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            super(referent, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            cacheKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        public CacheKey getCacheKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            return cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * Gets a resource bundle using the specified base name, the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * and the caller's class loader. Calling this method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * except that <code>getClassLoader()</code> is run with the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * privileges of <code>ResourceBundle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * for a complete description of the search and instantiation strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @param baseName the base name of the resource bundle, a fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @exception java.lang.NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *     if <code>baseName</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @exception MissingResourceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *     if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @return a resource bundle for the given base name and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public static final ResourceBundle getBundle(String baseName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return getBundleImpl(baseName, Locale.getDefault(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                             /* must determine loader here, else we break stack invariant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                             getLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                             Control.INSTANCE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Returns a resource bundle using the specified base name, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * default locale and the specified control. Calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * getBundle(baseName, Locale.getDefault(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *           this.getClass().getClassLoader(), control),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * except that <code>getClassLoader()</code> is run with the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * privileges of <code>ResourceBundle</code>.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * complete description of the resource bundle loading process with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * <code>ResourceBundle.Control</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *        the base name of the resource bundle, a fully qualified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *        name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @param control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *        the control which gives information for the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *        loading process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @return a resource bundle for the given base name and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *        locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *        if <code>baseName</code> or <code>control</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *        <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @exception MissingResourceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *        if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *        if the given <code>control</code> doesn't perform properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *        Note that validation of <code>control</code> is performed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *        needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    public static final ResourceBundle getBundle(String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                                                 Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return getBundleImpl(baseName, Locale.getDefault(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                             /* must determine loader here, else we break stack invariant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                             getLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                             control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Gets a resource bundle using the specified base name and locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * and the caller's class loader. Calling this method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * <code>getBundle(baseName, locale, this.getClass().getClassLoader())</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * except that <code>getClassLoader()</code> is run with the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * privileges of <code>ResourceBundle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * for a complete description of the search and instantiation strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *        the base name of the resource bundle, a fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *        the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *        if <code>baseName</code> or <code>locale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * @exception MissingResourceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *        if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @return a resource bundle for the given base name and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    public static final ResourceBundle getBundle(String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                                 Locale locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        return getBundleImpl(baseName, locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                             /* must determine loader here, else we break stack invariant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                             getLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                             Control.INSTANCE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * Returns a resource bundle using the specified base name, target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * locale and control, and the caller's class loader. Calling this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * getBundle(baseName, targetLocale, this.getClass().getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *           control),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * except that <code>getClassLoader()</code> is run with the security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * privileges of <code>ResourceBundle</code>.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * complete description of the resource bundle loading process with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * <code>ResourceBundle.Control</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *        the base name of the resource bundle, a fully qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *        class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param targetLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *        the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *        the control which gives information for the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *        bundle loading process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @return a resource bundle for the given base name and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *        <code>Locale</code> in <code>locales</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *        if <code>baseName</code>, <code>locales</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *        <code>control</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @exception MissingResourceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *        if no resource bundle for the specified base name in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *        of the <code>locales</code> can be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *        if the given <code>control</code> doesn't perform properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *        Note that validation of <code>control</code> is performed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *        needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    public static final ResourceBundle getBundle(String baseName, Locale targetLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                                                 Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return getBundleImpl(baseName, targetLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                             /* must determine loader here, else we break stack invariant */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                             getLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                             control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   848
     * Gets a resource bundle using the specified base name, locale, and class
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   849
     * loader.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   850
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   851
     * <p><a name="default_behavior"/>This method behaves the same as calling
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   852
     * {@link #getBundle(String, Locale, ClassLoader, Control)} passing a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   853
     * default instance of {@link Control}. The following describes this behavior.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   854
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   855
     * <p><code>getBundle</code> uses the base name, the specified locale, and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   856
     * the default locale (obtained from {@link java.util.Locale#getDefault()
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   857
     * Locale.getDefault}) to generate a sequence of <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   858
     * name="candidates"><em>candidate bundle names</em></a>.  If the specified
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   859
     * locale's language, script, country, and variant are all empty strings,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   860
     * then the base name is the only candidate bundle name.  Otherwise, a list
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   861
     * of candidate locales is generated from the attribute values of the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   862
     * specified locale (language, script, country and variant) and appended to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   863
     * the base name.  Typically, this will look like the following:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   864
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   865
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   866
     *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   867
     *     baseName + "_" + language + "_" + script + "_" + country
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   868
     *     baseName + "_" + language + "_" + script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   869
     *     baseName + "_" + language + "_" + country + "_" + variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   870
     *     baseName + "_" + language + "_" + country
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   871
     *     baseName + "_" + language
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   872
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   874
     * <p>Candidate bundle names where the final component is an empty string
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   875
     * are omitted, along with the underscore.  For example, if country is an
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   876
     * empty string, the second and the fifth candidate bundle names above
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   877
     * would be omitted.  Also, if script is an empty string, the candidate names
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   878
     * including script are omitted.  For example, a locale with language "de"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   879
     * and variant "JAVA" will produce candidate names with base name
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   880
     * "MyResource" below.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   881
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   882
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   883
     *     MyResource_de__JAVA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   884
     *     MyResource_de
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   885
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   886
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   887
     * In the case that the variant contains one or more underscores ('_'), a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   888
     * sequence of bundle names generated by truncating the last underscore and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   889
     * the part following it is inserted after a candidate bundle name with the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   890
     * original variant.  For example, for a locale with language "en", script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   891
     * "Latn, country "US" and variant "WINDOWS_VISTA", and bundle base name
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   892
     * "MyResource", the list of candidate bundle names below is generated:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   893
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   894
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   895
     * MyResource_en_Latn_US_WINDOWS_VISTA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   896
     * MyResource_en_Latn_US_WINDOWS
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   897
     * MyResource_en_Latn_US
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   898
     * MyResource_en_Latn
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   899
     * MyResource_en_US_WINDOWS_VISTA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   900
     * MyResource_en_US_WINDOWS
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   901
     * MyResource_en_US
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   902
     * MyResource_en
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   903
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   904
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   905
     * <blockquote><b>Note:</b> For some <code>Locale</code>s, the list of
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   906
     * candidate bundle names contains extra names, or the order of bundle names
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   907
     * is slightly modified.  See the description of the default implementation
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   908
     * of {@link Control#getCandidateLocales(String, Locale)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   909
     * getCandidateLocales} for details.</blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   911
     * <p><code>getBundle</code> then iterates over the candidate bundle names
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   912
     * to find the first one for which it can <em>instantiate</em> an actual
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   913
     * resource bundle. It uses the default controls' {@link Control#getFormats
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   914
     * getFormats} method, which generates two bundle names for each generated
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   915
     * name, the first a class name and the second a properties file name. For
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   916
     * each candidate bundle name, it attempts to create a resource bundle:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   917
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   918
     * <ul><li>First, it attempts to load a class using the generated class name.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   919
     * If such a class can be found and loaded using the specified class
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   920
     * loader, is assignment compatible with ResourceBundle, is accessible from
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   921
     * ResourceBundle, and can be instantiated, <code>getBundle</code> creates a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   922
     * new instance of this class and uses it as the <em>result resource
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   923
     * bundle</em>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   924
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   925
     * <li>Otherwise, <code>getBundle</code> attempts to locate a property
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   926
     * resource file using the generated properties file name.  It generates a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   927
     * path name from the candidate bundle name by replacing all "." characters
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   928
     * with "/" and appending the string ".properties".  It attempts to find a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   929
     * "resource" with this name using {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   930
     * java.lang.ClassLoader#getResource(java.lang.String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   931
     * ClassLoader.getResource}.  (Note that a "resource" in the sense of
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   932
     * <code>getResource</code> has nothing to do with the contents of a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   933
     * resource bundle, it is just a container of data, such as a file.)  If it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   934
     * finds a "resource", it attempts to create a new {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   935
     * PropertyResourceBundle} instance from its contents.  If successful, this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   936
     * instance becomes the <em>result resource bundle</em>.  </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   938
     * <p>This continues until a result resource bundle is instantiated or the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   939
     * list of candidate bundle names is exhausted.  If no matching resource
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   940
     * bundle is found, the default control's {@link Control#getFallbackLocale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   941
     * getFallbackLocale} method is called, which returns the current default
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   942
     * locale.  A new sequence of candidate locale names is generated using this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   943
     * locale and and searched again, as above.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   944
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   945
     * <p>If still no result bundle is found, the base name alone is looked up. If
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   946
     * this still fails, a <code>MissingResourceException</code> is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   948
     * <p><a name="parent_chain"/> Once a result resource bundle has been found,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   949
     * its <em>parent chain</em> is instantiated.  If the result bundle already
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   950
     * has a parent (perhaps because it was returned from a cache) the chain is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   951
     * complete.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   952
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   953
     * <p>Otherwise, <code>getBundle</code> examines the remainder of the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   954
     * candidate locale list that was used during the pass that generated the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   955
     * result resource bundle.  (As before, candidate bundle names where the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   956
     * final component is an empty string are omitted.)  When it comes to the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   957
     * end of the candidate list, it tries the plain bundle name.  With each of the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   958
     * candidate bundle names it attempts to instantiate a resource bundle (first
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   959
     * looking for a class and then a properties file, as described above).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   960
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   961
     * <p>Whenever it succeeds, it calls the previously instantiated resource
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * bundle's {@link #setParent(java.util.ResourceBundle) setParent} method
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   963
     * with the new resource bundle.  This continues until the list of names
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   964
     * is exhausted or the current bundle already has a non-null parent.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   965
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   966
     * <p>Once the parent chain is complete, the bundle is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   968
     * <p><b>Note:</b> <code>getBundle</code> caches instantiated resource
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   969
     * bundles and might return the same resource bundle instance multiple times.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   971
     * <p><b>Note:</b>The <code>baseName</code> argument should be a fully
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   972
     * qualified class name. However, for compatibility with earlier versions,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   973
     * Sun's Java SE Runtime Environments do not verify this, and so it is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   974
     * possible to access <code>PropertyResourceBundle</code>s by specifying a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   975
     * path name (using "/") instead of a fully qualified class name (using
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   976
     * ".").
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * <p><a name="default_behavior_example"/>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   979
     * <strong>Example:</strong>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   980
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   981
     * The following class and property files are provided:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *     MyResources.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *     MyResources.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *     MyResources_fr.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *     MyResources_fr_CH.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *     MyResources_fr_CH.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *     MyResources_en.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *     MyResources_es_ES.class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * </pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   991
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   992
     * The contents of all files are valid (that is, public non-abstract
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   993
     * subclasses of <code>ResourceBundle</code> for the ".class" files,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   994
     * syntactically correct ".properties" files).  The default locale is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   995
     * <code>Locale("en", "GB")</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   996
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   997
     * <p>Calling <code>getBundle</code> with the locale arguments below will
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   998
     * instantiate resource bundles as follows:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
   999
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1000
     * <table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1001
     * <tr><td>Locale("fr", "CH")</td><td>MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1002
     * <tr><td>Locale("fr", "FR")</td><td>MyResources_fr.properties, parent MyResources.class</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1003
     * <tr><td>Locale("de", "DE")</td><td>MyResources_en.properties, parent MyResources.class</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1004
     * <tr><td>Locale("en", "US")</td><td>MyResources_en.properties, parent MyResources.class</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1005
     * <tr><td>Locale("es", "ES")</td><td>MyResources_es_ES.class, parent MyResources.class</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1006
     * </table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1007
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1008
     * <p>The file MyResources_fr_CH.properties is never used because it is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1009
     * hidden by the MyResources_fr_CH.class. Likewise, MyResources.properties
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1010
     * is also hidden by MyResources.class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * @param baseName the base name of the resource bundle, a fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @param locale the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @param loader the class loader from which to load the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * @return a resource bundle for the given base name and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * @exception java.lang.NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *        if <code>baseName</code>, <code>locale</code>, or <code>loader</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @exception MissingResourceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *        if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    public static ResourceBundle getBundle(String baseName, Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                                           ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        return getBundleImpl(baseName, locale, loader, Control.INSTANCE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * Returns a resource bundle using the specified base name, target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * locale, class loader and control. Unlike the {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * #getBundle(String, Locale, ClassLoader) <code>getBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * factory methods with no <code>control</code> argument}, the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * <code>control</code> specifies how to locate and instantiate resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * bundles. Conceptually, the bundle loading process with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * <code>control</code> is performed in the following steps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * <li>This factory method looks up the resource bundle in the cache for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * the specified <code>baseName</code>, <code>targetLocale</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * <code>loader</code>.  If the requested resource bundle instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * found in the cache and the time-to-live periods of the instance and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * all of its parent instances have not expired, the instance is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * to the caller. Otherwise, this factory method proceeds with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * loading process below.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * <li>The {@link ResourceBundle.Control#getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * control.getFormats} method is called to get resource bundle formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * to produce bundle or resource names. The strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * <code>"java.class"</code> and <code>"java.properties"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * designate class-based and {@linkplain PropertyResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * property}-based resource bundles, respectively. Other strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * starting with <code>"java."</code> are reserved for future extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * and must not be used for application-defined formats. Other strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * designate application-defined formats.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <li>The {@link ResourceBundle.Control#getCandidateLocales(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * Locale) control.getCandidateLocales} method is called with the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * locale to get a list of <em>candidate <code>Locale</code>s</em> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * which resource bundles are searched.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * <li>The {@link ResourceBundle.Control#newBundle(String, Locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * String, ClassLoader, boolean) control.newBundle} method is called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * instantiate a <code>ResourceBundle</code> for the base bundle name, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * candidate locale, and a format. (Refer to the note on the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * lookup below.) This step is iterated over all combinations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * candidate locales and formats until the <code>newBundle</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * returns a <code>ResourceBundle</code> instance or the iteration has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * used up all the combinations. For example, if the candidate locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * are <code>Locale("de", "DE")</code>, <code>Locale("de")</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * <code>Locale("")</code> and the formats are <code>"java.class"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * and <code>"java.properties"</code>, then the following is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * sequence of locale-format combinations to be used to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * <code>control.newBundle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * <table style="width: 50%; text-align: left; margin-left: 40px;"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *  border="0" cellpadding="2" cellspacing="2">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * <tbody><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * <td
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;">Locale<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * <td
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;">format<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * <td style="vertical-align: top; width: 50%;">Locale("de", "DE")<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * <td style="vertical-align: top; width: 50%;">java.class<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * <td style="vertical-align: top; width: 50%;">Locale("de", "DE")</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * <td style="vertical-align: top; width: 50%;">java.properties<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * <td style="vertical-align: top; width: 50%;">Locale("de")</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * <td style="vertical-align: top; width: 50%;">java.class</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * <td style="vertical-align: top; width: 50%;">Locale("de")</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * <td style="vertical-align: top; width: 50%;">java.properties</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * <td style="vertical-align: top; width: 50%;">Locale("")<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * <td style="vertical-align: top; width: 50%;">java.class</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * <td style="vertical-align: top; width: 50%;">Locale("")</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * <td style="vertical-align: top; width: 50%;">java.properties</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * </code></tbody>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * <li>If the previous step has found no resource bundle, proceed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * Step 6. If a bundle has been found that is a base bundle (a bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * for <code>Locale("")</code>), and the candidate locale list only contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * <code>Locale("")</code>, return the bundle to the caller. If a bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * has been found that is a base bundle, but the candidate locale list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * contained locales other than Locale(""), put the bundle on hold and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * proceed to Step 6. If a bundle has been found that is not a base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * bundle, proceed to Step 7.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * <li>The {@link ResourceBundle.Control#getFallbackLocale(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Locale) control.getFallbackLocale} method is called to get a fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * locale (alternative to the current target locale) to try further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * finding a resource bundle. If the method returns a non-null locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * it becomes the next target locale and the loading process starts over
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * from Step 3. Otherwise, if a base bundle was found and put on hold in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * a previous Step 5, it is returned to the caller now. Otherwise, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * MissingResourceException is thrown.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * <li>At this point, we have found a resource bundle that's not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * base bundle. If this bundle set its parent during its instantiation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * it is returned to the caller. Otherwise, its <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * href="./ResourceBundle.html#parent_chain">parent chain</a> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * instantiated based on the list of candidate locales from which it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * found. Finally, the bundle is returned to the caller.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * <p>During the resource bundle loading process above, this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * method looks up the cache before calling the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Control#newBundle(String, Locale, String, ClassLoader, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * control.newBundle} method.  If the time-to-live period of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * resource bundle found in the cache has expired, the factory method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * calls the {@link ResourceBundle.Control#needsReload(String, Locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * String, ClassLoader, ResourceBundle, long) control.needsReload}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * method to determine whether the resource bundle needs to be reloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * If reloading is required, the factory method calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <code>control.newBundle</code> to reload the resource bundle.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * <code>control.newBundle</code> returns <code>null</code>, the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * method puts a dummy resource bundle in the cache as a mark of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * nonexistent resource bundles in order to avoid lookup overhead for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * subsequent requests. Such dummy resource bundles are under the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * expiration control as specified by <code>control</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <p>All resource bundles loaded are cached by default. Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * {@link Control#getTimeToLive(String,Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * control.getTimeToLive} for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * <p>The following is an example of the bundle loading process with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * default <code>ResourceBundle.Control</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * <p>Conditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * <li>Base bundle name: <code>foo.bar.Messages</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * <li>Requested <code>Locale</code>: {@link Locale#ITALY}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * <li>Default <code>Locale</code>: {@link Locale#FRENCH}</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * <li>Available resource bundles:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * <code>foo/bar/Messages_fr.properties</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * <code>foo/bar/Messages.properties</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * <p>First, <code>getBundle</code> tries loading a resource bundle in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * the following sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * <li>class <code>foo.bar.Messages_it_IT</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * <li>file <code>foo/bar/Messages_it_IT.properties</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * <li>class <code>foo.bar.Messages_it</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * <li>file <code>foo/bar/Messages_it.properties</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * <li>class <code>foo.bar.Messages</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * <li>file <code>foo/bar/Messages.properties</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * <p>At this point, <code>getBundle</code> finds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * <code>foo/bar/Messages.properties</code>, which is put on hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * because it's the base bundle.  <code>getBundle</code> calls {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Control#getFallbackLocale(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * control.getFallbackLocale("foo.bar.Messages", Locale.ITALY)} which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * returns <code>Locale.FRENCH</code>. Next, <code>getBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * tries loading a bundle in the following sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * <li>class <code>foo.bar.Messages_fr</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * <li>file <code>foo/bar/Messages_fr.properties</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <li>class <code>foo.bar.Messages</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <li>file <code>foo/bar/Messages.properties</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * <p><code>getBundle</code> finds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     * <code>foo/bar/Messages_fr.properties</code> and creates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * <code>ResourceBundle</code> instance. Then, <code>getBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * sets up its parent chain from the list of the candiate locales.  Only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * <code>foo/bar/Messages.properties</code> is found in the list and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * <code>getBundle</code> creates a <code>ResourceBundle</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * that becomes the parent of the instance for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * <code>foo/bar/Messages_fr.properties</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *        the base name of the resource bundle, a fully qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *        class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @param targetLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *        the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @param loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     *        the class loader from which to load the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * @param control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     *        the control which gives information for the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     *        bundle loading process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * @return a resource bundle for the given base name and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     *        if <code>baseName</code>, <code>targetLocale</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *        <code>loader</code>, or <code>control</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *        <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * @exception MissingResourceException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *        if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *        if the given <code>control</code> doesn't perform properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *        Note that validation of <code>control</code> is performed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *        needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    public static ResourceBundle getBundle(String baseName, Locale targetLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                                           ClassLoader loader, Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        if (loader == null || control == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        return getBundleImpl(baseName, targetLocale, loader, control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    private static ResourceBundle getBundleImpl(String baseName, Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                                                ClassLoader loader, Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        if (locale == null || control == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        // We create a CacheKey here for use by this call. The base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        // name and loader will never change during the bundle loading
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // process. We have to make sure that the locale is set before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        // using it as a cache key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        CacheKey cacheKey = new CacheKey(baseName, locale, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        ResourceBundle bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        // Quick lookup of the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        BundleReference bundleRef = cacheList.get(cacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        if (bundleRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            bundle = bundleRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            bundleRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        // If this bundle and all of its parents are valid (not expired),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        // then return this bundle. If any of the bundles is expired, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        // don't call control.needsReload here but instead drop into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        // complete loading process below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        if (isValidBundle(bundle) && hasValidParentChain(bundle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // No valid bundle was found in the cache, so we need to load the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        // resource bundle and its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        boolean isKnownControl = (control == Control.INSTANCE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                   (control instanceof SingleFormatControl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        List<String> formats = control.getFormats(baseName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        if (!isKnownControl && !checkList(formats)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            throw new IllegalArgumentException("Invalid Control: getFormats");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        ResourceBundle baseBundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        for (Locale targetLocale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
             targetLocale != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
             targetLocale = control.getFallbackLocale(baseName, targetLocale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            List<Locale> candidateLocales = control.getCandidateLocales(baseName, targetLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            if (!isKnownControl && !checkList(candidateLocales)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                throw new IllegalArgumentException("Invalid Control: getCandidateLocales");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            bundle = findBundle(cacheKey, candidateLocales, formats, 0, control, baseBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            // If the loaded bundle is the base bundle and exactly for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            // requested locale or the only candidate locale, then take the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            // bundle as the resulting one. If the loaded bundle is the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            // bundle, it's put on hold until we finish processing all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            // fallback locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            if (isValidBundle(bundle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                boolean isBaseBundle = Locale.ROOT.equals(bundle.locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                if (!isBaseBundle || bundle.locale.equals(locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                    || (candidateLocales.size() == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                        && bundle.locale.equals(candidateLocales.get(0)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                // If the base bundle has been loaded, keep the reference in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                // baseBundle so that we can avoid any redundant loading in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                // the control specify not to cache bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                if (isBaseBundle && baseBundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    baseBundle = bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        if (bundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            if (baseBundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                throwMissingResourceException(baseName, locale, cacheKey.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            bundle = baseBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * Checks if the given <code>List</code> is not null, not empty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * not having null in its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    private static final boolean checkList(List a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        boolean valid = (a != null && a.size() != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            int size = a.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            for (int i = 0; valid && i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                valid = (a.get(i) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    private static final ResourceBundle findBundle(CacheKey cacheKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                                                   List<Locale> candidateLocales,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                                                   List<String> formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                                                   int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                                                   Control control,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                                                   ResourceBundle baseBundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        Locale targetLocale = candidateLocales.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        ResourceBundle parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        if (index != candidateLocales.size() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            parent = findBundle(cacheKey, candidateLocales, formats, index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                                control, baseBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        } else if (baseBundle != null && Locale.ROOT.equals(targetLocale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            return baseBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        // Before we do the real loading work, see whether we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        // do some housekeeping: If references to class loaders or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        // resource bundles have been nulled out, remove all related
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        // information from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        Object ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        while ((ref = referenceQueue.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            cacheList.remove(((CacheKeyReference)ref).getCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        // flag indicating the resource bundle has expired in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        boolean expiredBundle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        // First, look up the cache to see if it's in the cache, without
6660
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1373
        // attempting to load bundle.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        cacheKey.setLocale(targetLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        ResourceBundle bundle = findBundleInCache(cacheKey, control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        if (isValidBundle(bundle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            expiredBundle = bundle.expired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            if (!expiredBundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                // If its parent is the one asked for by the candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                // locales (the runtime lookup path), we can take the cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                // one. (If it's not identical, then we'd have to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                // parent's parents to be consistent with what's been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                // requested.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                if (bundle.parent == parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                // Otherwise, remove the cached one since we can't keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                // the same bundles having different parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                BundleReference bundleRef = cacheList.get(cacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                if (bundleRef != null && bundleRef.get() == bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        if (bundle != NONEXISTENT_BUNDLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            CacheKey constKey = (CacheKey) cacheKey.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            try {
6660
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1400
                bundle = loadBundle(cacheKey, formats, control, expiredBundle);
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1401
                if (bundle != null) {
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1402
                    if (bundle.parent == null) {
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1403
                        bundle.setParent(parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    }
6660
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1405
                    bundle.locale = targetLocale;
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1406
                    bundle = putBundleInCache(cacheKey, bundle, control);
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1407
                    return bundle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
6660
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1410
                // Put NONEXISTENT_BUNDLE in the cache as a mark that there's no bundle
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1411
                // instance for the locale.
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1412
                putBundleInCache(cacheKey, NONEXISTENT_BUNDLE, control);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                if (constKey.getCause() instanceof InterruptedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                    Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    private static final ResourceBundle loadBundle(CacheKey cacheKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                                                   List<String> formats,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                                                   Control control,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                                                   boolean reload) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        // Here we actually load the bundle in the order of formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        // specified by the getFormats() value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        Locale targetLocale = cacheKey.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        ResourceBundle bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        int size = formats.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            String format = formats.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                bundle = control.newBundle(cacheKey.getName(), targetLocale, format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                                           cacheKey.getLoader(), reload);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            } catch (LinkageError error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                // We need to handle the LinkageError case due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                // inconsistent case-sensitivity in ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                // See 6572242 for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                cacheKey.setCause(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            } catch (Exception cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                cacheKey.setCause(cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            if (bundle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                // Set the format in the cache key so that it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                // used when calling needsReload later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                cacheKey.setFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                bundle.name = cacheKey.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                bundle.locale = targetLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                // Bundle provider might reuse instances. So we should make
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                // sure to clear the expired flag here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                bundle.expired = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    private static final boolean isValidBundle(ResourceBundle bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        return bundle != null && bundle != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * Determines whether any of resource bundles in the parent chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * including the leaf, have expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
    private static final boolean hasValidParentChain(ResourceBundle bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        while (bundle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            if (bundle.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            CacheKey key = bundle.cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                long expirationTime = key.expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                if (expirationTime >= 0 && expirationTime <= now) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            bundle = bundle.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * Throw a MissingResourceException with proper message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    private static final void throwMissingResourceException(String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                                                            Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                                                            Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        // If the cause is a MissingResourceException, avoid creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        // a long chain. (6355009)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        if (cause instanceof MissingResourceException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            cause = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        throw new MissingResourceException("Can't find bundle for base name "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                                           + baseName + ", locale " + locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                                           baseName + "_" + locale, // className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                                           "",                      // key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                                           cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * Finds a bundle in the cache. Any expired bundles are marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * `expired' and removed from the cache upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * @param cacheKey the key to look up the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * @param control the Control to be used for the expiration control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * @return the cached bundle, or null if the bundle is not found in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * cache or its parent has expired. <code>bundle.expire</code> is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * upon return if the bundle in the cache has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    private static final ResourceBundle findBundleInCache(CacheKey cacheKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                                                          Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        BundleReference bundleRef = cacheList.get(cacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        if (bundleRef == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        ResourceBundle bundle = bundleRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        if (bundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        ResourceBundle p = bundle.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        assert p != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        // If the parent has expired, then this one must also expire. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        // check only the immediate parent because the actual loading is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        // done from the root (base) to leaf (child) and the purpose of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        // checking is to propagate expiration towards the leaf. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        // example, if the requested locale is ja_JP_JP and there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        // bundles for all of the candidates in the cache, we have a list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        // base <- ja <- ja_JP <- ja_JP_JP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        // If ja has expired, then it will reload ja and the list becomes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        // tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        // base <- ja (new)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        //  "   <- ja (expired) <- ja_JP <- ja_JP_JP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        // When looking up ja_JP in the cache, it finds ja_JP in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        // which references to the expired ja. Then, ja_JP is marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        // expired and removed from the cache. This will be propagated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        // ja_JP_JP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        // Now, it's possible, for example, that while loading new ja_JP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        // someone else has started loading the same bundle and finds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        // base bundle has expired. Then, what we get from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        // getBundle call includes the expired base bundle. However, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        // someone else didn't start its loading, we wouldn't know if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        // base bundle has expired at the end of the loading process. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        // expiration control doesn't guarantee that the returned bundle and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        // its parents haven't expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        // We could check the entire parent chain to see if there's any in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        // the chain that has expired. But this process may never end. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        // extreme case would be that getTimeToLive returns 0 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        // needsReload always returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        if (p != null && p.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            assert bundle != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            bundle.expired = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            bundle.cacheKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
            cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            CacheKey key = bundleRef.getCacheKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            long expirationTime = key.expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
            if (!bundle.expired && expirationTime >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                expirationTime <= System.currentTimeMillis()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                // its TTL period has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                if (bundle != NONEXISTENT_BUNDLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                    // Synchronize here to call needsReload to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                    // redundant concurrent calls for the same bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                    synchronized (bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                        expirationTime = key.expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                        if (!bundle.expired && expirationTime >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                            expirationTime <= System.currentTimeMillis()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                                bundle.expired = control.needsReload(key.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                                                                     key.getLocale(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                                                                     key.getFormat(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                                                                     key.getLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                                                                     bundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                                                                     key.loadTime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                                cacheKey.setCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                            if (bundle.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                                // If the bundle needs to be reloaded, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                                // remove the bundle from the cache, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                                // return the bundle with the expired flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                                // on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                                bundle.cacheKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                                cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                                // Update the expiration control info. and reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                                // the same bundle instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                                setExpirationTime(key, control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                    // We just remove NONEXISTENT_BUNDLE from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                    cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                    bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Put a new bundle in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * @param cacheKey the key for the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * @param bundle the resource bundle to be put in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * @return the ResourceBundle for the cacheKey; if someone has put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * the bundle before this call, the one found in the cache is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    private static final ResourceBundle putBundleInCache(CacheKey cacheKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                                                         ResourceBundle bundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                                                         Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        setExpirationTime(cacheKey, control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        if (cacheKey.expirationTime != Control.TTL_DONT_CACHE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            CacheKey key = (CacheKey) cacheKey.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            BundleReference bundleRef = new BundleReference(bundle, referenceQueue, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            bundle.cacheKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            // Put the bundle in the cache if it's not been in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            BundleReference result = cacheList.putIfAbsent(key, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            // If someone else has put the same bundle in the cache before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            // us and it has not expired, we should use the one in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                ResourceBundle rb = result.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                if (rb != null && !rb.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                    // Clear the back link to the cache key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                    bundle.cacheKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    bundle = rb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    // Clear the reference in the BundleReference so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                    // it won't be enqueued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    bundleRef.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    // Replace the invalid (garbage collected or expired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    // instance with the valid one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                    cacheList.put(key, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    private static final void setExpirationTime(CacheKey cacheKey, Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        long ttl = control.getTimeToLive(cacheKey.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                                         cacheKey.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
        if (ttl >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            // If any expiration time is specified, set the time to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            // expired in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            cacheKey.loadTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            cacheKey.expirationTime = now + ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        } else if (ttl >= Control.TTL_NO_EXPIRATION_CONTROL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            cacheKey.expirationTime = ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            throw new IllegalArgumentException("Invalid Control: TTL=" + ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * Removes all resource bundles from the cache that have been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * using the caller's class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    public static final void clearCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        clearCache(getLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * Removes all resource bundles from the cache that have been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * using the given class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * @param loader the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * @exception NullPointerException if <code>loader</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    public static final void clearCache(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        Set<CacheKey> set = cacheList.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        for (CacheKey key : set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
            if (key.getLoader() == loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                set.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * Gets an object for the given key from this resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * Returns null if this resource bundle does not contain an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * object for the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * @param key the key for the desired object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * @exception NullPointerException if <code>key</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * @return the object for the given key, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    protected abstract Object handleGetObject(String key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * Returns an enumeration of the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * @return an <code>Enumeration</code> of the keys contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     *         this <code>ResourceBundle</code> and its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    public abstract Enumeration<String> getKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * Determines whether the given <code>key</code> is contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * this <code>ResourceBundle</code> or its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * @param key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     *        the resource <code>key</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * @return <code>true</code> if the given <code>key</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     *        contained in this <code>ResourceBundle</code> or its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     *        parent bundles; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     *         if <code>key</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
    public boolean containsKey(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            if (rb.handleKeySet().contains(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * Returns a <code>Set</code> of all keys contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * <code>ResourceBundle</code> and its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * @return a <code>Set</code> of all keys contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     *         <code>ResourceBundle</code> and its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    public Set<String> keySet() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  1757
        Set<String> keys = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            keys.addAll(rb.handleKeySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * Returns a <code>Set</code> of the keys contained <em>only</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * in this <code>ResourceBundle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     * <p>The default implementation returns a <code>Set</code> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * keys returned by the {@link #getKeys() getKeys} method except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * for the ones for which the {@link #handleGetObject(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * handleGetObject} method returns <code>null</code>. Once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * <code>Set</code> has been created, the value is kept in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * <code>ResourceBundle</code> in order to avoid producing the
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1774
     * same <code>Set</code> in subsequent calls. Subclasses can
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1775
     * override this method for faster handling.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @return a <code>Set</code> of the keys contained only in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *        <code>ResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    protected Set<String> handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        if (keySet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                if (keySet == null) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  1785
                    Set<String> keys = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                    Enumeration<String> enumKeys = getKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                    while (enumKeys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                        String key = enumKeys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                        if (handleGetObject(key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                            keys.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                    keySet = keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * <code>ResourceBundle.Control</code> defines a set of callback methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * that are invoked by the {@link ResourceBundle#getBundle(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * Locale, ClassLoader, Control) ResourceBundle.getBundle} factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * methods during the bundle loading process. In other words, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * <code>ResourceBundle.Control</code> collaborates with the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * methods for loading resource bundles. The default implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * the callback methods provides the information necessary for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * factory methods to perform the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * href="./ResourceBundle.html#default_behavior">default behavior</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * <p>In addition to the callback methods, the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * #toBundleName(String, Locale) toBundleName} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * #toResourceName(String, String) toResourceName} methods are defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * primarily for convenience in implementing the callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * methods. However, the <code>toBundleName</code> method could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * overridden to provide different conventions in the organization and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * packaging of localized resources.  The <code>toResourceName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * method is <code>final</code> to avoid use of wrong resource and class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * name separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * <p>Two factory methods, {@link #getControl(List)} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * #getNoFallbackControl(List)}, provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * <code>ResourceBundle.Control</code> instances that implement common
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * variations of the default bundle loading process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * <p>The formats returned by the {@link Control#getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * getFormats} method and candidate locales returned by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * ResourceBundle.Control#getCandidateLocales(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * getCandidateLocales} method must be consistent in all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * <code>ResourceBundle.getBundle</code> invocations for the same base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * bundle. Otherwise, the <code>ResourceBundle.getBundle</code> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * may return unintended bundles. For example, if only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * <code>"java.class"</code> is returned by the <code>getFormats</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     * method for the first call to <code>ResourceBundle.getBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     * and only <code>"java.properties"</code> for the second call, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * second call will return the class-based one that has been cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * during the first call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * <p>A <code>ResourceBundle.Control</code> instance must be thread-safe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * if it's simultaneously used by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * <code>ResourceBundle.getBundle</code> does not synchronize to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * the <code>ResourceBundle.Control</code> methods. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * implementations of the methods are thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * <p>Applications can specify <code>ResourceBundle.Control</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * instances returned by the <code>getControl</code> factory methods or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * created from a subclass of <code>ResourceBundle.Control</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * customize the bundle loading process. The following are examples of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * changing the default bundle loading process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * <p><b>Example 1</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * <p>The following code lets <code>ResourceBundle.getBundle</code> look
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * up only properties-based resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * import static java.util.ResourceBundle.Control.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * ResourceBundle bundle =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     *   ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     *                            ResourceBundle.Control.getControl(FORMAT_PROPERTIES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * Given the resource bundles in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * href="./ResourceBundle.html#default_behavior_example">example</a> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * the <code>ResourceBundle.getBundle</code> description, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * <code>ResourceBundle.getBundle</code> call loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * <code>MyResources_fr_CH.properties</code> whose parent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * <code>MyResources_fr.properties</code> whose parent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * <code>MyResources.properties</code>. (<code>MyResources_fr_CH.properties</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * is not hidden, but <code>MyResources_fr_CH.class</code> is.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * <p><b>Example 2</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * <p>The following is an example of loading XML-based bundles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * using {@link Properties#loadFromXML(java.io.InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * Properties.loadFromXML}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * ResourceBundle rb = ResourceBundle.getBundle("Messages",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     *     new ResourceBundle.Control() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     *         public List&lt;String&gt; getFormats(String baseName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     *             if (baseName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     *                 throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     *             return Arrays.asList("xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     *         public ResourceBundle newBundle(String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     *                                         Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *                                         String format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     *                                         ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *                                         boolean reload)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     *                          throws IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     *                                 InstantiationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *                                 IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *             if (baseName == null || locale == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     *                   || format == null || loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     *                 throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     *             ResourceBundle bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     *             if (format.equals("xml")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *                 String bundleName = toBundleName(baseName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     *                 String resourceName = toResourceName(bundleName, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     *                 InputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     *                 if (reload) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *                     URL url = loader.getResource(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *                     if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     *                         URLConnection connection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     *                         if (connection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     *                             // Disable caches to get fresh data for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     *                             // reloading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     *                             connection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     *                             stream = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     *                         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     *                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     *                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     *                     stream = loader.getResourceAsStream(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     *                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     *                 if (stream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     *                     BufferedInputStream bis = new BufferedInputStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     *                     bundle = new XMLResourceBundle(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     *                     bis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     *                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     *             return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     *     });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * private static class XMLResourceBundle extends ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     *     private Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     *     XMLResourceBundle(InputStream stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     *         props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     *         props.loadFromXML(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     *     protected Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     *         return props.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     *     public Enumeration&lt;String&gt; getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     *         ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    public static class Control {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
         * The default format <code>List</code>, which contains the strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
         * <code>"java.class"</code> and <code>"java.properties"</code>, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
         * this order. This <code>List</code> is {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
         * Collections#unmodifiableList(List) unmodifiable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
         * @see #getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        public static final List<String> FORMAT_DEFAULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            = Collections.unmodifiableList(Arrays.asList("java.class",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                                                         "java.properties"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
         * The class-only format <code>List</code> containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
         * <code>"java.class"</code>. This <code>List</code> is {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
         * Collections#unmodifiableList(List) unmodifiable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
         * @see #getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        public static final List<String> FORMAT_CLASS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            = Collections.unmodifiableList(Arrays.asList("java.class"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
         * The properties-only format <code>List</code> containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
         * <code>"java.properties"</code>. This <code>List</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
         * {@linkplain Collections#unmodifiableList(List) unmodifiable}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
         * @see #getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        public static final List<String> FORMAT_PROPERTIES
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            = Collections.unmodifiableList(Arrays.asList("java.properties"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
         * The time-to-live constant for not caching loaded resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
         * instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
         * @see #getTimeToLive(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        public static final long TTL_DONT_CACHE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
         * The time-to-live constant for disabling the expiration control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
         * for loaded resource bundle instances in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
         * @see #getTimeToLive(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        public static final long TTL_NO_EXPIRATION_CONTROL = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        private static final Control INSTANCE = new Control();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
         * Sole constructor. (For invocation by subclass constructors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
         * typically implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        protected Control() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
         * Returns a <code>ResourceBundle.Control</code> in which the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
         * #getFormats(String) getFormats} method returns the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
         * <code>formats</code>. The <code>formats</code> must be equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
         * one of {@link Control#FORMAT_PROPERTIES}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
         * Control#FORMAT_CLASS} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
         * Control#FORMAT_DEFAULT}. <code>ResourceBundle.Control</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
         * instances returned by this method are singletons and thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
         * <p>Specifying {@link Control#FORMAT_DEFAULT} is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
         * instantiating the <code>ResourceBundle.Control</code> class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
         * except that this method returns a singleton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
         * @param formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
         *        the formats to be returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
         *        <code>ResourceBundle.Control.getFormats</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
         * @return a <code>ResourceBundle.Control</code> supporting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
         *        specified <code>formats</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
         *        if <code>formats</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
         * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
         *        if <code>formats</code> is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        public static final Control getControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            if (formats.equals(Control.FORMAT_PROPERTIES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                return SingleFormatControl.PROPERTIES_ONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            if (formats.equals(Control.FORMAT_CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                return SingleFormatControl.CLASS_ONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            if (formats.equals(Control.FORMAT_DEFAULT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                return Control.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
         * Returns a <code>ResourceBundle.Control</code> in which the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
         * #getFormats(String) getFormats} method returns the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
         * <code>formats</code> and the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
         * Control#getFallbackLocale(String, Locale) getFallbackLocale}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
         * method returns <code>null</code>. The <code>formats</code> must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
         * be equal to one of {@link Control#FORMAT_PROPERTIES}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
         * Control#FORMAT_CLASS} or {@link Control#FORMAT_DEFAULT}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
         * <code>ResourceBundle.Control</code> instances returned by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
         * method are singletons and thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
         * @param formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
         *        the formats to be returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
         *        <code>ResourceBundle.Control.getFormats</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
         * @return a <code>ResourceBundle.Control</code> supporting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
         *        specified <code>formats</code> with no fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
         *        <code>Locale</code> support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
         *        if <code>formats</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
         * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
         *        if <code>formats</code> is unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        public static final Control getNoFallbackControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            if (formats.equals(Control.FORMAT_DEFAULT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                return NoFallbackControl.NO_FALLBACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
            if (formats.equals(Control.FORMAT_PROPERTIES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                return NoFallbackControl.PROPERTIES_ONLY_NO_FALLBACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            if (formats.equals(Control.FORMAT_CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                return NoFallbackControl.CLASS_ONLY_NO_FALLBACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
         * Returns a <code>List</code> of <code>String</code>s containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
         * formats to be used to load resource bundles for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
         * <code>baseName</code>. The <code>ResourceBundle.getBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
         * factory method tries to load resource bundles with formats in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
         * order specified by the list. The list returned by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
         * must have at least one <code>String</code>. The predefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
         * formats are <code>"java.class"</code> for class-based resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
         * bundles and <code>"java.properties"</code> for {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
         * PropertyResourceBundle properties-based} ones. Strings starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
         * with <code>"java."</code> are reserved for future extensions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
         * must not be used by application-defined formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
         * <p>It is not a requirement to return an immutable (unmodifiable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
         * <code>List</code>.  However, the returned <code>List</code> must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
         * not be mutated after it has been returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
         * <code>getFormats</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
         * <p>The default implementation returns {@link #FORMAT_DEFAULT} so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
         * that the <code>ResourceBundle.getBundle</code> factory method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
         * looks up first class-based resource bundles, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
         * properties-based ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
         *        the base name of the resource bundle, a fully qualified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
         *        name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
         * @return a <code>List</code> of <code>String</code>s containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
         *        formats for loading resource bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
         *        if <code>baseName</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
         * @see #FORMAT_DEFAULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
         * @see #FORMAT_CLASS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
         * @see #FORMAT_PROPERTIES
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        public List<String> getFormats(String baseName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            return FORMAT_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
         * Returns a <code>List</code> of <code>Locale</code>s as candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
         * locales for <code>baseName</code> and <code>locale</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
         * method is called by the <code>ResourceBundle.getBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
         * factory method each time the factory method tries finding a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
         * resource bundle for a target <code>Locale</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
         * <p>The sequence of the candidate locales also corresponds to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
         * runtime resource lookup path (also known as the <I>parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
         * chain</I>), if the corresponding resource bundles for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
         * candidate locales exist and their parents are not defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
         * loaded resource bundles themselves.  The last element of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
         * must be a {@linkplain Locale#ROOT root locale} if it is desired to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
         * have the base bundle as the terminal of the parent chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
         * <p>If the given locale is equal to <code>Locale.ROOT</code> (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
         * root locale), a <code>List</code> containing only the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
         * <code>Locale</code> must be returned. In this case, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
         * <code>ResourceBundle.getBundle</code> factory method loads only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
         * the base bundle as the resulting resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
         *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2140
         * <p>It is not a requirement to return an immutable (unmodifiable)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2141
         * <code>List</code>. However, the returned <code>List</code> must not
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2142
         * be mutated after it has been returned by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2143
         * <code>getCandidateLocales</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
         * <p>The default implementation returns a <code>List</code> containing
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2146
         * <code>Locale</code>s using the rules described below.  In the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2147
         * description below, <em>L</em>, <em>S</em>, <em>C</em> and <em>V</em>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2148
         * respectively represent non-empty language, script, country, and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2149
         * variant.  For example, [<em>L</em>, <em>C</em>] represents a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2150
         * <code>Locale</code> that has non-empty values only for language and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2151
         * country.  The form <em>L</em>("xx") represents the (non-empty)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2152
         * language value is "xx".  For all cases, <code>Locale</code>s whose
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
         * final component values are empty strings are omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
         *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2155
         * <ol><li>For an input <code>Locale</code> with an empty script value,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2156
         * append candidate <code>Locale</code>s by omitting the final component
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2157
         * one by one as below:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2158
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2159
         * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2160
         * <li> [<em>L</em>, <em>C</em>, <em>V</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2161
         * <li> [<em>L</em>, <em>C</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2162
         * <li> [<em>L</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2163
         * <li> <code>Locale.ROOT</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2164
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2165
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2166
         * <li>For an input <code>Locale</code> with a non-empty script value,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2167
         * append candidate <code>Locale</code>s by omitting the final component
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2168
         * up to language, then append candidates generated from the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2169
         * <code>Locale</code> with country and variant restored:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2170
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2171
         * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2172
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2173
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2174
         * <li> [<em>L</em>, <em>S</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2175
         * <li> [<em>L</em>, <em>C</em>, <em>V</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2176
         * <li> [<em>L</em>, <em>C</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2177
         * <li> [<em>L</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2178
         * <li> <code>Locale.ROOT</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2179
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2180
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2181
         * <li>For an input <code>Locale</code> with a variant value consisting
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2182
         * of multiple subtags separated by underscore, generate candidate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2183
         * <code>Locale</code>s by omitting the variant subtags one by one, then
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2184
         * insert them after every occurence of <code> Locale</code>s with the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2185
         * full variant value in the original list.  For example, if the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2186
         * the variant consists of two subtags <em>V1</em> and <em>V2</em>:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2187
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2188
         * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2189
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2190
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2191
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2192
         * <li> [<em>L</em>, <em>S</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2193
         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2194
         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2195
         * <li> [<em>L</em>, <em>C</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2196
         * <li> [<em>L</em>]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2197
         * <li> <code>Locale.ROOT</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2198
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2199
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2200
         * <li>Special cases for Chinese.  When an input <code>Locale</code> has the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2201
         * language "zh" (Chinese) and an empty script value, either "Hans" (Simplified) or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2202
         * "Hant" (Traditional) might be supplied, depending on the country.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2203
         * When the country is "CN" (China) or "SG" (Singapore), "Hans" is supplied.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2204
         * When the country is "HK" (Hong Kong SAR China), "MO" (Macau SAR China),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2205
         * or "TW" (Taiwan), "Hant" is supplied.  For all other countries or when the country
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2206
         * is empty, no script is supplied.  For example, for <code>Locale("zh", "CN")
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2207
         * </code>, the candidate list will be:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2208
         * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2209
         * <li> [<em>L</em>("zh"), <em>S</em>("Hans"), <em>C</em>("CN")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2210
         * <li> [<em>L</em>("zh"), <em>S</em>("Hans")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2211
         * <li> [<em>L</em>("zh"), <em>C</em>("CN")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2212
         * <li> [<em>L</em>("zh")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2213
         * <li> <code>Locale.ROOT</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2214
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2215
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2216
         * For <code>Locale("zh", "TW")</code>, the candidate list will be:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2217
         * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2218
         * <li> [<em>L</em>("zh"), <em>S</em>("Hant"), <em>C</em>("TW")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2219
         * <li> [<em>L</em>("zh"), <em>S</em>("Hant")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2220
         * <li> [<em>L</em>("zh"), <em>C</em>("TW")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2221
         * <li> [<em>L</em>("zh")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2222
         * <li> <code>Locale.ROOT</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2223
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2224
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2225
         * <li>Special cases for Norwegian.  Both <code>Locale("no", "NO",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2226
         * "NY")</code> and <code>Locale("nn", "NO")</code> represent Norwegian
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2227
         * Nynorsk.  When a locale's language is "nn", the standard candidate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2228
         * list is generated up to [<em>L</em>("nn")], and then the following
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2229
         * candidates are added:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2230
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2231
         * <ul><li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("NY")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2232
         * <li> [<em>L</em>("no"), <em>C</em>("NO")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2233
         * <li> [<em>L</em>("no")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2234
         * <li> <code>Locale.ROOT</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2235
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2236
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2237
         * If the locale is exactly <code>Locale("no", "NO", "NY")</code>, it is first
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2238
         * converted to <code>Locale("nn", "NO")</code> and then the above procedure is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2239
         * followed.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2240
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2241
         * <p>Also, Java treats the language "no" as a synonym of Norwegian
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2242
         * Bokm&#xE5;l "nb".  Except for the single case <code>Locale("no",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2243
         * "NO", "NY")</code> (handled above), when an input <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2244
         * has language "no" or "nb", candidate <code>Locale</code>s with
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2245
         * language code "no" and "nb" are interleaved, first using the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2246
         * requested language, then using its synonym. For example,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2247
         * <code>Locale("nb", "NO", "POSIX")</code> generates the following
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2248
         * candidate list:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2249
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2250
         * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2251
         * <li> [<em>L</em>("nb"), <em>C</em>("NO"), <em>V</em>("POSIX")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2252
         * <li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("POSIX")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2253
         * <li> [<em>L</em>("nb"), <em>C</em>("NO")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2254
         * <li> [<em>L</em>("no"), <em>C</em>("NO")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2255
         * <li> [<em>L</em>("nb")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2256
         * <li> [<em>L</em>("no")]
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2257
         * <li> <code>Locale.ROOT</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2258
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2259
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2260
         * <code>Locale("no", "NO", "POSIX")</code> would generate the same list
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2261
         * except that locales with "no" would appear before the corresponding
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2262
         * locales with "nb".</li>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2263
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2264
         * </li>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2265
         * </ol>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2266
         *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
         * <p>The default implementation uses an {@link ArrayList} that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
         * overriding implementations may modify before returning it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
         * caller. However, a subclass must not modify it after it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
         * been returned by <code>getCandidateLocales</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
         * <p>For example, if the given <code>baseName</code> is "Messages"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
         * and the given <code>locale</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
         * <code>List</code> of <code>Locale</code>s:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
         * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
         *     Locale("ja", "", "XX")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
         *     Locale("ja")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
         *     Locale.ROOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
         * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
         * is returned. And if the resource bundles for the "ja" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
         * "" <code>Locale</code>s are found, then the runtime resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
         * lookup path (parent chain) is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
         * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
         *     Messages_ja -> Messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
         * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
         *        the base name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
         *        qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
         *        the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
         * @return a <code>List</code> of candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
         *        <code>Locale</code>s for the given <code>locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
         *        if <code>baseName</code> or <code>locale</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
         *        <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        public List<Locale> getCandidateLocales(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2303
            return new ArrayList<>(CANDIDATES_CACHE.get(locale.getBaseLocale()));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2304
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2305
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2306
        private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2307
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2308
        private static class CandidateListCache extends LocaleObjectCache<BaseLocale, List<Locale>> {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2309
            protected List<Locale> createObject(BaseLocale base) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2310
                String language = base.getLanguage();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2311
                String script = base.getScript();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2312
                String region = base.getRegion();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2313
                String variant = base.getVariant();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2315
                // Special handling for Norwegian
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2316
                boolean isNorwegianBokmal = false;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2317
                boolean isNorwegianNynorsk = false;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2318
                if (language.equals("no")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2319
                    if (region.equals("NO") && variant.equals("NY")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2320
                        variant = "";
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2321
                        isNorwegianNynorsk = true;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2322
                    } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2323
                        isNorwegianBokmal = true;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2324
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2325
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2326
                if (language.equals("nb") || isNorwegianBokmal) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2327
                    List<Locale> tmpList = getDefaultList("nb", script, region, variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2328
                    // Insert a locale replacing "nb" with "no" for every list entry
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2329
                    List<Locale> bokmalList = new LinkedList<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2330
                    for (Locale l : tmpList) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2331
                        bokmalList.add(l);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2332
                        if (l.getLanguage().length() == 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2333
                            break;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2334
                        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2335
                        bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2336
                                l.getVariant(), null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2337
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2338
                    return bokmalList;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2339
                } else if (language.equals("nn") || isNorwegianNynorsk) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2340
                    // Insert no_NO_NY, no_NO, no after nn
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2341
                    List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2342
                    int idx = nynorskList.size() - 1;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2343
                    nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY"));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2344
                    nynorskList.add(idx++, Locale.getInstance("no", "NO", ""));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2345
                    nynorskList.add(idx++, Locale.getInstance("no", "", ""));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2346
                    return nynorskList;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2347
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2348
                // Special handling for Chinese
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2349
                else if (language.equals("zh")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2350
                    if (script.length() == 0 && region.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2351
                        // Supply script for users who want to use zh_Hans/zh_Hant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2352
                        // as bundle names (recommended for Java7+)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2353
                        if (region.equals("TW") || region.equals("HK") || region.equals("MO")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2354
                            script = "Hant";
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2355
                        } else if (region.equals("CN") || region.equals("SG")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2356
                            script = "Hans";
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2357
                        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2358
                    } else if (script.length() > 0 && region.length() == 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2359
                        // Supply region(country) for users who still package Chinese
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2360
                        // bundles using old convension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2361
                        if (script.equals("Hans")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2362
                            region = "CN";
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2363
                        } else if (script.equals("Hant")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2364
                            region = "TW";
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2365
                        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2366
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2367
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2368
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2369
                return getDefaultList(language, script, region, variant);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2371
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2372
            private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2373
                List<String> variants = null;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2374
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2375
                if (variant.length() > 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2376
                    variants = new LinkedList<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2377
                    int idx = variant.length();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2378
                    while (idx != -1) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2379
                        variants.add(variant.substring(0, idx));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2380
                        idx = variant.lastIndexOf('_', --idx);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2381
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2382
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2383
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2384
                List<Locale> list = new LinkedList<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2385
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2386
                if (variants != null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2387
                    for (String v : variants) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2388
                        list.add(Locale.getInstance(language, script, region, v, null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2389
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2390
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2391
                if (region.length() > 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2392
                    list.add(Locale.getInstance(language, script, region, "", null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2393
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2394
                if (script.length() > 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2395
                    list.add(Locale.getInstance(language, script, "", "", null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2396
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2397
                    // With script, after truncating variant, region and script,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2398
                    // start over without script.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2399
                    if (variants != null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2400
                        for (String v : variants) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2401
                            list.add(Locale.getInstance(language, "", region, v, null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2402
                        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2403
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2404
                    if (region.length() > 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2405
                        list.add(Locale.getInstance(language, "", region, "", null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2406
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2407
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2408
                if (language.length() > 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2409
                    list.add(Locale.getInstance(language, "", "", "", null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2410
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2411
                // Add root locale at the end
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2412
                list.add(Locale.ROOT);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2413
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2414
                return list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
         * Returns a <code>Locale</code> to be used as a fallback locale for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
         * further resource bundle searches by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
         * <code>ResourceBundle.getBundle</code> factory method. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
         * is called from the factory method every time when no resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
         * resource bundle has been found for <code>baseName</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
         * <code>locale</code>, where locale is either the parameter for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
         * <code>ResourceBundle.getBundle</code> or the previous fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
         * locale returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
         * <p>The method returns <code>null</code> if no further fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
         * search is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
         * <p>The default implementation returns the {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
         * Locale#getDefault() default <code>Locale</code>} if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
         * <code>locale</code> isn't the default one.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
         * <code>null</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
         *        the base name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
         *        qualified class name for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
         *        <code>ResourceBundle.getBundle</code> has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
         *        unable to find any resource bundles (except for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
         *        base bundle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
         *        the <code>Locale</code> for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
         *        <code>ResourceBundle.getBundle</code> has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
         *        unable to find any resource bundles (except for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
         *        base bundle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
         * @return a <code>Locale</code> for the fallback search,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
         *        or <code>null</code> if no further fallback search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
         *        is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
         *        if <code>baseName</code> or <code>locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
         *        is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        public Locale getFallbackLocale(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
            Locale defaultLocale = Locale.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
            return locale.equals(defaultLocale) ? null : defaultLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
         * Instantiates a resource bundle for the given bundle name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
         * given format and locale, using the given class loader if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
         * necessary. This method returns <code>null</code> if there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
         * resource bundle available for the given parameters. If a resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
         * bundle can't be instantiated due to an unexpected error, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
         * error must be reported by throwing an <code>Error</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
         * <code>Exception</code> rather than simply returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
         * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
         * <p>If the <code>reload</code> flag is <code>true</code>, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
         * indicates that this method is being called because the previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
         * loaded resource bundle has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
         * <p>The default implementation instantiates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
         * <code>ResourceBundle</code> as follows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
         * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
         * <li>The bundle name is obtained by calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
         * #toBundleName(String, Locale) toBundleName(baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
         * locale)}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
         * <li>If <code>format</code> is <code>"java.class"</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
         * {@link Class} specified by the bundle name is loaded by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
         * {@link ClassLoader#loadClass(String)}. Then, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
         * <code>ResourceBundle</code> is instantiated by calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
         * Class#newInstance()}.  Note that the <code>reload</code> flag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
         * ignored for loading class-based resource bundles in this default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
         * implementation.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
         * <li>If <code>format</code> is <code>"java.properties"</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
         * {@link #toResourceName(String, String) toResourceName(bundlename,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
         * "properties")} is called to get the resource name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
         * If <code>reload</code> is <code>true</code>, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
         * ClassLoader#getResource(String) load.getResource} is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
         * to get a {@link URL} for creating a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
         * URLConnection}. This <code>URLConnection</code> is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
         * {@linkplain URLConnection#setUseCaches(boolean) disable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
         * caches} of the underlying resource loading layers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
         * and to {@linkplain URLConnection#getInputStream() get an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
         * <code>InputStream</code>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
         * Otherwise, {@link ClassLoader#getResourceAsStream(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
         * loader.getResourceAsStream} is called to get an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
         * InputStream}. Then, a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
         * PropertyResourceBundle} is constructed with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
         * <code>InputStream</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
         * <li>If <code>format</code> is neither <code>"java.class"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
         * nor <code>"java.properties"</code>, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
         * <code>IllegalArgumentException</code> is thrown.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
         * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
         *        the base bundle name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
         *        qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
         *        the locale for which the resource bundle should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
         *        instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
         * @param format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
         *        the resource bundle format to be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
         * @param loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
         *        the <code>ClassLoader</code> to use to load the bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
         * @param reload
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
         *        the flag to indicate bundle reloading; <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
         *        if reloading an expired resource bundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
         *        <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
         * @return the resource bundle instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
         *        or <code>null</code> if none could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
         *        if <code>bundleName</code>, <code>locale</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
         *        <code>format</code>, or <code>loader</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
         *        <code>null</code>, or if <code>null</code> is returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
         *        {@link #toBundleName(String, Locale) toBundleName}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
         * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
         *        if <code>format</code> is unknown, or if the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
         *        found for the given parameters contains malformed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
         * @exception ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
         *        if the loaded class cannot be cast to <code>ResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
         * @exception IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
         *        if the class or its nullary constructor is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
         *        accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
         * @exception InstantiationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
         *        if the instantiation of a class fails for some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
         *        reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
         * @exception ExceptionInInitializerError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
         *        if the initialization provoked by this method fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
         * @exception SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
         *        If a security manager is present and creation of new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
         *        instances is denied. See {@link Class#newInstance()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
         *        for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
         * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
         *        if an error occurred when reading resources using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
         *        any I/O operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        public ResourceBundle newBundle(String baseName, Locale locale, String format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                                        ClassLoader loader, boolean reload)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                    throws IllegalAccessException, InstantiationException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            String bundleName = toBundleName(baseName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            ResourceBundle bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            if (format.equals("java.class")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                    Class<? extends ResourceBundle> bundleClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                        = (Class<? extends ResourceBundle>)loader.loadClass(bundleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                    // If the class isn't a ResourceBundle subclass, throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                    // ClassCastException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                    if (ResourceBundle.class.isAssignableFrom(bundleClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                        bundle = bundleClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                        throw new ClassCastException(bundleClass.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                                     + " cannot be cast to ResourceBundle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            } else if (format.equals("java.properties")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                final String resourceName = toResourceName(bundleName, "properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                final ClassLoader classLoader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                final boolean reloadFlag = reload;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                InputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                    stream = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                        new PrivilegedExceptionAction<InputStream>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                            public InputStream run() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                                InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                                if (reloadFlag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                                    URL url = classLoader.getResource(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                                    if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                                        URLConnection connection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                                        if (connection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                                            // Disable caches to get fresh data for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                                            // reloading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                                            connection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                                            is = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                                    is = classLoader.getResourceAsStream(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                                return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
                    throw (IOException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                if (stream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                        bundle = new PropertyResourceBundle(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                        stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
                throw new IllegalArgumentException("unknown format: " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
         * Returns the time-to-live (TTL) value for resource bundles that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
         * are loaded under this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
         * <code>ResourceBundle.Control</code>. Positive time-to-live values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
         * specify the number of milliseconds a bundle can remain in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
         * cache without being validated against the source data from which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
         * it was constructed. The value 0 indicates that a bundle must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
         * validated each time it is retrieved from the cache. {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
         * #TTL_DONT_CACHE} specifies that loaded resource bundles are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
         * put in the cache. {@link #TTL_NO_EXPIRATION_CONTROL} specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
         * that loaded resource bundles are put in the cache with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
         * expiration control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
         * <p>The expiration affects only the bundle loading process by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
         * <code>ResourceBundle.getBundle</code> factory method.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
         * if the factory method finds a resource bundle in the cache that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
         * has expired, the factory method calls the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
         * #needsReload(String, Locale, String, ClassLoader, ResourceBundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
         * long) needsReload} method to determine whether the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
         * bundle needs to be reloaded. If <code>needsReload</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
         * <code>true</code>, the cached resource bundle instance is removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
         * from the cache. Otherwise, the instance stays in the cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
         * updated with the new TTL value returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
         * <p>All cached resource bundles are subject to removal from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
         * cache due to memory constraints of the runtime environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
         * Returning a large positive value doesn't mean to lock loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
         * resource bundles in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
         * <p>The default implementation returns {@link #TTL_NO_EXPIRATION_CONTROL}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
         *        the base name of the resource bundle for which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
         *        expiration value is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
         *        the locale of the resource bundle for which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
         *        expiration value is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
         * @return the time (0 or a positive millisecond offset from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
         *        cached time) to get loaded bundles expired in the cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
         *        {@link #TTL_NO_EXPIRATION_CONTROL} to disable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
         *        expiration control, or {@link #TTL_DONT_CACHE} to disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
         *        caching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
         *        if <code>baseName</code> or <code>locale</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
         *        <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        public long getTimeToLive(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
            if (baseName == null || locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
            return TTL_NO_EXPIRATION_CONTROL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
         * Determines if the expired <code>bundle</code> in the cache needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
         * to be reloaded based on the loading time given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
         * <code>loadTime</code> or some other criteria. The method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
         * <code>true</code> if reloading is required; <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
         * otherwise. <code>loadTime</code> is a millisecond offset since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
         * the <a href="Calendar.html#Epoch"> <code>Calendar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
         * Epoch</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
         * The calling <code>ResourceBundle.getBundle</code> factory method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
         * calls this method on the <code>ResourceBundle.Control</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
         * instance used for its current invocation, not on the instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
         * used in the invocation that originally loaded the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
         * bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
         * <p>The default implementation compares <code>loadTime</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
         * the last modified time of the source data of the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
         * bundle. If it's determined that the source data has been modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
         * since <code>loadTime</code>, <code>true</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
         * returned. Otherwise, <code>false</code> is returned. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
         * implementation assumes that the given <code>format</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
         * same string as its file suffix if it's not one of the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
         * formats, <code>"java.class"</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
         * <code>"java.properties"</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
         *        the base bundle name of the resource bundle, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
         *        fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
         *        the locale for which the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
         *        should be instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
         * @param format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
         *        the resource bundle format to be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
         * @param loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
         *        the <code>ClassLoader</code> to use to load the bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
         * @param bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
         *        the resource bundle instance that has been expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
         *        in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
         * @param loadTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
         *        the time when <code>bundle</code> was loaded and put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
         *        in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
         * @return <code>true</code> if the expired bundle needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
         *        reloaded; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
         *        if <code>baseName</code>, <code>locale</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
         *        <code>format</code>, <code>loader</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
         *        <code>bundle</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        public boolean needsReload(String baseName, Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
                                   String format, ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
                                   ResourceBundle bundle, long loadTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
            if (bundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
            if (format.equals("java.class") || format.equals("java.properties")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                format = format.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
            boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                String resourceName = toResourceName(toBundleName(baseName, locale), format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                URL url = loader.getResource(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                    long lastModified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                    URLConnection connection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                    if (connection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                        // disable caches to get the correct data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                        connection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                        if (connection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                            JarEntry ent = ((JarURLConnection)connection).getJarEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                            if (ent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                                lastModified = ent.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
                                if (lastModified == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                                    lastModified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                            lastModified = connection.getLastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                    result = lastModified >= loadTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                throw npe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                // ignore other exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
         * Converts the given <code>baseName</code> and <code>locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
         * to the bundle name. This method is called from the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
         * implementation of the {@link #newBundle(String, Locale, String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
         * ClassLoader, boolean) newBundle} and {@link #needsReload(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
         * Locale, String, ClassLoader, ResourceBundle, long) needsReload}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
         * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
         * <p>This implementation returns the following value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
         * <pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2773
         *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
         * </pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2775
         * where <code>language</code>, <code>script</code>, <code>country</code>,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2776
         * and <code>variant</code> are the language, script, country, and variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2777
         * values of <code>locale</code>, respectively. Final component values that
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2778
         * are empty Strings are omitted along with the preceding '_'.  When the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2779
         * script is empty, the script value is ommitted along with the preceding '_'.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2780
         * If all of the values are empty strings, then <code>baseName</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
         * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
         * <p>For example, if <code>baseName</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
         * <code>"baseName"</code> and <code>locale</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
         * <code>"baseName_ja_&thinsp;_XX"</code> is returned. If the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
         * locale is <code>Locale("en")</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
         * <code>"baseName_en"</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
         * <p>Overriding this method allows applications to use different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
         * conventions in the organization and packaging of localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
         * resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
         *        the base name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
         *        qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
         *        the locale for which a resource bundle should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
         *        loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
         * @return the bundle name for the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
         *        if <code>baseName</code> or <code>locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
         *        is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
        public String toBundleName(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
            if (locale == Locale.ROOT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                return baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            String language = locale.getLanguage();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2811
            String script = locale.getScript();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
            String country = locale.getCountry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
            String variant = locale.getVariant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            if (language == "" && country == "" && variant == "") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                return baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
            StringBuilder sb = new StringBuilder(baseName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
            sb.append('_');
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2821
            if (script != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2822
                if (variant != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2823
                    sb.append(language).append('_').append(script).append('_').append(country).append('_').append(variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2824
                } else if (country != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2825
                    sb.append(language).append('_').append(script).append('_').append(country);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2826
                } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2827
                    sb.append(language).append('_').append(script);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2828
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
            } else {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2830
                if (variant != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2831
                    sb.append(language).append('_').append(country).append('_').append(variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2832
                } else if (country != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2833
                    sb.append(language).append('_').append(country);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2834
                } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2835
                    sb.append(language);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2836
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
         * Converts the given <code>bundleName</code> to the form required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
         * by the {@link ClassLoader#getResource ClassLoader.getResource}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
         * method by replacing all occurrences of <code>'.'</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
         * <code>bundleName</code> with <code>'/'</code> and appending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
         * <code>'.'</code> and the given file <code>suffix</code>. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
         * example, if <code>bundleName</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
         * <code>"foo.bar.MyResources_ja_JP"</code> and <code>suffix</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
         * is <code>"properties"</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
         * <code>"foo/bar/MyResources_ja_JP.properties"</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
         * @param bundleName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
         *        the bundle name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
         * @param suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
         *        the file type suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
         * @return the converted resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
         * @exception NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
         *         if <code>bundleName</code> or <code>suffix</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
         *         is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        public final String toResourceName(String bundleName, String suffix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
            StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
            sb.append(bundleName.replace('.', '/')).append('.').append(suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
    private static class SingleFormatControl extends Control {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        private static final Control PROPERTIES_ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            = new SingleFormatControl(FORMAT_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        private static final Control CLASS_ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            = new SingleFormatControl(FORMAT_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
        private final List<String> formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        protected SingleFormatControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
            this.formats = formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
        public List<String> getFormats(String baseName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
            return formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
    private static final class NoFallbackControl extends SingleFormatControl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
        private static final Control NO_FALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
            = new NoFallbackControl(FORMAT_DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
        private static final Control PROPERTIES_ONLY_NO_FALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
            = new NoFallbackControl(FORMAT_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
        private static final Control CLASS_ONLY_NO_FALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
            = new NoFallbackControl(FORMAT_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        protected NoFallbackControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
            super(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        public Locale getFallbackLocale(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
            if (baseName == null || locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
}