001    /**
002     * Copyright (c) 2000-2013 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                            URL url = classLoader.getResource("system.properties");
087    
088                            if (url != null) {
089                                    InputStream inputStream = url.openStream();
090    
091                                    properties.load(inputStream);
092    
093                                    inputStream.close();
094    
095                                    if (!systemPropertiesQuiet) {
096                                            System.out.println("Loading " + url);
097                                    }
098                            }
099                    }
100                    catch (Exception e) {
101                            e.printStackTrace();
102                    }
103    
104                    // system-ext.properties
105    
106                    try {
107                            URL url = classLoader.getResource("system-ext.properties");
108    
109                            if (url != null) {
110                                    _loaded = true;
111    
112                                    InputStream inputStream = url.openStream();
113    
114                                    properties.load(inputStream);
115    
116                                    inputStream.close();
117    
118                                    if (!systemPropertiesQuiet) {
119                                            System.out.println("Loading " + url);
120                                    }
121                            }
122                    }
123                    catch (Exception e) {
124                            e.printStackTrace();
125                    }
126    
127                    // Set environment properties
128    
129                    SystemEnv.setProperties(properties);
130    
131                    // Set system properties
132    
133                    boolean systemPropertiesLoad = GetterUtil.getBoolean(
134                            System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
135    
136                    boolean systemPropertiesFinal = GetterUtil.getBoolean(
137                            System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
138    
139                    if (systemPropertiesLoad) {
140                            Enumeration<String> enu =
141                                    (Enumeration<String>)properties.propertyNames();
142    
143                            while (enu.hasMoreElements()) {
144                                    String key = enu.nextElement();
145    
146                                    if (systemPropertiesFinal ||
147                                            Validator.isNull(System.getProperty(key))) {
148    
149                                            System.setProperty(key, properties.getProperty(key));
150                                    }
151                            }
152                    }
153    
154                    _properties = new ConcurrentHashMap<String, String>();
155    
156                    // Use a fast concurrent hash map implementation instead of the slower
157                    // java.util.Properties
158    
159                    PropertiesUtil.fromProperties(properties, _properties);
160            }
161    
162            public static void set(String key, String value) {
163                    System.setProperty(key, value);
164    
165                    _properties.put(key, value);
166            }
167    
168            private static boolean _loaded;
169            private static Map<String, String> _properties;
170    
171            static {
172                    reload();
173            }
174    
175    }