1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.PropertiesUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.SystemEnv;
29 import com.liferay.portal.kernel.util.Validator;
30
31 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
32
33 import java.io.InputStream;
34
35 import java.net.URL;
36
37 import java.util.Enumeration;
38 import java.util.Map;
39 import java.util.Properties;
40
41
49 public class SystemProperties {
50
51 public static final String SYSTEM_PROPERTIES_LOAD =
52 "system.properties.load";
53
54 public static final String SYSTEM_PROPERTIES_FINAL =
55 "system.properties.final";
56
57 public static final String TMP_DIR = "java.io.tmpdir";
58
59 public static String get(String key) {
60 String value = (String)_instance._props.get(key);
61
62 if (value == null) {
63 value = System.getProperty(key);
64 }
65
66 return value;
67 }
68
69 public static void set(String key, String value) {
70 System.setProperty(key, value);
71
72 _instance._props.put(key, value);
73 }
74
75 public static String[] getArray(String key) {
76 String value = get(key);
77
78 if (value == null) {
79 return new String[0];
80 }
81 else {
82 return StringUtil.split(value);
83 }
84 }
85
86 public static Properties getProperties() {
87 return PropertiesUtil.fromMap(_instance._props);
88 }
89
90 private SystemProperties() {
91 Properties p = new Properties();
92
93 ClassLoader classLoader = getClass().getClassLoader();
94
95
97 try {
98 URL url = classLoader.getResource("system.properties");
99
100 if (url != null) {
101 InputStream is = url.openStream();
102
103 p.load(is);
104
105 is.close();
106
107 System.out.println("Loading " + url);
108 }
109 }
110 catch (Exception e) {
111 e.printStackTrace();
112 }
113
114
116 try {
117 URL url = classLoader.getResource("system-ext.properties");
118
119 if (url != null) {
120 InputStream is = url.openStream();
121
122 p.load(is);
123
124 is.close();
125
126 System.out.println("Loading " + url);
127 }
128 }
129 catch (Exception e) {
130 e.printStackTrace();
131 }
132
133
135 SystemEnv.setProperties(p);
136
137
139 boolean systemPropertiesLoad = GetterUtil.getBoolean(
140 System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
141
142 boolean systemPropertiesFinal = GetterUtil.getBoolean(
143 System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
144
145 if (systemPropertiesLoad) {
146 Enumeration enu = p.propertyNames();
147
148 while (enu.hasMoreElements()) {
149 String key = (String)enu.nextElement();
150
151 if (systemPropertiesFinal ||
152 Validator.isNull(System.getProperty(key))) {
153
154 System.setProperty(key, (String)p.get(key));
155 }
156 }
157 }
158
159
162 _props = new ConcurrentHashMap();
164
165
168 PropertiesUtil.fromProperties(p, _props);
169 }
170
171 private static SystemProperties _instance = new SystemProperties();
172
173 private Map _props;
174
175 }