rt/emul/compact/src/main/java/java/util/ResourceBundle.java
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1337 c794024954b5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/ResourceBundle.java	Thu Oct 03 15:40:35 2013 +0200
     1.3 @@ -0,0 +1,2911 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
    1.31 + * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
    1.32 + *
    1.33 + * The original version of this source code and documentation
    1.34 + * is copyrighted and owned by Taligent, Inc., a wholly-owned
    1.35 + * subsidiary of IBM. These materials are provided under terms
    1.36 + * of a License Agreement between Taligent and Sun. This technology
    1.37 + * is protected by multiple US and International patents.
    1.38 + *
    1.39 + * This notice and attribution to Taligent may not be removed.
    1.40 + * Taligent is a registered trademark of Taligent, Inc.
    1.41 + *
    1.42 + */
    1.43 +
    1.44 +package java.util;
    1.45 +
    1.46 +import java.io.IOException;
    1.47 +import java.io.InputStream;
    1.48 +import java.lang.ref.ReferenceQueue;
    1.49 +import java.lang.ref.SoftReference;
    1.50 +import java.lang.ref.WeakReference;
    1.51 +import java.net.JarURLConnection;
    1.52 +import java.net.URL;
    1.53 +import java.net.URLConnection;
    1.54 +import java.security.AccessController;
    1.55 +import java.security.PrivilegedAction;
    1.56 +import java.security.PrivilegedActionException;
    1.57 +import java.security.PrivilegedExceptionAction;
    1.58 +import java.util.concurrent.ConcurrentHashMap;
    1.59 +import java.util.concurrent.ConcurrentMap;
    1.60 +import java.util.jar.JarEntry;
    1.61 +
    1.62 +import sun.util.locale.BaseLocale;
    1.63 +import sun.util.locale.LocaleObjectCache;
    1.64 +
    1.65 +
    1.66 +/**
    1.67 + *
    1.68 + * Resource bundles contain locale-specific objects.  When your program needs a
    1.69 + * locale-specific resource, a <code>String</code> for example, your program can
    1.70 + * load it from the resource bundle that is appropriate for the current user's
    1.71 + * locale. In this way, you can write program code that is largely independent
    1.72 + * of the user's locale isolating most, if not all, of the locale-specific
    1.73 + * information in resource bundles.
    1.74 + *
    1.75 + * <p>
    1.76 + * This allows you to write programs that can:
    1.77 + * <UL type=SQUARE>
    1.78 + * <LI> be easily localized, or translated, into different languages
    1.79 + * <LI> handle multiple locales at once
    1.80 + * <LI> be easily modified later to support even more locales
    1.81 + * </UL>
    1.82 + *
    1.83 + * <P>
    1.84 + * Resource bundles belong to families whose members share a common base
    1.85 + * name, but whose names also have additional components that identify
    1.86 + * their locales. For example, the base name of a family of resource
    1.87 + * bundles might be "MyResources". The family should have a default
    1.88 + * resource bundle which simply has the same name as its family -
    1.89 + * "MyResources" - and will be used as the bundle of last resort if a
    1.90 + * specific locale is not supported. The family can then provide as
    1.91 + * many locale-specific members as needed, for example a German one
    1.92 + * named "MyResources_de".
    1.93 + *
    1.94 + * <P>
    1.95 + * Each resource bundle in a family contains the same items, but the items have
    1.96 + * been translated for the locale represented by that resource bundle.
    1.97 + * For example, both "MyResources" and "MyResources_de" may have a
    1.98 + * <code>String</code> that's used on a button for canceling operations.
    1.99 + * In "MyResources" the <code>String</code> may contain "Cancel" and in
   1.100 + * "MyResources_de" it may contain "Abbrechen".
   1.101 + *
   1.102 + * <P>
   1.103 + * If there are different resources for different countries, you
   1.104 + * can make specializations: for example, "MyResources_de_CH" contains objects for
   1.105 + * the German language (de) in Switzerland (CH). If you want to only
   1.106 + * modify some of the resources
   1.107 + * in the specialization, you can do so.
   1.108 + *
   1.109 + * <P>
   1.110 + * When your program needs a locale-specific object, it loads
   1.111 + * the <code>ResourceBundle</code> class using the
   1.112 + * {@link #getBundle(java.lang.String, java.util.Locale) getBundle}
   1.113 + * method:
   1.114 + * <blockquote>
   1.115 + * <pre>
   1.116 + * ResourceBundle myResources =
   1.117 + *      ResourceBundle.getBundle("MyResources", currentLocale);
   1.118 + * </pre>
   1.119 + * </blockquote>
   1.120 + *
   1.121 + * <P>
   1.122 + * Resource bundles contain key/value pairs. The keys uniquely
   1.123 + * identify a locale-specific object in the bundle. Here's an
   1.124 + * example of a <code>ListResourceBundle</code> that contains
   1.125 + * two key/value pairs:
   1.126 + * <blockquote>
   1.127 + * <pre>
   1.128 + * public class MyResources extends ListResourceBundle {
   1.129 + *     protected Object[][] getContents() {
   1.130 + *         return new Object[][] {
   1.131 + *             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
   1.132 + *             {"OkKey", "OK"},
   1.133 + *             {"CancelKey", "Cancel"},
   1.134 + *             // END OF MATERIAL TO LOCALIZE
   1.135 + *        };
   1.136 + *     }
   1.137 + * }
   1.138 + * </pre>
   1.139 + * </blockquote>
   1.140 + * Keys are always <code>String</code>s.
   1.141 + * In this example, the keys are "OkKey" and "CancelKey".
   1.142 + * In the above example, the values
   1.143 + * are also <code>String</code>s--"OK" and "Cancel"--but
   1.144 + * they don't have to be. The values can be any type of object.
   1.145 + *
   1.146 + * <P>
   1.147 + * You retrieve an object from resource bundle using the appropriate
   1.148 + * getter method. Because "OkKey" and "CancelKey"
   1.149 + * are both strings, you would use <code>getString</code> to retrieve them:
   1.150 + * <blockquote>
   1.151 + * <pre>
   1.152 + * button1 = new Button(myResources.getString("OkKey"));
   1.153 + * button2 = new Button(myResources.getString("CancelKey"));
   1.154 + * </pre>
   1.155 + * </blockquote>
   1.156 + * The getter methods all require the key as an argument and return
   1.157 + * the object if found. If the object is not found, the getter method
   1.158 + * throws a <code>MissingResourceException</code>.
   1.159 + *
   1.160 + * <P>
   1.161 + * Besides <code>getString</code>, <code>ResourceBundle</code> also provides
   1.162 + * a method for getting string arrays, <code>getStringArray</code>,
   1.163 + * as well as a generic <code>getObject</code> method for any other
   1.164 + * type of object. When using <code>getObject</code>, you'll
   1.165 + * have to cast the result to the appropriate type. For example:
   1.166 + * <blockquote>
   1.167 + * <pre>
   1.168 + * int[] myIntegers = (int[]) myResources.getObject("intList");
   1.169 + * </pre>
   1.170 + * </blockquote>
   1.171 + *
   1.172 + * <P>
   1.173 + * The Java Platform provides two subclasses of <code>ResourceBundle</code>,
   1.174 + * <code>ListResourceBundle</code> and <code>PropertyResourceBundle</code>,
   1.175 + * that provide a fairly simple way to create resources.
   1.176 + * As you saw briefly in a previous example, <code>ListResourceBundle</code>
   1.177 + * manages its resource as a list of key/value pairs.
   1.178 + * <code>PropertyResourceBundle</code> uses a properties file to manage
   1.179 + * its resources.
   1.180 + *
   1.181 + * <p>
   1.182 + * If <code>ListResourceBundle</code> or <code>PropertyResourceBundle</code>
   1.183 + * do not suit your needs, you can write your own <code>ResourceBundle</code>
   1.184 + * subclass.  Your subclasses must override two methods: <code>handleGetObject</code>
   1.185 + * and <code>getKeys()</code>.
   1.186 + *
   1.187 + * <h4>ResourceBundle.Control</h4>
   1.188 + *
   1.189 + * The {@link ResourceBundle.Control} class provides information necessary
   1.190 + * to perform the bundle loading process by the <code>getBundle</code>
   1.191 + * factory methods that take a <code>ResourceBundle.Control</code>
   1.192 + * instance. You can implement your own subclass in order to enable
   1.193 + * non-standard resource bundle formats, change the search strategy, or
   1.194 + * define caching parameters. Refer to the descriptions of the class and the
   1.195 + * {@link #getBundle(String, Locale, ClassLoader, Control) getBundle}
   1.196 + * factory method for details.
   1.197 + *
   1.198 + * <h4>Cache Management</h4>
   1.199 + *
   1.200 + * Resource bundle instances created by the <code>getBundle</code> factory
   1.201 + * methods are cached by default, and the factory methods return the same
   1.202 + * resource bundle instance multiple times if it has been
   1.203 + * cached. <code>getBundle</code> clients may clear the cache, manage the
   1.204 + * lifetime of cached resource bundle instances using time-to-live values,
   1.205 + * or specify not to cache resource bundle instances. Refer to the
   1.206 + * descriptions of the {@linkplain #getBundle(String, Locale, ClassLoader,
   1.207 + * Control) <code>getBundle</code> factory method}, {@link
   1.208 + * #clearCache(ClassLoader) clearCache}, {@link
   1.209 + * Control#getTimeToLive(String, Locale)
   1.210 + * ResourceBundle.Control.getTimeToLive}, and {@link
   1.211 + * Control#needsReload(String, Locale, String, ClassLoader, ResourceBundle,
   1.212 + * long) ResourceBundle.Control.needsReload} for details.
   1.213 + *
   1.214 + * <h4>Example</h4>
   1.215 + *
   1.216 + * The following is a very simple example of a <code>ResourceBundle</code>
   1.217 + * subclass, <code>MyResources</code>, that manages two resources (for a larger number of
   1.218 + * resources you would probably use a <code>Map</code>).
   1.219 + * Notice that you don't need to supply a value if
   1.220 + * a "parent-level" <code>ResourceBundle</code> handles the same
   1.221 + * key with the same value (as for the okKey below).
   1.222 + * <blockquote>
   1.223 + * <pre>
   1.224 + * // default (English language, United States)
   1.225 + * public class MyResources extends ResourceBundle {
   1.226 + *     public Object handleGetObject(String key) {
   1.227 + *         if (key.equals("okKey")) return "Ok";
   1.228 + *         if (key.equals("cancelKey")) return "Cancel";
   1.229 + *         return null;
   1.230 + *     }
   1.231 + *
   1.232 + *     public Enumeration&lt;String&gt; getKeys() {
   1.233 + *         return Collections.enumeration(keySet());
   1.234 + *     }
   1.235 + *
   1.236 + *     // Overrides handleKeySet() so that the getKeys() implementation
   1.237 + *     // can rely on the keySet() value.
   1.238 + *     protected Set&lt;String&gt; handleKeySet() {
   1.239 + *         return new HashSet&lt;String&gt;(Arrays.asList("okKey", "cancelKey"));
   1.240 + *     }
   1.241 + * }
   1.242 + *
   1.243 + * // German language
   1.244 + * public class MyResources_de extends MyResources {
   1.245 + *     public Object handleGetObject(String key) {
   1.246 + *         // don't need okKey, since parent level handles it.
   1.247 + *         if (key.equals("cancelKey")) return "Abbrechen";
   1.248 + *         return null;
   1.249 + *     }
   1.250 + *
   1.251 + *     protected Set&lt;String&gt; handleKeySet() {
   1.252 + *         return new HashSet&lt;String&gt;(Arrays.asList("cancelKey"));
   1.253 + *     }
   1.254 + * }
   1.255 + * </pre>
   1.256 + * </blockquote>
   1.257 + * You do not have to restrict yourself to using a single family of
   1.258 + * <code>ResourceBundle</code>s. For example, you could have a set of bundles for
   1.259 + * exception messages, <code>ExceptionResources</code>
   1.260 + * (<code>ExceptionResources_fr</code>, <code>ExceptionResources_de</code>, ...),
   1.261 + * and one for widgets, <code>WidgetResource</code> (<code>WidgetResources_fr</code>,
   1.262 + * <code>WidgetResources_de</code>, ...); breaking up the resources however you like.
   1.263 + *
   1.264 + * @see ListResourceBundle
   1.265 + * @see PropertyResourceBundle
   1.266 + * @see MissingResourceException
   1.267 + * @since JDK1.1
   1.268 + */
   1.269 +public abstract class ResourceBundle {
   1.270 +
   1.271 +    /** initial size of the bundle cache */
   1.272 +    private static final int INITIAL_CACHE_SIZE = 32;
   1.273 +
   1.274 +    /** constant indicating that no resource bundle exists */
   1.275 +    private static final ResourceBundle NONEXISTENT_BUNDLE = new ResourceBundle() {
   1.276 +            public Enumeration<String> getKeys() { return null; }
   1.277 +            protected Object handleGetObject(String key) { return null; }
   1.278 +            public String toString() { return "NONEXISTENT_BUNDLE"; }
   1.279 +        };
   1.280 +
   1.281 +
   1.282 +    /**
   1.283 +     * The cache is a map from cache keys (with bundle base name, locale, and
   1.284 +     * class loader) to either a resource bundle or NONEXISTENT_BUNDLE wrapped by a
   1.285 +     * BundleReference.
   1.286 +     *
   1.287 +     * The cache is a ConcurrentMap, allowing the cache to be searched
   1.288 +     * concurrently by multiple threads.  This will also allow the cache keys
   1.289 +     * to be reclaimed along with the ClassLoaders they reference.
   1.290 +     *
   1.291 +     * This variable would be better named "cache", but we keep the old
   1.292 +     * name for compatibility with some workarounds for bug 4212439.
   1.293 +     */
   1.294 +    private static final ConcurrentMap<CacheKey, BundleReference> cacheList
   1.295 +        = new ConcurrentHashMap<>(INITIAL_CACHE_SIZE);
   1.296 +
   1.297 +    /**
   1.298 +     * Queue for reference objects referring to class loaders or bundles.
   1.299 +     */
   1.300 +    private static final ReferenceQueue referenceQueue = new ReferenceQueue();
   1.301 +
   1.302 +    /**
   1.303 +     * The parent bundle of this bundle.
   1.304 +     * The parent bundle is searched by {@link #getObject getObject}
   1.305 +     * when this bundle does not contain a particular resource.
   1.306 +     */
   1.307 +    protected ResourceBundle parent = null;
   1.308 +
   1.309 +    /**
   1.310 +     * The locale for this bundle.
   1.311 +     */
   1.312 +    private Locale locale = null;
   1.313 +
   1.314 +    /**
   1.315 +     * The base bundle name for this bundle.
   1.316 +     */
   1.317 +    private String name;
   1.318 +
   1.319 +    /**
   1.320 +     * The flag indicating this bundle has expired in the cache.
   1.321 +     */
   1.322 +    private volatile boolean expired;
   1.323 +
   1.324 +    /**
   1.325 +     * The back link to the cache key. null if this bundle isn't in
   1.326 +     * the cache (yet) or has expired.
   1.327 +     */
   1.328 +    private volatile CacheKey cacheKey;
   1.329 +
   1.330 +    /**
   1.331 +     * A Set of the keys contained only in this ResourceBundle.
   1.332 +     */
   1.333 +    private volatile Set<String> keySet;
   1.334 +
   1.335 +    /**
   1.336 +     * Sole constructor.  (For invocation by subclass constructors, typically
   1.337 +     * implicit.)
   1.338 +     */
   1.339 +    public ResourceBundle() {
   1.340 +    }
   1.341 +
   1.342 +    /**
   1.343 +     * Gets a string for the given key from this resource bundle or one of its parents.
   1.344 +     * Calling this method is equivalent to calling
   1.345 +     * <blockquote>
   1.346 +     * <code>(String) {@link #getObject(java.lang.String) getObject}(key)</code>.
   1.347 +     * </blockquote>
   1.348 +     *
   1.349 +     * @param key the key for the desired string
   1.350 +     * @exception NullPointerException if <code>key</code> is <code>null</code>
   1.351 +     * @exception MissingResourceException if no object for the given key can be found
   1.352 +     * @exception ClassCastException if the object found for the given key is not a string
   1.353 +     * @return the string for the given key
   1.354 +     */
   1.355 +    public final String getString(String key) {
   1.356 +        return (String) getObject(key);
   1.357 +    }
   1.358 +
   1.359 +    /**
   1.360 +     * Gets a string array for the given key from this resource bundle or one of its parents.
   1.361 +     * Calling this method is equivalent to calling
   1.362 +     * <blockquote>
   1.363 +     * <code>(String[]) {@link #getObject(java.lang.String) getObject}(key)</code>.
   1.364 +     * </blockquote>
   1.365 +     *
   1.366 +     * @param key the key for the desired string array
   1.367 +     * @exception NullPointerException if <code>key</code> is <code>null</code>
   1.368 +     * @exception MissingResourceException if no object for the given key can be found
   1.369 +     * @exception ClassCastException if the object found for the given key is not a string array
   1.370 +     * @return the string array for the given key
   1.371 +     */
   1.372 +    public final String[] getStringArray(String key) {
   1.373 +        return (String[]) getObject(key);
   1.374 +    }
   1.375 +
   1.376 +    /**
   1.377 +     * Gets an object for the given key from this resource bundle or one of its parents.
   1.378 +     * This method first tries to obtain the object from this resource bundle using
   1.379 +     * {@link #handleGetObject(java.lang.String) handleGetObject}.
   1.380 +     * If not successful, and the parent resource bundle is not null,
   1.381 +     * it calls the parent's <code>getObject</code> method.
   1.382 +     * If still not successful, it throws a MissingResourceException.
   1.383 +     *
   1.384 +     * @param key the key for the desired object
   1.385 +     * @exception NullPointerException if <code>key</code> is <code>null</code>
   1.386 +     * @exception MissingResourceException if no object for the given key can be found
   1.387 +     * @return the object for the given key
   1.388 +     */
   1.389 +    public final Object getObject(String key) {
   1.390 +        Object obj = handleGetObject(key);
   1.391 +        if (obj == null) {
   1.392 +            if (parent != null) {
   1.393 +                obj = parent.getObject(key);
   1.394 +            }
   1.395 +            if (obj == null)
   1.396 +                throw new MissingResourceException("Can't find resource for bundle "
   1.397 +                                                   +this.getClass().getName()
   1.398 +                                                   +", key "+key,
   1.399 +                                                   this.getClass().getName(),
   1.400 +                                                   key);
   1.401 +        }
   1.402 +        return obj;
   1.403 +    }
   1.404 +
   1.405 +    /**
   1.406 +     * Returns the locale of this resource bundle. This method can be used after a
   1.407 +     * call to getBundle() to determine whether the resource bundle returned really
   1.408 +     * corresponds to the requested locale or is a fallback.
   1.409 +     *
   1.410 +     * @return the locale of this resource bundle
   1.411 +     */
   1.412 +    public Locale getLocale() {
   1.413 +        return locale;
   1.414 +    }
   1.415 +
   1.416 +    /*
   1.417 +     * Automatic determination of the ClassLoader to be used to load
   1.418 +     * resources on behalf of the client.  N.B. The client is getLoader's
   1.419 +     * caller's caller.
   1.420 +     */
   1.421 +    private static ClassLoader getLoader() {
   1.422 +        Class[] stack = getClassContext();
   1.423 +        /* Magic number 2 identifies our caller's caller */
   1.424 +        Class c = stack[2];
   1.425 +        ClassLoader cl = (c == null) ? null : c.getClassLoader();
   1.426 +        if (cl == null) {
   1.427 +            // When the caller's loader is the boot class loader, cl is null
   1.428 +            // here. In that case, ClassLoader.getSystemClassLoader() may
   1.429 +            // return the same class loader that the application is
   1.430 +            // using. We therefore use a wrapper ClassLoader to create a
   1.431 +            // separate scope for bundles loaded on behalf of the Java
   1.432 +            // runtime so that these bundles cannot be returned from the
   1.433 +            // cache to the application (5048280).
   1.434 +            cl = RBClassLoader.INSTANCE;
   1.435 +        }
   1.436 +        return cl;
   1.437 +    }
   1.438 +
   1.439 +    private static native Class[] getClassContext();
   1.440 +
   1.441 +    /**
   1.442 +     * A wrapper of ClassLoader.getSystemClassLoader().
   1.443 +     */
   1.444 +    private static class RBClassLoader extends ClassLoader {
   1.445 +        private static final RBClassLoader INSTANCE = AccessController.doPrivileged(
   1.446 +                    new PrivilegedAction<RBClassLoader>() {
   1.447 +                        public RBClassLoader run() {
   1.448 +                            return new RBClassLoader();
   1.449 +                        }
   1.450 +                    });
   1.451 +        private static final ClassLoader loader = ClassLoader.getSystemClassLoader();
   1.452 +
   1.453 +        private RBClassLoader() {
   1.454 +        }
   1.455 +        public Class<?> loadClass(String name) throws ClassNotFoundException {
   1.456 +            if (loader != null) {
   1.457 +                return loader.loadClass(name);
   1.458 +            }
   1.459 +            return Class.forName(name);
   1.460 +        }
   1.461 +        public URL getResource(String name) {
   1.462 +            if (loader != null) {
   1.463 +                return loader.getResource(name);
   1.464 +            }
   1.465 +            return ClassLoader.getSystemResource(name);
   1.466 +        }
   1.467 +        public InputStream getResourceAsStream(String name) {
   1.468 +            if (loader != null) {
   1.469 +                return loader.getResourceAsStream(name);
   1.470 +            }
   1.471 +            return ClassLoader.getSystemResourceAsStream(name);
   1.472 +        }
   1.473 +    }
   1.474 +
   1.475 +    /**
   1.476 +     * Sets the parent bundle of this bundle.
   1.477 +     * The parent bundle is searched by {@link #getObject getObject}
   1.478 +     * when this bundle does not contain a particular resource.
   1.479 +     *
   1.480 +     * @param parent this bundle's parent bundle.
   1.481 +     */
   1.482 +    protected void setParent(ResourceBundle parent) {
   1.483 +        assert parent != NONEXISTENT_BUNDLE;
   1.484 +        this.parent = parent;
   1.485 +    }
   1.486 +
   1.487 +    /**
   1.488 +     * Key used for cached resource bundles.  The key checks the base
   1.489 +     * name, the locale, and the class loader to determine if the
   1.490 +     * resource is a match to the requested one. The loader may be
   1.491 +     * null, but the base name and the locale must have a non-null
   1.492 +     * value.
   1.493 +     */
   1.494 +    private static final class CacheKey implements Cloneable {
   1.495 +        // These three are the actual keys for lookup in Map.
   1.496 +        private String name;
   1.497 +        private Locale locale;
   1.498 +        private LoaderReference loaderRef;
   1.499 +
   1.500 +        // bundle format which is necessary for calling
   1.501 +        // Control.needsReload().
   1.502 +        private String format;
   1.503 +
   1.504 +        // These time values are in CacheKey so that NONEXISTENT_BUNDLE
   1.505 +        // doesn't need to be cloned for caching.
   1.506 +
   1.507 +        // The time when the bundle has been loaded
   1.508 +        private volatile long loadTime;
   1.509 +
   1.510 +        // The time when the bundle expires in the cache, or either
   1.511 +        // Control.TTL_DONT_CACHE or Control.TTL_NO_EXPIRATION_CONTROL.
   1.512 +        private volatile long expirationTime;
   1.513 +
   1.514 +        // Placeholder for an error report by a Throwable
   1.515 +        private Throwable cause;
   1.516 +
   1.517 +        // Hash code value cache to avoid recalculating the hash code
   1.518 +        // of this instance.
   1.519 +        private int hashCodeCache;
   1.520 +
   1.521 +        CacheKey(String baseName, Locale locale, ClassLoader loader) {
   1.522 +            this.name = baseName;
   1.523 +            this.locale = locale;
   1.524 +            if (loader == null) {
   1.525 +                this.loaderRef = null;
   1.526 +            } else {
   1.527 +                loaderRef = new LoaderReference(loader, referenceQueue, this);
   1.528 +            }
   1.529 +            calculateHashCode();
   1.530 +        }
   1.531 +
   1.532 +        String getName() {
   1.533 +            return name;
   1.534 +        }
   1.535 +
   1.536 +        CacheKey setName(String baseName) {
   1.537 +            if (!this.name.equals(baseName)) {
   1.538 +                this.name = baseName;
   1.539 +                calculateHashCode();
   1.540 +            }
   1.541 +            return this;
   1.542 +        }
   1.543 +
   1.544 +        Locale getLocale() {
   1.545 +            return locale;
   1.546 +        }
   1.547 +
   1.548 +        CacheKey setLocale(Locale locale) {
   1.549 +            if (!this.locale.equals(locale)) {
   1.550 +                this.locale = locale;
   1.551 +                calculateHashCode();
   1.552 +            }
   1.553 +            return this;
   1.554 +        }
   1.555 +
   1.556 +        ClassLoader getLoader() {
   1.557 +            return (loaderRef != null) ? loaderRef.get() : null;
   1.558 +        }
   1.559 +
   1.560 +        public boolean equals(Object other) {
   1.561 +            if (this == other) {
   1.562 +                return true;
   1.563 +            }
   1.564 +            try {
   1.565 +                final CacheKey otherEntry = (CacheKey)other;
   1.566 +                //quick check to see if they are not equal
   1.567 +                if (hashCodeCache != otherEntry.hashCodeCache) {
   1.568 +                    return false;
   1.569 +                }
   1.570 +                //are the names the same?
   1.571 +                if (!name.equals(otherEntry.name)) {
   1.572 +                    return false;
   1.573 +                }
   1.574 +                // are the locales the same?
   1.575 +                if (!locale.equals(otherEntry.locale)) {
   1.576 +                    return false;
   1.577 +                }
   1.578 +                //are refs (both non-null) or (both null)?
   1.579 +                if (loaderRef == null) {
   1.580 +                    return otherEntry.loaderRef == null;
   1.581 +                }
   1.582 +                ClassLoader loader = loaderRef.get();
   1.583 +                return (otherEntry.loaderRef != null)
   1.584 +                        // with a null reference we can no longer find
   1.585 +                        // out which class loader was referenced; so
   1.586 +                        // treat it as unequal
   1.587 +                        && (loader != null)
   1.588 +                        && (loader == otherEntry.loaderRef.get());
   1.589 +            } catch (NullPointerException e) {
   1.590 +            } catch (ClassCastException e) {
   1.591 +            }
   1.592 +            return false;
   1.593 +        }
   1.594 +
   1.595 +        public int hashCode() {
   1.596 +            return hashCodeCache;
   1.597 +        }
   1.598 +
   1.599 +        private void calculateHashCode() {
   1.600 +            hashCodeCache = name.hashCode() << 3;
   1.601 +            hashCodeCache ^= locale.hashCode();
   1.602 +            ClassLoader loader = getLoader();
   1.603 +            if (loader != null) {
   1.604 +                hashCodeCache ^= loader.hashCode();
   1.605 +            }
   1.606 +        }
   1.607 +
   1.608 +        public Object clone() {
   1.609 +            try {
   1.610 +                CacheKey clone = (CacheKey) super.clone();
   1.611 +                if (loaderRef != null) {
   1.612 +                    clone.loaderRef = new LoaderReference(loaderRef.get(),
   1.613 +                                                          referenceQueue, clone);
   1.614 +                }
   1.615 +                // Clear the reference to a Throwable
   1.616 +                clone.cause = null;
   1.617 +                return clone;
   1.618 +            } catch (CloneNotSupportedException e) {
   1.619 +                //this should never happen
   1.620 +                throw new InternalError();
   1.621 +            }
   1.622 +        }
   1.623 +
   1.624 +        String getFormat() {
   1.625 +            return format;
   1.626 +        }
   1.627 +
   1.628 +        void setFormat(String format) {
   1.629 +            this.format = format;
   1.630 +        }
   1.631 +
   1.632 +        private void setCause(Throwable cause) {
   1.633 +            if (this.cause == null) {
   1.634 +                this.cause = cause;
   1.635 +            } else {
   1.636 +                // Override the cause if the previous one is
   1.637 +                // ClassNotFoundException.
   1.638 +                if (this.cause instanceof ClassNotFoundException) {
   1.639 +                    this.cause = cause;
   1.640 +                }
   1.641 +            }
   1.642 +        }
   1.643 +
   1.644 +        private Throwable getCause() {
   1.645 +            return cause;
   1.646 +        }
   1.647 +
   1.648 +        public String toString() {
   1.649 +            String l = locale.toString();
   1.650 +            if (l.length() == 0) {
   1.651 +                if (locale.getVariant().length() != 0) {
   1.652 +                    l = "__" + locale.getVariant();
   1.653 +                } else {
   1.654 +                    l = "\"\"";
   1.655 +                }
   1.656 +            }
   1.657 +            return "CacheKey[" + name + ", lc=" + l + ", ldr=" + getLoader()
   1.658 +                + "(format=" + format + ")]";
   1.659 +        }
   1.660 +    }
   1.661 +
   1.662 +    /**
   1.663 +     * The common interface to get a CacheKey in LoaderReference and
   1.664 +     * BundleReference.
   1.665 +     */
   1.666 +    private static interface CacheKeyReference {
   1.667 +        public CacheKey getCacheKey();
   1.668 +    }
   1.669 +
   1.670 +    /**
   1.671 +     * References to class loaders are weak references, so that they can be
   1.672 +     * garbage collected when nobody else is using them. The ResourceBundle
   1.673 +     * class has no reason to keep class loaders alive.
   1.674 +     */
   1.675 +    private static final class LoaderReference extends WeakReference<ClassLoader>
   1.676 +                                               implements CacheKeyReference {
   1.677 +        private CacheKey cacheKey;
   1.678 +
   1.679 +        LoaderReference(ClassLoader referent, ReferenceQueue q, CacheKey key) {
   1.680 +            super(referent, q);
   1.681 +            cacheKey = key;
   1.682 +        }
   1.683 +
   1.684 +        public CacheKey getCacheKey() {
   1.685 +            return cacheKey;
   1.686 +        }
   1.687 +    }
   1.688 +
   1.689 +    /**
   1.690 +     * References to bundles are soft references so that they can be garbage
   1.691 +     * collected when they have no hard references.
   1.692 +     */
   1.693 +    private static final class BundleReference extends SoftReference<ResourceBundle>
   1.694 +                                               implements CacheKeyReference {
   1.695 +        private CacheKey cacheKey;
   1.696 +
   1.697 +        BundleReference(ResourceBundle referent, ReferenceQueue q, CacheKey key) {
   1.698 +            super(referent, q);
   1.699 +            cacheKey = key;
   1.700 +        }
   1.701 +
   1.702 +        public CacheKey getCacheKey() {
   1.703 +            return cacheKey;
   1.704 +        }
   1.705 +    }
   1.706 +
   1.707 +    /**
   1.708 +     * Gets a resource bundle using the specified base name, the default locale,
   1.709 +     * and the caller's class loader. Calling this method is equivalent to calling
   1.710 +     * <blockquote>
   1.711 +     * <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>,
   1.712 +     * </blockquote>
   1.713 +     * except that <code>getClassLoader()</code> is run with the security
   1.714 +     * privileges of <code>ResourceBundle</code>.
   1.715 +     * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
   1.716 +     * for a complete description of the search and instantiation strategy.
   1.717 +     *
   1.718 +     * @param baseName the base name of the resource bundle, a fully qualified class name
   1.719 +     * @exception java.lang.NullPointerException
   1.720 +     *     if <code>baseName</code> is <code>null</code>
   1.721 +     * @exception MissingResourceException
   1.722 +     *     if no resource bundle for the specified base name can be found
   1.723 +     * @return a resource bundle for the given base name and the default locale
   1.724 +     */
   1.725 +    public static final ResourceBundle getBundle(String baseName)
   1.726 +    {
   1.727 +        return getBundleImpl(baseName, Locale.getDefault(),
   1.728 +                             /* must determine loader here, else we break stack invariant */
   1.729 +                             getLoader(),
   1.730 +                             Control.INSTANCE);
   1.731 +    }
   1.732 +
   1.733 +    /**
   1.734 +     * Returns a resource bundle using the specified base name, the
   1.735 +     * default locale and the specified control. Calling this method
   1.736 +     * is equivalent to calling
   1.737 +     * <pre>
   1.738 +     * getBundle(baseName, Locale.getDefault(),
   1.739 +     *           this.getClass().getClassLoader(), control),
   1.740 +     * </pre>
   1.741 +     * except that <code>getClassLoader()</code> is run with the security
   1.742 +     * privileges of <code>ResourceBundle</code>.  See {@link
   1.743 +     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
   1.744 +     * complete description of the resource bundle loading process with a
   1.745 +     * <code>ResourceBundle.Control</code>.
   1.746 +     *
   1.747 +     * @param baseName
   1.748 +     *        the base name of the resource bundle, a fully qualified class
   1.749 +     *        name
   1.750 +     * @param control
   1.751 +     *        the control which gives information for the resource bundle
   1.752 +     *        loading process
   1.753 +     * @return a resource bundle for the given base name and the default
   1.754 +     *        locale
   1.755 +     * @exception NullPointerException
   1.756 +     *        if <code>baseName</code> or <code>control</code> is
   1.757 +     *        <code>null</code>
   1.758 +     * @exception MissingResourceException
   1.759 +     *        if no resource bundle for the specified base name can be found
   1.760 +     * @exception IllegalArgumentException
   1.761 +     *        if the given <code>control</code> doesn't perform properly
   1.762 +     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
   1.763 +     *        Note that validation of <code>control</code> is performed as
   1.764 +     *        needed.
   1.765 +     * @since 1.6
   1.766 +     */
   1.767 +    public static final ResourceBundle getBundle(String baseName,
   1.768 +                                                 Control control) {
   1.769 +        return getBundleImpl(baseName, Locale.getDefault(),
   1.770 +                             /* must determine loader here, else we break stack invariant */
   1.771 +                             getLoader(),
   1.772 +                             control);
   1.773 +    }
   1.774 +
   1.775 +    /**
   1.776 +     * Gets a resource bundle using the specified base name and locale,
   1.777 +     * and the caller's class loader. Calling this method is equivalent to calling
   1.778 +     * <blockquote>
   1.779 +     * <code>getBundle(baseName, locale, this.getClass().getClassLoader())</code>,
   1.780 +     * </blockquote>
   1.781 +     * except that <code>getClassLoader()</code> is run with the security
   1.782 +     * privileges of <code>ResourceBundle</code>.
   1.783 +     * See {@link #getBundle(String, Locale, ClassLoader) getBundle}
   1.784 +     * for a complete description of the search and instantiation strategy.
   1.785 +     *
   1.786 +     * @param baseName
   1.787 +     *        the base name of the resource bundle, a fully qualified class name
   1.788 +     * @param locale
   1.789 +     *        the locale for which a resource bundle is desired
   1.790 +     * @exception NullPointerException
   1.791 +     *        if <code>baseName</code> or <code>locale</code> is <code>null</code>
   1.792 +     * @exception MissingResourceException
   1.793 +     *        if no resource bundle for the specified base name can be found
   1.794 +     * @return a resource bundle for the given base name and locale
   1.795 +     */
   1.796 +    public static final ResourceBundle getBundle(String baseName,
   1.797 +                                                 Locale locale)
   1.798 +    {
   1.799 +        return getBundleImpl(baseName, locale,
   1.800 +                             /* must determine loader here, else we break stack invariant */
   1.801 +                             getLoader(),
   1.802 +                             Control.INSTANCE);
   1.803 +    }
   1.804 +
   1.805 +    /**
   1.806 +     * Returns a resource bundle using the specified base name, target
   1.807 +     * locale and control, and the caller's class loader. Calling this
   1.808 +     * method is equivalent to calling
   1.809 +     * <pre>
   1.810 +     * getBundle(baseName, targetLocale, this.getClass().getClassLoader(),
   1.811 +     *           control),
   1.812 +     * </pre>
   1.813 +     * except that <code>getClassLoader()</code> is run with the security
   1.814 +     * privileges of <code>ResourceBundle</code>.  See {@link
   1.815 +     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
   1.816 +     * complete description of the resource bundle loading process with a
   1.817 +     * <code>ResourceBundle.Control</code>.
   1.818 +     *
   1.819 +     * @param baseName
   1.820 +     *        the base name of the resource bundle, a fully qualified
   1.821 +     *        class name
   1.822 +     * @param targetLocale
   1.823 +     *        the locale for which a resource bundle is desired
   1.824 +     * @param control
   1.825 +     *        the control which gives information for the resource
   1.826 +     *        bundle loading process
   1.827 +     * @return a resource bundle for the given base name and a
   1.828 +     *        <code>Locale</code> in <code>locales</code>
   1.829 +     * @exception NullPointerException
   1.830 +     *        if <code>baseName</code>, <code>locales</code> or
   1.831 +     *        <code>control</code> is <code>null</code>
   1.832 +     * @exception MissingResourceException
   1.833 +     *        if no resource bundle for the specified base name in any
   1.834 +     *        of the <code>locales</code> can be found.
   1.835 +     * @exception IllegalArgumentException
   1.836 +     *        if the given <code>control</code> doesn't perform properly
   1.837 +     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
   1.838 +     *        Note that validation of <code>control</code> is performed as
   1.839 +     *        needed.
   1.840 +     * @since 1.6
   1.841 +     */
   1.842 +    public static final ResourceBundle getBundle(String baseName, Locale targetLocale,
   1.843 +                                                 Control control) {
   1.844 +        return getBundleImpl(baseName, targetLocale,
   1.845 +                             /* must determine loader here, else we break stack invariant */
   1.846 +                             getLoader(),
   1.847 +                             control);
   1.848 +    }
   1.849 +
   1.850 +    /**
   1.851 +     * Gets a resource bundle using the specified base name, locale, and class
   1.852 +     * loader.
   1.853 +     *
   1.854 +     * <p><a name="default_behavior"/>This method behaves the same as calling
   1.855 +     * {@link #getBundle(String, Locale, ClassLoader, Control)} passing a
   1.856 +     * default instance of {@link Control}. The following describes this behavior.
   1.857 +     *
   1.858 +     * <p><code>getBundle</code> uses the base name, the specified locale, and
   1.859 +     * the default locale (obtained from {@link java.util.Locale#getDefault()
   1.860 +     * Locale.getDefault}) to generate a sequence of <a
   1.861 +     * name="candidates"><em>candidate bundle names</em></a>.  If the specified
   1.862 +     * locale's language, script, country, and variant are all empty strings,
   1.863 +     * then the base name is the only candidate bundle name.  Otherwise, a list
   1.864 +     * of candidate locales is generated from the attribute values of the
   1.865 +     * specified locale (language, script, country and variant) and appended to
   1.866 +     * the base name.  Typically, this will look like the following:
   1.867 +     *
   1.868 +     * <pre>
   1.869 +     *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
   1.870 +     *     baseName + "_" + language + "_" + script + "_" + country
   1.871 +     *     baseName + "_" + language + "_" + script
   1.872 +     *     baseName + "_" + language + "_" + country + "_" + variant
   1.873 +     *     baseName + "_" + language + "_" + country
   1.874 +     *     baseName + "_" + language
   1.875 +     * </pre>
   1.876 +     *
   1.877 +     * <p>Candidate bundle names where the final component is an empty string
   1.878 +     * are omitted, along with the underscore.  For example, if country is an
   1.879 +     * empty string, the second and the fifth candidate bundle names above
   1.880 +     * would be omitted.  Also, if script is an empty string, the candidate names
   1.881 +     * including script are omitted.  For example, a locale with language "de"
   1.882 +     * and variant "JAVA" will produce candidate names with base name
   1.883 +     * "MyResource" below.
   1.884 +     *
   1.885 +     * <pre>
   1.886 +     *     MyResource_de__JAVA
   1.887 +     *     MyResource_de
   1.888 +     * </pre>
   1.889 +     *
   1.890 +     * In the case that the variant contains one or more underscores ('_'), a
   1.891 +     * sequence of bundle names generated by truncating the last underscore and
   1.892 +     * the part following it is inserted after a candidate bundle name with the
   1.893 +     * original variant.  For example, for a locale with language "en", script
   1.894 +     * "Latn, country "US" and variant "WINDOWS_VISTA", and bundle base name
   1.895 +     * "MyResource", the list of candidate bundle names below is generated:
   1.896 +     *
   1.897 +     * <pre>
   1.898 +     * MyResource_en_Latn_US_WINDOWS_VISTA
   1.899 +     * MyResource_en_Latn_US_WINDOWS
   1.900 +     * MyResource_en_Latn_US
   1.901 +     * MyResource_en_Latn
   1.902 +     * MyResource_en_US_WINDOWS_VISTA
   1.903 +     * MyResource_en_US_WINDOWS
   1.904 +     * MyResource_en_US
   1.905 +     * MyResource_en
   1.906 +     * </pre>
   1.907 +     *
   1.908 +     * <blockquote><b>Note:</b> For some <code>Locale</code>s, the list of
   1.909 +     * candidate bundle names contains extra names, or the order of bundle names
   1.910 +     * is slightly modified.  See the description of the default implementation
   1.911 +     * of {@link Control#getCandidateLocales(String, Locale)
   1.912 +     * getCandidateLocales} for details.</blockquote>
   1.913 +     *
   1.914 +     * <p><code>getBundle</code> then iterates over the candidate bundle names
   1.915 +     * to find the first one for which it can <em>instantiate</em> an actual
   1.916 +     * resource bundle. It uses the default controls' {@link Control#getFormats
   1.917 +     * getFormats} method, which generates two bundle names for each generated
   1.918 +     * name, the first a class name and the second a properties file name. For
   1.919 +     * each candidate bundle name, it attempts to create a resource bundle:
   1.920 +     *
   1.921 +     * <ul><li>First, it attempts to load a class using the generated class name.
   1.922 +     * If such a class can be found and loaded using the specified class
   1.923 +     * loader, is assignment compatible with ResourceBundle, is accessible from
   1.924 +     * ResourceBundle, and can be instantiated, <code>getBundle</code> creates a
   1.925 +     * new instance of this class and uses it as the <em>result resource
   1.926 +     * bundle</em>.
   1.927 +     *
   1.928 +     * <li>Otherwise, <code>getBundle</code> attempts to locate a property
   1.929 +     * resource file using the generated properties file name.  It generates a
   1.930 +     * path name from the candidate bundle name by replacing all "." characters
   1.931 +     * with "/" and appending the string ".properties".  It attempts to find a
   1.932 +     * "resource" with this name using {@link
   1.933 +     * java.lang.ClassLoader#getResource(java.lang.String)
   1.934 +     * ClassLoader.getResource}.  (Note that a "resource" in the sense of
   1.935 +     * <code>getResource</code> has nothing to do with the contents of a
   1.936 +     * resource bundle, it is just a container of data, such as a file.)  If it
   1.937 +     * finds a "resource", it attempts to create a new {@link
   1.938 +     * PropertyResourceBundle} instance from its contents.  If successful, this
   1.939 +     * instance becomes the <em>result resource bundle</em>.  </ul>
   1.940 +     *
   1.941 +     * <p>This continues until a result resource bundle is instantiated or the
   1.942 +     * list of candidate bundle names is exhausted.  If no matching resource
   1.943 +     * bundle is found, the default control's {@link Control#getFallbackLocale
   1.944 +     * getFallbackLocale} method is called, which returns the current default
   1.945 +     * locale.  A new sequence of candidate locale names is generated using this
   1.946 +     * locale and and searched again, as above.
   1.947 +     *
   1.948 +     * <p>If still no result bundle is found, the base name alone is looked up. If
   1.949 +     * this still fails, a <code>MissingResourceException</code> is thrown.
   1.950 +     *
   1.951 +     * <p><a name="parent_chain"/> Once a result resource bundle has been found,
   1.952 +     * its <em>parent chain</em> is instantiated.  If the result bundle already
   1.953 +     * has a parent (perhaps because it was returned from a cache) the chain is
   1.954 +     * complete.
   1.955 +     *
   1.956 +     * <p>Otherwise, <code>getBundle</code> examines the remainder of the
   1.957 +     * candidate locale list that was used during the pass that generated the
   1.958 +     * result resource bundle.  (As before, candidate bundle names where the
   1.959 +     * final component is an empty string are omitted.)  When it comes to the
   1.960 +     * end of the candidate list, it tries the plain bundle name.  With each of the
   1.961 +     * candidate bundle names it attempts to instantiate a resource bundle (first
   1.962 +     * looking for a class and then a properties file, as described above).
   1.963 +     *
   1.964 +     * <p>Whenever it succeeds, it calls the previously instantiated resource
   1.965 +     * bundle's {@link #setParent(java.util.ResourceBundle) setParent} method
   1.966 +     * with the new resource bundle.  This continues until the list of names
   1.967 +     * is exhausted or the current bundle already has a non-null parent.
   1.968 +     *
   1.969 +     * <p>Once the parent chain is complete, the bundle is returned.
   1.970 +     *
   1.971 +     * <p><b>Note:</b> <code>getBundle</code> caches instantiated resource
   1.972 +     * bundles and might return the same resource bundle instance multiple times.
   1.973 +     *
   1.974 +     * <p><b>Note:</b>The <code>baseName</code> argument should be a fully
   1.975 +     * qualified class name. However, for compatibility with earlier versions,
   1.976 +     * Sun's Java SE Runtime Environments do not verify this, and so it is
   1.977 +     * possible to access <code>PropertyResourceBundle</code>s by specifying a
   1.978 +     * path name (using "/") instead of a fully qualified class name (using
   1.979 +     * ".").
   1.980 +     *
   1.981 +     * <p><a name="default_behavior_example"/>
   1.982 +     * <strong>Example:</strong>
   1.983 +     * <p>
   1.984 +     * The following class and property files are provided:
   1.985 +     * <pre>
   1.986 +     *     MyResources.class
   1.987 +     *     MyResources.properties
   1.988 +     *     MyResources_fr.properties
   1.989 +     *     MyResources_fr_CH.class
   1.990 +     *     MyResources_fr_CH.properties
   1.991 +     *     MyResources_en.properties
   1.992 +     *     MyResources_es_ES.class
   1.993 +     * </pre>
   1.994 +     *
   1.995 +     * The contents of all files are valid (that is, public non-abstract
   1.996 +     * subclasses of <code>ResourceBundle</code> for the ".class" files,
   1.997 +     * syntactically correct ".properties" files).  The default locale is
   1.998 +     * <code>Locale("en", "GB")</code>.
   1.999 +     *
  1.1000 +     * <p>Calling <code>getBundle</code> with the locale arguments below will
  1.1001 +     * instantiate resource bundles as follows:
  1.1002 +     *
  1.1003 +     * <table>
  1.1004 +     * <tr><td>Locale("fr", "CH")</td><td>MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class</td></tr>
  1.1005 +     * <tr><td>Locale("fr", "FR")</td><td>MyResources_fr.properties, parent MyResources.class</td></tr>
  1.1006 +     * <tr><td>Locale("de", "DE")</td><td>MyResources_en.properties, parent MyResources.class</td></tr>
  1.1007 +     * <tr><td>Locale("en", "US")</td><td>MyResources_en.properties, parent MyResources.class</td></tr>
  1.1008 +     * <tr><td>Locale("es", "ES")</td><td>MyResources_es_ES.class, parent MyResources.class</td></tr>
  1.1009 +     * </table>
  1.1010 +     *
  1.1011 +     * <p>The file MyResources_fr_CH.properties is never used because it is
  1.1012 +     * hidden by the MyResources_fr_CH.class. Likewise, MyResources.properties
  1.1013 +     * is also hidden by MyResources.class.
  1.1014 +     *
  1.1015 +     * @param baseName the base name of the resource bundle, a fully qualified class name
  1.1016 +     * @param locale the locale for which a resource bundle is desired
  1.1017 +     * @param loader the class loader from which to load the resource bundle
  1.1018 +     * @return a resource bundle for the given base name and locale
  1.1019 +     * @exception java.lang.NullPointerException
  1.1020 +     *        if <code>baseName</code>, <code>locale</code>, or <code>loader</code> is <code>null</code>
  1.1021 +     * @exception MissingResourceException
  1.1022 +     *        if no resource bundle for the specified base name can be found
  1.1023 +     * @since 1.2
  1.1024 +     */
  1.1025 +    public static ResourceBundle getBundle(String baseName, Locale locale,
  1.1026 +                                           ClassLoader loader)
  1.1027 +    {
  1.1028 +        if (loader == null) {
  1.1029 +            throw new NullPointerException();
  1.1030 +        }
  1.1031 +        return getBundleImpl(baseName, locale, loader, Control.INSTANCE);
  1.1032 +    }
  1.1033 +
  1.1034 +    /**
  1.1035 +     * Returns a resource bundle using the specified base name, target
  1.1036 +     * locale, class loader and control. Unlike the {@linkplain
  1.1037 +     * #getBundle(String, Locale, ClassLoader) <code>getBundle</code>
  1.1038 +     * factory methods with no <code>control</code> argument}, the given
  1.1039 +     * <code>control</code> specifies how to locate and instantiate resource
  1.1040 +     * bundles. Conceptually, the bundle loading process with the given
  1.1041 +     * <code>control</code> is performed in the following steps.
  1.1042 +     *
  1.1043 +     * <p>
  1.1044 +     * <ol>
  1.1045 +     * <li>This factory method looks up the resource bundle in the cache for
  1.1046 +     * the specified <code>baseName</code>, <code>targetLocale</code> and
  1.1047 +     * <code>loader</code>.  If the requested resource bundle instance is
  1.1048 +     * found in the cache and the time-to-live periods of the instance and
  1.1049 +     * all of its parent instances have not expired, the instance is returned
  1.1050 +     * to the caller. Otherwise, this factory method proceeds with the
  1.1051 +     * loading process below.</li>
  1.1052 +     *
  1.1053 +     * <li>The {@link ResourceBundle.Control#getFormats(String)
  1.1054 +     * control.getFormats} method is called to get resource bundle formats
  1.1055 +     * to produce bundle or resource names. The strings
  1.1056 +     * <code>"java.class"</code> and <code>"java.properties"</code>
  1.1057 +     * designate class-based and {@linkplain PropertyResourceBundle
  1.1058 +     * property}-based resource bundles, respectively. Other strings
  1.1059 +     * starting with <code>"java."</code> are reserved for future extensions
  1.1060 +     * and must not be used for application-defined formats. Other strings
  1.1061 +     * designate application-defined formats.</li>
  1.1062 +     *
  1.1063 +     * <li>The {@link ResourceBundle.Control#getCandidateLocales(String,
  1.1064 +     * Locale) control.getCandidateLocales} method is called with the target
  1.1065 +     * locale to get a list of <em>candidate <code>Locale</code>s</em> for
  1.1066 +     * which resource bundles are searched.</li>
  1.1067 +     *
  1.1068 +     * <li>The {@link ResourceBundle.Control#newBundle(String, Locale,
  1.1069 +     * String, ClassLoader, boolean) control.newBundle} method is called to
  1.1070 +     * instantiate a <code>ResourceBundle</code> for the base bundle name, a
  1.1071 +     * candidate locale, and a format. (Refer to the note on the cache
  1.1072 +     * lookup below.) This step is iterated over all combinations of the
  1.1073 +     * candidate locales and formats until the <code>newBundle</code> method
  1.1074 +     * returns a <code>ResourceBundle</code> instance or the iteration has
  1.1075 +     * used up all the combinations. For example, if the candidate locales
  1.1076 +     * are <code>Locale("de", "DE")</code>, <code>Locale("de")</code> and
  1.1077 +     * <code>Locale("")</code> and the formats are <code>"java.class"</code>
  1.1078 +     * and <code>"java.properties"</code>, then the following is the
  1.1079 +     * sequence of locale-format combinations to be used to call
  1.1080 +     * <code>control.newBundle</code>.
  1.1081 +     *
  1.1082 +     * <table style="width: 50%; text-align: left; margin-left: 40px;"
  1.1083 +     *  border="0" cellpadding="2" cellspacing="2">
  1.1084 +     * <tbody><code>
  1.1085 +     * <tr>
  1.1086 +     * <td
  1.1087 +     * style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;">Locale<br>
  1.1088 +     * </td>
  1.1089 +     * <td
  1.1090 +     * style="vertical-align: top; text-align: left; font-weight: bold; width: 50%;">format<br>
  1.1091 +     * </td>
  1.1092 +     * </tr>
  1.1093 +     * <tr>
  1.1094 +     * <td style="vertical-align: top; width: 50%;">Locale("de", "DE")<br>
  1.1095 +     * </td>
  1.1096 +     * <td style="vertical-align: top; width: 50%;">java.class<br>
  1.1097 +     * </td>
  1.1098 +     * </tr>
  1.1099 +     * <tr>
  1.1100 +     * <td style="vertical-align: top; width: 50%;">Locale("de", "DE")</td>
  1.1101 +     * <td style="vertical-align: top; width: 50%;">java.properties<br>
  1.1102 +     * </td>
  1.1103 +     * </tr>
  1.1104 +     * <tr>
  1.1105 +     * <td style="vertical-align: top; width: 50%;">Locale("de")</td>
  1.1106 +     * <td style="vertical-align: top; width: 50%;">java.class</td>
  1.1107 +     * </tr>
  1.1108 +     * <tr>
  1.1109 +     * <td style="vertical-align: top; width: 50%;">Locale("de")</td>
  1.1110 +     * <td style="vertical-align: top; width: 50%;">java.properties</td>
  1.1111 +     * </tr>
  1.1112 +     * <tr>
  1.1113 +     * <td style="vertical-align: top; width: 50%;">Locale("")<br>
  1.1114 +     * </td>
  1.1115 +     * <td style="vertical-align: top; width: 50%;">java.class</td>
  1.1116 +     * </tr>
  1.1117 +     * <tr>
  1.1118 +     * <td style="vertical-align: top; width: 50%;">Locale("")</td>
  1.1119 +     * <td style="vertical-align: top; width: 50%;">java.properties</td>
  1.1120 +     * </tr>
  1.1121 +     * </code></tbody>
  1.1122 +     * </table>
  1.1123 +     * </li>
  1.1124 +     *
  1.1125 +     * <li>If the previous step has found no resource bundle, proceed to
  1.1126 +     * Step 6. If a bundle has been found that is a base bundle (a bundle
  1.1127 +     * for <code>Locale("")</code>), and the candidate locale list only contained
  1.1128 +     * <code>Locale("")</code>, return the bundle to the caller. If a bundle
  1.1129 +     * has been found that is a base bundle, but the candidate locale list
  1.1130 +     * contained locales other than Locale(""), put the bundle on hold and
  1.1131 +     * proceed to Step 6. If a bundle has been found that is not a base
  1.1132 +     * bundle, proceed to Step 7.</li>
  1.1133 +     *
  1.1134 +     * <li>The {@link ResourceBundle.Control#getFallbackLocale(String,
  1.1135 +     * Locale) control.getFallbackLocale} method is called to get a fallback
  1.1136 +     * locale (alternative to the current target locale) to try further
  1.1137 +     * finding a resource bundle. If the method returns a non-null locale,
  1.1138 +     * it becomes the next target locale and the loading process starts over
  1.1139 +     * from Step 3. Otherwise, if a base bundle was found and put on hold in
  1.1140 +     * a previous Step 5, it is returned to the caller now. Otherwise, a
  1.1141 +     * MissingResourceException is thrown.</li>
  1.1142 +     *
  1.1143 +     * <li>At this point, we have found a resource bundle that's not the
  1.1144 +     * base bundle. If this bundle set its parent during its instantiation,
  1.1145 +     * it is returned to the caller. Otherwise, its <a
  1.1146 +     * href="./ResourceBundle.html#parent_chain">parent chain</a> is
  1.1147 +     * instantiated based on the list of candidate locales from which it was
  1.1148 +     * found. Finally, the bundle is returned to the caller.</li>
  1.1149 +     * </ol>
  1.1150 +     *
  1.1151 +     * <p>During the resource bundle loading process above, this factory
  1.1152 +     * method looks up the cache before calling the {@link
  1.1153 +     * Control#newBundle(String, Locale, String, ClassLoader, boolean)
  1.1154 +     * control.newBundle} method.  If the time-to-live period of the
  1.1155 +     * resource bundle found in the cache has expired, the factory method
  1.1156 +     * calls the {@link ResourceBundle.Control#needsReload(String, Locale,
  1.1157 +     * String, ClassLoader, ResourceBundle, long) control.needsReload}
  1.1158 +     * method to determine whether the resource bundle needs to be reloaded.
  1.1159 +     * If reloading is required, the factory method calls
  1.1160 +     * <code>control.newBundle</code> to reload the resource bundle.  If
  1.1161 +     * <code>control.newBundle</code> returns <code>null</code>, the factory
  1.1162 +     * method puts a dummy resource bundle in the cache as a mark of
  1.1163 +     * nonexistent resource bundles in order to avoid lookup overhead for
  1.1164 +     * subsequent requests. Such dummy resource bundles are under the same
  1.1165 +     * expiration control as specified by <code>control</code>.
  1.1166 +     *
  1.1167 +     * <p>All resource bundles loaded are cached by default. Refer to
  1.1168 +     * {@link Control#getTimeToLive(String,Locale)
  1.1169 +     * control.getTimeToLive} for details.
  1.1170 +     *
  1.1171 +     * <p>The following is an example of the bundle loading process with the
  1.1172 +     * default <code>ResourceBundle.Control</code> implementation.
  1.1173 +     *
  1.1174 +     * <p>Conditions:
  1.1175 +     * <ul>
  1.1176 +     * <li>Base bundle name: <code>foo.bar.Messages</code>
  1.1177 +     * <li>Requested <code>Locale</code>: {@link Locale#ITALY}</li>
  1.1178 +     * <li>Default <code>Locale</code>: {@link Locale#FRENCH}</li>
  1.1179 +     * <li>Available resource bundles:
  1.1180 +     * <code>foo/bar/Messages_fr.properties</code> and
  1.1181 +     * <code>foo/bar/Messages.properties</code></li>
  1.1182 +     * </ul>
  1.1183 +     *
  1.1184 +     * <p>First, <code>getBundle</code> tries loading a resource bundle in
  1.1185 +     * the following sequence.
  1.1186 +     *
  1.1187 +     * <ul>
  1.1188 +     * <li>class <code>foo.bar.Messages_it_IT</code>
  1.1189 +     * <li>file <code>foo/bar/Messages_it_IT.properties</code>
  1.1190 +     * <li>class <code>foo.bar.Messages_it</code></li>
  1.1191 +     * <li>file <code>foo/bar/Messages_it.properties</code></li>
  1.1192 +     * <li>class <code>foo.bar.Messages</code></li>
  1.1193 +     * <li>file <code>foo/bar/Messages.properties</code></li>
  1.1194 +     * </ul>
  1.1195 +     *
  1.1196 +     * <p>At this point, <code>getBundle</code> finds
  1.1197 +     * <code>foo/bar/Messages.properties</code>, which is put on hold
  1.1198 +     * because it's the base bundle.  <code>getBundle</code> calls {@link
  1.1199 +     * Control#getFallbackLocale(String, Locale)
  1.1200 +     * control.getFallbackLocale("foo.bar.Messages", Locale.ITALY)} which
  1.1201 +     * returns <code>Locale.FRENCH</code>. Next, <code>getBundle</code>
  1.1202 +     * tries loading a bundle in the following sequence.
  1.1203 +     *
  1.1204 +     * <ul>
  1.1205 +     * <li>class <code>foo.bar.Messages_fr</code></li>
  1.1206 +     * <li>file <code>foo/bar/Messages_fr.properties</code></li>
  1.1207 +     * <li>class <code>foo.bar.Messages</code></li>
  1.1208 +     * <li>file <code>foo/bar/Messages.properties</code></li>
  1.1209 +     * </ul>
  1.1210 +     *
  1.1211 +     * <p><code>getBundle</code> finds
  1.1212 +     * <code>foo/bar/Messages_fr.properties</code> and creates a
  1.1213 +     * <code>ResourceBundle</code> instance. Then, <code>getBundle</code>
  1.1214 +     * sets up its parent chain from the list of the candiate locales.  Only
  1.1215 +     * <code>foo/bar/Messages.properties</code> is found in the list and
  1.1216 +     * <code>getBundle</code> creates a <code>ResourceBundle</code> instance
  1.1217 +     * that becomes the parent of the instance for
  1.1218 +     * <code>foo/bar/Messages_fr.properties</code>.
  1.1219 +     *
  1.1220 +     * @param baseName
  1.1221 +     *        the base name of the resource bundle, a fully qualified
  1.1222 +     *        class name
  1.1223 +     * @param targetLocale
  1.1224 +     *        the locale for which a resource bundle is desired
  1.1225 +     * @param loader
  1.1226 +     *        the class loader from which to load the resource bundle
  1.1227 +     * @param control
  1.1228 +     *        the control which gives information for the resource
  1.1229 +     *        bundle loading process
  1.1230 +     * @return a resource bundle for the given base name and locale
  1.1231 +     * @exception NullPointerException
  1.1232 +     *        if <code>baseName</code>, <code>targetLocale</code>,
  1.1233 +     *        <code>loader</code>, or <code>control</code> is
  1.1234 +     *        <code>null</code>
  1.1235 +     * @exception MissingResourceException
  1.1236 +     *        if no resource bundle for the specified base name can be found
  1.1237 +     * @exception IllegalArgumentException
  1.1238 +     *        if the given <code>control</code> doesn't perform properly
  1.1239 +     *        (e.g., <code>control.getCandidateLocales</code> returns null.)
  1.1240 +     *        Note that validation of <code>control</code> is performed as
  1.1241 +     *        needed.
  1.1242 +     * @since 1.6
  1.1243 +     */
  1.1244 +    public static ResourceBundle getBundle(String baseName, Locale targetLocale,
  1.1245 +                                           ClassLoader loader, Control control) {
  1.1246 +        if (loader == null || control == null) {
  1.1247 +            throw new NullPointerException();
  1.1248 +        }
  1.1249 +        return getBundleImpl(baseName, targetLocale, loader, control);
  1.1250 +    }
  1.1251 +
  1.1252 +    private static ResourceBundle getBundleImpl(String baseName, Locale locale,
  1.1253 +                                                ClassLoader loader, Control control) {
  1.1254 +        if (locale == null || control == null) {
  1.1255 +            throw new NullPointerException();
  1.1256 +        }
  1.1257 +
  1.1258 +        // We create a CacheKey here for use by this call. The base
  1.1259 +        // name and loader will never change during the bundle loading
  1.1260 +        // process. We have to make sure that the locale is set before
  1.1261 +        // using it as a cache key.
  1.1262 +        CacheKey cacheKey = new CacheKey(baseName, locale, loader);
  1.1263 +        ResourceBundle bundle = null;
  1.1264 +
  1.1265 +        // Quick lookup of the cache.
  1.1266 +        BundleReference bundleRef = cacheList.get(cacheKey);
  1.1267 +        if (bundleRef != null) {
  1.1268 +            bundle = bundleRef.get();
  1.1269 +            bundleRef = null;
  1.1270 +        }
  1.1271 +
  1.1272 +        // If this bundle and all of its parents are valid (not expired),
  1.1273 +        // then return this bundle. If any of the bundles is expired, we
  1.1274 +        // don't call control.needsReload here but instead drop into the
  1.1275 +        // complete loading process below.
  1.1276 +        if (isValidBundle(bundle) && hasValidParentChain(bundle)) {
  1.1277 +            return bundle;
  1.1278 +        }
  1.1279 +
  1.1280 +        // No valid bundle was found in the cache, so we need to load the
  1.1281 +        // resource bundle and its parents.
  1.1282 +
  1.1283 +        boolean isKnownControl = (control == Control.INSTANCE) ||
  1.1284 +                                   (control instanceof SingleFormatControl);
  1.1285 +        List<String> formats = control.getFormats(baseName);
  1.1286 +        if (!isKnownControl && !checkList(formats)) {
  1.1287 +            throw new IllegalArgumentException("Invalid Control: getFormats");
  1.1288 +        }
  1.1289 +
  1.1290 +        ResourceBundle baseBundle = null;
  1.1291 +        for (Locale targetLocale = locale;
  1.1292 +             targetLocale != null;
  1.1293 +             targetLocale = control.getFallbackLocale(baseName, targetLocale)) {
  1.1294 +            List<Locale> candidateLocales = control.getCandidateLocales(baseName, targetLocale);
  1.1295 +            if (!isKnownControl && !checkList(candidateLocales)) {
  1.1296 +                throw new IllegalArgumentException("Invalid Control: getCandidateLocales");
  1.1297 +            }
  1.1298 +
  1.1299 +            bundle = findBundle(cacheKey, candidateLocales, formats, 0, control, baseBundle);
  1.1300 +
  1.1301 +            // If the loaded bundle is the base bundle and exactly for the
  1.1302 +            // requested locale or the only candidate locale, then take the
  1.1303 +            // bundle as the resulting one. If the loaded bundle is the base
  1.1304 +            // bundle, it's put on hold until we finish processing all
  1.1305 +            // fallback locales.
  1.1306 +            if (isValidBundle(bundle)) {
  1.1307 +                boolean isBaseBundle = Locale.ROOT.equals(bundle.locale);
  1.1308 +                if (!isBaseBundle || bundle.locale.equals(locale)
  1.1309 +                    || (candidateLocales.size() == 1
  1.1310 +                        && bundle.locale.equals(candidateLocales.get(0)))) {
  1.1311 +                    break;
  1.1312 +                }
  1.1313 +
  1.1314 +                // If the base bundle has been loaded, keep the reference in
  1.1315 +                // baseBundle so that we can avoid any redundant loading in case
  1.1316 +                // the control specify not to cache bundles.
  1.1317 +                if (isBaseBundle && baseBundle == null) {
  1.1318 +                    baseBundle = bundle;
  1.1319 +                }
  1.1320 +            }
  1.1321 +        }
  1.1322 +
  1.1323 +        if (bundle == null) {
  1.1324 +            if (baseBundle == null) {
  1.1325 +                throwMissingResourceException(baseName, locale, cacheKey.getCause());
  1.1326 +            }
  1.1327 +            bundle = baseBundle;
  1.1328 +        }
  1.1329 +
  1.1330 +        return bundle;
  1.1331 +    }
  1.1332 +
  1.1333 +    /**
  1.1334 +     * Checks if the given <code>List</code> is not null, not empty,
  1.1335 +     * not having null in its elements.
  1.1336 +     */
  1.1337 +    private static final boolean checkList(List a) {
  1.1338 +        boolean valid = (a != null && a.size() != 0);
  1.1339 +        if (valid) {
  1.1340 +            int size = a.size();
  1.1341 +            for (int i = 0; valid && i < size; i++) {
  1.1342 +                valid = (a.get(i) != null);
  1.1343 +            }
  1.1344 +        }
  1.1345 +        return valid;
  1.1346 +    }
  1.1347 +
  1.1348 +    private static final ResourceBundle findBundle(CacheKey cacheKey,
  1.1349 +                                                   List<Locale> candidateLocales,
  1.1350 +                                                   List<String> formats,
  1.1351 +                                                   int index,
  1.1352 +                                                   Control control,
  1.1353 +                                                   ResourceBundle baseBundle) {
  1.1354 +        Locale targetLocale = candidateLocales.get(index);
  1.1355 +        ResourceBundle parent = null;
  1.1356 +        if (index != candidateLocales.size() - 1) {
  1.1357 +            parent = findBundle(cacheKey, candidateLocales, formats, index + 1,
  1.1358 +                                control, baseBundle);
  1.1359 +        } else if (baseBundle != null && Locale.ROOT.equals(targetLocale)) {
  1.1360 +            return baseBundle;
  1.1361 +        }
  1.1362 +
  1.1363 +        // Before we do the real loading work, see whether we need to
  1.1364 +        // do some housekeeping: If references to class loaders or
  1.1365 +        // resource bundles have been nulled out, remove all related
  1.1366 +        // information from the cache.
  1.1367 +        Object ref;
  1.1368 +        while ((ref = referenceQueue.poll()) != null) {
  1.1369 +            cacheList.remove(((CacheKeyReference)ref).getCacheKey());
  1.1370 +        }
  1.1371 +
  1.1372 +        // flag indicating the resource bundle has expired in the cache
  1.1373 +        boolean expiredBundle = false;
  1.1374 +
  1.1375 +        // First, look up the cache to see if it's in the cache, without
  1.1376 +        // attempting to load bundle.
  1.1377 +        cacheKey.setLocale(targetLocale);
  1.1378 +        ResourceBundle bundle = findBundleInCache(cacheKey, control);
  1.1379 +        if (isValidBundle(bundle)) {
  1.1380 +            expiredBundle = bundle.expired;
  1.1381 +            if (!expiredBundle) {
  1.1382 +                // If its parent is the one asked for by the candidate
  1.1383 +                // locales (the runtime lookup path), we can take the cached
  1.1384 +                // one. (If it's not identical, then we'd have to check the
  1.1385 +                // parent's parents to be consistent with what's been
  1.1386 +                // requested.)
  1.1387 +                if (bundle.parent == parent) {
  1.1388 +                    return bundle;
  1.1389 +                }
  1.1390 +                // Otherwise, remove the cached one since we can't keep
  1.1391 +                // the same bundles having different parents.
  1.1392 +                BundleReference bundleRef = cacheList.get(cacheKey);
  1.1393 +                if (bundleRef != null && bundleRef.get() == bundle) {
  1.1394 +                    cacheList.remove(cacheKey, bundleRef);
  1.1395 +                }
  1.1396 +            }
  1.1397 +        }
  1.1398 +
  1.1399 +        if (bundle != NONEXISTENT_BUNDLE) {
  1.1400 +            CacheKey constKey = (CacheKey) cacheKey.clone();
  1.1401 +
  1.1402 +            try {
  1.1403 +                bundle = loadBundle(cacheKey, formats, control, expiredBundle);
  1.1404 +                if (bundle != null) {
  1.1405 +                    if (bundle.parent == null) {
  1.1406 +                        bundle.setParent(parent);
  1.1407 +                    }
  1.1408 +                    bundle.locale = targetLocale;
  1.1409 +                    bundle = putBundleInCache(cacheKey, bundle, control);
  1.1410 +                    return bundle;
  1.1411 +                }
  1.1412 +
  1.1413 +                // Put NONEXISTENT_BUNDLE in the cache as a mark that there's no bundle
  1.1414 +                // instance for the locale.
  1.1415 +                putBundleInCache(cacheKey, NONEXISTENT_BUNDLE, control);
  1.1416 +            } finally {
  1.1417 +                if (constKey.getCause() instanceof InterruptedException) {
  1.1418 +                    Thread.currentThread().interrupt();
  1.1419 +                }
  1.1420 +            }
  1.1421 +        }
  1.1422 +        return parent;
  1.1423 +    }
  1.1424 +
  1.1425 +    private static final ResourceBundle loadBundle(CacheKey cacheKey,
  1.1426 +                                                   List<String> formats,
  1.1427 +                                                   Control control,
  1.1428 +                                                   boolean reload) {
  1.1429 +
  1.1430 +        // Here we actually load the bundle in the order of formats
  1.1431 +        // specified by the getFormats() value.
  1.1432 +        Locale targetLocale = cacheKey.getLocale();
  1.1433 +
  1.1434 +        ResourceBundle bundle = null;
  1.1435 +        int size = formats.size();
  1.1436 +        for (int i = 0; i < size; i++) {
  1.1437 +            String format = formats.get(i);
  1.1438 +            try {
  1.1439 +                bundle = control.newBundle(cacheKey.getName(), targetLocale, format,
  1.1440 +                                           cacheKey.getLoader(), reload);
  1.1441 +            } catch (LinkageError error) {
  1.1442 +                // We need to handle the LinkageError case due to
  1.1443 +                // inconsistent case-sensitivity in ClassLoader.
  1.1444 +                // See 6572242 for details.
  1.1445 +                cacheKey.setCause(error);
  1.1446 +            } catch (Exception cause) {
  1.1447 +                cacheKey.setCause(cause);
  1.1448 +            }
  1.1449 +            if (bundle != null) {
  1.1450 +                // Set the format in the cache key so that it can be
  1.1451 +                // used when calling needsReload later.
  1.1452 +                cacheKey.setFormat(format);
  1.1453 +                bundle.name = cacheKey.getName();
  1.1454 +                bundle.locale = targetLocale;
  1.1455 +                // Bundle provider might reuse instances. So we should make
  1.1456 +                // sure to clear the expired flag here.
  1.1457 +                bundle.expired = false;
  1.1458 +                break;
  1.1459 +            }
  1.1460 +        }
  1.1461 +
  1.1462 +        return bundle;
  1.1463 +    }
  1.1464 +
  1.1465 +    private static final boolean isValidBundle(ResourceBundle bundle) {
  1.1466 +        return bundle != null && bundle != NONEXISTENT_BUNDLE;
  1.1467 +    }
  1.1468 +
  1.1469 +    /**
  1.1470 +     * Determines whether any of resource bundles in the parent chain,
  1.1471 +     * including the leaf, have expired.
  1.1472 +     */
  1.1473 +    private static final boolean hasValidParentChain(ResourceBundle bundle) {
  1.1474 +        long now = System.currentTimeMillis();
  1.1475 +        while (bundle != null) {
  1.1476 +            if (bundle.expired) {
  1.1477 +                return false;
  1.1478 +            }
  1.1479 +            CacheKey key = bundle.cacheKey;
  1.1480 +            if (key != null) {
  1.1481 +                long expirationTime = key.expirationTime;
  1.1482 +                if (expirationTime >= 0 && expirationTime <= now) {
  1.1483 +                    return false;
  1.1484 +                }
  1.1485 +            }
  1.1486 +            bundle = bundle.parent;
  1.1487 +        }
  1.1488 +        return true;
  1.1489 +    }
  1.1490 +
  1.1491 +    /**
  1.1492 +     * Throw a MissingResourceException with proper message
  1.1493 +     */
  1.1494 +    private static final void throwMissingResourceException(String baseName,
  1.1495 +                                                            Locale locale,
  1.1496 +                                                            Throwable cause) {
  1.1497 +        // If the cause is a MissingResourceException, avoid creating
  1.1498 +        // a long chain. (6355009)
  1.1499 +        if (cause instanceof MissingResourceException) {
  1.1500 +            cause = null;
  1.1501 +        }
  1.1502 +        throw new MissingResourceException("Can't find bundle for base name "
  1.1503 +                                           + baseName + ", locale " + locale,
  1.1504 +                                           baseName + "_" + locale, // className
  1.1505 +                                           "",                      // key
  1.1506 +                                           cause);
  1.1507 +    }
  1.1508 +
  1.1509 +    /**
  1.1510 +     * Finds a bundle in the cache. Any expired bundles are marked as
  1.1511 +     * `expired' and removed from the cache upon return.
  1.1512 +     *
  1.1513 +     * @param cacheKey the key to look up the cache
  1.1514 +     * @param control the Control to be used for the expiration control
  1.1515 +     * @return the cached bundle, or null if the bundle is not found in the
  1.1516 +     * cache or its parent has expired. <code>bundle.expire</code> is true
  1.1517 +     * upon return if the bundle in the cache has expired.
  1.1518 +     */
  1.1519 +    private static final ResourceBundle findBundleInCache(CacheKey cacheKey,
  1.1520 +                                                          Control control) {
  1.1521 +        BundleReference bundleRef = cacheList.get(cacheKey);
  1.1522 +        if (bundleRef == null) {
  1.1523 +            return null;
  1.1524 +        }
  1.1525 +        ResourceBundle bundle = bundleRef.get();
  1.1526 +        if (bundle == null) {
  1.1527 +            return null;
  1.1528 +        }
  1.1529 +        ResourceBundle p = bundle.parent;
  1.1530 +        assert p != NONEXISTENT_BUNDLE;
  1.1531 +        // If the parent has expired, then this one must also expire. We
  1.1532 +        // check only the immediate parent because the actual loading is
  1.1533 +        // done from the root (base) to leaf (child) and the purpose of
  1.1534 +        // checking is to propagate expiration towards the leaf. For
  1.1535 +        // example, if the requested locale is ja_JP_JP and there are
  1.1536 +        // bundles for all of the candidates in the cache, we have a list,
  1.1537 +        //
  1.1538 +        // base <- ja <- ja_JP <- ja_JP_JP
  1.1539 +        //
  1.1540 +        // If ja has expired, then it will reload ja and the list becomes a
  1.1541 +        // tree.
  1.1542 +        //
  1.1543 +        // base <- ja (new)
  1.1544 +        //  "   <- ja (expired) <- ja_JP <- ja_JP_JP
  1.1545 +        //
  1.1546 +        // When looking up ja_JP in the cache, it finds ja_JP in the cache
  1.1547 +        // which references to the expired ja. Then, ja_JP is marked as
  1.1548 +        // expired and removed from the cache. This will be propagated to
  1.1549 +        // ja_JP_JP.
  1.1550 +        //
  1.1551 +        // Now, it's possible, for example, that while loading new ja_JP,
  1.1552 +        // someone else has started loading the same bundle and finds the
  1.1553 +        // base bundle has expired. Then, what we get from the first
  1.1554 +        // getBundle call includes the expired base bundle. However, if
  1.1555 +        // someone else didn't start its loading, we wouldn't know if the
  1.1556 +        // base bundle has expired at the end of the loading process. The
  1.1557 +        // expiration control doesn't guarantee that the returned bundle and
  1.1558 +        // its parents haven't expired.
  1.1559 +        //
  1.1560 +        // We could check the entire parent chain to see if there's any in
  1.1561 +        // the chain that has expired. But this process may never end. An
  1.1562 +        // extreme case would be that getTimeToLive returns 0 and
  1.1563 +        // needsReload always returns true.
  1.1564 +        if (p != null && p.expired) {
  1.1565 +            assert bundle != NONEXISTENT_BUNDLE;
  1.1566 +            bundle.expired = true;
  1.1567 +            bundle.cacheKey = null;
  1.1568 +            cacheList.remove(cacheKey, bundleRef);
  1.1569 +            bundle = null;
  1.1570 +        } else {
  1.1571 +            CacheKey key = bundleRef.getCacheKey();
  1.1572 +            long expirationTime = key.expirationTime;
  1.1573 +            if (!bundle.expired && expirationTime >= 0 &&
  1.1574 +                expirationTime <= System.currentTimeMillis()) {
  1.1575 +                // its TTL period has expired.
  1.1576 +                if (bundle != NONEXISTENT_BUNDLE) {
  1.1577 +                    // Synchronize here to call needsReload to avoid
  1.1578 +                    // redundant concurrent calls for the same bundle.
  1.1579 +                    synchronized (bundle) {
  1.1580 +                        expirationTime = key.expirationTime;
  1.1581 +                        if (!bundle.expired && expirationTime >= 0 &&
  1.1582 +                            expirationTime <= System.currentTimeMillis()) {
  1.1583 +                            try {
  1.1584 +                                bundle.expired = control.needsReload(key.getName(),
  1.1585 +                                                                     key.getLocale(),
  1.1586 +                                                                     key.getFormat(),
  1.1587 +                                                                     key.getLoader(),
  1.1588 +                                                                     bundle,
  1.1589 +                                                                     key.loadTime);
  1.1590 +                            } catch (Exception e) {
  1.1591 +                                cacheKey.setCause(e);
  1.1592 +                            }
  1.1593 +                            if (bundle.expired) {
  1.1594 +                                // If the bundle needs to be reloaded, then
  1.1595 +                                // remove the bundle from the cache, but
  1.1596 +                                // return the bundle with the expired flag
  1.1597 +                                // on.
  1.1598 +                                bundle.cacheKey = null;
  1.1599 +                                cacheList.remove(cacheKey, bundleRef);
  1.1600 +                            } else {
  1.1601 +                                // Update the expiration control info. and reuse
  1.1602 +                                // the same bundle instance
  1.1603 +                                setExpirationTime(key, control);
  1.1604 +                            }
  1.1605 +                        }
  1.1606 +                    }
  1.1607 +                } else {
  1.1608 +                    // We just remove NONEXISTENT_BUNDLE from the cache.
  1.1609 +                    cacheList.remove(cacheKey, bundleRef);
  1.1610 +                    bundle = null;
  1.1611 +                }
  1.1612 +            }
  1.1613 +        }
  1.1614 +        return bundle;
  1.1615 +    }
  1.1616 +
  1.1617 +    /**
  1.1618 +     * Put a new bundle in the cache.
  1.1619 +     *
  1.1620 +     * @param cacheKey the key for the resource bundle
  1.1621 +     * @param bundle the resource bundle to be put in the cache
  1.1622 +     * @return the ResourceBundle for the cacheKey; if someone has put
  1.1623 +     * the bundle before this call, the one found in the cache is
  1.1624 +     * returned.
  1.1625 +     */
  1.1626 +    private static final ResourceBundle putBundleInCache(CacheKey cacheKey,
  1.1627 +                                                         ResourceBundle bundle,
  1.1628 +                                                         Control control) {
  1.1629 +        setExpirationTime(cacheKey, control);
  1.1630 +        if (cacheKey.expirationTime != Control.TTL_DONT_CACHE) {
  1.1631 +            CacheKey key = (CacheKey) cacheKey.clone();
  1.1632 +            BundleReference bundleRef = new BundleReference(bundle, referenceQueue, key);
  1.1633 +            bundle.cacheKey = key;
  1.1634 +
  1.1635 +            // Put the bundle in the cache if it's not been in the cache.
  1.1636 +            BundleReference result = cacheList.putIfAbsent(key, bundleRef);
  1.1637 +
  1.1638 +            // If someone else has put the same bundle in the cache before
  1.1639 +            // us and it has not expired, we should use the one in the cache.
  1.1640 +            if (result != null) {
  1.1641 +                ResourceBundle rb = result.get();
  1.1642 +                if (rb != null && !rb.expired) {
  1.1643 +                    // Clear the back link to the cache key
  1.1644 +                    bundle.cacheKey = null;
  1.1645 +                    bundle = rb;
  1.1646 +                    // Clear the reference in the BundleReference so that
  1.1647 +                    // it won't be enqueued.
  1.1648 +                    bundleRef.clear();
  1.1649 +                } else {
  1.1650 +                    // Replace the invalid (garbage collected or expired)
  1.1651 +                    // instance with the valid one.
  1.1652 +                    cacheList.put(key, bundleRef);
  1.1653 +                }
  1.1654 +            }
  1.1655 +        }
  1.1656 +        return bundle;
  1.1657 +    }
  1.1658 +
  1.1659 +    private static final void setExpirationTime(CacheKey cacheKey, Control control) {
  1.1660 +        long ttl = control.getTimeToLive(cacheKey.getName(),
  1.1661 +                                         cacheKey.getLocale());
  1.1662 +        if (ttl >= 0) {
  1.1663 +            // If any expiration time is specified, set the time to be
  1.1664 +            // expired in the cache.
  1.1665 +            long now = System.currentTimeMillis();
  1.1666 +            cacheKey.loadTime = now;
  1.1667 +            cacheKey.expirationTime = now + ttl;
  1.1668 +        } else if (ttl >= Control.TTL_NO_EXPIRATION_CONTROL) {
  1.1669 +            cacheKey.expirationTime = ttl;
  1.1670 +        } else {
  1.1671 +            throw new IllegalArgumentException("Invalid Control: TTL=" + ttl);
  1.1672 +        }
  1.1673 +    }
  1.1674 +
  1.1675 +    /**
  1.1676 +     * Removes all resource bundles from the cache that have been loaded
  1.1677 +     * using the caller's class loader.
  1.1678 +     *
  1.1679 +     * @since 1.6
  1.1680 +     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
  1.1681 +     */
  1.1682 +    public static final void clearCache() {
  1.1683 +        clearCache(getLoader());
  1.1684 +    }
  1.1685 +
  1.1686 +    /**
  1.1687 +     * Removes all resource bundles from the cache that have been loaded
  1.1688 +     * using the given class loader.
  1.1689 +     *
  1.1690 +     * @param loader the class loader
  1.1691 +     * @exception NullPointerException if <code>loader</code> is null
  1.1692 +     * @since 1.6
  1.1693 +     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
  1.1694 +     */
  1.1695 +    public static final void clearCache(ClassLoader loader) {
  1.1696 +        if (loader == null) {
  1.1697 +            throw new NullPointerException();
  1.1698 +        }
  1.1699 +        Set<CacheKey> set = cacheList.keySet();
  1.1700 +        for (CacheKey key : set) {
  1.1701 +            if (key.getLoader() == loader) {
  1.1702 +                set.remove(key);
  1.1703 +            }
  1.1704 +        }
  1.1705 +    }
  1.1706 +
  1.1707 +    /**
  1.1708 +     * Gets an object for the given key from this resource bundle.
  1.1709 +     * Returns null if this resource bundle does not contain an
  1.1710 +     * object for the given key.
  1.1711 +     *
  1.1712 +     * @param key the key for the desired object
  1.1713 +     * @exception NullPointerException if <code>key</code> is <code>null</code>
  1.1714 +     * @return the object for the given key, or null
  1.1715 +     */
  1.1716 +    protected abstract Object handleGetObject(String key);
  1.1717 +
  1.1718 +    /**
  1.1719 +     * Returns an enumeration of the keys.
  1.1720 +     *
  1.1721 +     * @return an <code>Enumeration</code> of the keys contained in
  1.1722 +     *         this <code>ResourceBundle</code> and its parent bundles.
  1.1723 +     */
  1.1724 +    public abstract Enumeration<String> getKeys();
  1.1725 +
  1.1726 +    /**
  1.1727 +     * Determines whether the given <code>key</code> is contained in
  1.1728 +     * this <code>ResourceBundle</code> or its parent bundles.
  1.1729 +     *
  1.1730 +     * @param key
  1.1731 +     *        the resource <code>key</code>
  1.1732 +     * @return <code>true</code> if the given <code>key</code> is
  1.1733 +     *        contained in this <code>ResourceBundle</code> or its
  1.1734 +     *        parent bundles; <code>false</code> otherwise.
  1.1735 +     * @exception NullPointerException
  1.1736 +     *         if <code>key</code> is <code>null</code>
  1.1737 +     * @since 1.6
  1.1738 +     */
  1.1739 +    public boolean containsKey(String key) {
  1.1740 +        if (key == null) {
  1.1741 +            throw new NullPointerException();
  1.1742 +        }
  1.1743 +        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
  1.1744 +            if (rb.handleKeySet().contains(key)) {
  1.1745 +                return true;
  1.1746 +            }
  1.1747 +        }
  1.1748 +        return false;
  1.1749 +    }
  1.1750 +
  1.1751 +    /**
  1.1752 +     * Returns a <code>Set</code> of all keys contained in this
  1.1753 +     * <code>ResourceBundle</code> and its parent bundles.
  1.1754 +     *
  1.1755 +     * @return a <code>Set</code> of all keys contained in this
  1.1756 +     *         <code>ResourceBundle</code> and its parent bundles.
  1.1757 +     * @since 1.6
  1.1758 +     */
  1.1759 +    public Set<String> keySet() {
  1.1760 +        Set<String> keys = new HashSet<>();
  1.1761 +        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
  1.1762 +            keys.addAll(rb.handleKeySet());
  1.1763 +        }
  1.1764 +        return keys;
  1.1765 +    }
  1.1766 +
  1.1767 +    /**
  1.1768 +     * Returns a <code>Set</code> of the keys contained <em>only</em>
  1.1769 +     * in this <code>ResourceBundle</code>.
  1.1770 +     *
  1.1771 +     * <p>The default implementation returns a <code>Set</code> of the
  1.1772 +     * keys returned by the {@link #getKeys() getKeys} method except
  1.1773 +     * for the ones for which the {@link #handleGetObject(String)
  1.1774 +     * handleGetObject} method returns <code>null</code>. Once the
  1.1775 +     * <code>Set</code> has been created, the value is kept in this
  1.1776 +     * <code>ResourceBundle</code> in order to avoid producing the
  1.1777 +     * same <code>Set</code> in subsequent calls. Subclasses can
  1.1778 +     * override this method for faster handling.
  1.1779 +     *
  1.1780 +     * @return a <code>Set</code> of the keys contained only in this
  1.1781 +     *        <code>ResourceBundle</code>
  1.1782 +     * @since 1.6
  1.1783 +     */
  1.1784 +    protected Set<String> handleKeySet() {
  1.1785 +        if (keySet == null) {
  1.1786 +            synchronized (this) {
  1.1787 +                if (keySet == null) {
  1.1788 +                    Set<String> keys = new HashSet<>();
  1.1789 +                    Enumeration<String> enumKeys = getKeys();
  1.1790 +                    while (enumKeys.hasMoreElements()) {
  1.1791 +                        String key = enumKeys.nextElement();
  1.1792 +                        if (handleGetObject(key) != null) {
  1.1793 +                            keys.add(key);
  1.1794 +                        }
  1.1795 +                    }
  1.1796 +                    keySet = keys;
  1.1797 +                }
  1.1798 +            }
  1.1799 +        }
  1.1800 +        return keySet;
  1.1801 +    }
  1.1802 +
  1.1803 +
  1.1804 +
  1.1805 +    /**
  1.1806 +     * <code>ResourceBundle.Control</code> defines a set of callback methods
  1.1807 +     * that are invoked by the {@link ResourceBundle#getBundle(String,
  1.1808 +     * Locale, ClassLoader, Control) ResourceBundle.getBundle} factory
  1.1809 +     * methods during the bundle loading process. In other words, a
  1.1810 +     * <code>ResourceBundle.Control</code> collaborates with the factory
  1.1811 +     * methods for loading resource bundles. The default implementation of
  1.1812 +     * the callback methods provides the information necessary for the
  1.1813 +     * factory methods to perform the <a
  1.1814 +     * href="./ResourceBundle.html#default_behavior">default behavior</a>.
  1.1815 +     *
  1.1816 +     * <p>In addition to the callback methods, the {@link
  1.1817 +     * #toBundleName(String, Locale) toBundleName} and {@link
  1.1818 +     * #toResourceName(String, String) toResourceName} methods are defined
  1.1819 +     * primarily for convenience in implementing the callback
  1.1820 +     * methods. However, the <code>toBundleName</code> method could be
  1.1821 +     * overridden to provide different conventions in the organization and
  1.1822 +     * packaging of localized resources.  The <code>toResourceName</code>
  1.1823 +     * method is <code>final</code> to avoid use of wrong resource and class
  1.1824 +     * name separators.
  1.1825 +     *
  1.1826 +     * <p>Two factory methods, {@link #getControl(List)} and {@link
  1.1827 +     * #getNoFallbackControl(List)}, provide
  1.1828 +     * <code>ResourceBundle.Control</code> instances that implement common
  1.1829 +     * variations of the default bundle loading process.
  1.1830 +     *
  1.1831 +     * <p>The formats returned by the {@link Control#getFormats(String)
  1.1832 +     * getFormats} method and candidate locales returned by the {@link
  1.1833 +     * ResourceBundle.Control#getCandidateLocales(String, Locale)
  1.1834 +     * getCandidateLocales} method must be consistent in all
  1.1835 +     * <code>ResourceBundle.getBundle</code> invocations for the same base
  1.1836 +     * bundle. Otherwise, the <code>ResourceBundle.getBundle</code> methods
  1.1837 +     * may return unintended bundles. For example, if only
  1.1838 +     * <code>"java.class"</code> is returned by the <code>getFormats</code>
  1.1839 +     * method for the first call to <code>ResourceBundle.getBundle</code>
  1.1840 +     * and only <code>"java.properties"</code> for the second call, then the
  1.1841 +     * second call will return the class-based one that has been cached
  1.1842 +     * during the first call.
  1.1843 +     *
  1.1844 +     * <p>A <code>ResourceBundle.Control</code> instance must be thread-safe
  1.1845 +     * if it's simultaneously used by multiple threads.
  1.1846 +     * <code>ResourceBundle.getBundle</code> does not synchronize to call
  1.1847 +     * the <code>ResourceBundle.Control</code> methods. The default
  1.1848 +     * implementations of the methods are thread-safe.
  1.1849 +     *
  1.1850 +     * <p>Applications can specify <code>ResourceBundle.Control</code>
  1.1851 +     * instances returned by the <code>getControl</code> factory methods or
  1.1852 +     * created from a subclass of <code>ResourceBundle.Control</code> to
  1.1853 +     * customize the bundle loading process. The following are examples of
  1.1854 +     * changing the default bundle loading process.
  1.1855 +     *
  1.1856 +     * <p><b>Example 1</b>
  1.1857 +     *
  1.1858 +     * <p>The following code lets <code>ResourceBundle.getBundle</code> look
  1.1859 +     * up only properties-based resources.
  1.1860 +     *
  1.1861 +     * <pre>
  1.1862 +     * import java.util.*;
  1.1863 +     * import static java.util.ResourceBundle.Control.*;
  1.1864 +     * ...
  1.1865 +     * ResourceBundle bundle =
  1.1866 +     *   ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"),
  1.1867 +     *                            ResourceBundle.Control.getControl(FORMAT_PROPERTIES));
  1.1868 +     * </pre>
  1.1869 +     *
  1.1870 +     * Given the resource bundles in the <a
  1.1871 +     * href="./ResourceBundle.html#default_behavior_example">example</a> in
  1.1872 +     * the <code>ResourceBundle.getBundle</code> description, this
  1.1873 +     * <code>ResourceBundle.getBundle</code> call loads
  1.1874 +     * <code>MyResources_fr_CH.properties</code> whose parent is
  1.1875 +     * <code>MyResources_fr.properties</code> whose parent is
  1.1876 +     * <code>MyResources.properties</code>. (<code>MyResources_fr_CH.properties</code>
  1.1877 +     * is not hidden, but <code>MyResources_fr_CH.class</code> is.)
  1.1878 +     *
  1.1879 +     * <p><b>Example 2</b>
  1.1880 +     *
  1.1881 +     * <p>The following is an example of loading XML-based bundles
  1.1882 +     * using {@link Properties#loadFromXML(java.io.InputStream)
  1.1883 +     * Properties.loadFromXML}.
  1.1884 +     *
  1.1885 +     * <pre>
  1.1886 +     * ResourceBundle rb = ResourceBundle.getBundle("Messages",
  1.1887 +     *     new ResourceBundle.Control() {
  1.1888 +     *         public List&lt;String&gt; getFormats(String baseName) {
  1.1889 +     *             if (baseName == null)
  1.1890 +     *                 throw new NullPointerException();
  1.1891 +     *             return Arrays.asList("xml");
  1.1892 +     *         }
  1.1893 +     *         public ResourceBundle newBundle(String baseName,
  1.1894 +     *                                         Locale locale,
  1.1895 +     *                                         String format,
  1.1896 +     *                                         ClassLoader loader,
  1.1897 +     *                                         boolean reload)
  1.1898 +     *                          throws IllegalAccessException,
  1.1899 +     *                                 InstantiationException,
  1.1900 +     *                                 IOException {
  1.1901 +     *             if (baseName == null || locale == null
  1.1902 +     *                   || format == null || loader == null)
  1.1903 +     *                 throw new NullPointerException();
  1.1904 +     *             ResourceBundle bundle = null;
  1.1905 +     *             if (format.equals("xml")) {
  1.1906 +     *                 String bundleName = toBundleName(baseName, locale);
  1.1907 +     *                 String resourceName = toResourceName(bundleName, format);
  1.1908 +     *                 InputStream stream = null;
  1.1909 +     *                 if (reload) {
  1.1910 +     *                     URL url = loader.getResource(resourceName);
  1.1911 +     *                     if (url != null) {
  1.1912 +     *                         URLConnection connection = url.openConnection();
  1.1913 +     *                         if (connection != null) {
  1.1914 +     *                             // Disable caches to get fresh data for
  1.1915 +     *                             // reloading.
  1.1916 +     *                             connection.setUseCaches(false);
  1.1917 +     *                             stream = connection.getInputStream();
  1.1918 +     *                         }
  1.1919 +     *                     }
  1.1920 +     *                 } else {
  1.1921 +     *                     stream = loader.getResourceAsStream(resourceName);
  1.1922 +     *                 }
  1.1923 +     *                 if (stream != null) {
  1.1924 +     *                     BufferedInputStream bis = new BufferedInputStream(stream);
  1.1925 +     *                     bundle = new XMLResourceBundle(bis);
  1.1926 +     *                     bis.close();
  1.1927 +     *                 }
  1.1928 +     *             }
  1.1929 +     *             return bundle;
  1.1930 +     *         }
  1.1931 +     *     });
  1.1932 +     *
  1.1933 +     * ...
  1.1934 +     *
  1.1935 +     * private static class XMLResourceBundle extends ResourceBundle {
  1.1936 +     *     private Properties props;
  1.1937 +     *     XMLResourceBundle(InputStream stream) throws IOException {
  1.1938 +     *         props = new Properties();
  1.1939 +     *         props.loadFromXML(stream);
  1.1940 +     *     }
  1.1941 +     *     protected Object handleGetObject(String key) {
  1.1942 +     *         return props.getProperty(key);
  1.1943 +     *     }
  1.1944 +     *     public Enumeration&lt;String&gt; getKeys() {
  1.1945 +     *         ...
  1.1946 +     *     }
  1.1947 +     * }
  1.1948 +     * </pre>
  1.1949 +     *
  1.1950 +     * @since 1.6
  1.1951 +     */
  1.1952 +    public static class Control {
  1.1953 +        /**
  1.1954 +         * The default format <code>List</code>, which contains the strings
  1.1955 +         * <code>"java.class"</code> and <code>"java.properties"</code>, in
  1.1956 +         * this order. This <code>List</code> is {@linkplain
  1.1957 +         * Collections#unmodifiableList(List) unmodifiable}.
  1.1958 +         *
  1.1959 +         * @see #getFormats(String)
  1.1960 +         */
  1.1961 +        public static final List<String> FORMAT_DEFAULT
  1.1962 +            = Collections.unmodifiableList(Arrays.asList("java.class",
  1.1963 +                                                         "java.properties"));
  1.1964 +
  1.1965 +        /**
  1.1966 +         * The class-only format <code>List</code> containing
  1.1967 +         * <code>"java.class"</code>. This <code>List</code> is {@linkplain
  1.1968 +         * Collections#unmodifiableList(List) unmodifiable}.
  1.1969 +         *
  1.1970 +         * @see #getFormats(String)
  1.1971 +         */
  1.1972 +        public static final List<String> FORMAT_CLASS
  1.1973 +            = Collections.unmodifiableList(Arrays.asList("java.class"));
  1.1974 +
  1.1975 +        /**
  1.1976 +         * The properties-only format <code>List</code> containing
  1.1977 +         * <code>"java.properties"</code>. This <code>List</code> is
  1.1978 +         * {@linkplain Collections#unmodifiableList(List) unmodifiable}.
  1.1979 +         *
  1.1980 +         * @see #getFormats(String)
  1.1981 +         */
  1.1982 +        public static final List<String> FORMAT_PROPERTIES
  1.1983 +            = Collections.unmodifiableList(Arrays.asList("java.properties"));
  1.1984 +
  1.1985 +        /**
  1.1986 +         * The time-to-live constant for not caching loaded resource bundle
  1.1987 +         * instances.
  1.1988 +         *
  1.1989 +         * @see #getTimeToLive(String, Locale)
  1.1990 +         */
  1.1991 +        public static final long TTL_DONT_CACHE = -1;
  1.1992 +
  1.1993 +        /**
  1.1994 +         * The time-to-live constant for disabling the expiration control
  1.1995 +         * for loaded resource bundle instances in the cache.
  1.1996 +         *
  1.1997 +         * @see #getTimeToLive(String, Locale)
  1.1998 +         */
  1.1999 +        public static final long TTL_NO_EXPIRATION_CONTROL = -2;
  1.2000 +
  1.2001 +        private static final Control INSTANCE = new Control();
  1.2002 +
  1.2003 +        /**
  1.2004 +         * Sole constructor. (For invocation by subclass constructors,
  1.2005 +         * typically implicit.)
  1.2006 +         */
  1.2007 +        protected Control() {
  1.2008 +        }
  1.2009 +
  1.2010 +        /**
  1.2011 +         * Returns a <code>ResourceBundle.Control</code> in which the {@link
  1.2012 +         * #getFormats(String) getFormats} method returns the specified
  1.2013 +         * <code>formats</code>. The <code>formats</code> must be equal to
  1.2014 +         * one of {@link Control#FORMAT_PROPERTIES}, {@link
  1.2015 +         * Control#FORMAT_CLASS} or {@link
  1.2016 +         * Control#FORMAT_DEFAULT}. <code>ResourceBundle.Control</code>
  1.2017 +         * instances returned by this method are singletons and thread-safe.
  1.2018 +         *
  1.2019 +         * <p>Specifying {@link Control#FORMAT_DEFAULT} is equivalent to
  1.2020 +         * instantiating the <code>ResourceBundle.Control</code> class,
  1.2021 +         * except that this method returns a singleton.
  1.2022 +         *
  1.2023 +         * @param formats
  1.2024 +         *        the formats to be returned by the
  1.2025 +         *        <code>ResourceBundle.Control.getFormats</code> method
  1.2026 +         * @return a <code>ResourceBundle.Control</code> supporting the
  1.2027 +         *        specified <code>formats</code>
  1.2028 +         * @exception NullPointerException
  1.2029 +         *        if <code>formats</code> is <code>null</code>
  1.2030 +         * @exception IllegalArgumentException
  1.2031 +         *        if <code>formats</code> is unknown
  1.2032 +         */
  1.2033 +        public static final Control getControl(List<String> formats) {
  1.2034 +            if (formats.equals(Control.FORMAT_PROPERTIES)) {
  1.2035 +                return SingleFormatControl.PROPERTIES_ONLY;
  1.2036 +            }
  1.2037 +            if (formats.equals(Control.FORMAT_CLASS)) {
  1.2038 +                return SingleFormatControl.CLASS_ONLY;
  1.2039 +            }
  1.2040 +            if (formats.equals(Control.FORMAT_DEFAULT)) {
  1.2041 +                return Control.INSTANCE;
  1.2042 +            }
  1.2043 +            throw new IllegalArgumentException();
  1.2044 +        }
  1.2045 +
  1.2046 +        /**
  1.2047 +         * Returns a <code>ResourceBundle.Control</code> in which the {@link
  1.2048 +         * #getFormats(String) getFormats} method returns the specified
  1.2049 +         * <code>formats</code> and the {@link
  1.2050 +         * Control#getFallbackLocale(String, Locale) getFallbackLocale}
  1.2051 +         * method returns <code>null</code>. The <code>formats</code> must
  1.2052 +         * be equal to one of {@link Control#FORMAT_PROPERTIES}, {@link
  1.2053 +         * Control#FORMAT_CLASS} or {@link Control#FORMAT_DEFAULT}.
  1.2054 +         * <code>ResourceBundle.Control</code> instances returned by this
  1.2055 +         * method are singletons and thread-safe.
  1.2056 +         *
  1.2057 +         * @param formats
  1.2058 +         *        the formats to be returned by the
  1.2059 +         *        <code>ResourceBundle.Control.getFormats</code> method
  1.2060 +         * @return a <code>ResourceBundle.Control</code> supporting the
  1.2061 +         *        specified <code>formats</code> with no fallback
  1.2062 +         *        <code>Locale</code> support
  1.2063 +         * @exception NullPointerException
  1.2064 +         *        if <code>formats</code> is <code>null</code>
  1.2065 +         * @exception IllegalArgumentException
  1.2066 +         *        if <code>formats</code> is unknown
  1.2067 +         */
  1.2068 +        public static final Control getNoFallbackControl(List<String> formats) {
  1.2069 +            if (formats.equals(Control.FORMAT_DEFAULT)) {
  1.2070 +                return NoFallbackControl.NO_FALLBACK;
  1.2071 +            }
  1.2072 +            if (formats.equals(Control.FORMAT_PROPERTIES)) {
  1.2073 +                return NoFallbackControl.PROPERTIES_ONLY_NO_FALLBACK;
  1.2074 +            }
  1.2075 +            if (formats.equals(Control.FORMAT_CLASS)) {
  1.2076 +                return NoFallbackControl.CLASS_ONLY_NO_FALLBACK;
  1.2077 +            }
  1.2078 +            throw new IllegalArgumentException();
  1.2079 +        }
  1.2080 +
  1.2081 +        /**
  1.2082 +         * Returns a <code>List</code> of <code>String</code>s containing
  1.2083 +         * formats to be used to load resource bundles for the given
  1.2084 +         * <code>baseName</code>. The <code>ResourceBundle.getBundle</code>
  1.2085 +         * factory method tries to load resource bundles with formats in the
  1.2086 +         * order specified by the list. The list returned by this method
  1.2087 +         * must have at least one <code>String</code>. The predefined
  1.2088 +         * formats are <code>"java.class"</code> for class-based resource
  1.2089 +         * bundles and <code>"java.properties"</code> for {@linkplain
  1.2090 +         * PropertyResourceBundle properties-based} ones. Strings starting
  1.2091 +         * with <code>"java."</code> are reserved for future extensions and
  1.2092 +         * must not be used by application-defined formats.
  1.2093 +         *
  1.2094 +         * <p>It is not a requirement to return an immutable (unmodifiable)
  1.2095 +         * <code>List</code>.  However, the returned <code>List</code> must
  1.2096 +         * not be mutated after it has been returned by
  1.2097 +         * <code>getFormats</code>.
  1.2098 +         *
  1.2099 +         * <p>The default implementation returns {@link #FORMAT_DEFAULT} so
  1.2100 +         * that the <code>ResourceBundle.getBundle</code> factory method
  1.2101 +         * looks up first class-based resource bundles, then
  1.2102 +         * properties-based ones.
  1.2103 +         *
  1.2104 +         * @param baseName
  1.2105 +         *        the base name of the resource bundle, a fully qualified class
  1.2106 +         *        name
  1.2107 +         * @return a <code>List</code> of <code>String</code>s containing
  1.2108 +         *        formats for loading resource bundles.
  1.2109 +         * @exception NullPointerException
  1.2110 +         *        if <code>baseName</code> is null
  1.2111 +         * @see #FORMAT_DEFAULT
  1.2112 +         * @see #FORMAT_CLASS
  1.2113 +         * @see #FORMAT_PROPERTIES
  1.2114 +         */
  1.2115 +        public List<String> getFormats(String baseName) {
  1.2116 +            if (baseName == null) {
  1.2117 +                throw new NullPointerException();
  1.2118 +            }
  1.2119 +            return FORMAT_DEFAULT;
  1.2120 +        }
  1.2121 +
  1.2122 +        /**
  1.2123 +         * Returns a <code>List</code> of <code>Locale</code>s as candidate
  1.2124 +         * locales for <code>baseName</code> and <code>locale</code>. This
  1.2125 +         * method is called by the <code>ResourceBundle.getBundle</code>
  1.2126 +         * factory method each time the factory method tries finding a
  1.2127 +         * resource bundle for a target <code>Locale</code>.
  1.2128 +         *
  1.2129 +         * <p>The sequence of the candidate locales also corresponds to the
  1.2130 +         * runtime resource lookup path (also known as the <I>parent
  1.2131 +         * chain</I>), if the corresponding resource bundles for the
  1.2132 +         * candidate locales exist and their parents are not defined by
  1.2133 +         * loaded resource bundles themselves.  The last element of the list
  1.2134 +         * must be a {@linkplain Locale#ROOT root locale} if it is desired to
  1.2135 +         * have the base bundle as the terminal of the parent chain.
  1.2136 +         *
  1.2137 +         * <p>If the given locale is equal to <code>Locale.ROOT</code> (the
  1.2138 +         * root locale), a <code>List</code> containing only the root
  1.2139 +         * <code>Locale</code> must be returned. In this case, the
  1.2140 +         * <code>ResourceBundle.getBundle</code> factory method loads only
  1.2141 +         * the base bundle as the resulting resource bundle.
  1.2142 +         *
  1.2143 +         * <p>It is not a requirement to return an immutable (unmodifiable)
  1.2144 +         * <code>List</code>. However, the returned <code>List</code> must not
  1.2145 +         * be mutated after it has been returned by
  1.2146 +         * <code>getCandidateLocales</code>.
  1.2147 +         *
  1.2148 +         * <p>The default implementation returns a <code>List</code> containing
  1.2149 +         * <code>Locale</code>s using the rules described below.  In the
  1.2150 +         * description below, <em>L</em>, <em>S</em>, <em>C</em> and <em>V</em>
  1.2151 +         * respectively represent non-empty language, script, country, and
  1.2152 +         * variant.  For example, [<em>L</em>, <em>C</em>] represents a
  1.2153 +         * <code>Locale</code> that has non-empty values only for language and
  1.2154 +         * country.  The form <em>L</em>("xx") represents the (non-empty)
  1.2155 +         * language value is "xx".  For all cases, <code>Locale</code>s whose
  1.2156 +         * final component values are empty strings are omitted.
  1.2157 +         *
  1.2158 +         * <ol><li>For an input <code>Locale</code> with an empty script value,
  1.2159 +         * append candidate <code>Locale</code>s by omitting the final component
  1.2160 +         * one by one as below:
  1.2161 +         *
  1.2162 +         * <ul>
  1.2163 +         * <li> [<em>L</em>, <em>C</em>, <em>V</em>]
  1.2164 +         * <li> [<em>L</em>, <em>C</em>]
  1.2165 +         * <li> [<em>L</em>]
  1.2166 +         * <li> <code>Locale.ROOT</code>
  1.2167 +         * </ul>
  1.2168 +         *
  1.2169 +         * <li>For an input <code>Locale</code> with a non-empty script value,
  1.2170 +         * append candidate <code>Locale</code>s by omitting the final component
  1.2171 +         * up to language, then append candidates generated from the
  1.2172 +         * <code>Locale</code> with country and variant restored:
  1.2173 +         *
  1.2174 +         * <ul>
  1.2175 +         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V</em>]
  1.2176 +         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]
  1.2177 +         * <li> [<em>L</em>, <em>S</em>]
  1.2178 +         * <li> [<em>L</em>, <em>C</em>, <em>V</em>]
  1.2179 +         * <li> [<em>L</em>, <em>C</em>]
  1.2180 +         * <li> [<em>L</em>]
  1.2181 +         * <li> <code>Locale.ROOT</code>
  1.2182 +         * </ul>
  1.2183 +         *
  1.2184 +         * <li>For an input <code>Locale</code> with a variant value consisting
  1.2185 +         * of multiple subtags separated by underscore, generate candidate
  1.2186 +         * <code>Locale</code>s by omitting the variant subtags one by one, then
  1.2187 +         * insert them after every occurence of <code> Locale</code>s with the
  1.2188 +         * full variant value in the original list.  For example, if the
  1.2189 +         * the variant consists of two subtags <em>V1</em> and <em>V2</em>:
  1.2190 +         *
  1.2191 +         * <ul>
  1.2192 +         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]
  1.2193 +         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>]
  1.2194 +         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]
  1.2195 +         * <li> [<em>L</em>, <em>S</em>]
  1.2196 +         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]
  1.2197 +         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>]
  1.2198 +         * <li> [<em>L</em>, <em>C</em>]
  1.2199 +         * <li> [<em>L</em>]
  1.2200 +         * <li> <code>Locale.ROOT</code>
  1.2201 +         * </ul>
  1.2202 +         *
  1.2203 +         * <li>Special cases for Chinese.  When an input <code>Locale</code> has the
  1.2204 +         * language "zh" (Chinese) and an empty script value, either "Hans" (Simplified) or
  1.2205 +         * "Hant" (Traditional) might be supplied, depending on the country.
  1.2206 +         * When the country is "CN" (China) or "SG" (Singapore), "Hans" is supplied.
  1.2207 +         * When the country is "HK" (Hong Kong SAR China), "MO" (Macau SAR China),
  1.2208 +         * or "TW" (Taiwan), "Hant" is supplied.  For all other countries or when the country
  1.2209 +         * is empty, no script is supplied.  For example, for <code>Locale("zh", "CN")
  1.2210 +         * </code>, the candidate list will be:
  1.2211 +         * <ul>
  1.2212 +         * <li> [<em>L</em>("zh"), <em>S</em>("Hans"), <em>C</em>("CN")]
  1.2213 +         * <li> [<em>L</em>("zh"), <em>S</em>("Hans")]
  1.2214 +         * <li> [<em>L</em>("zh"), <em>C</em>("CN")]
  1.2215 +         * <li> [<em>L</em>("zh")]
  1.2216 +         * <li> <code>Locale.ROOT</code>
  1.2217 +         * </ul>
  1.2218 +         *
  1.2219 +         * For <code>Locale("zh", "TW")</code>, the candidate list will be:
  1.2220 +         * <ul>
  1.2221 +         * <li> [<em>L</em>("zh"), <em>S</em>("Hant"), <em>C</em>("TW")]
  1.2222 +         * <li> [<em>L</em>("zh"), <em>S</em>("Hant")]
  1.2223 +         * <li> [<em>L</em>("zh"), <em>C</em>("TW")]
  1.2224 +         * <li> [<em>L</em>("zh")]
  1.2225 +         * <li> <code>Locale.ROOT</code>
  1.2226 +         * </ul>
  1.2227 +         *
  1.2228 +         * <li>Special cases for Norwegian.  Both <code>Locale("no", "NO",
  1.2229 +         * "NY")</code> and <code>Locale("nn", "NO")</code> represent Norwegian
  1.2230 +         * Nynorsk.  When a locale's language is "nn", the standard candidate
  1.2231 +         * list is generated up to [<em>L</em>("nn")], and then the following
  1.2232 +         * candidates are added:
  1.2233 +         *
  1.2234 +         * <ul><li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("NY")]
  1.2235 +         * <li> [<em>L</em>("no"), <em>C</em>("NO")]
  1.2236 +         * <li> [<em>L</em>("no")]
  1.2237 +         * <li> <code>Locale.ROOT</code>
  1.2238 +         * </ul>
  1.2239 +         *
  1.2240 +         * If the locale is exactly <code>Locale("no", "NO", "NY")</code>, it is first
  1.2241 +         * converted to <code>Locale("nn", "NO")</code> and then the above procedure is
  1.2242 +         * followed.
  1.2243 +         *
  1.2244 +         * <p>Also, Java treats the language "no" as a synonym of Norwegian
  1.2245 +         * Bokm&#xE5;l "nb".  Except for the single case <code>Locale("no",
  1.2246 +         * "NO", "NY")</code> (handled above), when an input <code>Locale</code>
  1.2247 +         * has language "no" or "nb", candidate <code>Locale</code>s with
  1.2248 +         * language code "no" and "nb" are interleaved, first using the
  1.2249 +         * requested language, then using its synonym. For example,
  1.2250 +         * <code>Locale("nb", "NO", "POSIX")</code> generates the following
  1.2251 +         * candidate list:
  1.2252 +         *
  1.2253 +         * <ul>
  1.2254 +         * <li> [<em>L</em>("nb"), <em>C</em>("NO"), <em>V</em>("POSIX")]
  1.2255 +         * <li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("POSIX")]
  1.2256 +         * <li> [<em>L</em>("nb"), <em>C</em>("NO")]
  1.2257 +         * <li> [<em>L</em>("no"), <em>C</em>("NO")]
  1.2258 +         * <li> [<em>L</em>("nb")]
  1.2259 +         * <li> [<em>L</em>("no")]
  1.2260 +         * <li> <code>Locale.ROOT</code>
  1.2261 +         * </ul>
  1.2262 +         *
  1.2263 +         * <code>Locale("no", "NO", "POSIX")</code> would generate the same list
  1.2264 +         * except that locales with "no" would appear before the corresponding
  1.2265 +         * locales with "nb".</li>
  1.2266 +         *
  1.2267 +         * </li>
  1.2268 +         * </ol>
  1.2269 +         *
  1.2270 +         * <p>The default implementation uses an {@link ArrayList} that
  1.2271 +         * overriding implementations may modify before returning it to the
  1.2272 +         * caller. However, a subclass must not modify it after it has
  1.2273 +         * been returned by <code>getCandidateLocales</code>.
  1.2274 +         *
  1.2275 +         * <p>For example, if the given <code>baseName</code> is "Messages"
  1.2276 +         * and the given <code>locale</code> is
  1.2277 +         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then a
  1.2278 +         * <code>List</code> of <code>Locale</code>s:
  1.2279 +         * <pre>
  1.2280 +         *     Locale("ja", "", "XX")
  1.2281 +         *     Locale("ja")
  1.2282 +         *     Locale.ROOT
  1.2283 +         * </pre>
  1.2284 +         * is returned. And if the resource bundles for the "ja" and
  1.2285 +         * "" <code>Locale</code>s are found, then the runtime resource
  1.2286 +         * lookup path (parent chain) is:
  1.2287 +         * <pre>
  1.2288 +         *     Messages_ja -> Messages
  1.2289 +         * </pre>
  1.2290 +         *
  1.2291 +         * @param baseName
  1.2292 +         *        the base name of the resource bundle, a fully
  1.2293 +         *        qualified class name
  1.2294 +         * @param locale
  1.2295 +         *        the locale for which a resource bundle is desired
  1.2296 +         * @return a <code>List</code> of candidate
  1.2297 +         *        <code>Locale</code>s for the given <code>locale</code>
  1.2298 +         * @exception NullPointerException
  1.2299 +         *        if <code>baseName</code> or <code>locale</code> is
  1.2300 +         *        <code>null</code>
  1.2301 +         */
  1.2302 +        public List<Locale> getCandidateLocales(String baseName, Locale locale) {
  1.2303 +            if (baseName == null) {
  1.2304 +                throw new NullPointerException();
  1.2305 +            }
  1.2306 +            return new ArrayList<>(CANDIDATES_CACHE.get(locale.getBaseLocale()));
  1.2307 +        }
  1.2308 +
  1.2309 +        private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache();
  1.2310 +
  1.2311 +        private static class CandidateListCache extends LocaleObjectCache<BaseLocale, List<Locale>> {
  1.2312 +            protected List<Locale> createObject(BaseLocale base) {
  1.2313 +                String language = base.getLanguage();
  1.2314 +                String script = base.getScript();
  1.2315 +                String region = base.getRegion();
  1.2316 +                String variant = base.getVariant();
  1.2317 +
  1.2318 +                // Special handling for Norwegian
  1.2319 +                boolean isNorwegianBokmal = false;
  1.2320 +                boolean isNorwegianNynorsk = false;
  1.2321 +                if (language.equals("no")) {
  1.2322 +                    if (region.equals("NO") && variant.equals("NY")) {
  1.2323 +                        variant = "";
  1.2324 +                        isNorwegianNynorsk = true;
  1.2325 +                    } else {
  1.2326 +                        isNorwegianBokmal = true;
  1.2327 +                    }
  1.2328 +                }
  1.2329 +                if (language.equals("nb") || isNorwegianBokmal) {
  1.2330 +                    List<Locale> tmpList = getDefaultList("nb", script, region, variant);
  1.2331 +                    // Insert a locale replacing "nb" with "no" for every list entry
  1.2332 +                    List<Locale> bokmalList = new LinkedList<>();
  1.2333 +                    for (Locale l : tmpList) {
  1.2334 +                        bokmalList.add(l);
  1.2335 +                        if (l.getLanguage().length() == 0) {
  1.2336 +                            break;
  1.2337 +                        }
  1.2338 +                        bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
  1.2339 +                                l.getVariant(), null));
  1.2340 +                    }
  1.2341 +                    return bokmalList;
  1.2342 +                } else if (language.equals("nn") || isNorwegianNynorsk) {
  1.2343 +                    // Insert no_NO_NY, no_NO, no after nn
  1.2344 +                    List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
  1.2345 +                    int idx = nynorskList.size() - 1;
  1.2346 +                    nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY"));
  1.2347 +                    nynorskList.add(idx++, Locale.getInstance("no", "NO", ""));
  1.2348 +                    nynorskList.add(idx++, Locale.getInstance("no", "", ""));
  1.2349 +                    return nynorskList;
  1.2350 +                }
  1.2351 +                // Special handling for Chinese
  1.2352 +                else if (language.equals("zh")) {
  1.2353 +                    if (script.length() == 0 && region.length() > 0) {
  1.2354 +                        // Supply script for users who want to use zh_Hans/zh_Hant
  1.2355 +                        // as bundle names (recommended for Java7+)
  1.2356 +                        if (region.equals("TW") || region.equals("HK") || region.equals("MO")) {
  1.2357 +                            script = "Hant";
  1.2358 +                        } else if (region.equals("CN") || region.equals("SG")) {
  1.2359 +                            script = "Hans";
  1.2360 +                        }
  1.2361 +                    } else if (script.length() > 0 && region.length() == 0) {
  1.2362 +                        // Supply region(country) for users who still package Chinese
  1.2363 +                        // bundles using old convension.
  1.2364 +                        if (script.equals("Hans")) {
  1.2365 +                            region = "CN";
  1.2366 +                        } else if (script.equals("Hant")) {
  1.2367 +                            region = "TW";
  1.2368 +                        }
  1.2369 +                    }
  1.2370 +                }
  1.2371 +
  1.2372 +                return getDefaultList(language, script, region, variant);
  1.2373 +            }
  1.2374 +
  1.2375 +            private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
  1.2376 +                List<String> variants = null;
  1.2377 +
  1.2378 +                if (variant.length() > 0) {
  1.2379 +                    variants = new LinkedList<>();
  1.2380 +                    int idx = variant.length();
  1.2381 +                    while (idx != -1) {
  1.2382 +                        variants.add(variant.substring(0, idx));
  1.2383 +                        idx = variant.lastIndexOf('_', --idx);
  1.2384 +                    }
  1.2385 +                }
  1.2386 +
  1.2387 +                List<Locale> list = new LinkedList<>();
  1.2388 +
  1.2389 +                if (variants != null) {
  1.2390 +                    for (String v : variants) {
  1.2391 +                        list.add(Locale.getInstance(language, script, region, v, null));
  1.2392 +                    }
  1.2393 +                }
  1.2394 +                if (region.length() > 0) {
  1.2395 +                    list.add(Locale.getInstance(language, script, region, "", null));
  1.2396 +                }
  1.2397 +                if (script.length() > 0) {
  1.2398 +                    list.add(Locale.getInstance(language, script, "", "", null));
  1.2399 +
  1.2400 +                    // With script, after truncating variant, region and script,
  1.2401 +                    // start over without script.
  1.2402 +                    if (variants != null) {
  1.2403 +                        for (String v : variants) {
  1.2404 +                            list.add(Locale.getInstance(language, "", region, v, null));
  1.2405 +                        }
  1.2406 +                    }
  1.2407 +                    if (region.length() > 0) {
  1.2408 +                        list.add(Locale.getInstance(language, "", region, "", null));
  1.2409 +                    }
  1.2410 +                }
  1.2411 +                if (language.length() > 0) {
  1.2412 +                    list.add(Locale.getInstance(language, "", "", "", null));
  1.2413 +                }
  1.2414 +                // Add root locale at the end
  1.2415 +                list.add(Locale.ROOT);
  1.2416 +
  1.2417 +                return list;
  1.2418 +            }
  1.2419 +        }
  1.2420 +
  1.2421 +        /**
  1.2422 +         * Returns a <code>Locale</code> to be used as a fallback locale for
  1.2423 +         * further resource bundle searches by the
  1.2424 +         * <code>ResourceBundle.getBundle</code> factory method. This method
  1.2425 +         * is called from the factory method every time when no resulting
  1.2426 +         * resource bundle has been found for <code>baseName</code> and
  1.2427 +         * <code>locale</code>, where locale is either the parameter for
  1.2428 +         * <code>ResourceBundle.getBundle</code> or the previous fallback
  1.2429 +         * locale returned by this method.
  1.2430 +         *
  1.2431 +         * <p>The method returns <code>null</code> if no further fallback
  1.2432 +         * search is desired.
  1.2433 +         *
  1.2434 +         * <p>The default implementation returns the {@linkplain
  1.2435 +         * Locale#getDefault() default <code>Locale</code>} if the given
  1.2436 +         * <code>locale</code> isn't the default one.  Otherwise,
  1.2437 +         * <code>null</code> is returned.
  1.2438 +         *
  1.2439 +         * @param baseName
  1.2440 +         *        the base name of the resource bundle, a fully
  1.2441 +         *        qualified class name for which
  1.2442 +         *        <code>ResourceBundle.getBundle</code> has been
  1.2443 +         *        unable to find any resource bundles (except for the
  1.2444 +         *        base bundle)
  1.2445 +         * @param locale
  1.2446 +         *        the <code>Locale</code> for which
  1.2447 +         *        <code>ResourceBundle.getBundle</code> has been
  1.2448 +         *        unable to find any resource bundles (except for the
  1.2449 +         *        base bundle)
  1.2450 +         * @return a <code>Locale</code> for the fallback search,
  1.2451 +         *        or <code>null</code> if no further fallback search
  1.2452 +         *        is desired.
  1.2453 +         * @exception NullPointerException
  1.2454 +         *        if <code>baseName</code> or <code>locale</code>
  1.2455 +         *        is <code>null</code>
  1.2456 +         */
  1.2457 +        public Locale getFallbackLocale(String baseName, Locale locale) {
  1.2458 +            if (baseName == null) {
  1.2459 +                throw new NullPointerException();
  1.2460 +            }
  1.2461 +            Locale defaultLocale = Locale.getDefault();
  1.2462 +            return locale.equals(defaultLocale) ? null : defaultLocale;
  1.2463 +        }
  1.2464 +
  1.2465 +        /**
  1.2466 +         * Instantiates a resource bundle for the given bundle name of the
  1.2467 +         * given format and locale, using the given class loader if
  1.2468 +         * necessary. This method returns <code>null</code> if there is no
  1.2469 +         * resource bundle available for the given parameters. If a resource
  1.2470 +         * bundle can't be instantiated due to an unexpected error, the
  1.2471 +         * error must be reported by throwing an <code>Error</code> or
  1.2472 +         * <code>Exception</code> rather than simply returning
  1.2473 +         * <code>null</code>.
  1.2474 +         *
  1.2475 +         * <p>If the <code>reload</code> flag is <code>true</code>, it
  1.2476 +         * indicates that this method is being called because the previously
  1.2477 +         * loaded resource bundle has expired.
  1.2478 +         *
  1.2479 +         * <p>The default implementation instantiates a
  1.2480 +         * <code>ResourceBundle</code> as follows.
  1.2481 +         *
  1.2482 +         * <ul>
  1.2483 +         *
  1.2484 +         * <li>The bundle name is obtained by calling {@link
  1.2485 +         * #toBundleName(String, Locale) toBundleName(baseName,
  1.2486 +         * locale)}.</li>
  1.2487 +         *
  1.2488 +         * <li>If <code>format</code> is <code>"java.class"</code>, the
  1.2489 +         * {@link Class} specified by the bundle name is loaded by calling
  1.2490 +         * {@link ClassLoader#loadClass(String)}. Then, a
  1.2491 +         * <code>ResourceBundle</code> is instantiated by calling {@link
  1.2492 +         * Class#newInstance()}.  Note that the <code>reload</code> flag is
  1.2493 +         * ignored for loading class-based resource bundles in this default
  1.2494 +         * implementation.</li>
  1.2495 +         *
  1.2496 +         * <li>If <code>format</code> is <code>"java.properties"</code>,
  1.2497 +         * {@link #toResourceName(String, String) toResourceName(bundlename,
  1.2498 +         * "properties")} is called to get the resource name.
  1.2499 +         * If <code>reload</code> is <code>true</code>, {@link
  1.2500 +         * ClassLoader#getResource(String) load.getResource} is called
  1.2501 +         * to get a {@link URL} for creating a {@link
  1.2502 +         * URLConnection}. This <code>URLConnection</code> is used to
  1.2503 +         * {@linkplain URLConnection#setUseCaches(boolean) disable the
  1.2504 +         * caches} of the underlying resource loading layers,
  1.2505 +         * and to {@linkplain URLConnection#getInputStream() get an
  1.2506 +         * <code>InputStream</code>}.
  1.2507 +         * Otherwise, {@link ClassLoader#getResourceAsStream(String)
  1.2508 +         * loader.getResourceAsStream} is called to get an {@link
  1.2509 +         * InputStream}. Then, a {@link
  1.2510 +         * PropertyResourceBundle} is constructed with the
  1.2511 +         * <code>InputStream</code>.</li>
  1.2512 +         *
  1.2513 +         * <li>If <code>format</code> is neither <code>"java.class"</code>
  1.2514 +         * nor <code>"java.properties"</code>, an
  1.2515 +         * <code>IllegalArgumentException</code> is thrown.</li>
  1.2516 +         *
  1.2517 +         * </ul>
  1.2518 +         *
  1.2519 +         * @param baseName
  1.2520 +         *        the base bundle name of the resource bundle, a fully
  1.2521 +         *        qualified class name
  1.2522 +         * @param locale
  1.2523 +         *        the locale for which the resource bundle should be
  1.2524 +         *        instantiated
  1.2525 +         * @param format
  1.2526 +         *        the resource bundle format to be loaded
  1.2527 +         * @param loader
  1.2528 +         *        the <code>ClassLoader</code> to use to load the bundle
  1.2529 +         * @param reload
  1.2530 +         *        the flag to indicate bundle reloading; <code>true</code>
  1.2531 +         *        if reloading an expired resource bundle,
  1.2532 +         *        <code>false</code> otherwise
  1.2533 +         * @return the resource bundle instance,
  1.2534 +         *        or <code>null</code> if none could be found.
  1.2535 +         * @exception NullPointerException
  1.2536 +         *        if <code>bundleName</code>, <code>locale</code>,
  1.2537 +         *        <code>format</code>, or <code>loader</code> is
  1.2538 +         *        <code>null</code>, or if <code>null</code> is returned by
  1.2539 +         *        {@link #toBundleName(String, Locale) toBundleName}
  1.2540 +         * @exception IllegalArgumentException
  1.2541 +         *        if <code>format</code> is unknown, or if the resource
  1.2542 +         *        found for the given parameters contains malformed data.
  1.2543 +         * @exception ClassCastException
  1.2544 +         *        if the loaded class cannot be cast to <code>ResourceBundle</code>
  1.2545 +         * @exception IllegalAccessException
  1.2546 +         *        if the class or its nullary constructor is not
  1.2547 +         *        accessible.
  1.2548 +         * @exception InstantiationException
  1.2549 +         *        if the instantiation of a class fails for some other
  1.2550 +         *        reason.
  1.2551 +         * @exception ExceptionInInitializerError
  1.2552 +         *        if the initialization provoked by this method fails.
  1.2553 +         * @exception SecurityException
  1.2554 +         *        If a security manager is present and creation of new
  1.2555 +         *        instances is denied. See {@link Class#newInstance()}
  1.2556 +         *        for details.
  1.2557 +         * @exception IOException
  1.2558 +         *        if an error occurred when reading resources using
  1.2559 +         *        any I/O operations
  1.2560 +         */
  1.2561 +        public ResourceBundle newBundle(String baseName, Locale locale, String format,
  1.2562 +                                        ClassLoader loader, boolean reload)
  1.2563 +                    throws IllegalAccessException, InstantiationException, IOException {
  1.2564 +            String bundleName = toBundleName(baseName, locale);
  1.2565 +            ResourceBundle bundle = null;
  1.2566 +            if (format.equals("java.class")) {
  1.2567 +                try {
  1.2568 +                    Class<? extends ResourceBundle> bundleClass
  1.2569 +                        = (Class<? extends ResourceBundle>)loader.loadClass(bundleName);
  1.2570 +
  1.2571 +                    // If the class isn't a ResourceBundle subclass, throw a
  1.2572 +                    // ClassCastException.
  1.2573 +                    if (ResourceBundle.class.isAssignableFrom(bundleClass)) {
  1.2574 +                        bundle = bundleClass.newInstance();
  1.2575 +                    } else {
  1.2576 +                        throw new ClassCastException(bundleClass.getName()
  1.2577 +                                     + " cannot be cast to ResourceBundle");
  1.2578 +                    }
  1.2579 +                } catch (ClassNotFoundException e) {
  1.2580 +                }
  1.2581 +            } else if (format.equals("java.properties")) {
  1.2582 +                final String resourceName = toResourceName(bundleName, "properties");
  1.2583 +                final ClassLoader classLoader = loader;
  1.2584 +                final boolean reloadFlag = reload;
  1.2585 +                InputStream stream = null;
  1.2586 +                try {
  1.2587 +                    stream = AccessController.doPrivileged(
  1.2588 +                        new PrivilegedExceptionAction<InputStream>() {
  1.2589 +                            public InputStream run() throws IOException {
  1.2590 +                                InputStream is = null;
  1.2591 +                                if (reloadFlag) {
  1.2592 +                                    URL url = classLoader.getResource(resourceName);
  1.2593 +                                    if (url != null) {
  1.2594 +                                        URLConnection connection = url.openConnection();
  1.2595 +                                        if (connection != null) {
  1.2596 +                                            // Disable caches to get fresh data for
  1.2597 +                                            // reloading.
  1.2598 +                                            connection.setUseCaches(false);
  1.2599 +                                            is = connection.getInputStream();
  1.2600 +                                        }
  1.2601 +                                    }
  1.2602 +                                } else {
  1.2603 +                                    is = classLoader.getResourceAsStream(resourceName);
  1.2604 +                                }
  1.2605 +                                return is;
  1.2606 +                            }
  1.2607 +                        });
  1.2608 +                } catch (PrivilegedActionException e) {
  1.2609 +                    throw (IOException) e.getException();
  1.2610 +                }
  1.2611 +                if (stream != null) {
  1.2612 +                    try {
  1.2613 +                        bundle = new PropertyResourceBundle(stream);
  1.2614 +                    } finally {
  1.2615 +                        stream.close();
  1.2616 +                    }
  1.2617 +                }
  1.2618 +            } else {
  1.2619 +                throw new IllegalArgumentException("unknown format: " + format);
  1.2620 +            }
  1.2621 +            return bundle;
  1.2622 +        }
  1.2623 +
  1.2624 +        /**
  1.2625 +         * Returns the time-to-live (TTL) value for resource bundles that
  1.2626 +         * are loaded under this
  1.2627 +         * <code>ResourceBundle.Control</code>. Positive time-to-live values
  1.2628 +         * specify the number of milliseconds a bundle can remain in the
  1.2629 +         * cache without being validated against the source data from which
  1.2630 +         * it was constructed. The value 0 indicates that a bundle must be
  1.2631 +         * validated each time it is retrieved from the cache. {@link
  1.2632 +         * #TTL_DONT_CACHE} specifies that loaded resource bundles are not
  1.2633 +         * put in the cache. {@link #TTL_NO_EXPIRATION_CONTROL} specifies
  1.2634 +         * that loaded resource bundles are put in the cache with no
  1.2635 +         * expiration control.
  1.2636 +         *
  1.2637 +         * <p>The expiration affects only the bundle loading process by the
  1.2638 +         * <code>ResourceBundle.getBundle</code> factory method.  That is,
  1.2639 +         * if the factory method finds a resource bundle in the cache that
  1.2640 +         * has expired, the factory method calls the {@link
  1.2641 +         * #needsReload(String, Locale, String, ClassLoader, ResourceBundle,
  1.2642 +         * long) needsReload} method to determine whether the resource
  1.2643 +         * bundle needs to be reloaded. If <code>needsReload</code> returns
  1.2644 +         * <code>true</code>, the cached resource bundle instance is removed
  1.2645 +         * from the cache. Otherwise, the instance stays in the cache,
  1.2646 +         * updated with the new TTL value returned by this method.
  1.2647 +         *
  1.2648 +         * <p>All cached resource bundles are subject to removal from the
  1.2649 +         * cache due to memory constraints of the runtime environment.
  1.2650 +         * Returning a large positive value doesn't mean to lock loaded
  1.2651 +         * resource bundles in the cache.
  1.2652 +         *
  1.2653 +         * <p>The default implementation returns {@link #TTL_NO_EXPIRATION_CONTROL}.
  1.2654 +         *
  1.2655 +         * @param baseName
  1.2656 +         *        the base name of the resource bundle for which the
  1.2657 +         *        expiration value is specified.
  1.2658 +         * @param locale
  1.2659 +         *        the locale of the resource bundle for which the
  1.2660 +         *        expiration value is specified.
  1.2661 +         * @return the time (0 or a positive millisecond offset from the
  1.2662 +         *        cached time) to get loaded bundles expired in the cache,
  1.2663 +         *        {@link #TTL_NO_EXPIRATION_CONTROL} to disable the
  1.2664 +         *        expiration control, or {@link #TTL_DONT_CACHE} to disable
  1.2665 +         *        caching.
  1.2666 +         * @exception NullPointerException
  1.2667 +         *        if <code>baseName</code> or <code>locale</code> is
  1.2668 +         *        <code>null</code>
  1.2669 +         */
  1.2670 +        public long getTimeToLive(String baseName, Locale locale) {
  1.2671 +            if (baseName == null || locale == null) {
  1.2672 +                throw new NullPointerException();
  1.2673 +            }
  1.2674 +            return TTL_NO_EXPIRATION_CONTROL;
  1.2675 +        }
  1.2676 +
  1.2677 +        /**
  1.2678 +         * Determines if the expired <code>bundle</code> in the cache needs
  1.2679 +         * to be reloaded based on the loading time given by
  1.2680 +         * <code>loadTime</code> or some other criteria. The method returns
  1.2681 +         * <code>true</code> if reloading is required; <code>false</code>
  1.2682 +         * otherwise. <code>loadTime</code> is a millisecond offset since
  1.2683 +         * the <a href="Calendar.html#Epoch"> <code>Calendar</code>
  1.2684 +         * Epoch</a>.
  1.2685 +         *
  1.2686 +         * The calling <code>ResourceBundle.getBundle</code> factory method
  1.2687 +         * calls this method on the <code>ResourceBundle.Control</code>
  1.2688 +         * instance used for its current invocation, not on the instance
  1.2689 +         * used in the invocation that originally loaded the resource
  1.2690 +         * bundle.
  1.2691 +         *
  1.2692 +         * <p>The default implementation compares <code>loadTime</code> and
  1.2693 +         * the last modified time of the source data of the resource
  1.2694 +         * bundle. If it's determined that the source data has been modified
  1.2695 +         * since <code>loadTime</code>, <code>true</code> is
  1.2696 +         * returned. Otherwise, <code>false</code> is returned. This
  1.2697 +         * implementation assumes that the given <code>format</code> is the
  1.2698 +         * same string as its file suffix if it's not one of the default
  1.2699 +         * formats, <code>"java.class"</code> or
  1.2700 +         * <code>"java.properties"</code>.
  1.2701 +         *
  1.2702 +         * @param baseName
  1.2703 +         *        the base bundle name of the resource bundle, a
  1.2704 +         *        fully qualified class name
  1.2705 +         * @param locale
  1.2706 +         *        the locale for which the resource bundle
  1.2707 +         *        should be instantiated
  1.2708 +         * @param format
  1.2709 +         *        the resource bundle format to be loaded
  1.2710 +         * @param loader
  1.2711 +         *        the <code>ClassLoader</code> to use to load the bundle
  1.2712 +         * @param bundle
  1.2713 +         *        the resource bundle instance that has been expired
  1.2714 +         *        in the cache
  1.2715 +         * @param loadTime
  1.2716 +         *        the time when <code>bundle</code> was loaded and put
  1.2717 +         *        in the cache
  1.2718 +         * @return <code>true</code> if the expired bundle needs to be
  1.2719 +         *        reloaded; <code>false</code> otherwise.
  1.2720 +         * @exception NullPointerException
  1.2721 +         *        if <code>baseName</code>, <code>locale</code>,
  1.2722 +         *        <code>format</code>, <code>loader</code>, or
  1.2723 +         *        <code>bundle</code> is <code>null</code>
  1.2724 +         */
  1.2725 +        public boolean needsReload(String baseName, Locale locale,
  1.2726 +                                   String format, ClassLoader loader,
  1.2727 +                                   ResourceBundle bundle, long loadTime) {
  1.2728 +            if (bundle == null) {
  1.2729 +                throw new NullPointerException();
  1.2730 +            }
  1.2731 +            if (format.equals("java.class") || format.equals("java.properties")) {
  1.2732 +                format = format.substring(5);
  1.2733 +            }
  1.2734 +            boolean result = false;
  1.2735 +            try {
  1.2736 +                String resourceName = toResourceName(toBundleName(baseName, locale), format);
  1.2737 +                URL url = loader.getResource(resourceName);
  1.2738 +                if (url != null) {
  1.2739 +                    long lastModified = 0;
  1.2740 +                    URLConnection connection = url.openConnection();
  1.2741 +                    if (connection != null) {
  1.2742 +                        // disable caches to get the correct data
  1.2743 +                        connection.setUseCaches(false);
  1.2744 +                        if (connection instanceof JarURLConnection) {
  1.2745 +                            JarEntry ent = ((JarURLConnection)connection).getJarEntry();
  1.2746 +                            if (ent != null) {
  1.2747 +                                lastModified = ent.getTime();
  1.2748 +                                if (lastModified == -1) {
  1.2749 +                                    lastModified = 0;
  1.2750 +                                }
  1.2751 +                            }
  1.2752 +                        } else {
  1.2753 +                            lastModified = connection.getLastModified();
  1.2754 +                        }
  1.2755 +                    }
  1.2756 +                    result = lastModified >= loadTime;
  1.2757 +                }
  1.2758 +            } catch (NullPointerException npe) {
  1.2759 +                throw npe;
  1.2760 +            } catch (Exception e) {
  1.2761 +                // ignore other exceptions
  1.2762 +            }
  1.2763 +            return result;
  1.2764 +        }
  1.2765 +
  1.2766 +        /**
  1.2767 +         * Converts the given <code>baseName</code> and <code>locale</code>
  1.2768 +         * to the bundle name. This method is called from the default
  1.2769 +         * implementation of the {@link #newBundle(String, Locale, String,
  1.2770 +         * ClassLoader, boolean) newBundle} and {@link #needsReload(String,
  1.2771 +         * Locale, String, ClassLoader, ResourceBundle, long) needsReload}
  1.2772 +         * methods.
  1.2773 +         *
  1.2774 +         * <p>This implementation returns the following value:
  1.2775 +         * <pre>
  1.2776 +         *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
  1.2777 +         * </pre>
  1.2778 +         * where <code>language</code>, <code>script</code>, <code>country</code>,
  1.2779 +         * and <code>variant</code> are the language, script, country, and variant
  1.2780 +         * values of <code>locale</code>, respectively. Final component values that
  1.2781 +         * are empty Strings are omitted along with the preceding '_'.  When the
  1.2782 +         * script is empty, the script value is ommitted along with the preceding '_'.
  1.2783 +         * If all of the values are empty strings, then <code>baseName</code>
  1.2784 +         * is returned.
  1.2785 +         *
  1.2786 +         * <p>For example, if <code>baseName</code> is
  1.2787 +         * <code>"baseName"</code> and <code>locale</code> is
  1.2788 +         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then
  1.2789 +         * <code>"baseName_ja_&thinsp;_XX"</code> is returned. If the given
  1.2790 +         * locale is <code>Locale("en")</code>, then
  1.2791 +         * <code>"baseName_en"</code> is returned.
  1.2792 +         *
  1.2793 +         * <p>Overriding this method allows applications to use different
  1.2794 +         * conventions in the organization and packaging of localized
  1.2795 +         * resources.
  1.2796 +         *
  1.2797 +         * @param baseName
  1.2798 +         *        the base name of the resource bundle, a fully
  1.2799 +         *        qualified class name
  1.2800 +         * @param locale
  1.2801 +         *        the locale for which a resource bundle should be
  1.2802 +         *        loaded
  1.2803 +         * @return the bundle name for the resource bundle
  1.2804 +         * @exception NullPointerException
  1.2805 +         *        if <code>baseName</code> or <code>locale</code>
  1.2806 +         *        is <code>null</code>
  1.2807 +         */
  1.2808 +        public String toBundleName(String baseName, Locale locale) {
  1.2809 +            if (locale == Locale.ROOT) {
  1.2810 +                return baseName;
  1.2811 +            }
  1.2812 +
  1.2813 +            String language = locale.getLanguage();
  1.2814 +            String script = locale.getScript();
  1.2815 +            String country = locale.getCountry();
  1.2816 +            String variant = locale.getVariant();
  1.2817 +
  1.2818 +            if (language == "" && country == "" && variant == "") {
  1.2819 +                return baseName;
  1.2820 +            }
  1.2821 +
  1.2822 +            StringBuilder sb = new StringBuilder(baseName);
  1.2823 +            sb.append('_');
  1.2824 +            if (script != "") {
  1.2825 +                if (variant != "") {
  1.2826 +                    sb.append(language).append('_').append(script).append('_').append(country).append('_').append(variant);
  1.2827 +                } else if (country != "") {
  1.2828 +                    sb.append(language).append('_').append(script).append('_').append(country);
  1.2829 +                } else {
  1.2830 +                    sb.append(language).append('_').append(script);
  1.2831 +                }
  1.2832 +            } else {
  1.2833 +                if (variant != "") {
  1.2834 +                    sb.append(language).append('_').append(country).append('_').append(variant);
  1.2835 +                } else if (country != "") {
  1.2836 +                    sb.append(language).append('_').append(country);
  1.2837 +                } else {
  1.2838 +                    sb.append(language);
  1.2839 +                }
  1.2840 +            }
  1.2841 +            return sb.toString();
  1.2842 +
  1.2843 +        }
  1.2844 +
  1.2845 +        /**
  1.2846 +         * Converts the given <code>bundleName</code> to the form required
  1.2847 +         * by the {@link ClassLoader#getResource ClassLoader.getResource}
  1.2848 +         * method by replacing all occurrences of <code>'.'</code> in
  1.2849 +         * <code>bundleName</code> with <code>'/'</code> and appending a
  1.2850 +         * <code>'.'</code> and the given file <code>suffix</code>. For
  1.2851 +         * example, if <code>bundleName</code> is
  1.2852 +         * <code>"foo.bar.MyResources_ja_JP"</code> and <code>suffix</code>
  1.2853 +         * is <code>"properties"</code>, then
  1.2854 +         * <code>"foo/bar/MyResources_ja_JP.properties"</code> is returned.
  1.2855 +         *
  1.2856 +         * @param bundleName
  1.2857 +         *        the bundle name
  1.2858 +         * @param suffix
  1.2859 +         *        the file type suffix
  1.2860 +         * @return the converted resource name
  1.2861 +         * @exception NullPointerException
  1.2862 +         *         if <code>bundleName</code> or <code>suffix</code>
  1.2863 +         *         is <code>null</code>
  1.2864 +         */
  1.2865 +        public final String toResourceName(String bundleName, String suffix) {
  1.2866 +            StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length());
  1.2867 +            sb.append(bundleName.replace('.', '/')).append('.').append(suffix);
  1.2868 +            return sb.toString();
  1.2869 +        }
  1.2870 +    }
  1.2871 +
  1.2872 +    private static class SingleFormatControl extends Control {
  1.2873 +        private static final Control PROPERTIES_ONLY
  1.2874 +            = new SingleFormatControl(FORMAT_PROPERTIES);
  1.2875 +
  1.2876 +        private static final Control CLASS_ONLY
  1.2877 +            = new SingleFormatControl(FORMAT_CLASS);
  1.2878 +
  1.2879 +        private final List<String> formats;
  1.2880 +
  1.2881 +        protected SingleFormatControl(List<String> formats) {
  1.2882 +            this.formats = formats;
  1.2883 +        }
  1.2884 +
  1.2885 +        public List<String> getFormats(String baseName) {
  1.2886 +            if (baseName == null) {
  1.2887 +                throw new NullPointerException();
  1.2888 +            }
  1.2889 +            return formats;
  1.2890 +        }
  1.2891 +    }
  1.2892 +
  1.2893 +    private static final class NoFallbackControl extends SingleFormatControl {
  1.2894 +        private static final Control NO_FALLBACK
  1.2895 +            = new NoFallbackControl(FORMAT_DEFAULT);
  1.2896 +
  1.2897 +        private static final Control PROPERTIES_ONLY_NO_FALLBACK
  1.2898 +            = new NoFallbackControl(FORMAT_PROPERTIES);
  1.2899 +
  1.2900 +        private static final Control CLASS_ONLY_NO_FALLBACK
  1.2901 +            = new NoFallbackControl(FORMAT_CLASS);
  1.2902 +
  1.2903 +        protected NoFallbackControl(List<String> formats) {
  1.2904 +            super(formats);
  1.2905 +        }
  1.2906 +
  1.2907 +        public Locale getFallbackLocale(String baseName, Locale locale) {
  1.2908 +            if (baseName == null || locale == null) {
  1.2909 +                throw new NullPointerException();
  1.2910 +            }
  1.2911 +            return null;
  1.2912 +        }
  1.2913 +    }
  1.2914 +}