Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
I am learning GoF Java Design Patterns and I want to see some real life examples of them. Can you guys point to some good usage of these Design Patterns.(preferably in Java's core libraries). Thank you
I am trying to use ResourceBundle#getStringArray to retrieve a String[] from a properties file. The description of this method in the documentation reads: Gets a string array for the given key from this resource bundle or one of its parents. However, I have attempted to store the values in the properties file as multiple individual key/value pairs: key=value1 key=value2 key=value3 and ...
It's tedious and ugly to write things like: <input type="button" value="<fmt:message key="submitKey" />" /> And in case you want to nest the message tag in another tag's attribute it becomes even worse. Is there any shorthand for that. For example (like in JSF): <h:commandButton value="#{msg.shareKey}" /> (spring-mvc-only solutions applicable)
I have a rather big number of source files that I need parse and extract all string literals and put them in a file as play old java constant. For exemple: Label l = new Label("Cat"); Would become: Label l = new Label(Constants.CAT); And in Constants.java I would have: public final static String CAT = "Cat"; I do not want the strings to be externalized in a property text file. One reason ...
I would like to know what is the right way to handle internationalization for statements with runtime data added to it. For example 1) Your input "xyz" is excellent! 2) You were "4 years" old when you switched from "Barney and Freinds" to "Spongebob" shows. The double quoted values are user data obtained or calculated at run time. My platforms are primarily Java/Android. A right solution for...
I have a Java web application at my work and I'd like simplify how we deploy to our DEV, QA, and PROD environments. The application reads in a series of properties at startup, and the properties files are different for dev, qa, and prod. Whenever I want to deploy to a certain environment I drop the environment-specific properties file into my app folder, build the war, and then deploy it t...
colleagues, where do you store user-specific and machine-specific runtime configuration data for J2SE application? (For example, C:\Users\USERNAME\AppData\Roaming\ on Windows and /home/username on Unix) How do you get these locations in the filesystem in platform-independent way? Thanks for your advice!
Has anyone had any experiences developing large Java applications using GNU gettext for internationalization? I find that I really like having the English text in my source code, but I want to make sure that what I'm doing is practical for the relatively large software project I am part of. If you have experience with this, what are you using to look up the resources in Java? I'm currently t...
I want to create a Producer that makes it possible to inject a java.util.ResourceBundle into any class in order to get localized Strings easily. My ResourceBundle-Producer looks like this: public class ResourceBundleProducer { @Inject public Locale locale; @Inject public FacesContext facesContext; @Produces public ResourceBundle getResourceBundle() { return Reso...
I learnt from Google that Internationalization is the process by which i can make my web application to use all languages. I want to understand unicode for the process of internationalization, so i learnt about unicode from here and there. I am able to understand about unicode that how a charset set in encoded to bytes and again bytes decoded to charset. But i dont know how to move forward fu...
We need to start adding internationalisation to our program. Thankfully not the whole thing yet, just a few bits, but I want the way we do it to scale up to potentially cover the whole program. The thing is, our program is based on plugins, so not all strings belong in the same place. As far as I understand it, Java's ResourceBundle work like this. You create a class that extends ResourceBundl...
A friend of mine is now building a web application with J2EE and Struts, and it's going to be prepared to display pages in several languages. I was told that the best way to support a multi-language site is to use a properties file where you store all the strings of your pages, something like: welcome.english = "Welcome!" welcome.spanish = "¡Bienvenido!" ... This solution is ok, but what ha...
I'm refactoring an existing Java desktop application to load internationalized UI text labels from a .properties file. When in the application lifecycle is the appropriate time to load the properties file and populate the strings in memory? The existing implementation defines each component's label as e.g.: public static final String foo = "bar"; It seems inefficient to load the text in the co...
I see a lot of J2EE developers put labels in property files but don't use different Locales. So, you get a lot of missing property exceptions. And the main thing is that it makes it hard to debug and read a JSP page. So over time, you have thousands of lines of property files that may or may not be used with the JSP file. For me, it seems like a bad design, especially if you don't intend to...
I'd like to eliminate dependencies on hardcoded paths for configuration data in my Java apps, I understand that using ResourceBundle will help me use the classloader to find resources. Can someone tell me how I would replace a hardcoded path to a resource (say a .properties configuration data file required by a class) with appropriate use of ResourceBundle? Simple clear example if possible, th...
Consider the following class DialgBean.java, which defines the properties of a dialog box on a web page. Below is the class and its bean definition public class DialogBean{ private int height; public void setHeight(int height) ... } <bean id="dialogBean" class="org.springhelp.DialogBean"> <property name="height" value="${dialogBean.height}"/> ... </bean> From th...
I want to reload the ResourceBundles used by JSF programmatically on a button click. The ResourceBundles in the classpath (i.e. WEB-INF/classes) are modified by an external application and the modification event is known to me.
Does Java and/or Spring have the concept of properties? I have bunch of domain models, each of which has several properties. Example: public class Person { private String name; private Date dateOfBirth; private float height; private float weight; // getters and setters not shown } When displaying a person, the property names are hardcoded in the JSP. Name: ${person.name}...
I need some configuration files, that can be changed without recompiling the project in my GWT application. However, the GWT i18n doesn't allow to be used serverside. So what's the solution for getting configuration constants to be used serverside? Thanks.
What is a best practice for storing user messages in a configuration file and then retrieving them for certain events throughout an application? I was thinking of having 1 single configuration file with entries such as REQUIRED_FIELD = {0} is a required field INVALID_FORMAT = The format for {0} is {1} etc. and then calling them from a class that would be something like this public class U...
I maintain an applet that helps users to upload photos to our service. The applet jar file has a few .properties files: >> jar -tf applet.jar | grep prop res/messages.properties res/messages_ca.properties res/messages_es.properties ... These are loaded during applet initialization. messages = ResourceBundle.getBundle("res.messages"); This call however generates 4 to 5 requests to th...
   /*
    * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    *
    * This code is free software; you can redistribute it and/or modify it
    * under the terms of the GNU General Public License version 2 only, as
    * published by the Free Software Foundation.  Sun designates this
    * particular file as subject to the "Classpath" exception as provided
    * by Sun in the LICENSE file that accompanied this code.
   *
   * This code is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   * version 2 for more details (a copy is included in the LICENSE file that
   * accompanied this code).
   *
   * You should have received a copy of the GNU General Public License version
   * 2 along with this work; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   *
   * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   * CA 95054 USA or visit www.sun.com if you need additional information or
   * have any questions.
   */
  
  /*
   * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
   * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
   *
   * The original version of this source code and documentation
   * is copyrighted and owned by Taligent, Inc., a wholly-owned
   * subsidiary of IBM. These materials are provided under terms
   * of a License Agreement between Taligent and Sun. This technology
   * is protected by multiple US and International patents.
   *
   * This notice and attribution to Taligent may not be removed.
   * Taligent is a registered trademark of Taligent, Inc.
   *
   */
  
  package java.util;
  
  import java.net.URL;
Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.

This allows you to write programs that can:

  • be easily localized, or translated, into different languages
  • handle multiple locales at once
  • be easily modified later to support even more locales

Resource bundles belong to families whose members share a common base name, but whose names also have additional components that identify their locales. For example, the base name of a family of resource bundles might be "MyResources". The family should have a default resource bundle which simply has the same name as its family - "MyResources" - and will be used as the bundle of last resort if a specific locale is not supported. The family can then provide as many locale-specific members as needed, for example a German one named "MyResources_de".

Each resource bundle in a family contains the same items, but the items have been translated for the locale represented by that resource bundle. For example, both "MyResources" and "MyResources_de" may have a String that's used on a button for canceling operations. In "MyResources" the String may contain "Cancel" and in "MyResources_de" it may contain "Abbrechen".

If there are different resources for different countries, you can make specializations: for example, "MyResources_de_CH" contains objects for the German language (de) in Switzerland (CH). If you want to only modify some of the resources in the specialization, you can do so.

When your program needs a locale-specific object, it loads the ResourceBundle class using the getBundle method:

 ResourceBundle myResources =
      ResourceBundle.getBundle("MyResources", currentLocale);
 

Resource bundles contain key/value pairs. The keys uniquely identify a locale-specific object in the bundle. Here's an example of a ListResourceBundle that contains two key/value pairs:

 public class MyResources extends ListResourceBundle {
     protected Object[][] getContents() {
         return new Object[][] {
             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
             {"OkKey", "OK"},
             {"CancelKey", "Cancel"},
             // END OF MATERIAL TO LOCALIZE
        };
     }
 }
 
Keys are always Strings. In this example, the keys are "OkKey" and "CancelKey". In the above example, the values are also Strings--"OK" and "Cancel"--but they don't have to be. The values can be any type of object.

You retrieve an object from resource bundle using the appropriate getter method. Because "OkKey" and "CancelKey" are both strings, you would use getString to retrieve them:

 button1 = new Button(myResources.getString("OkKey"));
 button2 = new Button(myResources.getString("CancelKey"));
 
The getter methods all require the key as an argument and return the object if found. If the object is not found, the getter method throws a MissingResourceException.

Besides getString, ResourceBundle also provides a method for getting string arrays, getStringArray, as well as a generic getObject method for any other type of object. When using getObject, you'll have to cast the result to the appropriate type. For example:

 int[] myIntegers = (int[]) myResources.getObject("intList");
 

The Java Platform provides two subclasses of ResourceBundle, ListResourceBundle and PropertyResourceBundle, that provide a fairly simple way to create resources. As you saw briefly in a previous example, ListResourceBundle manages its resource as a list of key/value pairs. PropertyResourceBundle uses a properties file to manage its resources.

If ListResourceBundle or PropertyResourceBundle do not suit your needs, you can write your own ResourceBundle subclass. Your subclasses must override two methods: handleGetObject and getKeys().

ResourceBundle.Control

The ResourceBundle.Control class provides information necessary to perform the bundle loading process by the getBundle factory methods that take a ResourceBundle.Control instance. You can implement your own subclass in order to enable non-standard resource bundle formats, change the search strategy, or define caching parameters. Refer to the descriptions of the class and the getBundle factory method for details.

Cache Management

Resource bundle instances created by the getBundle factory methods are cached by default, and the factory methods return the same resource bundle instance multiple times if it has been cached. getBundle clients may clear the cache, manage the lifetime of cached resource bundle instances using time-to-live values, or specify not to cache resource bundle instances. Refer to the descriptions of the getBundle factory method, clearCache(java.lang.ClassLoader), ResourceBundle.Control.getTimeToLive(java.lang.String,java.util.Locale), and ResourceBundle.Control.needsReload(java.lang.String,java.util.Locale,java.lang.String,java.lang.ClassLoader,java.util.ResourceBundle,long) for details.

Example

The following is a very simple example of a ResourceBundle subclass, MyResources, that manages two resources (for a larger number of resources you would probably use a Map). Notice that you don't need to supply a value if a "parent-level" ResourceBundle handles the same key with the same value (as for the okKey below).
 // default (English language, United States)
 public class MyResources extends ResourceBundle {
     public Object handleGetObject(String key) {
         if (key.equals("okKey")) return "Ok";
         if (key.equals("cancelKey")) return "Cancel";
         return null;
     }

     public Enumeration<String> getKeys() {
         return Collections.enumeration(keySet());
     }

     // Overrides handleKeySet() so that the getKeys() implementation
     // can rely on the keySet() value.
     protected Set<String> handleKeySet() {
         return new HashSet<String>(Arrays.asList("okKey", "cancelKey"));
     }
 }

 // German language
 public class MyResources_de extends MyResources {
     public Object handleGetObject(String key) {
         // don't need okKey, since parent level handles it.
         if (key.equals("cancelKey")) return "Abbrechen";
         return null;
     }

     protected Set<String> handleKeySet() {
         return new HashSet<String>(Arrays.asList("cancelKey"));
     }
 }
 
You do not have to restrict yourself to using a single family of ResourceBundles. For example, you could have a set of bundles for exception messages, ExceptionResources (ExceptionResources_fr, ExceptionResources_de, ...), and one for widgets, WidgetResource (WidgetResources_fr, WidgetResources_de, ...); breaking up the resources however you like.

 
 public abstract class ResourceBundle {

    
initial size of the bundle cache
 
     private static final int INITIAL_CACHE_SIZE = 32;

    
constant indicating that no resource bundle exists
 
     private static final ResourceBundle NONEXISTENT_BUNDLE = new ResourceBundle() {
             public Enumeration<StringgetKeys() { return null; }
             protected Object handleGetObject(String key) { return null; }
             public String toString() { return "NONEXISTENT_BUNDLE"; }
         };


    
The cache is a map from cache keys (with bundle base name, locale, and class loader) to either a resource bundle or NONEXISTENT_BUNDLE wrapped by a BundleReference. The cache is a ConcurrentMap, allowing the cache to be searched concurrently by multiple threads. This will also allow the cache keys to be reclaimed along with the ClassLoaders they reference. This variable would be better named "cache", but we keep the old name for compatibility with some workarounds for bug 4212439.
 
     private static final ConcurrentMap<CacheKeyBundleReferencecacheList
         = new ConcurrentHashMap<CacheKeyBundleReference>();

    
This ConcurrentMap is used to keep multiple threads from loading the same bundle concurrently. The table entries are <CacheKey, Thread> where CacheKey is the key for the bundle that is under construction and Thread is the thread that is constructing the bundle. This list is manipulated in findBundleInCache and putBundleInCache.
 
     private static final ConcurrentMap<CacheKeyThreadunderConstruction
         = new ConcurrentHashMap<CacheKeyThread>();

    
Queue for reference objects referring to class loaders or bundles.
 
     private static final ReferenceQueue referenceQueue = new ReferenceQueue();

    
The parent bundle of this bundle. The parent bundle is searched by getObject when this bundle does not contain a particular resource.
 
     protected ResourceBundle parent = null;

    
The locale for this bundle.
 
     private Locale locale = null;

    
The base bundle name for this bundle.
 
     private String name;

    
The flag indicating this bundle has expired in the cache.
 
     private volatile boolean expired;

    
The back link to the cache key. null if this bundle isn't in the cache (yet) or has expired.
 
     private volatile CacheKey cacheKey;

    
A Set of the keys contained only in this ResourceBundle.
 
     private volatile Set<StringkeySet;

    
Sole constructor. (For invocation by subclass constructors, typically implicit.)
 
     public ResourceBundle() {
     }

    
Gets a string for the given key from this resource bundle or one of its parents. Calling this method is equivalent to calling
(String) getObject(key).

Parameters:
key the key for the desired string
Returns:
the string for the given key
Throws:
java.lang.NullPointerException if key is null
MissingResourceException if no object for the given key can be found
java.lang.ClassCastException if the object found for the given key is not a string
 
     public final String getString(String key) {
         return (StringgetObject(key);
     }

    
Gets a string array for the given key from this resource bundle or one of its parents. Calling this method is equivalent to calling
(String[]) getObject(key).

Parameters:
key the key for the desired string array
Returns:
the string array for the given key
Throws:
java.lang.NullPointerException if key is null
MissingResourceException if no object for the given key can be found
java.lang.ClassCastException if the object found for the given key is not a string array
 
     public final String[] getStringArray(String key) {
         return (String[]) getObject(key);
     }

    
Gets an object for the given key from this resource bundle or one of its parents. This method first tries to obtain the object from this resource bundle using handleGetObject. If not successful, and the parent resource bundle is not null, it calls the parent's getObject method. If still not successful, it throws a MissingResourceException.

Parameters:
key the key for the desired object
Returns:
the object for the given key
Throws:
java.lang.NullPointerException if key is null
MissingResourceException if no object for the given key can be found
 
     public final Object getObject(String key) {
         Object obj = handleGetObject(key);
         if (obj == null) {
             if ( != null) {
                 obj = .getObject(key);
             }
             if (obj == null)
                 throw new MissingResourceException("Can't find resource for bundle "
                                                    +this.getClass().getName()
                                                    +", key "+key,
                                                    this.getClass().getName(),
                                                    key);
         }
         return obj;
     }

    
Returns the locale of this resource bundle. This method can be used after a call to getBundle() to determine whether the resource bundle returned really corresponds to the requested locale or is a fallback.

Returns:
the locale of this resource bundle
 
     public Locale getLocale() {
         return ;
     }
 
     /*
      * Automatic determination of the ClassLoader to be used to load
      * resources on behalf of the client.  N.B. The client is getLoader's
      * caller's caller.
      */
     private static ClassLoader getLoader() {
         Class[] stack = getClassContext();
         /* Magic number 2 identifies our caller's caller */
         Class c = stack[2];
         ClassLoader cl = (c == null) ? null : c.getClassLoader();
         if (cl == null) {
             // When the caller's loader is the boot class loader, cl is null
             // here. In that case, ClassLoader.getSystemClassLoader() may
             // return the same class loader that the application is
             // using. We therefore use a wrapper ClassLoader to create a
             // separate scope for bundles loaded on behalf of the Java
             // runtime so that these bundles cannot be returned from the
             // cache to the application (5048280).
             cl = .;
         }
         return cl;
     }
 
     private static native Class[] getClassContext();

    
A wrapper of ClassLoader.getSystemClassLoader().
 
     private static class RBClassLoader extends ClassLoader {
         private static final RBClassLoader INSTANCE = AccessController.doPrivileged(
                     new PrivilegedAction<RBClassLoader>() {
                         public RBClassLoader run() {
                             return new RBClassLoader();
                         }
                     });
         private static final ClassLoader loader = ClassLoader.getSystemClassLoader();
 
         private RBClassLoader() {
         }
         public Class<?> loadClass(String namethrows ClassNotFoundException {
             if ( != null) {
                 return .loadClass(name);
             }
             return Class.forName(name);
         }
         public URL getResource(String name) {
             if ( != null) {
                 return .getResource(name);
             }
             return ClassLoader.getSystemResource(name);
         }
         public InputStream getResourceAsStream(String name) {
             if ( != null) {
                 return .getResourceAsStream(name);
             }
             return ClassLoader.getSystemResourceAsStream(name);
         }
     }

    
Sets the parent bundle of this bundle. The parent bundle is searched by getObject when this bundle does not contain a particular resource.

Parameters:
parent this bundle's parent bundle.
 
     protected void setParent(ResourceBundle parent) {
         assert parent != ;
         this. = parent;
     }

    
Key used for cached resource bundles. The key checks the base name, the locale, and the class loader to determine if the resource is a match to the requested one. The loader may be null, but the base name and the locale must have a non-null value.
 
     private static final class CacheKey implements Cloneable {
         // These three are the actual keys for lookup in Map.
         private String name;
         private Locale locale;
         private LoaderReference loaderRef;
 
         // bundle format which is necessary for calling
         // Control.needsReload().
         private String format;
 
         // These time values are in CacheKey so that NONEXISTENT_BUNDLE
         // doesn't need to be cloned for caching.
 
         // The time when the bundle has been loaded
         private volatile long loadTime;
 
         // The time when the bundle expires in the cache, or either
         // Control.TTL_DONT_CACHE or Control.TTL_NO_EXPIRATION_CONTROL.
         private volatile long expirationTime;
 
         // Placeholder for an error report by a Throwable
         private Throwable cause;
 
         // Hash code value cache to avoid recalculating the hash code
         // of this instance.
         private int hashCodeCache;
 
         CacheKey(String baseNameLocale localeClassLoader loader) {
             this. = baseName;
             this. = locale;
             if (loader == null) {
                 this. = null;
             } else {
                  = new LoaderReference(loaderthis);
             }
             calculateHashCode();
         }
 
         String getName() {
             return ;
         }
 
         CacheKey setName(String baseName) {
             if (!this..equals(baseName)) {
                 this. = baseName;
                 calculateHashCode();
             }
             return this;
         }
 
         Locale getLocale() {
             return ;
         }
 
         CacheKey setLocale(Locale locale) {
             if (!this..equals(locale)) {
                 this. = locale;
                 calculateHashCode();
             }
             return this;
         }
 
         ClassLoader getLoader() {
             return ( != null) ? .get() : null;
         }
 
         public boolean equals(Object other) {
             if (this == other) {
                 return true;
             }
             try {
                 final CacheKey otherEntry = (CacheKey)other;
                 //quick check to see if they are not equal
                 if ( != otherEntry.hashCodeCache) {
                     return false;
                 }
                 //are the names the same?
                 if (!.equals(otherEntry.name)) {
                     return false;
                 }
                 // are the locales the same?
                 if (!.equals(otherEntry.locale)) {
                     return false;
                 }
                 //are refs (both non-null) or (both null)?
                 if ( == null) {
                     return otherEntry.loaderRef == null;
                 }
                 ClassLoader loader = .get();
                 return (otherEntry.loaderRef != null)
                         // with a null reference we can no longer find
                         // out which class loader was referenced; so
                         // treat it as unequal
                         && (loader != null)
                         && (loader == otherEntry.loaderRef.get());
             } catch (NullPointerException e) {
             } catch (ClassCastException e) {
             }
             return false;
         }
 
         public int hashCode() {
             return ;
         }
 
         private void calculateHashCode() {
              = .hashCode() << 3;
              ^= .hashCode();
             ClassLoader loader = getLoader();
             if (loader != null) {
                  ^= loader.hashCode();
             }
         }
 
         public Object clone() {
             try {
                 CacheKey clone = (CacheKeysuper.clone();
                 if ( != null) {
                     clone.loaderRef = new LoaderReference(.get(),
                                                           clone);
                 }
                 // Clear the reference to a Throwable
                 clone.cause = null;
                 return clone;
             } catch (CloneNotSupportedException e) {
                 //this should never happen
                 throw new InternalError();
             }
         }
 
         String getFormat() {
             return ;
         }
 
         void setFormat(String format) {
             this. = format;
         }
 
         private void setCause(Throwable cause) {
             if (this. == null) {
                 this. = cause;
             } else {
                 // Override the cause if the previous one is
                 // ClassNotFoundException.
                 if (this. instanceof ClassNotFoundException) {
                     this. = cause;
                 }
             }
         }
 
         private Throwable getCause() {
             return ;
         }
 
         public String toString() {
             String l = .toString();
             if (l.length() == 0) {
                 if (.getVariant().length() != 0) {
                     l = "__" + .getVariant();
                 } else {
                     l = "\"\"";
                 }
             }
             return "CacheKey[" +  + ", lc=" + l + ", ldr=" + getLoader()
                 + "(format=" +  + ")]";
         }
     }

    
The common interface to get a CacheKey in LoaderReference and BundleReference.
 
     private static interface CacheKeyReference {
         public CacheKey getCacheKey();
     }

    
References to class loaders are weak references, so that they can be garbage collected when nobody else is using them. The ResourceBundle class has no reason to keep class loaders alive.
 
     private static final class LoaderReference extends WeakReference<ClassLoader>
                                                implements CacheKeyReference {
         private CacheKey cacheKey;
 
         LoaderReference(ClassLoader referentReferenceQueue qCacheKey key) {
             super(referentq);
              = key;
         }
 
         public CacheKey getCacheKey() {
             return ;
         }
     }

    
References to bundles are soft references so that they can be garbage collected when they have no hard references.
 
     private static final class BundleReference extends SoftReference<ResourceBundle>
                                                implements CacheKeyReference {
         private CacheKey cacheKey;
 
         BundleReference(ResourceBundle referentReferenceQueue qCacheKey key) {
             super(referentq);
              = key;
         }
 
         public CacheKey getCacheKey() {
             return ;
         }
     }

    
Gets a resource bundle using the specified base name, the default locale, and the caller's class loader. Calling this method is equivalent to calling
getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader()),
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle for a complete description of the search and instantiation strategy.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
Returns:
a resource bundle for the given base name and the default locale
Throws:
java.lang.NullPointerException if baseName is null
MissingResourceException if no resource bundle for the specified base name can be found
 
     public static final ResourceBundle getBundle(String baseName)
     {
         return getBundleImpl(baseName, Locale.getDefault(),
                              /* must determine loader here, else we break stack invariant */
                              getLoader(),
                              .);
     }

    
Returns a resource bundle using the specified base name, the default locale and the specified control. Calling this method is equivalent to calling
 getBundle(baseName, Locale.getDefault(),
           this.getClass().getClassLoader(), control),
 
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle(java.lang.String,java.util.Locale,java.lang.ClassLoader,java.util.ResourceBundle.Control) for the complete description of the resource bundle loading process with a ResourceBundle.Control.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
control the control which gives information for the resource bundle loading process
Returns:
a resource bundle for the given base name and the default locale
Throws:
java.lang.NullPointerException if baseName or control is null
MissingResourceException if no resource bundle for the specified base name can be found
java.lang.IllegalArgumentException if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.
Since:
1.6
 
     public static final ResourceBundle getBundle(String baseName,
                                                  Control control) {
         return getBundleImpl(baseName, Locale.getDefault(),
                              /* must determine loader here, else we break stack invariant */
                              getLoader(),
                              control);
     }

    
Gets a resource bundle using the specified base name and locale, and the caller's class loader. Calling this method is equivalent to calling
getBundle(baseName, locale, this.getClass().getClassLoader()),
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle for a complete description of the search and instantiation strategy.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
locale the locale for which a resource bundle is desired
Returns:
a resource bundle for the given base name and locale
Throws:
java.lang.NullPointerException if baseName or locale is null
MissingResourceException if no resource bundle for the specified base name can be found
 
     public static final ResourceBundle getBundle(String baseName,
                                                  Locale locale)
     {
         return getBundleImpl(baseNamelocale,
                              /* must determine loader here, else we break stack invariant */
                              getLoader(),
                              .);
     }

    
Returns a resource bundle using the specified base name, target locale and control, and the caller's class loader. Calling this method is equivalent to calling
 getBundle(baseName, targetLocale, this.getClass().getClassLoader(),
           control),
 
except that getClassLoader() is run with the security privileges of ResourceBundle. See getBundle(java.lang.String,java.util.Locale,java.lang.ClassLoader,java.util.ResourceBundle.Control) for the complete description of the resource bundle loading process with a ResourceBundle.Control.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
targetLocale the locale for which a resource bundle is desired
control the control which gives information for the resource bundle loading process
Returns:
a resource bundle for the given base name and a Locale in locales
Throws:
java.lang.NullPointerException if baseName, locales or control is null
MissingResourceException if no resource bundle for the specified base name in any of the locales can be found.
java.lang.IllegalArgumentException if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.
Since:
1.6
 
     public static final ResourceBundle getBundle(String baseNameLocale targetLocale,
                                                  Control control) {
         return getBundleImpl(baseNametargetLocale,
                              /* must determine loader here, else we break stack invariant */
                              getLoader(),
                              control);
     }

    
Gets a resource bundle using the specified base name, locale, and class loader.

Conceptually, getBundle uses the following strategy for locating and instantiating resource bundles:

getBundle uses the base name, the specified locale, and the default locale (obtained from Locale.getDefault) to generate a sequence of candidate bundle names. If the specified locale's language, country, and variant are all empty strings, then the base name is the only candidate bundle name. Otherwise, the following sequence is generated from the attribute values of the specified locale (language1, country1, and variant1) and of the default locale (language2, country2, and variant2):

  • baseName + "_" + language1 + "_" + country1 + "_" + variant1
  • baseName + "_" + language1 + "_" + country1
  • baseName + "_" + language1
  • baseName + "_" + language2 + "_" + country2 + "_" + variant2
  • baseName + "_" + language2 + "_" + country2
  • baseName + "_" + language2
  • baseName

Candidate bundle names where the final component is an empty string are omitted. For example, if country1 is an empty string, the second candidate bundle name is omitted.

getBundle then iterates over the candidate bundle names to find the first one for which it can instantiate an actual resource bundle. For each candidate bundle name, it attempts to create a resource bundle:

  • First, it attempts to load a class using the candidate bundle name. If such a class can be found and loaded using the specified class loader, is assignment compatible with ResourceBundle, is accessible from ResourceBundle, and can be instantiated, getBundle creates a new instance of this class and uses it as the result resource bundle.
  • Otherwise, getBundle attempts to locate a property resource file. It generates a path name from the candidate bundle name by replacing all "." characters with "/" and appending the string ".properties". It attempts to find a "resource" with this name using ClassLoader.getResource. (Note that a "resource" in the sense of getResource has nothing to do with the contents of a resource bundle, it is just a container of data, such as a file.) If it finds a "resource", it attempts to create a new PropertyResourceBundle instance from its contents. If successful, this instance becomes the result resource bundle.

If no result resource bundle has been found, a MissingResourceException is thrown.

Once a result resource bundle has been found, its parent chain is instantiated. getBundle iterates over the candidate bundle names that can be obtained by successively removing variant, country, and language (each time with the preceding "_") from the bundle name of the result resource bundle. As above, candidate bundle names where the final component is an empty string are omitted. With each of the candidate bundle names it attempts to instantiate a resource bundle, as described above. Whenever it succeeds, it calls the previously instantiated resource bundle's setParent method with the new resource bundle, unless the previously instantiated resource bundle already has a non-null parent.

getBundle caches instantiated resource bundles and may return the same resource bundle instance multiple times.

The baseName argument should be a fully qualified class name. However, for compatibility with earlier versions, Sun's Java SE Runtime Environments do not verify this, and so it is possible to access PropertyResourceBundles by specifying a path name (using "/") instead of a fully qualified class name (using ".").

Example:
The following class and property files are provided:

     MyResources.class
     MyResources.properties
     MyResources_fr.properties
     MyResources_fr_CH.class
     MyResources_fr_CH.properties
     MyResources_en.properties
     MyResources_es_ES.class
 
The contents of all files are valid (that is, public non-abstract subclasses of ResourceBundle for the ".class" files, syntactically correct ".properties" files). The default locale is Locale("en", "GB").

Calling getBundle with the shown locale argument values instantiates resource bundles from the following sources:

  • Locale("fr", "CH"): result MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class
  • Locale("fr", "FR"): result MyResources_fr.properties, parent MyResources.class
  • Locale("de", "DE"): result MyResources_en.properties, parent MyResources.class
  • Locale("en", "US"): result MyResources_en.properties, parent MyResources.class
  • Locale("es", "ES"): result MyResources_es_ES.class, parent MyResources.class

The file MyResources_fr_CH.properties is never used because it is hidden by MyResources_fr_CH.class. Likewise, MyResources.properties is also hidden by MyResources.class.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
locale the locale for which a resource bundle is desired
loader the class loader from which to load the resource bundle
Returns:
a resource bundle for the given base name and locale
Throws:
java.lang.NullPointerException if baseName, locale, or loader is null
MissingResourceException if no resource bundle for the specified base name can be found
Since:
1.2

 
     public static ResourceBundle getBundle(String baseNameLocale locale,
                                            ClassLoader loader)
     {
         if (loader == null) {
             throw new NullPointerException();
         }
         return getBundleImpl(baseNamelocaleloader.);
     }

    
Returns a resource bundle using the specified base name, target locale, class loader and control. Unlike the getBundle(java.lang.String,java.util.Locale,java.lang.ClassLoader), the given control specifies how to locate and instantiate resource bundles. Conceptually, the bundle loading process with the given control is performed in the following steps.

  1. This factory method looks up the resource bundle in the cache for the specified baseName, targetLocale and loader. If the requested resource bundle instance is found in the cache and the time-to-live periods of the instance and all of its parent instances have not expired, the instance is returned to the caller. Otherwise, this factory method proceeds with the loading process below.
  2. The control.getFormats method is called to get resource bundle formats to produce bundle or resource names. The strings "java.class" and "java.properties" designate class-based and property-based resource bundles, respectively. Other strings starting with "java." are reserved for future extensions and must not be used for application-defined formats. Other strings designate application-defined formats.
  3. The control.getCandidateLocales method is called with the target locale to get a list of candidate Locales for which resource bundles are searched.
  4. The control.newBundle method is called to instantiate a ResourceBundle for the base bundle name, a candidate locale, and a format. (Refer to the note on the cache lookup below.) This step is iterated over all combinations of the candidate locales and formats until the newBundle method returns a ResourceBundle instance or the iteration has used up all the combinations. For example, if the candidate locales are Locale("de", "DE"), Locale("de") and Locale("") and the formats are "java.class" and "java.properties", then the following is the sequence of locale-format combinations to be used to call control.newBundle.
    Locale
    format
    Locale("de", "DE")
    java.class
    Locale("de", "DE")java.properties
    Locale("de")java.class
    Locale("de")java.properties
    Locale("")
    java.class
    Locale("")java.properties
  5. If the previous step has found no resource bundle, proceed to Step 6. If a bundle has been found that is a base bundle (a bundle for Locale("")), and the candidate locale list only contained Locale(""), return the bundle to the caller. If a bundle has been found that is a base bundle, but the candidate locale list contained locales other than Locale(""), put the bundle on hold and proceed to Step 6. If a bundle has been found that is not a base bundle, proceed to Step 7.
  6. The control.getFallbackLocale method is called to get a fallback locale (alternative to the current target locale) to try further finding a resource bundle. If the method returns a non-null locale, it becomes the next target locale and the loading process starts over from Step 3. Otherwise, if a base bundle was found and put on hold in a previous Step 5, it is returned to the caller now. Otherwise, a MissingResourceException is thrown.
  7. At this point, we have found a resource bundle that's not the base bundle. If this bundle set its parent during its instantiation, it is returned to the caller. Otherwise, its parent chain is instantiated based on the list of candidate locales from which it was found. Finally, the bundle is returned to the caller.

During the resource bundle loading process above, this factory method looks up the cache before calling the ResourceBundle.Control.newBundle(java.lang.String,java.util.Locale,java.lang.String,java.lang.ClassLoader,boolean) method. If the time-to-live period of the resource bundle found in the cache has expired, the factory method calls the control.needsReload method to determine whether the resource bundle needs to be reloaded. If reloading is required, the factory method calls control.newBundle to reload the resource bundle. If control.newBundle returns null, the factory method puts a dummy resource bundle in the cache as a mark of nonexistent resource bundles in order to avoid lookup overhead for subsequent requests. Such dummy resource bundles are under the same expiration control as specified by control.

All resource bundles loaded are cached by default. Refer to control.getTimeToLive for details.

The following is an example of the bundle loading process with the default ResourceBundle.Control implementation.

Conditions:

  • Base bundle name: foo.bar.Messages
  • Requested Locale: Locale.ITALY
  • Default Locale: Locale.FRENCH
  • Available resource bundles: foo/bar/Messages_fr.properties and foo/bar/Messages.properties

First, getBundle tries loading a resource bundle in the following sequence.

  • class foo.bar.Messages_it_IT
  • file foo/bar/Messages_it_IT.properties
  • class foo.bar.Messages_it
  • file foo/bar/Messages_it.properties
  • class foo.bar.Messages
  • file foo/bar/Messages.properties

At this point, getBundle finds foo/bar/Messages.properties, which is put on hold because it's the base bundle. getBundle calls ResourceBundle.Control.getFallbackLocale(java.lang.String,java.util.Locale) which returns Locale.FRENCH. Next, getBundle tries loading a bundle in the following sequence.

  • class foo.bar.Messages_fr
  • file foo/bar/Messages_fr.properties
  • class foo.bar.Messages
  • file foo/bar/Messages.properties

getBundle finds foo/bar/Messages_fr.properties and creates a ResourceBundle instance. Then, getBundle sets up its parent chain from the list of the candiate locales. Only foo/bar/Messages.properties is found in the list and getBundle creates a ResourceBundle instance that becomes the parent of the instance for foo/bar/Messages_fr.properties.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
targetLocale the locale for which a resource bundle is desired
loader the class loader from which to load the resource bundle
control the control which gives information for the resource bundle loading process
Returns:
a resource bundle for the given base name and locale
Throws:
java.lang.NullPointerException if baseName, targetLocale, loader, or control is null
MissingResourceException if no resource bundle for the specified base name can be found
java.lang.IllegalArgumentException if the given control doesn't perform properly (e.g., control.getCandidateLocales returns null.) Note that validation of control is performed as needed.
Since:
1.6
    public static ResourceBundle getBundle(String baseNameLocale targetLocale,
                                           ClassLoader loaderControl control) {
        if (loader == null || control == null) {
            throw new NullPointerException();
        }
        return getBundleImpl(baseNametargetLocaleloadercontrol);
    }
    private static ResourceBundle getBundleImpl(String baseNameLocale locale,
                                                ClassLoader loaderControl control) {
        if (locale == null || control == null) {
            throw new NullPointerException();
        }
        // We create a CacheKey here for use by this call. The base
        // name and loader will never change during the bundle loading
        // process. We have to make sure that the locale is set before
        // using it as a cache key.
        CacheKey cacheKey = new CacheKey(baseNamelocaleloader);
        ResourceBundle bundle = null;
        // Quick lookup of the cache.
        BundleReference bundleRef = .get(cacheKey);
        if (bundleRef != null) {
            bundle = bundleRef.get();
            bundleRef = null;
        }
        // If this bundle and all of its parents are valid (not expired),
        // then return this bundle. If any of the bundles is expired, we
        // don't call control.needsReload here but instead drop into the
        // complete loading process below.
        if (isValidBundle(bundle) && hasValidParentChain(bundle)) {
            return bundle;
        }
        // No valid bundle was found in the cache, so we need to load the
        // resource bundle and its parents.
        boolean isKnownControl = (control == .) ||
                                   (control instanceof SingleFormatControl);
        List<Stringformats = control.getFormats(baseName);
        if (!isKnownControl && !checkList(formats)) {
            throw new IllegalArgumentException("Invalid Control: getFormats");
        }
        ResourceBundle baseBundle = null;
        for (Locale targetLocale = locale;
             targetLocale != null;
             targetLocale = control.getFallbackLocale(baseNametargetLocale)) {
            List<LocalecandidateLocales = control.getCandidateLocales(baseNametargetLocale);
            if (!isKnownControl && !checkList(candidateLocales)) {
                throw new IllegalArgumentException("Invalid Control: getCandidateLocales");
            }
            bundle = findBundle(cacheKeycandidateLocalesformats, 0, controlbaseBundle);
            // If the loaded bundle is the base bundle and exactly for the
            // requested locale or the only candidate locale, then take the
            // bundle as the resulting one. If the loaded bundle is the base
            // bundle, it's put on hold until we finish processing all
            // fallback locales.
            if (isValidBundle(bundle)) {
                boolean isBaseBundle = ..equals(bundle.locale);
                if (!isBaseBundle || bundle.locale.equals(locale)
                    || (candidateLocales.size() == 1
                        && bundle.locale.equals(candidateLocales.get(0)))) {
                    break;
                }
                // If the base bundle has been loaded, keep the reference in
                // baseBundle so that we can avoid any redundant loading in case
                // the control specify not to cache bundles.
                if (isBaseBundle && baseBundle == null) {
                    baseBundle = bundle;
                }
            }
        }
        if (bundle == null) {
            if (baseBundle == null) {
                throwMissingResourceException(baseNamelocalecacheKey.getCause());
            }
            bundle = baseBundle;
        }
        return bundle;
    }

    
Checks if the given List is not null, not empty, not having null in its elements.
    private static final boolean checkList(List a) {
        boolean valid = (a != null && a.size() != 0);
        if (valid) {
            int size = a.size();
            for (int i = 0; valid && i < sizei++) {
                valid = (a.get(i) != null);
            }
        }
        return valid;
    }
    private static final ResourceBundle findBundle(CacheKey cacheKey,
                                                   List<LocalecandidateLocales,
                                                   List<Stringformats,
                                                   int index,
                                                   Control control,
                                                   ResourceBundle baseBundle) {
        Locale targetLocale = candidateLocales.get(index);
        ResourceBundle parent = null;
        if (index != candidateLocales.size() - 1) {
            parent = findBundle(cacheKeycandidateLocalesformatsindex + 1,
                                controlbaseBundle);
        } else if (baseBundle != null && ..equals(targetLocale)) {
            return baseBundle;
        }
        // Before we do the real loading work, see whether we need to
        // do some housekeeping: If references to class loaders or
        // resource bundles have been nulled out, remove all related
        // information from the cache.
        Object ref;
        while ((ref = .poll()) != null) {
            .remove(((CacheKeyReference)ref).getCacheKey());
        }
        // flag indicating the resource bundle has expired in the cache
        boolean expiredBundle = false;
        // First, look up the cache to see if it's in the cache, without
        // declaring beginLoading.
        cacheKey.setLocale(targetLocale);
        ResourceBundle bundle = findBundleInCache(cacheKeycontrol);
        if (isValidBundle(bundle)) {
            expiredBundle = bundle.expired;
            if (!expiredBundle) {
                // If its parent is the one asked for by the candidate
                // locales (the runtime lookup path), we can take the cached
                // one. (If it's not identical, then we'd have to check the
                // parent's parents to be consistent with what's been
                // requested.)
                if (bundle.parent == parent) {
                    return bundle;
                }
                // Otherwise, remove the cached one since we can't keep
                // the same bundles having different parents.
                BundleReference bundleRef = .get(cacheKey);
                if (bundleRef != null && bundleRef.get() == bundle) {
                    .remove(cacheKeybundleRef);
                }
            }
        }
        if (bundle != ) {
            CacheKey constKey = (CacheKeycacheKey.clone();
            try {
                // Try declaring loading. If beginLoading() returns true,
                // then we can proceed. Otherwise, we need to take a look
                // at the cache again to see if someone else has loaded
                // the bundle and put it in the cache while we've been
                // waiting for other loading work to complete.
                while (!beginLoading(constKey)) {
                    bundle = findBundleInCache(cacheKeycontrol);
                    if (bundle == null) {
                        continue;
                    }
                    if (bundle == ) {
                        // If the bundle is NONEXISTENT_BUNDLE, the bundle doesn't exist.
                        return parent;
                    }
                    expiredBundle = bundle.expired;
                    if (!expiredBundle) {
                        if (bundle.parent == parent) {
                            return bundle;
                        }
                        BundleReference bundleRef = .get(cacheKey);
                        if (bundleRef != null && bundleRef.get() == bundle) {
                            .remove(cacheKeybundleRef);
                        }
                    }
                }
                try {
                    bundle = loadBundle(cacheKeyformatscontrolexpiredBundle);
                    if (bundle != null) {
                        if (bundle.parent == null) {
                            bundle.setParent(parent);
                        }
                        bundle.locale = targetLocale;
                        bundle = putBundleInCache(cacheKeybundlecontrol);
                        return bundle;
                    }
                    // Put NONEXISTENT_BUNDLE in the cache as a mark that there's no bundle
                    // instance for the locale.
                    putBundleInCache(cacheKeycontrol);
                } finally {
                    endLoading(constKey);
                }
            } finally {
                if (constKey.getCause() instanceof InterruptedException) {
                    Thread.currentThread().interrupt();
                }
            }
        }
        assert .get(cacheKey) != Thread.currentThread();
        return parent;
    }
    private static final ResourceBundle loadBundle(CacheKey cacheKey,
                                                   List<Stringformats,
                                                   Control control,
                                                   boolean reload) {
        assert .get(cacheKey) == Thread.currentThread();
        // Here we actually load the bundle in the order of formats
        // specified by the getFormats() value.
        Locale targetLocale = cacheKey.getLocale();
        ResourceBundle bundle = null;
        int size = formats.size();
        for (int i = 0; i < sizei++) {
            String format = formats.get(i);
            try {
                bundle = control.newBundle(cacheKey.getName(), targetLocaleformat,
                                           cacheKey.getLoader(), reload);
            } catch (LinkageError error) {
                // We need to handle the LinkageError case due to
                // inconsistent case-sensitivity in ClassLoader.
                // See 6572242 for details.
                cacheKey.setCause(error);
            } catch (Exception cause) {
                cacheKey.setCause(cause);
            }
            if (bundle != null) {
                // Set the format in the cache key so that it can be
                // used when calling needsReload later.
                cacheKey.setFormat(format);
                bundle.name = cacheKey.getName();
                bundle.locale = targetLocale;
                // Bundle provider might reuse instances. So we should make
                // sure to clear the expired flag here.
                bundle.expired = false;
                break;
            }
        }
        assert .get(cacheKey) == Thread.currentThread();
        return bundle;
    }
    private static final boolean isValidBundle(ResourceBundle bundle) {
        return bundle != null && bundle != ;
    }

    
Determines whether any of resource bundles in the parent chain, including the leaf, have expired.
    private static final boolean hasValidParentChain(ResourceBundle bundle) {
        long now = System.currentTimeMillis();
        while (bundle != null) {
            if (bundle.expired) {
                return false;
            }
            CacheKey key = bundle.cacheKey;
            if (key != null) {
                long expirationTime = key.expirationTime;
                if (expirationTime >= 0 && expirationTime <= now) {
                    return false;
                }
            }
            bundle = bundle.parent;
        }
        return true;
    }

    
Declares the beginning of actual resource bundle loading. This method returns true if the declaration is successful and the current thread has been put in underConstruction. If someone else has already begun loading, this method waits until that loading work is complete and returns false.
    private static final boolean beginLoading(CacheKey constKey) {
        Thread me = Thread.currentThread();
        Thread worker;
        // We need to declare by putting the current Thread (me) to
        // underConstruction that we are working on loading the specified
        // resource bundle. If we are already working the loading, it means
        // that the resource loading requires a recursive call. In that case,
        // we have to proceed. (4300693)
        if (((worker = .putIfAbsent(constKeyme)) == null)
            || worker == me) {
            return true;
        }
        // If someone else is working on the loading, wait until
        // the Thread finishes the bundle loading.
        synchronized (worker) {
            while (.get(constKey) == worker) {
                try {
                    worker.wait();
                } catch (InterruptedException e) {
                    // record the interruption
                    constKey.setCause(e);
                }
            }
        }
        return false;
    }

    
Declares the end of the bundle loading. This method calls notifyAll for those who are waiting for this completion.
    private static final void endLoading(CacheKey constKey) {
        // Remove this Thread from the underConstruction map and wake up
        // those who have been waiting for me to complete this bundle
        // loading.
        Thread me = Thread.currentThread();
        assert (.get(constKey) == me);
        .remove(constKey);
        synchronized (me) {
            me.notifyAll();
        }
    }

    
Throw a MissingResourceException with proper message
    private static final void throwMissingResourceException(String baseName,
                                                            Locale locale,
                                                            Throwable cause) {
        // If the cause is a MissingResourceException, avoid creating
        // a long chain. (6355009)
        if (cause instanceof MissingResourceException) {
            cause = null;
        }
        throw new MissingResourceException("Can't find bundle for base name "
                                           + baseName + ", locale " + locale,
                                           baseName + "_" + locale// className
                                           "",                      // key
                                           cause);
    }

    
Finds a bundle in the cache. Any expired bundles are marked as `expired' and removed from the cache upon return.

Parameters:
cacheKey the key to look up the cache
control the Control to be used for the expiration control
Returns:
the cached bundle, or null if the bundle is not found in the cache or its parent has expired. bundle.expire is true upon return if the bundle in the cache has expired.
    private static final ResourceBundle findBundleInCache(CacheKey cacheKey,
                                                          Control control) {
        BundleReference bundleRef = .get(cacheKey);
        if (bundleRef == null) {
            return null;
        }
        ResourceBundle bundle = bundleRef.get();
        if (bundle == null) {
            return null;
        }
        ResourceBundle p = bundle.parent;
        assert p != ;
        // If the parent has expired, then this one must also expire. We
        // check only the immediate parent because the actual loading is
        // done from the root (base) to leaf (child) and the purpose of
        // checking is to propagate expiration towards the leaf. For
        // example, if the requested locale is ja_JP_JP and there are
        // bundles for all of the candidates in the cache, we have a list,
        //
        // base <- ja <- ja_JP <- ja_JP_JP
        //
        // If ja has expired, then it will reload ja and the list becomes a
        // tree.
        //
        // base <- ja (new)
        //  "   <- ja (expired) <- ja_JP <- ja_JP_JP
        //
        // When looking up ja_JP in the cache, it finds ja_JP in the cache
        // which references to the expired ja. Then, ja_JP is marked as
        // expired and removed from the cache. This will be propagated to
        // ja_JP_JP.
        //
        // Now, it's possible, for example, that while loading new ja_JP,
        // someone else has started loading the same bundle and finds the
        // base bundle has expired. Then, what we get from the first
        // getBundle call includes the expired base bundle. However, if
        // someone else didn't start its loading, we wouldn't know if the
        // base bundle has expired at the end of the loading process. The
        // expiration control doesn't guarantee that the returned bundle and
        // its parents haven't expired.
        //
        // We could check the entire parent chain to see if there's any in
        // the chain that has expired. But this process may never end. An
        // extreme case would be that getTimeToLive returns 0 and
        // needsReload always returns true.
        if (p != null && p.expired) {
            assert bundle != ;
            bundle.expired = true;
            bundle.cacheKey = null;
            .remove(cacheKeybundleRef);
            bundle = null;
        } else {
            CacheKey key = bundleRef.getCacheKey();
            long expirationTime = key.expirationTime;
            if (!bundle.expired && expirationTime >= 0 &&
                expirationTime <= System.currentTimeMillis()) {
                // its TTL period has expired.
                if (bundle != ) {
                    // Synchronize here to call needsReload to avoid
                    // redundant concurrent calls for the same bundle.
                    synchronized (bundle) {
                        expirationTime = key.expirationTime;
                        if (!bundle.expired && expirationTime >= 0 &&
                            expirationTime <= System.currentTimeMillis()) {
                            try {
                                bundle.expired = control.needsReload(key.getName(),
                                                                     key.getLocale(),
                                                                     key.getFormat(),
                                                                     key.getLoader(),
                                                                     bundle,
                                                                     key.loadTime);
                            } catch (Exception e) {
                                cacheKey.setCause(e);
                            }
                            if (bundle.expired) {
                                // If the bundle needs to be reloaded, then
                                // remove the bundle from the cache, but
                                // return the bundle with the expired flag
                                // on.
                                bundle.cacheKey = null;
                                .remove(cacheKeybundleRef);
                            } else {
                                // Update the expiration control info. and reuse
                                // the same bundle instance
                                setExpirationTime(keycontrol);
                            }
                        }
                    }
                } else {
                    // We just remove NONEXISTENT_BUNDLE from the cache.
                    .remove(cacheKeybundleRef);
                    bundle = null;
                }
            }
        }
        return bundle;
    }

    
Put a new bundle in the cache.

Parameters:
cacheKey the key for the resource bundle
bundle the resource bundle to be put in the cache
Returns:
the ResourceBundle for the cacheKey; if someone has put the bundle before this call, the one found in the cache is returned.
    private static final ResourceBundle putBundleInCache(CacheKey cacheKey,
                                                         ResourceBundle bundle,
                                                         Control control) {
        setExpirationTime(cacheKeycontrol);
        if (cacheKey.expirationTime != .) {
            CacheKey key = (CacheKeycacheKey.clone();
            BundleReference bundleRef = new BundleReference(bundlekey);
            bundle.cacheKey = key;
            // Put the bundle in the cache if it's not been in the cache.
            BundleReference result = .putIfAbsent(keybundleRef);
            // If someone else has put the same bundle in the cache before
            // us and it has not expired, we should use the one in the cache.
            if (result != null) {
                ResourceBundle rb = result.get();
                if (rb != null && !rb.expired) {
                    // Clear the back link to the cache key
                    bundle.cacheKey = null;
                    bundle = rb;
                    // Clear the reference in the BundleReference so that
                    // it won't be enqueued.
                    bundleRef.clear();
                } else {
                    // Replace the invalid (garbage collected or expired)
                    // instance with the valid one.
                    .put(keybundleRef);
                }
            }
        }
        return bundle;
    }
    private static final void setExpirationTime(CacheKey cacheKeyControl control) {
        long ttl = control.getTimeToLive(cacheKey.getName(),
                                         cacheKey.getLocale());
        if (ttl >= 0) {
            // If any expiration time is specified, set the time to be
            // expired in the cache.
            long now = System.currentTimeMillis();
            cacheKey.loadTime = now;
            cacheKey.expirationTime = now + ttl;
        } else if (ttl >= .) {
            cacheKey.expirationTime = ttl;
        } else {
            throw new IllegalArgumentException("Invalid Control: TTL=" + ttl);
        }
    }

    
Removes all resource bundles from the cache that have been loaded using the caller's class loader.

    public static final void clearCache() {
        clearCache(getLoader());
    }

    
Removes all resource bundles from the cache that have been loaded using the given class loader.

Parameters:
loader the class loader
Throws:
java.lang.NullPointerException if loader is null
Since:
1.6
See also:
ResourceBundle.Control.getTimeToLive(java.lang.String,java.util.Locale)
    public static final void clearCache(ClassLoader loader) {
        if (loader == null) {
            throw new NullPointerException();
        }
        Set<CacheKeyset = .keySet();
        for (CacheKey key : set) {
            if (key.getLoader() == loader) {
                set.remove(key);
            }
        }
    }

    
Gets an object for the given key from this resource bundle. Returns null if this resource bundle does not contain an object for the given key.

Parameters:
key the key for the desired object
Returns:
the object for the given key, or null
Throws:
java.lang.NullPointerException if key is null
    protected abstract Object handleGetObject(String key);

    
Returns an enumeration of the keys.

Returns:
an Enumeration of the keys contained in this ResourceBundle and its parent bundles.
    public abstract Enumeration<StringgetKeys();

    
Determines whether the given key is contained in this ResourceBundle or its parent bundles.

Parameters:
key the resource key
Returns:
true if the given key is contained in this ResourceBundle or its parent bundles; false otherwise.
Throws:
java.lang.NullPointerException if key is null
Since:
1.6
    public boolean containsKey(String key) {
        if (key == null) {
            throw new NullPointerException();
        }
        for (ResourceBundle rb = thisrb != nullrb = rb.parent) {
            if (rb.handleKeySet().contains(key)) {
                return true;
            }
        }
        return false;
    }

    
Returns a Set of all keys contained in this ResourceBundle and its parent bundles.

Returns:
a Set of all keys contained in this ResourceBundle and its parent bundles.
Since:
1.6
    public Set<StringkeySet() {
        Set<Stringkeys = new HashSet<String>();
        for (ResourceBundle rb = thisrb != nullrb = rb.parent) {
            keys.addAll(rb.handleKeySet());
        }
        return keys;
    }

    
Returns a Set of the keys contained only in this ResourceBundle.

The default implementation returns a Set of the keys returned by the getKeys method except for the ones for which the handleGetObject method returns null. Once the Set has been created, the value is kept in this ResourceBundle in order to avoid producing the same Set in the next calls. Override this method in subclass implementations for faster handling.

Returns:
a Set of the keys contained only in this ResourceBundle
Since:
1.6
    protected Set<StringhandleKeySet() {
        if ( == null) {
            synchronized (this) {
                if ( == null) {
                    Set<Stringkeys = new HashSet<String>();
                    Enumeration<StringenumKeys = getKeys();
                    while (enumKeys.hasMoreElements()) {
                        String key = enumKeys.nextElement();
                        if (handleGetObject(key) != null) {
                            keys.add(key);
                        }
                    }
                     = keys;
                }
            }
        }
        return ;
    }



    
ResourceBundle.Control defines a set of callback methods that are invoked by the ResourceBundle.getBundle factory methods during the bundle loading process. In other words, a ResourceBundle.Control collaborates with the factory methods for loading resource bundles. The default implementation of the callback methods provides the information necessary for the factory methods to perform the default behavior.

In addition to the callback methods, the toBundleName(java.lang.String,java.util.Locale) and toResourceName(java.lang.String,java.lang.String) methods are defined primarily for convenience in implementing the callback methods. However, the toBundleName method could be overridden to provide different conventions in the organization and packaging of localized resources. The toResourceName method is final to avoid use of wrong resource and class name separators.

Two factory methods, getControl(java.util.List) and getNoFallbackControl(java.util.List), provide ResourceBundle.Control instances that implement common variations of the default bundle loading process.

The formats returned by the getFormats method and candidate locales returned by the getCandidateLocales(java.lang.String,java.util.Locale) method must be consistent in all ResourceBundle.getBundle invocations for the same base bundle. Otherwise, the ResourceBundle.getBundle methods may return unintended bundles. For example, if only "java.class" is returned by the getFormats method for the first call to ResourceBundle.getBundle and only "java.properties" for the second call, then the second call will return the class-based one that has been cached during the first call.

A ResourceBundle.Control instance must be thread-safe if it's simultaneously used by multiple threads. ResourceBundle.getBundle does not synchronize to call the ResourceBundle.Control methods. The default implementations of the methods are thread-safe.

Applications can specify ResourceBundle.Control instances returned by the getControl factory methods or created from a subclass of ResourceBundle.Control to customize the bundle loading process. The following are examples of changing the default bundle loading process.

Example 1

The following code lets ResourceBundle.getBundle look up only properties-based resources.

 import java.util.*;
 import static java.util.ResourceBundle.Control.*;
 ...
 ResourceBundle bundle =
   ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"),
                            ResourceBundle.Control.getControl(FORMAT_PROPERTIES));
 
Given the resource bundles in the example in the ResourceBundle.getBundle description, this ResourceBundle.getBundle call loads MyResources_fr_CH.properties whose parent is MyResources_fr.properties whose parent is MyResources.properties. (MyResources_fr_CH.properties is not hidden, but MyResources_fr_CH.class is.)

Example 2

The following is an example of loading XML-based bundles using Properties.loadFromXML.

 ResourceBundle rb = ResourceBundle.getBundle("Messages",
     new ResourceBundle.Control() {
         public List<String> getFormats(String baseName) {
             if (baseName == null)
                 throw new NullPointerException();
             return Arrays.asList("xml");
         }
         public ResourceBundle newBundle(String baseName,
                                         Locale locale,
                                         String format,
                                         ClassLoader loader,
                                         boolean reload)
                          throws IllegalAccessException,
                                 InstantiationException,
                                 IOException {
             if (baseName == null || locale == null
                   || format == null || loader == null)
                 throw new NullPointerException();
             ResourceBundle bundle = null;
             if (format.equals("xml")) {
                 String bundleName = toBundleName(baseName, locale);
                 String resourceName = toResourceName(bundleName, format);
                 InputStream stream = null;
                 if (reload) {
                     URL url = loader.getResource(resourceName);
                     if (url != null) {
                         URLConnection connection = url.openConnection();
                         if (connection != null) {
                             // Disable caches to get fresh data for
                             // reloading.
                             connection.setUseCaches(false);
                             stream = connection.getInputStream();
                         }
                     }
                 } else {
                     stream = loader.getResourceAsStream(resourceName);
                 }
                 if (stream != null) {
                     BufferedInputStream bis = new BufferedInputStream(stream);
                     bundle = new XMLResourceBundle(bis);
                     bis.close();
                 }
             }
             return bundle;
         }
     });

 ...

 private static class XMLResourceBundle extends ResourceBundle {
     private Properties props;
     XMLResourceBundle(InputStream stream) throws IOException {
         props = new Properties();
         props.loadFromXML(stream);
     }
     protected Object handleGetObject(String key) {
         return props.getProperty(key);
     }
     public Enumeration<String> getKeys() {
         ...
     }
 }
 

Since:
1.6
    public static class Control {
        
The default format List, which contains the strings "java.class" and "java.properties", in this order. This List is Collections.unmodifiableList(java.util.List).

        public static final List<StringFORMAT_DEFAULT
            = Collections.unmodifiableList(Arrays.asList("java.class",
                                                         "java.properties"));

        
The class-only format List containing "java.class". This List is Collections.unmodifiableList(java.util.List).

        public static final List<StringFORMAT_CLASS
            = Collections.unmodifiableList(Arrays.asList("java.class"));

        
The properties-only format List containing "java.properties". This List is unmodifiable.

        public static final List<StringFORMAT_PROPERTIES
            = Collections.unmodifiableList(Arrays.asList("java.properties"));

        
The time-to-live constant for not caching loaded resource bundle instances.

        public static final long TTL_DONT_CACHE = -1;

        
The time-to-live constant for disabling the expiration control for loaded resource bundle instances in the cache.

        public static final long TTL_NO_EXPIRATION_CONTROL = -2;
        private static final Control INSTANCE = new Control();

        
Sole constructor. (For invocation by subclass constructors, typically implicit.)
        protected Control() {
        }

        
Returns a ResourceBundle.Control in which the getFormats(java.lang.String) method returns the specified formats. The formats must be equal to one of FORMAT_PROPERTIES, FORMAT_CLASS or FORMAT_DEFAULT. ResourceBundle.Control instances returned by this method are singletons and thread-safe.

Specifying FORMAT_DEFAULT is equivalent to instantiating the ResourceBundle.Control class, except that this method returns a singleton.

Parameters:
formats the formats to be returned by the ResourceBundle.Control.getFormats method
Returns:
a ResourceBundle.Control supporting the specified formats
Throws:
java.lang.NullPointerException if formats is null
java.lang.IllegalArgumentException if formats is unknown
        public static final Control getControl(List<Stringformats) {
            if (formats.equals(.)) {
                return .;
            }
            if (formats.equals(.)) {
                return .;
            }
            if (formats.equals(.)) {
                return .;
            }
            throw new IllegalArgumentException();
        }

        
Returns a ResourceBundle.Control in which the getFormats(java.lang.String) method returns the specified formats and the getFallbackLocale(java.lang.String,java.util.Locale) method returns null. The formats must be equal to one of FORMAT_PROPERTIES, FORMAT_CLASS or FORMAT_DEFAULT. ResourceBundle.Control instances returned by this method are singletons and thread-safe.

Parameters:
formats the formats to be returned by the ResourceBundle.Control.getFormats method
Returns:
a ResourceBundle.Control supporting the specified formats with no fallback Locale support
Throws:
java.lang.NullPointerException if formats is null
java.lang.IllegalArgumentException if formats is unknown
        public static final Control getNoFallbackControl(List<Stringformats) {
            if (formats.equals(.)) {
                return .;
            }
            if (formats.equals(.)) {
                return .;
            }
            if (formats.equals(.)) {
                return .;
            }
            throw new IllegalArgumentException();
        }

        
Returns a List of Strings containing formats to be used to load resource bundles for the given baseName. The ResourceBundle.getBundle factory method tries to load resource bundles with formats in the order specified by the list. The list returned by this method must have at least one String. The predefined formats are "java.class" for class-based resource bundles and "java.properties" for PropertyResourceBundle ones. Strings starting with "java." are reserved for future extensions and must not be used by application-defined formats.

It is not a requirement to return an immutable (unmodifiable) List. However, the returned List must not be mutated after it has been returned by getFormats.

The default implementation returns FORMAT_DEFAULT so that the ResourceBundle.getBundle factory method looks up first class-based resource bundles, then properties-based ones.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
Returns:
a List of Strings containing formats for loading resource bundles.
Throws:
java.lang.NullPointerException if baseName is null
See also:
FORMAT_DEFAULT
FORMAT_CLASS
FORMAT_PROPERTIES
        public List<StringgetFormats(String baseName) {
            if (baseName == null) {
                throw new NullPointerException();
            }
            return ;
        }

        
Returns a List of Locales as candidate locales for baseName and locale. This method is called by the ResourceBundle.getBundle factory method each time the factory method tries finding a resource bundle for a target Locale.

The sequence of the candidate locales also corresponds to the runtime resource lookup path (also known as the parent chain), if the corresponding resource bundles for the candidate locales exist and their parents are not defined by loaded resource bundles themselves. The last element of the list must be a root locale if it is desired to have the base bundle as the terminal of the parent chain.

If the given locale is equal to Locale.ROOT (the root locale), a List containing only the root Locale must be returned. In this case, the ResourceBundle.getBundle factory method loads only the base bundle as the resulting resource bundle.

It is not a requirement to return an immutable (unmodifiable) List. However, the returned List must not be mutated after it has been returned by getCandidateLocales.

The default implementation returns a List containing Locales in the following sequence:

     Locale(language, country, variant)
     Locale(language, country)
     Locale(language)
     Locale.ROOT
 
where language, country and variant are the language, country and variant values of the given locale, respectively. Locales where the final component values are empty strings are omitted.

The default implementation uses an ArrayList that overriding implementations may modify before returning it to the caller. However, a subclass must not modify it after it has been returned by getCandidateLocales.

For example, if the given baseName is "Messages" and the given locale is Locale("ja", "", "XX"), then a List of Locales:

     Locale("ja", "", "XX")
     Locale("ja")
     Locale.ROOT
 
is returned. And if the resource bundles for the "ja" and "" Locales are found, then the runtime resource lookup path (parent chain) is:
     Messages_ja -> Messages
 

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
locale the locale for which a resource bundle is desired
Returns:
a List of candidate Locales for the given locale
Throws:
java.lang.NullPointerException if baseName or locale is null
        public List<LocalegetCandidateLocales(String baseNameLocale locale) {
            if (baseName == null) {
                throw new NullPointerException();
            }
            String language = locale.getLanguage();
            String country = locale.getCountry();
            String variant = locale.getVariant();
            List<Localelocales = new ArrayList<Locale>(4);
            if (variant.length() > 0) {
                locales.add(locale);
            }
            if (country.length() > 0) {
                locales.add((locales.size() == 0) ?
                            locale : Locale.getInstance(languagecountry""));
            }
            if (language.length() > 0) {
                locales.add((locales.size() == 0) ?
                            locale : Locale.getInstance(language""""));
            }
            locales.add(.);
            return locales;
        }

        
Returns a Locale to be used as a fallback locale for further resource bundle searches by the ResourceBundle.getBundle factory method. This method is called from the factory method every time when no resulting resource bundle has been found for baseName and locale, where locale is either the parameter for ResourceBundle.getBundle or the previous fallback locale returned by this method.

The method returns null if no further fallback search is desired.

The default implementation returns the Locale.getDefault() if the given locale isn't the default one. Otherwise, null is returned.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name for which ResourceBundle.getBundle has been unable to find any resource bundles (except for the base bundle)
locale the Locale for which ResourceBundle.getBundle has been unable to find any resource bundles (except for the base bundle)
Returns:
a Locale for the fallback search, or null if no further fallback search is desired.
Throws:
java.lang.NullPointerException if baseName or locale is null
        public Locale getFallbackLocale(String baseNameLocale locale) {
            if (baseName == null) {
                throw new NullPointerException();
            }
            Locale defaultLocale = Locale.getDefault();
            return locale.equals(defaultLocale) ? null : defaultLocale;
        }

        
Instantiates a resource bundle for the given bundle name of the given format and locale, using the given class loader if necessary. This method returns null if there is no resource bundle available for the given parameters. If a resource bundle can't be instantiated due to an unexpected error, the error must be reported by throwing an Error or Exception rather than simply returning null.

If the reload flag is true, it indicates that this method is being called because the previously loaded resource bundle has expired.

The default implementation instantiates a ResourceBundle as follows.

Parameters:
baseName the base bundle name of the resource bundle, a fully qualified class name
locale the locale for which the resource bundle should be instantiated
format the resource bundle format to be loaded
loader the ClassLoader to use to load the bundle
reload the flag to indicate bundle reloading; true if reloading an expired resource bundle, false otherwise
Returns:
the resource bundle instance, or null if none could be found.
Throws:
java.lang.NullPointerException if bundleName, locale, format, or loader is null, or if null is returned by toBundleName
java.lang.IllegalArgumentException if format is unknown, or if the resource found for the given parameters contains malformed data.
java.lang.ClassCastException if the loaded class cannot be cast to ResourceBundle
java.lang.IllegalAccessException if the class or its nullary constructor is not accessible.
java.lang.InstantiationException if the instantiation of a class fails for some other reason.
java.lang.ExceptionInInitializerError if the initialization provoked by this method fails.
java.lang.SecurityException If a security manager is present and creation of new instances is denied. See java.lang.Class.newInstance() for details.
java.io.IOException if an error occurred when reading resources using any I/O operations
        public ResourceBundle newBundle(String baseNameLocale localeString format,
                                        ClassLoader loaderboolean reload)
                    throws IllegalAccessExceptionInstantiationExceptionIOException {
            String bundleName = toBundleName(baseNamelocale);
            ResourceBundle bundle = null;
            if (format.equals("java.class")) {
                try {
                    Class<? extends ResourceBundlebundleClass
                        = (Class<? extends ResourceBundle>)loader.loadClass(bundleName);
                    // If the class isn't a ResourceBundle subclass, throw a
                    // ClassCastException.
                    if (ResourceBundle.class.isAssignableFrom(bundleClass)) {
                        bundle = bundleClass.newInstance();
                    } else {
                        throw new ClassCastException(bundleClass.getName()
                                     + " cannot be cast to ResourceBundle");
                    }
                } catch (ClassNotFoundException e) {
                }
            } else if (format.equals("java.properties")) {
                final String resourceName = toResourceName(bundleName"properties");
                final ClassLoader classLoader = loader;
                final boolean reloadFlag = reload;
                InputStream stream = null;
                try {
                    stream = AccessController.doPrivileged(
                        new PrivilegedExceptionAction<InputStream>() {
                            public InputStream run() throws IOException {
                                InputStream is = null;
                                if (reloadFlag) {
                                    URL url = classLoader.getResource(resourceName);
                                    if (url != null) {
                                        URLConnection connection = url.openConnection();
                                        if (connection != null) {
                                            // Disable caches to get fresh data for
                                            // reloading.
                                            connection.setUseCaches(false);
                                            is = connection.getInputStream();
                                        }
                                    }
                                } else {
                                    is = classLoader.getResourceAsStream(resourceName);
                                }
                                return is;
                            }
                        });
                } catch (PrivilegedActionException e) {
                    throw (IOExceptione.getException();
                }
                if (stream != null) {
                    try {
                        bundle = new PropertyResourceBundle(stream);
                    } finally {
                        stream.close();
                    }
                }
            } else {
                throw new IllegalArgumentException("unknown format: " + format);
            }
            return bundle;
        }

        
Returns the time-to-live (TTL) value for resource bundles that are loaded under this ResourceBundle.Control. Positive time-to-live values specify the number of milliseconds a bundle can remain in the cache without being validated against the source data from which it was constructed. The value 0 indicates that a bundle must be validated each time it is retrieved from the cache. TTL_DONT_CACHE specifies that loaded resource bundles are not put in the cache. TTL_NO_EXPIRATION_CONTROL specifies that loaded resource bundles are put in the cache with no expiration control.

The expiration affects only the bundle loading process by the ResourceBundle.getBundle factory method. That is, if the factory method finds a resource bundle in the cache that has expired, the factory method calls the needsReload(java.lang.String,java.util.Locale,java.lang.String,java.lang.ClassLoader,java.util.ResourceBundle,long) method to determine whether the resource bundle needs to be reloaded. If needsReload returns true, the cached resource bundle instance is removed from the cache. Otherwise, the instance stays in the cache, updated with the new TTL value returned by this method.

All cached resource bundles are subject to removal from the cache due to memory constraints of the runtime environment. Returning a large positive value doesn't mean to lock loaded resource bundles in the cache.

The default implementation returns TTL_NO_EXPIRATION_CONTROL.

Parameters:
baseName the base name of the resource bundle for which the expiration value is specified.
locale the locale of the resource bundle for which the expiration value is specified.
Returns:
the time (0 or a positive millisecond offset from the cached time) to get loaded bundles expired in the cache, TTL_NO_EXPIRATION_CONTROL to disable the expiration control, or TTL_DONT_CACHE to disable caching.
Throws:
java.lang.NullPointerException if baseName or locale is null
        public long getTimeToLive(String baseNameLocale locale) {
            if (baseName == null || locale == null) {
                throw new NullPointerException();
            }
            return ;
        }

        
Determines if the expired bundle in the cache needs to be reloaded based on the loading time given by loadTime or some other criteria. The method returns true if reloading is required; false otherwise. loadTime is a millisecond offset since the Calendar Epoch. The calling ResourceBundle.getBundle factory method calls this method on the ResourceBundle.Control instance used for its current invocation, not on the instance used in the invocation that originally loaded the resource bundle.

The default implementation compares loadTime and the last modified time of the source data of the resource bundle. If it's determined that the source data has been modified since loadTime, true is returned. Otherwise, false is returned. This implementation assumes that the given format is the same string as its file suffix if it's not one of the default formats, "java.class" or "java.properties".

Parameters:
baseName the base bundle name of the resource bundle, a fully qualified class name
locale the locale for which the resource bundle should be instantiated
format the resource bundle format to be loaded
loader the ClassLoader to use to load the bundle
bundle the resource bundle instance that has been expired in the cache
loadTime the time when bundle was loaded and put in the cache
Returns:
true if the expired bundle needs to be reloaded; false otherwise.
Throws:
java.lang.NullPointerException if baseName, locale, format, loader, or bundle is null
        public boolean needsReload(String baseNameLocale locale,
                                   String formatClassLoader loader,
                                   ResourceBundle bundlelong loadTime) {
            if (bundle == null) {
                throw new NullPointerException();
            }
            if (format.equals("java.class") || format.equals("java.properties")) {
                format = format.substring(5);
            }
            boolean result = false;
            try {
                String resourceName = toResourceName(toBundleName(baseNamelocale), format);
                URL url = loader.getResource(resourceName);
                if (url != null) {
                    long lastModified = 0;
                    URLConnection connection = url.openConnection();
                    if (connection != null) {
                        // disable caches to get the correct data
                        connection.setUseCaches(false);
                        if (connection instanceof JarURLConnection) {
                            JarEntry ent = ((JarURLConnection)connection).getJarEntry();
                            if (ent != null) {
                                lastModified = ent.getTime();
                                if (lastModified == -1) {
                                    lastModified = 0;
                                }
                            }
                        } else {
                            lastModified = connection.getLastModified();
                        }
                    }
                    result = lastModified >= loadTime;
                }
            } catch (NullPointerException npe) {
                throw npe;
            } catch (Exception e) {
                // ignore other exceptions
            }
            return result;
        }

        
Converts the given baseName and locale to the bundle name. This method is called from the default implementation of the newBundle and needsReload methods.

This implementation returns the following value:

     baseName + "_" + language + "_" + country + "_" + variant
 
where language, country and variant are the language, country and variant values of locale, respectively. Final component values that are empty Strings are omitted along with the preceding '_'. If all of the values are empty strings, then baseName is returned.

For example, if baseName is "baseName" and locale is Locale("ja", "", "XX"), then "baseName_ja_ _XX" is returned. If the given locale is Locale("en"), then "baseName_en" is returned.

Overriding this method allows applications to use different conventions in the organization and packaging of localized resources.

Parameters:
baseName the base name of the resource bundle, a fully qualified class name
locale the locale for which a resource bundle should be loaded
Returns:
the bundle name for the resource bundle
Throws:
java.lang.NullPointerException if baseName or locale is null
        public String toBundleName(String baseNameLocale locale) {
            if (locale == .) {
                return baseName;
            }
            String language = locale.getLanguage();
            String country = locale.getCountry();
            String variant = locale.getVariant();
            if (language == "" && country == "" && variant == "") {
                return baseName;
            }
            StringBuilder sb = new StringBuilder(baseName);
            sb.append('_');
            if (variant != "") {
                sb.append(language).append('_').append(country).append('_').append(variant);
            } else if (country != "") {
                sb.append(language).append('_').append(country);
            } else {
                sb.append(language);
            }
            return sb.toString();
        }

        
Converts the given bundleName to the form required by the ClassLoader.getResource method by replacing all occurrences of '.' in bundleName with '/' and appending a '.' and the given file suffix. For example, if bundleName is "foo.bar.MyResources_ja_JP" and suffix is "properties", then "foo/bar/MyResources_ja_JP.properties" is returned.

Parameters:
bundleName the bundle name
suffix the file type suffix
Returns:
the converted resource name
Throws:
java.lang.NullPointerException if bundleName or suffix is null
        public final String toResourceName(String bundleNameString suffix) {
            StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length());
            sb.append(bundleName.replace('.''/')).append('.').append(suffix);
            return sb.toString();
        }
    }
    private static class SingleFormatControl extends Control {
        private static final Control PROPERTIES_ONLY
            = new SingleFormatControl();
        private static final Control CLASS_ONLY
            = new SingleFormatControl();
        private final List<Stringformats;
        protected SingleFormatControl(List<Stringformats) {
            this. = formats;
        }
        public List<StringgetFormats(String baseName) {
            if (baseName == null) {
                throw new NullPointerException();
            }
            return ;
        }
    }
    private static final class NoFallbackControl extends SingleFormatControl {
        private static final Control NO_FALLBACK
            = new NoFallbackControl();
        private static final Control PROPERTIES_ONLY_NO_FALLBACK
            = new NoFallbackControl();
        private static final Control CLASS_ONLY_NO_FALLBACK
            = new NoFallbackControl();
        protected NoFallbackControl(List<Stringformats) {
            super(formats);
        }
        public Locale getFallbackLocale(String baseNameLocale locale) {
            if (baseName == null || locale == null) {
                throw new NullPointerException();
            }
            return null;
        }
    }
New to GrepCode? Check out our FAQ X