Class Resources.ShadowResourceBundle

  • Enclosing class:
    Resources

    public abstract static class Resources.ShadowResourceBundle
    extends java.util.ResourceBundle
    ShadowResourceBundle is an abstract base class for ResourceBundle classes which are backed by a properties file. When the class is created, it loads a properties file with the same name as the class.

    In the standard scheme (see ResourceBundle), if you call ResourceBundle.getBundle(java.lang.String)("foo.MyResource"), it first looks for a class called foo.MyResource, then looks for a file called foo/MyResource.properties. If it finds the file, it creates a PropertyResourceBundle and loads the class. The problem is if you want to load the .properties file into a dedicated class; ShadowResourceBundle helps with this case.

    You should create a class as follows:

    package foo;
    class MyResource extends ShadowResourceBundle {
        public MyResource() throws java.io.IOException {
        }
    }
    Then when you call ResourceBundle.getBundle("foo.MyResource"), it will find the class before the properties file, but still automatically load the properties file based upon the name of the class.
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.ResourceBundle

        java.util.ResourceBundle.Control
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.PropertyResourceBundle bundle  
      • Fields inherited from class java.util.ResourceBundle

        parent
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ShadowResourceBundle()
      Creates a ShadowResourceBundle, and reads resources from a .properties file with the same name as the current class.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Enumeration<java.lang.String> getKeys()  
      protected java.lang.Object handleGetObject​(java.lang.String key)  
      protected static Resources.ShadowResourceBundle instance​(java.lang.String baseName, java.util.Locale locale, java.util.ResourceBundle bundle)
      Returns the instance of the baseName resource bundle for the given locale.
      private static java.io.InputStream openPropertiesFile​(java.lang.Class clazz)
      Opens the properties file corresponding to a given class.
      • Methods inherited from class java.util.ResourceBundle

        clearCache, clearCache, containsKey, getBaseBundleName, getBundle, getBundle, getBundle, getBundle, getBundle, getBundle, getBundle, getBundle, getLocale, getObject, getString, getStringArray, handleKeySet, keySet, setParent
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • bundle

        private java.util.PropertyResourceBundle bundle
    • Constructor Detail

      • ShadowResourceBundle

        protected ShadowResourceBundle()
                                throws java.io.IOException
        Creates a ShadowResourceBundle, and reads resources from a .properties file with the same name as the current class. For example, if the class is called foo.MyResource_en_US, reads from foo/MyResource_en_US.properties, then foo/MyResource_en.properties, then foo/MyResource.properties.
        Throws:
        java.io.IOException - on error
    • Method Detail

      • openPropertiesFile

        private static java.io.InputStream openPropertiesFile​(java.lang.Class clazz)
        Opens the properties file corresponding to a given class. The code is copied from ResourceBundle.
      • getKeys

        public java.util.Enumeration<java.lang.String> getKeys()
        Specified by:
        getKeys in class java.util.ResourceBundle
      • handleGetObject

        protected java.lang.Object handleGetObject​(java.lang.String key)
        Specified by:
        handleGetObject in class java.util.ResourceBundle
      • instance

        protected static Resources.ShadowResourceBundle instance​(java.lang.String baseName,
                                                                 java.util.Locale locale,
                                                                 java.util.ResourceBundle bundle)
        Returns the instance of the baseName resource bundle for the given locale.

        This method should be called from a derived class, with the proper casting:

        class MyResource extends ShadowResourceBundle {
            ...
        
            /**
              * Retrieves the instance of {@link MyResource} appropriate
              * to the given locale.
              **/
            public static MyResource instance(Locale locale) {
               return (MyResource) instance(
                   MyResource.class.getName(), locale,
                   ResourceBundle.getBundle(MyResource.class.getName(), locale));
            }
            ...
         }
        Parameters:
        baseName - Base name
        locale - Locale
        bundle - Resource bundle
        Returns:
        Resource bundle