Class LionUtils


  • public final class LionUtils
    extends java.lang.Object
    Utility methods for dealing with Localization Bundles etc.

    Note that each of the getBundledResource(java.lang.String) methods use a custom ResourceBundle.Control instance which reads in the input stream using UTF-8.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.ResourceBundle getBundledResource​(java.lang.Class<?> c)
      This method takes a class and returns a ResourceBundle which is loaded from a properties file with the same base name as the class.
      static java.util.ResourceBundle getBundledResource​(java.lang.Class<?> c, java.lang.String baseName)
      This method returns a ResourceBundle which is loaded from a properties file with the specified base name from the same package as the specified class.
      static java.util.ResourceBundle getBundledResource​(java.lang.String basename)
      This method takes a fully qualified name and returns a ResourceBundle which is loaded from a properties file with that base name.
      static java.util.ResourceBundle getBundledResource​(java.lang.String basename, java.util.Locale locale, java.lang.ClassLoader classLoader)
      This method takes a fully qualified name and returns a ResourceBundle which is loaded from a properties file with that base name.
      static java.util.Locale localeFromString​(java.lang.String s)
      Parses the given string into language and country codes, and returns a Locale instance initialized with those parameters.
      static java.util.Locale setupRuntimeLocale()
      Sets the default locale, based on the Java properties shown below.
      • Methods inherited from class java.lang.Object

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

      • localeFromString

        public static java.util.Locale localeFromString​(java.lang.String s)
        Parses the given string into language and country codes, and returns a Locale instance initialized with those parameters. For example:
             Locale locale = LionUtils.localeFromString("en_GB");
             locale.getLanguage();   // "en"
             locale.getCountry();    // "GB"
        
             locale = LionUtils.localeFromString("ru");
             locale.getLanguage();   // "ru"
             locale.getCountry();    // ""
         
        Parameters:
        s - the locale string
        Returns:
        a locale instance
      • setupRuntimeLocale

        public static java.util.Locale setupRuntimeLocale()
        Sets the default locale, based on the Java properties shown below.
           user.language
           user.country
         
        It is expected that the host system will have set these properties appropriately. Note, however, that the default values can be overridden by use of the environment variable ONOS_LOCALE.

        For example, to set the Locale to French-Canadian one can invoke (from the shell)...

         $ ONOS_LOCALE=fr_CA {command-to-invoke-onos} ...
         
        Returns:
        the runtime locale
      • getBundledResource

        public static java.util.ResourceBundle getBundledResource​(java.lang.String basename)
        This method takes a fully qualified name and returns a ResourceBundle which is loaded from a properties file with that base name.

        For example, supposing the jar file contains:

         org/onosproject/util/example/SomeBundle.properties
         

        Then, to correctly load the resource bundle associated with SomeBundle, call:

         String fqname = "org.onosproject.util.example.SomeBundle";
         ResourceBundle res = ResourceUtils.getBundledResource(fqname);
         

        Note that no error is thrown if the properties file does not exist. This condition will not become apparent until you try and access a property from the bundle, at which time a MissingResourceException will be thrown.

        Parameters:
        basename - the (fully qualified) basename of the bundle properties file
        Returns:
        the associated resource bundle
      • getBundledResource

        public static java.util.ResourceBundle getBundledResource​(java.lang.String basename,
                                                                  java.util.Locale locale,
                                                                  java.lang.ClassLoader classLoader)
        This method takes a fully qualified name and returns a ResourceBundle which is loaded from a properties file with that base name. The locale to use for bundle selection, and the class loader to use for the search path are also specified.
        Parameters:
        basename - the (fully qualified) basename of the bundle properties file
        locale - the locale
        classLoader - the class loader
        Returns:
        the appropriate resource bundle
      • getBundledResource

        public static java.util.ResourceBundle getBundledResource​(java.lang.Class<?> c)
        This method takes a class and returns a ResourceBundle which is loaded from a properties file with the same base name as the class. Note that both the class and the properties file(s) need to be in the same jar file.

        For example, supposing the jar file contains:

         org/onosproject/util/example/SomeObject.class
         org/onosproject/util/example/SomeObject.properties
         

        Then, to correctly load the resource bundle associated with SomeObject, call:

         ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class);
         

        Note that no error is thrown if the properties file does not exist. This condition will not become apparent until you try and access a property from the bundle, at which time a MissingResourceException will be thrown.

        Parameters:
        c - the class
        Returns:
        the associated resource bundle
      • getBundledResource

        public static java.util.ResourceBundle getBundledResource​(java.lang.Class<?> c,
                                                                  java.lang.String baseName)
        This method returns a ResourceBundle which is loaded from a properties file with the specified base name from the same package as the specified class. Note that both the class and the properties file(s) need to be in the same jar file.

        For example, supposing the jar file contains:

         org/onosproject/util/example/SomeObject.class
         org/onosproject/util/example/DisplayStrings.properties
         

        Then, to correctly load the resource bundle call:

         ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class,
                                                               "DisplayStrings");
         

        Note that no error is thrown if the properties file does not exist. This condition will not become apparent until you try and access a property from the bundle, at which time a MissingResourceException will be thrown.

        Parameters:
        c - the class requesting the bundle
        baseName - the base name of the resource bundle
        Returns:
        the associated resource bundle