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 _instance._props.put(key, value);
71 }
72
73 public static String[] getArray(String key) {
74 String value = get(key);
75
76 if (value == null) {
77 return new String[0];
78 }
79 else {
80 return StringUtil.split(value);
81 }
82 }
83
84 public static Properties getProperties() {
85 return PropertiesUtil.fromMap(_instance._props);
86 }
87
88 private SystemProperties() {
89 Properties p = new Properties();
90
91 ClassLoader classLoader = getClass().getClassLoader();
92
93
95 try {
96 URL url = classLoader.getResource("system.properties");
97
98 if (url != null) {
99 InputStream is = url.openStream();
100
101 p.load(is);
102
103 is.close();
104
105 System.out.println("Loading " + url);
106 }
107 }
108 catch (Exception e) {
109 e.printStackTrace();
110 }
111
112
114 try {
115 URL url = classLoader.getResource("system-ext.properties");
116
117 if (url != null) {
118 InputStream is = url.openStream();
119
120 p.load(is);
121
122 is.close();
123
124 System.out.println("Loading " + url);
125 }
126 }
127 catch (Exception e) {
128 e.printStackTrace();
129 }
130
131
133 SystemEnv.setProperties(p);
134
135
137 boolean systemPropertiesLoad = GetterUtil.getBoolean(
138 System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
139
140 boolean systemPropertiesFinal = GetterUtil.getBoolean(
141 System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
142
143 if (systemPropertiesLoad) {
144 Enumeration enu = p.propertyNames();
145
146 while (enu.hasMoreElements()) {
147 String key = (String)enu.nextElement();
148
149 if (systemPropertiesFinal ||
150 Validator.isNull(System.getProperty(key))) {
151
152 System.setProperty(key, (String)p.get(key));
153 }
154 }
155 }
156
157
160 _props = new ConcurrentHashMap();
162
163
166 PropertiesUtil.fromProperties(p, _props);
167 }
168
169 private static SystemProperties _instance = new SystemProperties();
170
171 private Map _props;
172
173 }