001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.PropertiesUtil;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.util.SystemEnv;
027: import com.liferay.portal.kernel.util.Validator;
028:
029: import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
030:
031: import java.io.InputStream;
032:
033: import java.net.URL;
034:
035: import java.util.Enumeration;
036: import java.util.Map;
037: import java.util.Properties;
038:
039: /**
040: * <a href="SystemProperties.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: * @author Mirco Tamburini
044: * @author Brett Randall
045: *
046: */
047: public class SystemProperties {
048:
049: public static final String SYSTEM_PROPERTIES_LOAD = "system.properties.load";
050:
051: public static final String SYSTEM_PROPERTIES_FINAL = "system.properties.final";
052:
053: public static final String TMP_DIR = "java.io.tmpdir";
054:
055: public static String get(String key) {
056: String value = (String) _instance._props.get(key);
057:
058: if (value == null) {
059: value = System.getProperty(key);
060: }
061:
062: return value;
063: }
064:
065: public static void set(String key, String value) {
066: System.setProperty(key, value);
067:
068: _instance._props.put(key, value);
069: }
070:
071: public static String[] getArray(String key) {
072: String value = get(key);
073:
074: if (value == null) {
075: return new String[0];
076: } else {
077: return StringUtil.split(value);
078: }
079: }
080:
081: public static Properties getProperties() {
082: return PropertiesUtil.fromMap(_instance._props);
083: }
084:
085: private SystemProperties() {
086: Properties p = new Properties();
087:
088: ClassLoader classLoader = getClass().getClassLoader();
089:
090: // system.properties
091:
092: try {
093: URL url = classLoader.getResource("system.properties");
094:
095: if (url != null) {
096: InputStream is = url.openStream();
097:
098: p.load(is);
099:
100: is.close();
101:
102: System.out.println("Loading " + url);
103: }
104: } catch (Exception e) {
105: e.printStackTrace();
106: }
107:
108: // system-ext.properties
109:
110: try {
111: URL url = classLoader.getResource("system-ext.properties");
112:
113: if (url != null) {
114: InputStream is = url.openStream();
115:
116: p.load(is);
117:
118: is.close();
119:
120: System.out.println("Loading " + url);
121: }
122: } catch (Exception e) {
123: e.printStackTrace();
124: }
125:
126: // Set environment properties
127:
128: SystemEnv.setProperties(p);
129:
130: // Set system properties
131:
132: boolean systemPropertiesLoad = GetterUtil.getBoolean(System
133: .getProperty(SYSTEM_PROPERTIES_LOAD), true);
134:
135: boolean systemPropertiesFinal = GetterUtil.getBoolean(System
136: .getProperty(SYSTEM_PROPERTIES_FINAL), true);
137:
138: if (systemPropertiesLoad) {
139: Enumeration enu = p.propertyNames();
140:
141: while (enu.hasMoreElements()) {
142: String key = (String) enu.nextElement();
143:
144: if (systemPropertiesFinal
145: || Validator.isNull(System.getProperty(key))) {
146:
147: System.setProperty(key, (String) p.get(key));
148: }
149: }
150: }
151:
152: // You cannot call CollectionFactory directly because CollectionFactory
153: // also references SystemProperties
154:
155: //_props = CollectionFactory.getSyncHashMap();
156: _props = new ConcurrentHashMap();
157:
158: // Use a fast concurrent hash map implementation instead of the slower
159: // java.util.Properties
160:
161: PropertiesUtil.fromProperties(p, _props);
162: }
163:
164: private static SystemProperties _instance = new SystemProperties();
165:
166: private Map _props;
167:
168: }
|