]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/ICUConfig.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / ICUConfig.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2008-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.impl;\r
8 \r
9 import java.io.IOException;\r
10 import java.io.InputStream;\r
11 import java.security.AccessControlException;\r
12 import java.security.AccessController;\r
13 import java.security.PrivilegedAction;\r
14 import java.util.MissingResourceException;\r
15 import java.util.Properties;\r
16 \r
17 /**\r
18  * ICUConfig is a class used for accessing ICU4J runtime configuration.\r
19  */\r
20 public class ICUConfig {\r
21     public static final String CONFIG_PROPS_FILE = "/com/ibm/icu/ICUConfig.properties";\r
22     private static final Properties CONFIG_PROPS;\r
23 \r
24     static {\r
25         CONFIG_PROPS = new Properties();\r
26         try {\r
27             InputStream is = ICUData.getStream(CONFIG_PROPS_FILE);\r
28             if (is != null) {\r
29                 CONFIG_PROPS.load(is);\r
30             }\r
31         } catch (MissingResourceException mre) {\r
32             // If it does not exist, ignore.\r
33         } catch (IOException ioe) {\r
34             // Any IO errors, ignore\r
35         }\r
36     }\r
37 \r
38     /**\r
39      * Get ICU configuration property value for the given name.\r
40      * @param name The configuration property name\r
41      * @return The configuration property value, or null if it does not exist.\r
42      */\r
43     public static String get(String name) {\r
44         return get(name, null);\r
45     }\r
46 \r
47     /**\r
48      * Get ICU configuration property value for the given name.\r
49      * @param name The configuration property name\r
50      * @param def The default value\r
51      * @return The configuration property value.  If the property does not\r
52      * exist, <code>def</code> is returned.\r
53      */\r
54     public static String get(String name, String def) {\r
55         String val = null;\r
56         final String fname = name;\r
57         if (System.getSecurityManager() != null) {\r
58             try {\r
59                 val = AccessController.doPrivileged(new PrivilegedAction<String>() {\r
60                     public String run() {\r
61                         return System.getProperty(fname);\r
62                     }\r
63                 });\r
64             } catch (AccessControlException e) {\r
65                 // ignore\r
66                 // TODO log this message\r
67             }\r
68         } else {\r
69             val = System.getProperty(name);\r
70         }\r
71 \r
72         if (val == null) {\r
73             val = CONFIG_PROPS.getProperty(name, def);\r
74         }\r
75         return val;\r
76     }\r
77 }\r