001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            /**
039             * @deprecated As of 7.0.0, with no direct replacement
040             */
041            @Deprecated
042            public static final String SYSTEM_PROPERTIES_FINAL =
043                    "system.properties.final";
044    
045            /**
046             * @deprecated As of 7.0.0, with no direct replacement
047             */
048            @Deprecated
049            public static final String SYSTEM_PROPERTIES_LOAD =
050                    "system.properties.load";
051    
052            public static final String SYSTEM_PROPERTIES_QUIET =
053                    "system.properties.quiet";
054    
055            public static final String SYSTEM_PROPERTIES_SET = "system.properties.set";
056    
057            public static final String SYSTEM_PROPERTIES_SET_OVERRIDE =
058                    "system.properties.set.override";
059    
060            public static final String TMP_DIR = "java.io.tmpdir";
061    
062            public static String get(String key) {
063                    String value = _properties.get(key);
064    
065                    if (value == null) {
066                            value = System.getProperty(key);
067                    }
068    
069                    return value;
070            }
071    
072            /**
073             * @deprecated As of 7.0.0, with no direct replacement
074             */
075            @Deprecated
076            public static String[] getArray(String key) {
077                    String value = get(key);
078    
079                    if (value == null) {
080                            return new String[0];
081                    }
082                    else {
083                            return StringUtil.split(value);
084                    }
085            }
086    
087            public static Properties getProperties() {
088                    return PropertiesUtil.fromMap(_properties);
089            }
090    
091            /**
092             * @deprecated As of 7.0.0, replaced by {@link #load(ClassLoader)}
093             */
094            @Deprecated
095            public static void reload() {
096                    if (_loaded) {
097                            return;
098                    }
099    
100                    Properties properties = new Properties();
101    
102                    Thread currentThread = Thread.currentThread();
103    
104                    ClassLoader classLoader = currentThread.getContextClassLoader();
105    
106                    boolean systemPropertiesQuiet = GetterUtil.getBoolean(
107                            System.getProperty(SYSTEM_PROPERTIES_QUIET));
108    
109                    // system.properties
110    
111                    try {
112                            URL url = classLoader.getResource("system.properties");
113    
114                            if (url != null) {
115                                    InputStream inputStream = url.openStream();
116    
117                                    properties.load(inputStream);
118    
119                                    inputStream.close();
120    
121                                    if (!systemPropertiesQuiet) {
122                                            System.out.println("Loading " + url);
123                                    }
124                            }
125                    }
126                    catch (Exception e) {
127                            e.printStackTrace();
128                    }
129    
130                    // system-ext.properties
131    
132                    try {
133                            URL url = classLoader.getResource("system-ext.properties");
134    
135                            if (url != null) {
136                                    _loaded = true;
137    
138                                    InputStream inputStream = url.openStream();
139    
140                                    properties.load(inputStream);
141    
142                                    inputStream.close();
143    
144                                    if (!systemPropertiesQuiet) {
145                                            System.out.println("Loading " + url);
146                                    }
147                            }
148                    }
149                    catch (Exception e) {
150                            e.printStackTrace();
151                    }
152    
153                    // Set environment properties
154    
155                    SystemEnv.setProperties(properties);
156    
157                    // Set system properties
158    
159                    boolean systemPropertiesLoad = GetterUtil.getBoolean(
160                            System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
161    
162                    boolean systemPropertiesFinal = GetterUtil.getBoolean(
163                            System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
164    
165                    if (systemPropertiesLoad) {
166                            Enumeration<String> enu =
167                                    (Enumeration<String>)properties.propertyNames();
168    
169                            while (enu.hasMoreElements()) {
170                                    String key = enu.nextElement();
171    
172                                    if (systemPropertiesFinal ||
173                                            Validator.isNull(System.getProperty(key))) {
174    
175                                            System.setProperty(key, properties.getProperty(key));
176                                    }
177                            }
178                    }
179    
180                    // Use a fast concurrent hash map implementation instead of the slower
181                    // java.util.Properties
182    
183                    PropertiesUtil.fromProperties(properties, _properties);
184            }
185    
186            /**
187             * @deprecated As of 7.0.0, with no direct replacement
188             */
189            @Deprecated
190            private static boolean _loaded;
191    
192            public static void load(ClassLoader classLoader) {
193                    Properties properties = new Properties();
194    
195                    List<URL> urls = null;
196    
197                    if (!GetterUtil.getBoolean(
198                                    System.getProperty(SYSTEM_PROPERTIES_QUIET))) {
199    
200                            urls = new ArrayList<URL>();
201                    }
202    
203                    // system.properties
204    
205                    try {
206                            URL url = classLoader.getResource("system.properties");
207    
208                            if (url != null) {
209                                    InputStream inputStream = url.openStream();
210    
211                                    properties.load(inputStream);
212    
213                                    inputStream.close();
214    
215                                    if (urls != null) {
216                                            urls.add(url);
217                                    }
218                            }
219                    }
220                    catch (IOException ioe) {
221                            throw new ExceptionInInitializerError(ioe);
222                    }
223    
224                    // system-ext.properties
225    
226                    try {
227                            URL url = classLoader.getResource("system-ext.properties");
228    
229                            if (url != null) {
230                                    InputStream inputStream = url.openStream();
231    
232                                    properties.load(inputStream);
233    
234                                    inputStream.close();
235    
236                                    if (urls != null) {
237                                            urls.add(url);
238                                    }
239                            }
240                    }
241                    catch (IOException ioe) {
242                            throw new ExceptionInInitializerError(ioe);
243                    }
244    
245                    // Set environment properties
246    
247                    SystemEnv.setProperties(properties);
248    
249                    // Set system properties
250    
251                    if (GetterUtil.getBoolean(
252                                    System.getProperty(SYSTEM_PROPERTIES_SET), true)) {
253    
254                            boolean systemPropertiesSetOverride = GetterUtil.getBoolean(
255                                    System.getProperty(SYSTEM_PROPERTIES_SET_OVERRIDE), true);
256    
257                            for (Entry<Object, Object> entry : properties.entrySet()) {
258                                    String key = String.valueOf(entry.getKey());
259    
260                                    if (systemPropertiesSetOverride ||
261                                            Validator.isNull(System.getProperty(key))) {
262    
263                                            System.setProperty(key, String.valueOf(entry.getValue()));
264                                    }
265                            }
266                    }
267    
268                    // Use a fast concurrent hash map implementation instead of the slower
269                    // java.util.Properties
270    
271                    PropertiesUtil.fromProperties(properties, _properties);
272    
273                    if (urls != null) {
274                            for (URL url : urls) {
275                                    System.out.println("Loading " + url);
276                            }
277                    }
278            }
279    
280            public static void set(String key, String value) {
281                    System.setProperty(key, value);
282    
283                    _properties.put(key, value);
284            }
285    
286            private static final Map<String, String> _properties =
287                    new ConcurrentHashMap<String, String>();
288    
289            static {
290                    Thread currentThread = Thread.currentThread();
291    
292                    ClassLoader classLoader = currentThread.getContextClassLoader();
293    
294                    load(classLoader);
295            }
296    
297    }