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 = _instance._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(_instance._properties);
067            }
068    
069            public static void set(String key, String value) {
070                    System.setProperty(key, value);
071    
072                    _instance._properties.put(key, value);
073            }
074    
075            private SystemProperties() {
076                    Properties properties = new Properties();
077    
078                    Thread currentThread = Thread.currentThread();
079    
080                    ClassLoader classLoader = currentThread.getContextClassLoader();
081    
082                    boolean systemPropertiesQuiet = GetterUtil.getBoolean(
083                            System.getProperty(SYSTEM_PROPERTIES_QUIET));
084    
085                    // system.properties
086    
087                    try {
088                            URL url = classLoader.getResource("system.properties");
089    
090                            if (url != null) {
091                                    InputStream inputStream = url.openStream();
092    
093                                    properties.load(inputStream);
094    
095                                    inputStream.close();
096    
097                                    if (!systemPropertiesQuiet) {
098                                            System.out.println("Loading " + url);
099                                    }
100                            }
101                    }
102                    catch (Exception e) {
103                            e.printStackTrace();
104                    }
105    
106                    // system-ext.properties
107    
108                    try {
109                            URL url = classLoader.getResource("system-ext.properties");
110    
111                            if (url != null) {
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            private static SystemProperties _instance = new SystemProperties();
163    
164            private Map<String, String> _properties;
165    
166    }