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