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.IOException;
018    import java.io.InputStream;
019    
020    import java.net.URL;
021    
022    import java.util.ArrayList;
023    import java.util.Enumeration;
024    import java.util.List;
025    import java.util.Map;
026    import java.util.Map.Entry;
027    import java.util.Properties;
028    import java.util.concurrent.ConcurrentHashMap;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Mirco Tamburini
033     * @author Brett Randall
034     * @author Shuyang Zhou
035     */
036    public class SystemProperties {
037    
038            public static final String SYSTEM_PROPERTIES_QUIET =
039                    "system.properties.quiet";
040    
041            public static final String SYSTEM_PROPERTIES_SET = "system.properties.set";
042    
043            public static final String SYSTEM_PROPERTIES_SET_OVERRIDE =
044                    "system.properties.set.override";
045    
046            public static final String TMP_DIR = "java.io.tmpdir";
047    
048            public static void clear(String key) {
049                    System.clearProperty(key);
050    
051                    _properties.remove(key);
052            }
053    
054            public static String get(String key) {
055                    String value = _properties.get(key);
056    
057                    if (value == null) {
058                            value = System.getProperty(key);
059                    }
060    
061                    return value;
062            }
063    
064            public static Properties getProperties() {
065                    return PropertiesUtil.fromMap(_properties);
066            }
067    
068            public static void set(String key, String value) {
069                    System.setProperty(key, value);
070    
071                    _properties.put(key, value);
072            }
073    
074            private static final Map<String, String> _properties;
075    
076            static {
077                    Properties properties = new Properties();
078    
079                    Thread currentThread = Thread.currentThread();
080    
081                    ClassLoader classLoader = currentThread.getContextClassLoader();
082    
083                    List<URL> urls = null;
084    
085                    if (!GetterUtil.getBoolean(
086                                    System.getProperty(SYSTEM_PROPERTIES_QUIET))) {
087    
088                            urls = new ArrayList<>();
089                    }
090    
091                    // system.properties
092    
093                    try {
094                            Enumeration<URL> enumeration = classLoader.getResources(
095                                    "system.properties");
096    
097                            while (enumeration.hasMoreElements()) {
098                                    URL url = enumeration.nextElement();
099    
100                                    try (InputStream inputStream = url.openStream()) {
101                                            properties.load(inputStream);
102                                    }
103    
104                                    if (urls != null) {
105                                            urls.add(url);
106                                    }
107                            }
108                    }
109                    catch (IOException ioe) {
110                            throw new ExceptionInInitializerError(ioe);
111                    }
112    
113                    // system-ext.properties
114    
115                    try {
116                            Enumeration<URL> enumeration = classLoader.getResources(
117                                    "system-ext.properties");
118    
119                            while (enumeration.hasMoreElements()) {
120                                    URL url = enumeration.nextElement();
121    
122                                    try (InputStream inputStream = url.openStream()) {
123                                            properties.load(inputStream);
124                                    }
125    
126                                    if (urls != null) {
127                                            urls.add(url);
128                                    }
129                            }
130                    }
131                    catch (IOException ioe) {
132                            throw new ExceptionInInitializerError(ioe);
133                    }
134    
135                    // Set environment properties
136    
137                    SystemEnv.setProperties(properties);
138    
139                    // Set system properties
140    
141                    if (GetterUtil.getBoolean(
142                                    System.getProperty(SYSTEM_PROPERTIES_SET), true)) {
143    
144                            boolean systemPropertiesSetOverride = GetterUtil.getBoolean(
145                                    System.getProperty(SYSTEM_PROPERTIES_SET_OVERRIDE), true);
146    
147                            for (Entry<Object, Object> entry : properties.entrySet()) {
148                                    String key = String.valueOf(entry.getKey());
149    
150                                    if (systemPropertiesSetOverride ||
151                                            Validator.isNull(System.getProperty(key))) {
152    
153                                            System.setProperty(key, String.valueOf(entry.getValue()));
154                                    }
155                            }
156                    }
157    
158                    _properties = new ConcurrentHashMap<>();
159    
160                    // Use a fast concurrent hash map implementation instead of the slower
161                    // java.util.Properties
162    
163                    PropertiesUtil.fromProperties(properties, _properties);
164    
165                    if (urls != null) {
166                            for (URL url : urls) {
167                                    System.out.println("Loading " + url);
168                            }
169                    }
170            }
171    
172    }