001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.InputStream;
018    
019    import java.net.URL;
020    
021    import java.util.Enumeration;
022    import java.util.Map;
023    import java.util.Properties;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Mirco Tamburini
029     * @author Brett Randall
030     */
031    public class SystemProperties {
032    
033            public static final String SYSTEM_PROPERTIES_FINAL =
034                    "system.properties.final";
035    
036            public static final String SYSTEM_PROPERTIES_LOAD =
037                    "system.properties.load";
038    
039            public static final String SYSTEM_PROPERTIES_QUIET =
040                    "system.properties.quiet";
041    
042            public static final String TMP_DIR = "java.io.tmpdir";
043    
044            public static String get(String key) {
045                    String value = _properties.get(key);
046    
047                    if (value == null) {
048                            value = System.getProperty(key);
049                    }
050    
051                    return value;
052            }
053    
054            public static String[] getArray(String key) {
055                    String value = get(key);
056    
057                    if (value == null) {
058                            return new String[0];
059                    }
060                    else {
061                            return StringUtil.split(value);
062                    }
063            }
064    
065            public static Properties getProperties() {
066                    return PropertiesUtil.fromMap(_properties);
067            }
068    
069            public static void reload() {
070                    if (_loaded) {
071                            return;
072                    }
073    
074                    Properties properties = new Properties();
075    
076                    Thread currentThread = Thread.currentThread();
077    
078                    ClassLoader classLoader = currentThread.getContextClassLoader();
079    
080                    boolean systemPropertiesQuiet = GetterUtil.getBoolean(
081                            System.getProperty(SYSTEM_PROPERTIES_QUIET));
082    
083                    // system.properties
084    
085                    try {
086                            Enumeration<URL> enumeration = classLoader.getResources(
087                                    "system.properties");
088    
089                            while (enumeration.hasMoreElements()) {
090                                    URL url = enumeration.nextElement();
091    
092                                    try (InputStream inputStream = url.openStream()) {
093                                            properties.load(inputStream);
094                                    }
095    
096                                    if (!systemPropertiesQuiet) {
097                                            System.out.println("Loading " + url);
098                                    }
099                            }
100                    }
101                    catch (Exception e) {
102                            e.printStackTrace();
103                    }
104    
105                    // system-ext.properties
106    
107                    try {
108                            Enumeration<URL> enumeration = classLoader.getResources(
109                                    "system-ext.properties");
110    
111                            while (enumeration.hasMoreElements()) {
112                                    URL url = enumeration.nextElement();
113    
114                                    _loaded = true;
115    
116                                    try (InputStream inputStream = url.openStream()) {
117                                            properties.load(inputStream);
118                                    }
119    
120                                    if (!systemPropertiesQuiet) {
121                                            System.out.println("Loading " + url);
122                                    }
123                            }
124                    }
125                    catch (Exception e) {
126                            e.printStackTrace();
127                    }
128    
129                    // Set environment properties
130    
131                    SystemEnv.setProperties(properties);
132    
133                    // Set system properties
134    
135                    boolean systemPropertiesLoad = GetterUtil.getBoolean(
136                            System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
137    
138                    boolean systemPropertiesFinal = GetterUtil.getBoolean(
139                            System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
140    
141                    if (systemPropertiesLoad) {
142                            Enumeration<String> enu =
143                                    (Enumeration<String>)properties.propertyNames();
144    
145                            while (enu.hasMoreElements()) {
146                                    String key = enu.nextElement();
147    
148                                    if (systemPropertiesFinal ||
149                                            Validator.isNull(System.getProperty(key))) {
150    
151                                            System.setProperty(key, properties.getProperty(key));
152                                    }
153                            }
154                    }
155    
156                    _properties = new ConcurrentHashMap<String, String>();
157    
158                    // Use a fast concurrent hash map implementation instead of the slower
159                    // java.util.Properties
160    
161                    PropertiesUtil.fromProperties(properties, _properties);
162            }
163    
164            public static void set(String key, String value) {
165                    System.setProperty(key, value);
166    
167                    _properties.put(key, value);
168            }
169    
170            private static boolean _loaded;
171            private static Map<String, String> _properties;
172    
173            static {
174                    reload();
175            }
176    
177    }