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 void clear(String key) {
045                    System.clearProperty(key);
046    
047                    _properties.remove(key);
048            }
049    
050            public static String get(String key) {
051                    String value = _properties.get(key);
052    
053                    if (value == null) {
054                            value = System.getProperty(key);
055                    }
056    
057                    return value;
058            }
059    
060            public static String[] getArray(String key) {
061                    String value = get(key);
062    
063                    if (value == null) {
064                            return new String[0];
065                    }
066                    else {
067                            return StringUtil.split(value);
068                    }
069            }
070    
071            public static Properties getProperties() {
072                    return PropertiesUtil.fromMap(_properties);
073            }
074    
075            public static void reload() {
076                    if (_loaded) {
077                            return;
078                    }
079    
080                    Properties properties = new Properties();
081    
082                    Thread currentThread = Thread.currentThread();
083    
084                    ClassLoader classLoader = currentThread.getContextClassLoader();
085    
086                    boolean systemPropertiesQuiet = GetterUtil.getBoolean(
087                            System.getProperty(SYSTEM_PROPERTIES_QUIET));
088    
089                    // system.properties
090    
091                    try {
092                            Enumeration<URL> enumeration = classLoader.getResources(
093                                    "system.properties");
094    
095                            while (enumeration.hasMoreElements()) {
096                                    URL url = enumeration.nextElement();
097    
098                                    try (InputStream inputStream = url.openStream()) {
099                                            properties.load(inputStream);
100                                    }
101    
102                                    if (!systemPropertiesQuiet) {
103                                            System.out.println("Loading " + url);
104                                    }
105                            }
106                    }
107                    catch (Exception e) {
108                            e.printStackTrace();
109                    }
110    
111                    // system-ext.properties
112    
113                    try {
114                            Enumeration<URL> enumeration = classLoader.getResources(
115                                    "system-ext.properties");
116    
117                            while (enumeration.hasMoreElements()) {
118                                    URL url = enumeration.nextElement();
119    
120                                    _loaded = true;
121    
122                                    try (InputStream inputStream = url.openStream()) {
123                                            properties.load(inputStream);
124                                    }
125    
126                                    if (!systemPropertiesQuiet) {
127                                            System.out.println("Loading " + url);
128                                    }
129                            }
130                    }
131                    catch (Exception e) {
132                            e.printStackTrace();
133                    }
134    
135                    // Set environment properties
136    
137                    SystemEnv.setProperties(properties);
138    
139                    // Set system properties
140    
141                    boolean systemPropertiesLoad = GetterUtil.getBoolean(
142                            System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
143    
144                    boolean systemPropertiesFinal = GetterUtil.getBoolean(
145                            System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
146    
147                    if (systemPropertiesLoad) {
148                            Enumeration<String> enu =
149                                    (Enumeration<String>)properties.propertyNames();
150    
151                            while (enu.hasMoreElements()) {
152                                    String key = enu.nextElement();
153    
154                                    if (systemPropertiesFinal ||
155                                            Validator.isNull(System.getProperty(key))) {
156    
157                                            System.setProperty(key, properties.getProperty(key));
158                                    }
159                            }
160                    }
161    
162                    _properties = new ConcurrentHashMap<>();
163    
164                    // Use a fast concurrent hash map implementation instead of the slower
165                    // java.util.Properties
166    
167                    PropertiesUtil.fromProperties(properties, _properties);
168            }
169    
170            public static void set(String key, String value) {
171                    System.setProperty(key, value);
172    
173                    _properties.put(key, value);
174            }
175    
176            private static boolean _loaded;
177            private static Map<String, String> _properties;
178    
179            static {
180                    reload();
181            }
182    
183    }