Home | History | Annotate | Download | only in impl
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 2008-2015, International Business Machines Corporation and
      6  * others. All Rights Reserved.
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.impl;
     10 
     11 import java.io.IOException;
     12 import java.io.InputStream;
     13 import java.security.AccessControlException;
     14 import java.security.AccessController;
     15 import java.security.PrivilegedAction;
     16 import java.util.MissingResourceException;
     17 import java.util.Properties;
     18 
     19 /**
     20  * ICUConfig is a class used for accessing ICU4J runtime configuration.
     21  */
     22 public class ICUConfig {
     23     public static final String CONFIG_PROPS_FILE = "/com/ibm/icu/ICUConfig.properties";
     24     private static final Properties CONFIG_PROPS;
     25 
     26     static {
     27         CONFIG_PROPS = new Properties();
     28         try {
     29             InputStream is = ICUData.getStream(CONFIG_PROPS_FILE);
     30             if (is != null) {
     31                 try {
     32                     CONFIG_PROPS.load(is);
     33                 } finally {
     34                     is.close();
     35                 }
     36             }
     37         } catch (MissingResourceException mre) {
     38             // If it does not exist, ignore.
     39         } catch (IOException ioe) {
     40             // Any IO errors, ignore
     41         }
     42     }
     43 
     44     /**
     45      * Get ICU configuration property value for the given name.
     46      * @param name The configuration property name
     47      * @return The configuration property value, or null if it does not exist.
     48      */
     49     public static String get(String name) {
     50         return get(name, null);
     51     }
     52 
     53     /**
     54      * Get ICU configuration property value for the given name.
     55      * @param name The configuration property name
     56      * @param def The default value
     57      * @return The configuration property value.  If the property does not
     58      * exist, <code>def</code> is returned.
     59      */
     60     public static String get(String name, String def) {
     61         String val = null;
     62         final String fname = name;
     63         if (System.getSecurityManager() != null) {
     64             try {
     65                 val = AccessController.doPrivileged(new PrivilegedAction<String>() {
     66                     @Override
     67                     public String run() {
     68                         return System.getProperty(fname);
     69                     }
     70                 });
     71             } catch (AccessControlException e) {
     72                 // ignore
     73                 // TODO log this message
     74             }
     75         } else {
     76             val = System.getProperty(name);
     77         }
     78 
     79         if (val == null) {
     80             val = CONFIG_PROPS.getProperty(name, def);
     81         }
     82         return val;
     83     }
     84 }
     85