< prev index next >

src/java.base/share/classes/java/util/Locale.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


  28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  29  *
  30  * The original version of this source code and documentation
  31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  32  * subsidiary of IBM. These materials are provided under terms
  33  * of a License Agreement between Taligent and Sun. This technology
  34  * is protected by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package java.util;
  42 
  43 import java.io.IOException;
  44 import java.io.ObjectInputStream;
  45 import java.io.ObjectOutputStream;
  46 import java.io.ObjectStreamField;
  47 import java.io.Serializable;
  48 import java.security.AccessController;
  49 import java.text.MessageFormat;
  50 import java.util.spi.LocaleNameProvider;
  51 
  52 import sun.security.action.GetPropertyAction;
  53 import sun.util.locale.BaseLocale;
  54 import sun.util.locale.InternalLocaleBuilder;
  55 import sun.util.locale.LanguageTag;
  56 import sun.util.locale.LocaleExtensions;
  57 import sun.util.locale.LocaleMatcher;
  58 import sun.util.locale.LocaleObjectCache;
  59 import sun.util.locale.LocaleSyntaxException;
  60 import sun.util.locale.LocaleUtils;
  61 import sun.util.locale.ParseStatus;
  62 import sun.util.locale.provider.LocaleProviderAdapter;
  63 import sun.util.locale.provider.LocaleResources;
  64 import sun.util.locale.provider.LocaleServiceProviderPool;
  65 
  66 /**
  67  * A <code>Locale</code> object represents a specific geographical, political,
  68  * or cultural region. An operation that requires a <code>Locale</code> to perform


 842                 }
 843             }
 844             return defaultDisplayLocale;
 845         case FORMAT:
 846             if (defaultFormatLocale == null) {
 847                 synchronized(Locale.class) {
 848                     if (defaultFormatLocale == null) {
 849                         defaultFormatLocale = initDefault(category);
 850                     }
 851                 }
 852             }
 853             return defaultFormatLocale;
 854         default:
 855             assert false: "Unknown Category";
 856         }
 857         return getDefault();
 858     }
 859 
 860     private static Locale initDefault() {
 861         String language, region, script, country, variant;
 862         language = AccessController.doPrivileged(
 863             new GetPropertyAction("user.language", "en"));
 864         // for compatibility, check for old user.region property
 865         region = AccessController.doPrivileged(
 866             new GetPropertyAction("user.region"));
 867         if (region != null) {
 868             // region can be of form country, country_variant, or _variant
 869             int i = region.indexOf('_');
 870             if (i >= 0) {
 871                 country = region.substring(0, i);
 872                 variant = region.substring(i + 1);
 873             } else {
 874                 country = region;
 875                 variant = "";
 876             }
 877             script = "";
 878         } else {
 879             script = AccessController.doPrivileged(
 880                 new GetPropertyAction("user.script", ""));
 881             country = AccessController.doPrivileged(
 882                 new GetPropertyAction("user.country", ""));
 883             variant = AccessController.doPrivileged(
 884                 new GetPropertyAction("user.variant", ""));
 885         }
 886 
 887         return getInstance(language, script, country, variant, null);
 888     }
 889 
 890     private static Locale initDefault(Locale.Category category) {

 891         return getInstance(
 892             AccessController.doPrivileged(
 893                 new GetPropertyAction(category.languageKey, defaultLocale.getLanguage())),
 894             AccessController.doPrivileged(
 895                 new GetPropertyAction(category.scriptKey, defaultLocale.getScript())),
 896             AccessController.doPrivileged(
 897                 new GetPropertyAction(category.countryKey, defaultLocale.getCountry())),
 898             AccessController.doPrivileged(
 899                 new GetPropertyAction(category.variantKey, defaultLocale.getVariant())),
 900             null);
 901     }
 902 
 903     /**
 904      * Sets the default locale for this instance of the Java Virtual Machine.
 905      * This does not affect the host locale.
 906      * <p>
 907      * If there is a security manager, its <code>checkPermission</code>
 908      * method is called with a <code>PropertyPermission("user.language", "write")</code>
 909      * permission before the default locale is changed.
 910      * <p>
 911      * The Java Virtual Machine sets the default locale during startup
 912      * based on the host environment. It is used by many locale-sensitive
 913      * methods if no locale is explicitly specified.
 914      * <p>
 915      * Since changing the default locale may affect many different areas
 916      * of functionality, this method should only be used if the caller
 917      * is prepared to reinitialize locale-sensitive code running
 918      * within the same Java Virtual Machine.
 919      * <p>




  28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  29  *
  30  * The original version of this source code and documentation
  31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  32  * subsidiary of IBM. These materials are provided under terms
  33  * of a License Agreement between Taligent and Sun. This technology
  34  * is protected by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package java.util;
  42 
  43 import java.io.IOException;
  44 import java.io.ObjectInputStream;
  45 import java.io.ObjectOutputStream;
  46 import java.io.ObjectStreamField;
  47 import java.io.Serializable;

  48 import java.text.MessageFormat;
  49 import java.util.spi.LocaleNameProvider;
  50 
  51 import sun.security.action.GetPropertyAction;
  52 import sun.util.locale.BaseLocale;
  53 import sun.util.locale.InternalLocaleBuilder;
  54 import sun.util.locale.LanguageTag;
  55 import sun.util.locale.LocaleExtensions;
  56 import sun.util.locale.LocaleMatcher;
  57 import sun.util.locale.LocaleObjectCache;
  58 import sun.util.locale.LocaleSyntaxException;
  59 import sun.util.locale.LocaleUtils;
  60 import sun.util.locale.ParseStatus;
  61 import sun.util.locale.provider.LocaleProviderAdapter;
  62 import sun.util.locale.provider.LocaleResources;
  63 import sun.util.locale.provider.LocaleServiceProviderPool;
  64 
  65 /**
  66  * A <code>Locale</code> object represents a specific geographical, political,
  67  * or cultural region. An operation that requires a <code>Locale</code> to perform


 841                 }
 842             }
 843             return defaultDisplayLocale;
 844         case FORMAT:
 845             if (defaultFormatLocale == null) {
 846                 synchronized(Locale.class) {
 847                     if (defaultFormatLocale == null) {
 848                         defaultFormatLocale = initDefault(category);
 849                     }
 850                 }
 851             }
 852             return defaultFormatLocale;
 853         default:
 854             assert false: "Unknown Category";
 855         }
 856         return getDefault();
 857     }
 858 
 859     private static Locale initDefault() {
 860         String language, region, script, country, variant;
 861         Properties props = GetPropertyAction.getProperties();
 862         language = props.getProperty("user.language", "en");
 863         // for compatibility, check for old user.region property
 864         region = props.getProperty("user.region");

 865         if (region != null) {
 866             // region can be of form country, country_variant, or _variant
 867             int i = region.indexOf('_');
 868             if (i >= 0) {
 869                 country = region.substring(0, i);
 870                 variant = region.substring(i + 1);
 871             } else {
 872                 country = region;
 873                 variant = "";
 874             }
 875             script = "";
 876         } else {
 877             script = props.getProperty("user.script", "");
 878             country = props.getProperty("user.country", "");
 879             variant = props.getProperty("user.variant", "");



 880         }
 881 
 882         return getInstance(language, script, country, variant, null);
 883     }
 884 
 885     private static Locale initDefault(Locale.Category category) {
 886         Properties props = GetPropertyAction.getProperties();
 887         return getInstance(
 888             props.getProperty(category.languageKey,
 889                     defaultLocale.getLanguage()),
 890             props.getProperty(category.scriptKey,
 891                     defaultLocale.getScript()),
 892             props.getProperty(category.countryKey,
 893                     defaultLocale.getCountry()),
 894             props.getProperty(category.variantKey,
 895                     defaultLocale.getVariant()),
 896             null);
 897     }
 898 
 899     /**
 900      * Sets the default locale for this instance of the Java Virtual Machine.
 901      * This does not affect the host locale.
 902      * <p>
 903      * If there is a security manager, its <code>checkPermission</code>
 904      * method is called with a <code>PropertyPermission("user.language", "write")</code>
 905      * permission before the default locale is changed.
 906      * <p>
 907      * The Java Virtual Machine sets the default locale during startup
 908      * based on the host environment. It is used by many locale-sensitive
 909      * methods if no locale is explicitly specified.
 910      * <p>
 911      * Since changing the default locale may affect many different areas
 912      * of functionality, this method should only be used if the caller
 913      * is prepared to reinitialize locale-sensitive code running
 914      * within the same Java Virtual Machine.
 915      * <p>


< prev index next >