1/*
2 *******************************************************************************
3 * Copyright (C) 2008-2015, 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                try {
30                    CONFIG_PROPS.load(is);
31                } finally {
32                    is.close();
33                }
34            }
35        } catch (MissingResourceException mre) {
36            // If it does not exist, ignore.
37        } catch (IOException ioe) {
38            // Any IO errors, ignore
39        }
40    }
41
42    /**
43     * Get ICU configuration property value for the given name.
44     * @param name The configuration property name
45     * @return The configuration property value, or null if it does not exist.
46     */
47    public static String get(String name) {
48        return get(name, null);
49    }
50
51    /**
52     * Get ICU configuration property value for the given name.
53     * @param name The configuration property name
54     * @param def The default value
55     * @return The configuration property value.  If the property does not
56     * exist, <code>def</code> is returned.
57     */
58    public static String get(String name, String def) {
59        String val = null;
60        final String fname = name;
61        if (System.getSecurityManager() != null) {
62            try {
63                val = AccessController.doPrivileged(new PrivilegedAction<String>() {
64                    public String run() {
65                        return System.getProperty(fname);
66                    }
67                });
68            } catch (AccessControlException e) {
69                // ignore
70                // TODO log this message
71            }
72        } else {
73            val = System.getProperty(name);
74        }
75
76        if (val == null) {
77            val = CONFIG_PROPS.getProperty(name, def);
78        }
79        return val;
80    }
81}
82