001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.io.IOException;
018 import java.io.InputStream;
019
020 import java.net.URL;
021
022 import java.util.ArrayList;
023 import java.util.Enumeration;
024 import java.util.List;
025 import java.util.Map;
026 import java.util.Map.Entry;
027 import java.util.Properties;
028 import java.util.concurrent.ConcurrentHashMap;
029
030
036 public class SystemProperties {
037
038 public static final String SYSTEM_PROPERTIES_QUIET =
039 "system.properties.quiet";
040
041 public static final String SYSTEM_PROPERTIES_SET = "system.properties.set";
042
043 public static final String SYSTEM_PROPERTIES_SET_OVERRIDE =
044 "system.properties.set.override";
045
046 public static final String TMP_DIR = "java.io.tmpdir";
047
048 public static void clear(String key) {
049 System.clearProperty(key);
050
051 _properties.remove(key);
052 }
053
054 public static String get(String key) {
055 String value = _properties.get(key);
056
057 if (value == null) {
058 value = System.getProperty(key);
059 }
060
061 return value;
062 }
063
064 public static Properties getProperties() {
065 return PropertiesUtil.fromMap(_properties);
066 }
067
068 public static void load(ClassLoader classLoader) {
069 Properties properties = new Properties();
070
071 List<URL> urls = null;
072
073 if (!GetterUtil.getBoolean(
074 System.getProperty(SYSTEM_PROPERTIES_QUIET))) {
075
076 urls = new ArrayList<>();
077 }
078
079
080
081 try {
082 Enumeration<URL> enumeration = classLoader.getResources(
083 "system.properties");
084
085 while (enumeration.hasMoreElements()) {
086 URL url = enumeration.nextElement();
087
088 try (InputStream inputStream = url.openStream()) {
089 properties.load(inputStream);
090 }
091
092 if (urls != null) {
093 urls.add(url);
094 }
095 }
096 }
097 catch (IOException ioe) {
098 throw new ExceptionInInitializerError(ioe);
099 }
100
101
102
103 try {
104 Enumeration<URL> enumeration = classLoader.getResources(
105 "system-ext.properties");
106
107 while (enumeration.hasMoreElements()) {
108 URL url = enumeration.nextElement();
109
110 try (InputStream inputStream = url.openStream()) {
111 properties.load(inputStream);
112 }
113
114 if (urls != null) {
115 urls.add(url);
116 }
117 }
118 }
119 catch (IOException ioe) {
120 throw new ExceptionInInitializerError(ioe);
121 }
122
123
124
125 SystemEnv.setProperties(properties);
126
127
128
129 if (GetterUtil.getBoolean(
130 System.getProperty(SYSTEM_PROPERTIES_SET), true)) {
131
132 boolean systemPropertiesSetOverride = GetterUtil.getBoolean(
133 System.getProperty(SYSTEM_PROPERTIES_SET_OVERRIDE), true);
134
135 for (Entry<Object, Object> entry : properties.entrySet()) {
136 String key = String.valueOf(entry.getKey());
137
138 if (systemPropertiesSetOverride ||
139 Validator.isNull(System.getProperty(key))) {
140
141 System.setProperty(key, String.valueOf(entry.getValue()));
142 }
143 }
144 }
145
146
147
148
149 PropertiesUtil.fromProperties(properties, _properties);
150
151 if (urls != null) {
152 for (URL url : urls) {
153 System.out.println("Loading " + url);
154 }
155 }
156 }
157
158 public static void set(String key, String value) {
159 System.setProperty(key, value);
160
161 _properties.put(key, value);
162 }
163
164 private static final Map<String, String> _properties =
165 new ConcurrentHashMap<>();
166
167 static {
168 Thread currentThread = Thread.currentThread();
169
170 ClassLoader classLoader = currentThread.getContextClassLoader();
171
172 load(classLoader);
173 }
174
175 }