001
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
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 SYSTEM_PROPERTIES_QUIET =
040 "system.properties.quiet";
041
042 public static final String TMP_DIR = "java.io.tmpdir";
043
044 public static void clear(String key) {
045 System.clearProperty(key);
046
047 _properties.remove(key);
048 }
049
050 public static String get(String key) {
051 String value = _properties.get(key);
052
053 if (value == null) {
054 value = System.getProperty(key);
055 }
056
057 return value;
058 }
059
060 public static String[] getArray(String key) {
061 String value = get(key);
062
063 if (value == null) {
064 return new String[0];
065 }
066 else {
067 return StringUtil.split(value);
068 }
069 }
070
071 public static Properties getProperties() {
072 return PropertiesUtil.fromMap(_properties);
073 }
074
075 public static void reload() {
076 if (_loaded) {
077 return;
078 }
079
080 Properties properties = new Properties();
081
082 Thread currentThread = Thread.currentThread();
083
084 ClassLoader classLoader = currentThread.getContextClassLoader();
085
086 boolean systemPropertiesQuiet = GetterUtil.getBoolean(
087 System.getProperty(SYSTEM_PROPERTIES_QUIET));
088
089
090
091 try {
092 Enumeration<URL> enumeration = classLoader.getResources(
093 "system.properties");
094
095 while (enumeration.hasMoreElements()) {
096 URL url = enumeration.nextElement();
097
098 try (InputStream inputStream = url.openStream()) {
099 properties.load(inputStream);
100 }
101
102 if (!systemPropertiesQuiet) {
103 System.out.println("Loading " + url);
104 }
105 }
106 }
107 catch (Exception e) {
108 e.printStackTrace();
109 }
110
111
112
113 try {
114 Enumeration<URL> enumeration = classLoader.getResources(
115 "system-ext.properties");
116
117 while (enumeration.hasMoreElements()) {
118 URL url = enumeration.nextElement();
119
120 _loaded = true;
121
122 try (InputStream inputStream = url.openStream()) {
123 properties.load(inputStream);
124 }
125
126 if (!systemPropertiesQuiet) {
127 System.out.println("Loading " + url);
128 }
129 }
130 }
131 catch (Exception e) {
132 e.printStackTrace();
133 }
134
135
136
137 SystemEnv.setProperties(properties);
138
139
140
141 boolean systemPropertiesLoad = GetterUtil.getBoolean(
142 System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
143
144 boolean systemPropertiesFinal = GetterUtil.getBoolean(
145 System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
146
147 if (systemPropertiesLoad) {
148 Enumeration<String> enu =
149 (Enumeration<String>)properties.propertyNames();
150
151 while (enu.hasMoreElements()) {
152 String key = enu.nextElement();
153
154 if (systemPropertiesFinal ||
155 Validator.isNull(System.getProperty(key))) {
156
157 System.setProperty(key, properties.getProperty(key));
158 }
159 }
160 }
161
162 _properties = new ConcurrentHashMap<>();
163
164
165
166
167 PropertiesUtil.fromProperties(properties, _properties);
168 }
169
170 public static void set(String key, String value) {
171 System.setProperty(key, value);
172
173 _properties.put(key, value);
174 }
175
176 private static boolean _loaded;
177 private static Map<String, String> _properties;
178
179 static {
180 reload();
181 }
182
183 }