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