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