001
014
015 package com.liferay.util;
016
017 import com.liferay.portal.kernel.util.GetterUtil;
018 import com.liferay.portal.kernel.util.PropertiesUtil;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.util.SystemEnv;
021 import com.liferay.portal.kernel.util.Validator;
022
023 import java.io.InputStream;
024
025 import java.net.URL;
026
027 import java.util.Enumeration;
028 import java.util.Map;
029 import java.util.Properties;
030 import java.util.concurrent.ConcurrentHashMap;
031
032
037 public class SystemProperties {
038
039 public static final String SYSTEM_PROPERTIES_LOAD =
040 "system.properties.load";
041
042 public static final String SYSTEM_PROPERTIES_FINAL =
043 "system.properties.final";
044
045 public static final String TMP_DIR = "java.io.tmpdir";
046
047 public static String get(String key) {
048 String value = _instance._props.get(key);
049
050 if (value == null) {
051 value = System.getProperty(key);
052 }
053
054 return value;
055 }
056
057 public static void set(String key, String value) {
058 System.setProperty(key, value);
059
060 _instance._props.put(key, value);
061 }
062
063 public static String[] getArray(String key) {
064 String value = get(key);
065
066 if (value == null) {
067 return new String[0];
068 }
069 else {
070 return StringUtil.split(value);
071 }
072 }
073
074 public static Properties getProperties() {
075 return PropertiesUtil.fromMap(_instance._props);
076 }
077
078 private SystemProperties() {
079 Properties p = new Properties();
080
081 ClassLoader classLoader = getClass().getClassLoader();
082
083
084
085 try {
086 URL url = classLoader.getResource("system.properties");
087
088 if (url != null) {
089 InputStream is = url.openStream();
090
091 p.load(is);
092
093 is.close();
094
095 System.out.println("Loading " + url);
096 }
097 }
098 catch (Exception e) {
099 e.printStackTrace();
100 }
101
102
103
104 try {
105 URL url = classLoader.getResource("system-ext.properties");
106
107 if (url != null) {
108 InputStream is = url.openStream();
109
110 p.load(is);
111
112 is.close();
113
114 System.out.println("Loading " + url);
115 }
116 }
117 catch (Exception e) {
118 e.printStackTrace();
119 }
120
121
122
123 SystemEnv.setProperties(p);
124
125
126
127 boolean systemPropertiesLoad = GetterUtil.getBoolean(
128 System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
129
130 boolean systemPropertiesFinal = GetterUtil.getBoolean(
131 System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
132
133 if (systemPropertiesLoad) {
134 Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
135
136 while (enu.hasMoreElements()) {
137 String key = enu.nextElement();
138
139 if (systemPropertiesFinal ||
140 Validator.isNull(System.getProperty(key))) {
141
142 System.setProperty(key, p.getProperty(key));
143 }
144 }
145 }
146
147 _props = new ConcurrentHashMap<String, String>();
148
149
150
151
152 PropertiesUtil.fromProperties(p, _props);
153 }
154
155 private static SystemProperties _instance = new SystemProperties();
156
157 private Map<String, String> _props;
158
159 }