001    /**
002     * Copyright (c) 2000-present 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    import javax.servlet.Servlet;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
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                            // Default liferay home directory
103    
104                            SystemProperties.set(
105                                    PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
106    
107                            // Global shared lib directory
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                            // Global lib directory
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                            // Portal lib directory
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                            // Portal web directory
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                            // Liferay home directory
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                    // Ehcache disk directory
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<Long, Configuration>();
194                    }
195            }
196    
197            private void _addProperties(Properties properties) {
198                    _getConfiguration().addProperties(properties);
199            }
200    
201            private void _addProperties(UnicodeProperties unicodeProperties) {
202                    Properties properties = new Properties();
203    
204                    properties.putAll(unicodeProperties);
205    
206                    _addProperties(properties);
207            }
208    
209            private boolean _contains(String key) {
210                    return _getConfiguration().contains(key);
211            }
212    
213            private String _get(String key) {
214                    return _getConfiguration().get(key);
215            }
216    
217            private String _get(String key, Filter filter) {
218                    return _getConfiguration().get(key, filter);
219            }
220    
221            private String[] _getArray(String key) {
222                    return _getConfiguration().getArray(key);
223            }
224    
225            private String[] _getArray(String key, Filter filter) {
226                    return _getConfiguration().getArray(key, filter);
227            }
228    
229            private Configuration _getConfiguration() {
230                    if (_configurations == null) {
231                            return _configuration;
232                    }
233    
234                    Long companyId = CompanyThreadLocal.getCompanyId();
235    
236                    if (companyId > CompanyConstants.SYSTEM) {
237                            Configuration configuration = _configurations.get(companyId);
238    
239                            if (configuration == null) {
240                                    configuration = new ConfigurationImpl(
241                                            PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
242                                            companyId);
243    
244                                    _configurations.put(companyId, configuration);
245                            }
246    
247                            return configuration;
248                    }
249                    else {
250                            return _configuration;
251                    }
252            }
253    
254            private String _getDefaultLiferayHome() {
255                    String defaultLiferayHome = null;
256    
257                    if (ServerDetector.isGeronimo()) {
258                            defaultLiferayHome =
259                                    SystemProperties.get("org.apache.geronimo.home.dir") + "/..";
260                    }
261                    else if (ServerDetector.isGlassfish()) {
262                            defaultLiferayHome =
263                                    SystemProperties.get("com.sun.aas.installRoot") + "/..";
264                    }
265                    else if (ServerDetector.isJBoss()) {
266                            defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
267                    }
268                    else if (ServerDetector.isJOnAS()) {
269                            defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
270                    }
271                    else if (ServerDetector.isWebLogic()) {
272                            defaultLiferayHome =
273                                    SystemProperties.get("env.DOMAIN_HOME") + "/..";
274                    }
275                    else if (ServerDetector.isJetty()) {
276                            defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
277                    }
278                    else if (ServerDetector.isResin()) {
279                            defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
280                    }
281                    else if (ServerDetector.isTomcat()) {
282                            defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
283                    }
284                    else {
285                            defaultLiferayHome = SystemProperties.get("user.dir") + "/liferay";
286                    }
287    
288                    defaultLiferayHome = StringUtil.replace(
289                            defaultLiferayHome, CharPool.BACK_SLASH, CharPool.SLASH);
290    
291                    defaultLiferayHome = StringUtil.replace(
292                            defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
293    
294                    if (defaultLiferayHome.endsWith("/..")) {
295                            int pos = defaultLiferayHome.lastIndexOf(
296                                    CharPool.SLASH, defaultLiferayHome.length() - 4);
297    
298                            if (pos != -1) {
299                                    defaultLiferayHome = defaultLiferayHome.substring(0, pos);
300                            }
301                    }
302    
303                    if (_log.isDebugEnabled()) {
304                            _log.debug("Default Liferay home " + defaultLiferayHome);
305                    }
306    
307                    return defaultLiferayHome;
308            }
309    
310            private String _getLibDir(Class<?> clazz) {
311                    String path = ClassUtil.getParentPath(
312                            clazz.getClassLoader(), clazz.getName());
313    
314                    int pos = path.lastIndexOf(".jar!");
315    
316                    if (pos == -1) {
317                            pos = path.lastIndexOf(".jar/");
318                    }
319    
320                    pos = path.lastIndexOf(CharPool.SLASH, pos);
321    
322                    path = path.substring(0, pos + 1);
323    
324                    return path;
325            }
326    
327            private Properties _getProperties() {
328                    return _getConfiguration().getProperties();
329            }
330    
331            private Properties _getProperties(String prefix, boolean removePrefix) {
332                    return _getConfiguration().getProperties(prefix, removePrefix);
333            }
334    
335            private void _removeProperties(Properties properties) {
336                    _getConfiguration().removeProperties(properties);
337            }
338    
339            private void _set(String key, String value) {
340                    _getConfiguration().set(key, value);
341            }
342    
343            private static final Log _log = LogFactoryUtil.getLog(PropsUtil.class);
344    
345            private static PropsUtil _instance = new PropsUtil();
346    
347            private final Configuration _configuration;
348            private Map<Long, Configuration> _configurations;
349    
350    }