001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class PropsUtil {
044    
045            public static void addProperties(Properties properties) {
046                    _instance._addProperties(properties);
047            }
048    
049            public static void addProperties(UnicodeProperties unicodeProperties) {
050                    _instance._addProperties(unicodeProperties);
051            }
052    
053            public static boolean contains(String key) {
054                    return _instance._contains(key);
055            }
056    
057            public static String get(String key) {
058                    return _instance._get(key);
059            }
060    
061            public static String get(String key, Filter filter) {
062                    return _instance._get(key, filter);
063            }
064    
065            public static String[] getArray(String key) {
066                    return _instance._getArray(key);
067            }
068    
069            public static String[] getArray(String key, Filter filter) {
070                    return _instance._getArray(key, filter);
071            }
072    
073            public static Properties getProperties() {
074                    return _instance._getProperties();
075            }
076    
077            public static Properties getProperties(
078                    String prefix, boolean removePrefix) {
079    
080                    return _instance._getProperties(prefix, removePrefix);
081            }
082    
083            public static void reload() {
084                    _instance = new PropsUtil();
085            }
086    
087            public static void removeProperties(Properties properties) {
088                    _instance._removeProperties(properties);
089            }
090    
091            public static void set(String key, String value) {
092                    _instance._set(key, value);
093            }
094    
095            private PropsUtil() {
096                    try {
097    
098                            // Default liferay home directory
099    
100                            SystemProperties.set(
101                                    PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
102    
103                            // Global lib directory
104    
105                            String globalLibDir = ClassUtil.getParentPath(
106                                    ReleaseInfo.class.getClassLoader(),
107                                    ReleaseInfo.class.getName());
108    
109                            int pos = globalLibDir.lastIndexOf(".jar!");
110    
111                            if (pos == -1) {
112                                    pos = globalLibDir.lastIndexOf(".jar/");
113                            }
114    
115                            pos = globalLibDir.lastIndexOf(CharPool.SLASH, pos);
116    
117                            globalLibDir = globalLibDir.substring(0, pos + 1);
118    
119                            if (_log.isInfoEnabled()) {
120                                    _log.info("Global lib directory " + globalLibDir);
121                            }
122    
123                            SystemProperties.set(
124                                    PropsKeys.LIFERAY_LIB_GLOBAL_DIR, globalLibDir);
125    
126                            // Portal lib directory
127    
128                            ClassLoader classLoader = getClass().getClassLoader();
129    
130                            String portalLibDir = WebDirDetector.getLibDir(classLoader);
131    
132                            String portalLibDirProperty = System.getProperty(
133                                    PropsKeys.LIFERAY_LIB_PORTAL_DIR);
134    
135                            if (portalLibDirProperty != null) {
136                                    if (!portalLibDirProperty.endsWith(StringPool.SLASH)) {
137                                            portalLibDirProperty += StringPool.SLASH;
138                                    }
139    
140                                    portalLibDir = portalLibDirProperty;
141                            }
142    
143                            if (_log.isInfoEnabled()) {
144                                    _log.info("Portal lib directory " + portalLibDir);
145                            }
146    
147                            SystemProperties.set(
148                                    PropsKeys.LIFERAY_LIB_PORTAL_DIR, portalLibDir);
149    
150                            // Portal web directory
151    
152                            String portalWebDir = WebDirDetector.getRootDir(portalLibDir);
153    
154                            if (_log.isDebugEnabled()) {
155                                    _log.debug("Portal web directory " + portalWebDir);
156                            }
157    
158                            SystemProperties.set(
159                                    PropsKeys.LIFERAY_WEB_PORTAL_DIR, portalWebDir);
160    
161                            // Liferay home directory
162    
163                            _configuration = new ConfigurationImpl(
164                                    PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
165    
166                            String liferayHome = _get(PropsKeys.LIFERAY_HOME);
167    
168                            if (_log.isDebugEnabled()) {
169                                    _log.debug("Configured Liferay home " + liferayHome);
170                            }
171    
172                            SystemProperties.set(PropsKeys.LIFERAY_HOME, liferayHome);
173    
174                            // Ehcache disk directory
175    
176                            SystemProperties.set(
177                                    "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
178    
179                            if (GetterUtil.getBoolean(
180                                            SystemProperties.get("company-id-properties"))) {
181    
182                                    _configurations = new HashMap<Long, Configuration>();
183                            }
184                    }
185                    catch (Exception e) {
186                            if (_log.isErrorEnabled()) {
187                                    _log.error("Unable to initialize PropsUtil", e);
188                            }
189                    }
190            }
191    
192            private void _addProperties(Properties properties) {
193                    _getConfiguration().addProperties(properties);
194            }
195    
196            private void _addProperties(UnicodeProperties unicodeProperties) {
197                    Properties properties = new Properties();
198    
199                    properties.putAll(unicodeProperties);
200    
201                    _addProperties(properties);
202            }
203    
204            private boolean _contains(String key) {
205                    return _getConfiguration().contains(key);
206            }
207    
208            private String _get(String key) {
209                    return _getConfiguration().get(key);
210            }
211    
212            private String _get(String key, Filter filter) {
213                    return _getConfiguration().get(key, filter);
214            }
215    
216            private String[] _getArray(String key) {
217                    return _getConfiguration().getArray(key);
218            }
219    
220            private String[] _getArray(String key, Filter filter) {
221                    return _getConfiguration().getArray(key, filter);
222            }
223    
224            private Configuration _getConfiguration() {
225                    if (_configurations == null) {
226                            return _configuration;
227                    }
228    
229                    Long companyId = CompanyThreadLocal.getCompanyId();
230    
231                    if (companyId > CompanyConstants.SYSTEM) {
232                            Configuration configuration = _configurations.get(companyId);
233    
234                            if (configuration == null) {
235                                    configuration = new ConfigurationImpl(
236                                            PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
237                                            companyId);
238    
239                                    _configurations.put(companyId, configuration);
240                            }
241    
242                            return configuration;
243                    }
244                    else {
245                            return _configuration;
246                    }
247            }
248    
249            private String _getDefaultLiferayHome() {
250                    String defaultLiferayHome = null;
251    
252                    if (ServerDetector.isGeronimo()) {
253                            defaultLiferayHome =
254                                    SystemProperties.get("org.apache.geronimo.home.dir") + "/..";
255                    }
256                    else if (ServerDetector.isGlassfish()) {
257                            defaultLiferayHome =
258                                    SystemProperties.get("com.sun.aas.installRoot") + "/..";
259                    }
260                    else if (ServerDetector.isJBoss()) {
261                            defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
262                    }
263                    else if (ServerDetector.isJOnAS()) {
264                            defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
265                    }
266                    else if (ServerDetector.isWebLogic()) {
267                            defaultLiferayHome =
268                                    SystemProperties.get("env.DOMAIN_HOME") + "/..";
269                    }
270                    else if (ServerDetector.isJetty()) {
271                            defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
272                    }
273                    else if (ServerDetector.isResin()) {
274                            defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
275                    }
276                    else if (ServerDetector.isTomcat()) {
277                            defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
278                    }
279                    else {
280                            defaultLiferayHome = SystemProperties.get("user.dir") + "/liferay";
281                    }
282    
283                    defaultLiferayHome = StringUtil.replace(
284                            defaultLiferayHome, CharPool.BACK_SLASH, CharPool.SLASH);
285    
286                    defaultLiferayHome = StringUtil.replace(
287                            defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
288    
289                    if (defaultLiferayHome.endsWith("/..")) {
290                            int pos = defaultLiferayHome.lastIndexOf(
291                                    CharPool.SLASH, defaultLiferayHome.length() - 4);
292    
293                            if (pos != -1) {
294                                    defaultLiferayHome = defaultLiferayHome.substring(0, pos);
295                            }
296                    }
297    
298                    if (_log.isDebugEnabled()) {
299                            _log.debug("Default Liferay home " + defaultLiferayHome);
300                    }
301    
302                    return defaultLiferayHome;
303            }
304    
305            private Properties _getProperties() {
306                    return _getConfiguration().getProperties();
307            }
308    
309            private Properties _getProperties(String prefix, boolean removePrefix) {
310                    return _getConfiguration().getProperties(prefix, removePrefix);
311            }
312    
313            private void _removeProperties(Properties properties) {
314                    _getConfiguration().removeProperties(properties);
315            }
316    
317            private void _set(String key, String value) {
318                    _getConfiguration().set(key, value);
319            }
320    
321            private static Log _log = LogFactoryUtil.getLog(PropsUtil.class);
322    
323            private static PropsUtil _instance = new PropsUtil();
324    
325            private Configuration _configuration;
326            private Map<Long, Configuration> _configurations;
327    
328    }