001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.configuration.ConfigurationImpl;
018 import com.liferay.portal.kernel.configuration.Configuration;
019 import com.liferay.portal.kernel.configuration.Filter;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.servlet.WebDirDetector;
023 import com.liferay.portal.kernel.util.CharPool;
024 import com.liferay.portal.kernel.util.ClassUtil;
025 import com.liferay.portal.kernel.util.GetterUtil;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.kernel.util.ReleaseInfo;
028 import com.liferay.portal.kernel.util.ServerDetector;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.SystemProperties;
032 import com.liferay.portal.kernel.util.UnicodeProperties;
033 import com.liferay.portal.model.CompanyConstants;
034 import com.liferay.portal.security.auth.CompanyThreadLocal;
035
036 import java.util.HashMap;
037 import java.util.Map;
038 import java.util.Properties;
039
040 import javax.servlet.Servlet;
041
042
045 public class PropsUtil {
046
047 public static void addProperties(Properties properties) {
048 _instance._addProperties(properties);
049 }
050
051 public static void addProperties(UnicodeProperties unicodeProperties) {
052 _instance._addProperties(unicodeProperties);
053 }
054
055 public static boolean contains(String key) {
056 return _instance._contains(key);
057 }
058
059 public static String get(String key) {
060 return _instance._get(key);
061 }
062
063 public static String get(String key, Filter filter) {
064 return _instance._get(key, filter);
065 }
066
067 public static String[] getArray(String key) {
068 return _instance._getArray(key);
069 }
070
071 public static String[] getArray(String key, Filter filter) {
072 return _instance._getArray(key, filter);
073 }
074
075 public static Properties getProperties() {
076 return _instance._getProperties();
077 }
078
079 public static Properties getProperties(
080 String prefix, boolean removePrefix) {
081
082 return _instance._getProperties(prefix, removePrefix);
083 }
084
085 public static void reload() {
086 _instance = new PropsUtil();
087 }
088
089 public static void removeProperties(Properties properties) {
090 _instance._removeProperties(properties);
091 }
092
093 public static void set(String key, String value) {
094 _instance._set(key, value);
095 }
096
097 private PropsUtil() {
098 Configuration configuration = null;
099
100 try {
101
102
103
104 SystemProperties.set(
105 PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
106
107
108
109 String globalSharedLibDir = _getLibDir(Servlet.class);
110
111 if (_log.isInfoEnabled()) {
112 _log.info("Global shared lib directory " + globalSharedLibDir);
113 }
114
115 SystemProperties.set(
116 PropsKeys.LIFERAY_LIB_GLOBAL_SHARED_DIR, globalSharedLibDir);
117
118
119
120 String globalLibDir = _getLibDir(ReleaseInfo.class);
121
122 if (_log.isInfoEnabled()) {
123 _log.info("Global lib directory " + globalLibDir);
124 }
125
126 SystemProperties.set(
127 PropsKeys.LIFERAY_LIB_GLOBAL_DIR, globalLibDir);
128
129
130
131 Class<?> clazz = getClass();
132
133 ClassLoader classLoader = clazz.getClassLoader();
134
135 String portalLibDir = WebDirDetector.getLibDir(classLoader);
136
137 String portalLibDirProperty = System.getProperty(
138 PropsKeys.LIFERAY_LIB_PORTAL_DIR);
139
140 if (portalLibDirProperty != null) {
141 if (!portalLibDirProperty.endsWith(StringPool.SLASH)) {
142 portalLibDirProperty += StringPool.SLASH;
143 }
144
145 portalLibDir = portalLibDirProperty;
146 }
147
148 if (_log.isInfoEnabled()) {
149 _log.info("Portal lib directory " + portalLibDir);
150 }
151
152 SystemProperties.set(
153 PropsKeys.LIFERAY_LIB_PORTAL_DIR, portalLibDir);
154
155
156
157 String portalWebDir = WebDirDetector.getRootDir(portalLibDir);
158
159 if (_log.isDebugEnabled()) {
160 _log.debug("Portal web directory " + portalWebDir);
161 }
162
163 SystemProperties.set(
164 PropsKeys.LIFERAY_WEB_PORTAL_DIR, portalWebDir);
165
166
167
168 configuration = new ConfigurationImpl(
169 PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
170 }
171 catch (Exception e) {
172 _log.error("Unable to initialize PropsUtil", e);
173 }
174
175 _configuration = configuration;
176
177 String liferayHome = _get(PropsKeys.LIFERAY_HOME);
178
179 if (_log.isDebugEnabled()) {
180 _log.debug("Configured Liferay home " + liferayHome);
181 }
182
183 SystemProperties.set(PropsKeys.LIFERAY_HOME, liferayHome);
184
185
186
187 SystemProperties.set(
188 "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
189
190 if (GetterUtil.getBoolean(
191 SystemProperties.get("company-id-properties"))) {
192
193 _configurations = new HashMap<>();
194 }
195 else {
196 _configurations = null;
197 }
198 }
199
200 private void _addProperties(Properties properties) {
201 _getConfiguration().addProperties(properties);
202 }
203
204 private void _addProperties(UnicodeProperties unicodeProperties) {
205 Properties properties = new Properties();
206
207 properties.putAll(unicodeProperties);
208
209 _addProperties(properties);
210 }
211
212 private boolean _contains(String key) {
213 return _getConfiguration().contains(key);
214 }
215
216 private String _get(String key) {
217 return _getConfiguration().get(key);
218 }
219
220 private String _get(String key, Filter filter) {
221 return _getConfiguration().get(key, filter);
222 }
223
224 private String[] _getArray(String key) {
225 return _getConfiguration().getArray(key);
226 }
227
228 private String[] _getArray(String key, Filter filter) {
229 return _getConfiguration().getArray(key, filter);
230 }
231
232 private Configuration _getConfiguration() {
233 if (_configurations == null) {
234 return _configuration;
235 }
236
237 Long companyId = CompanyThreadLocal.getCompanyId();
238
239 if (companyId > CompanyConstants.SYSTEM) {
240 Configuration configuration = _configurations.get(companyId);
241
242 if (configuration == null) {
243 configuration = new ConfigurationImpl(
244 PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
245 companyId);
246
247 _configurations.put(companyId, configuration);
248 }
249
250 return configuration;
251 }
252 else {
253 return _configuration;
254 }
255 }
256
257 private String _getDefaultLiferayHome() {
258 String defaultLiferayHome = null;
259
260 if (ServerDetector.isGlassfish()) {
261 defaultLiferayHome =
262 SystemProperties.get("com.sun.aas.installRoot") + "/..";
263 }
264 else if (ServerDetector.isJBoss()) {
265 defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
266 }
267 else if (ServerDetector.isJOnAS()) {
268 defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
269 }
270 else if (ServerDetector.isWebLogic()) {
271 defaultLiferayHome =
272 SystemProperties.get("env.DOMAIN_HOME") + "/..";
273 }
274 else if (ServerDetector.isJetty()) {
275 defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
276 }
277 else if (ServerDetector.isResin()) {
278 defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
279 }
280 else if (ServerDetector.isTomcat()) {
281 defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
282 }
283 else {
284 defaultLiferayHome = SystemProperties.get("user.dir") + "/liferay";
285 }
286
287 defaultLiferayHome = StringUtil.replace(
288 defaultLiferayHome, CharPool.BACK_SLASH, CharPool.SLASH);
289
290 defaultLiferayHome = StringUtil.replace(
291 defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
292
293 if (defaultLiferayHome.endsWith("/..")) {
294 int pos = defaultLiferayHome.lastIndexOf(
295 CharPool.SLASH, defaultLiferayHome.length() - 4);
296
297 if (pos != -1) {
298 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
299 }
300 }
301
302 if (_log.isDebugEnabled()) {
303 _log.debug("Default Liferay home " + defaultLiferayHome);
304 }
305
306 return defaultLiferayHome;
307 }
308
309 private String _getLibDir(Class<?> clazz) {
310 String path = ClassUtil.getParentPath(
311 clazz.getClassLoader(), clazz.getName());
312
313 int pos = path.lastIndexOf(".jar!");
314
315 if (pos == -1) {
316 pos = path.lastIndexOf(".jar/");
317 }
318
319 pos = path.lastIndexOf(CharPool.SLASH, pos);
320
321 path = path.substring(0, pos + 1);
322
323 return path;
324 }
325
326 private Properties _getProperties() {
327 return _getConfiguration().getProperties();
328 }
329
330 private Properties _getProperties(String prefix, boolean removePrefix) {
331 return _getConfiguration().getProperties(prefix, removePrefix);
332 }
333
334 private void _removeProperties(Properties properties) {
335 _getConfiguration().removeProperties(properties);
336 }
337
338 private void _set(String key, String value) {
339 _getConfiguration().set(key, value);
340 }
341
342 private static final Log _log = LogFactoryUtil.getLog(PropsUtil.class);
343
344 private static PropsUtil _instance = new PropsUtil();
345
346 private final Configuration _configuration;
347 private final Map<Long, Configuration> _configurations;
348
349 }