001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Company;
034    import com.liferay.portal.model.CompanyConstants;
035    import com.liferay.portal.security.auth.CompanyThreadLocal;
036    
037    import java.util.HashMap;
038    import java.util.Map;
039    import java.util.Properties;
040    
041    import javax.servlet.Servlet;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     */
046    public class PropsUtil {
047    
048            public static void addProperties(Company company, Properties properties) {
049                    _instance._addProperties(company, properties);
050            }
051    
052            public static void addProperties(
053                    Company company, UnicodeProperties unicodeProperties) {
054    
055                    _instance._addProperties(company, unicodeProperties);
056            }
057    
058            public static void addProperties(Properties properties) {
059                    _instance._addProperties(properties);
060            }
061    
062            public static void addProperties(UnicodeProperties unicodeProperties) {
063                    _instance._addProperties(unicodeProperties);
064            }
065    
066            public static boolean contains(String key) {
067                    return _instance._contains(key);
068            }
069    
070            public static String get(String key) {
071                    return _instance._get(key);
072            }
073    
074            public static String get(String key, Filter filter) {
075                    return _instance._get(key, filter);
076            }
077    
078            public static String[] getArray(String key) {
079                    return _instance._getArray(key);
080            }
081    
082            public static String[] getArray(String key, Filter filter) {
083                    return _instance._getArray(key, filter);
084            }
085    
086            public static Properties getProperties() {
087                    return getProperties(false);
088            }
089    
090            public static Properties getProperties(boolean includeSystem) {
091                    Properties properties = _instance._getProperties();
092    
093                    if (!includeSystem) {
094                            return properties;
095                    }
096    
097                    Properties systemCompanyProperties = _instance._getProperties(
098                            CompanyConstants.SYSTEM);
099    
100                    Properties mergedProperties =
101                            (Properties)systemCompanyProperties.clone();
102    
103                    mergedProperties.putAll(properties);
104    
105                    return mergedProperties;
106            }
107    
108            public static Properties getProperties(
109                    String prefix, boolean removePrefix) {
110    
111                    return _instance._getProperties(prefix, removePrefix);
112            }
113    
114            public static void reload() {
115                    _instance = new PropsUtil();
116            }
117    
118            public static void removeProperties(Properties properties) {
119                    _instance._removeProperties(properties);
120            }
121    
122            public static void set(String key, String value) {
123                    _instance._set(key, value);
124            }
125    
126            private PropsUtil() {
127                    try {
128    
129                            // Default liferay home directory
130    
131                            SystemProperties.set(
132                                    PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
133    
134                            // Global shared lib directory
135    
136                            String globalSharedLibDir = _getLibDir(Servlet.class);
137    
138                            if (_log.isInfoEnabled()) {
139                                    _log.info("Global shared lib directory " + globalSharedLibDir);
140                            }
141    
142                            SystemProperties.set(
143                                    PropsKeys.LIFERAY_LIB_GLOBAL_SHARED_DIR, globalSharedLibDir);
144    
145                            // Global lib directory
146    
147                            String globalLibDir = _getLibDir(ReleaseInfo.class);
148    
149                            if (_log.isInfoEnabled()) {
150                                    _log.info("Global lib directory " + globalLibDir);
151                            }
152    
153                            SystemProperties.set(
154                                    PropsKeys.LIFERAY_LIB_GLOBAL_DIR, globalLibDir);
155    
156                            // Portal lib directory
157    
158                            Class<?> clazz = getClass();
159    
160                            ClassLoader classLoader = clazz.getClassLoader();
161    
162                            String portalLibDir = WebDirDetector.getLibDir(classLoader);
163    
164                            String portalLibDirProperty = System.getProperty(
165                                    PropsKeys.LIFERAY_LIB_PORTAL_DIR);
166    
167                            if (portalLibDirProperty != null) {
168                                    if (!portalLibDirProperty.endsWith(StringPool.SLASH)) {
169                                            portalLibDirProperty += StringPool.SLASH;
170                                    }
171    
172                                    portalLibDir = portalLibDirProperty;
173                            }
174    
175                            if (_log.isInfoEnabled()) {
176                                    _log.info("Portal lib directory " + portalLibDir);
177                            }
178    
179                            SystemProperties.set(
180                                    PropsKeys.LIFERAY_LIB_PORTAL_DIR, portalLibDir);
181    
182                            // Portal web directory
183    
184                            String portalWebDir = WebDirDetector.getRootDir(portalLibDir);
185    
186                            if (_log.isDebugEnabled()) {
187                                    _log.debug("Portal web directory " + portalWebDir);
188                            }
189    
190                            SystemProperties.set(
191                                    PropsKeys.LIFERAY_WEB_PORTAL_DIR, portalWebDir);
192    
193                            // Liferay home directory
194    
195                            _configuration = new ConfigurationImpl(
196                                    PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
197    
198                            String liferayHome = _get(PropsKeys.LIFERAY_HOME);
199    
200                            if (_log.isDebugEnabled()) {
201                                    _log.debug("Configured Liferay home " + liferayHome);
202                            }
203    
204                            SystemProperties.set(PropsKeys.LIFERAY_HOME, liferayHome);
205    
206                            // Ehcache disk directory
207    
208                            SystemProperties.set(
209                                    "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
210    
211                            if (GetterUtil.getBoolean(
212                                            SystemProperties.get("company-id-properties"))) {
213    
214                                    _configurations = new HashMap<Long, Configuration>();
215                            }
216                    }
217                    catch (Exception e) {
218                            if (_log.isErrorEnabled()) {
219                                    _log.error("Unable to initialize PropsUtil", e);
220                            }
221                    }
222            }
223    
224            private void _addProperties(Company company, Properties properties) {
225                    Configuration configuration = _getConfiguration(company);
226    
227                    configuration.addProperties(properties);
228            }
229    
230            private void _addProperties(
231                    Company company, UnicodeProperties unicodeProperties) {
232    
233                    Properties properties = new Properties();
234    
235                    properties.putAll(unicodeProperties);
236    
237                    _addProperties(company, properties);
238            }
239    
240            private void _addProperties(Properties properties) {
241                    Configuration configuration = _getConfiguration();
242    
243                    configuration.addProperties(properties);
244            }
245    
246            private void _addProperties(UnicodeProperties unicodeProperties) {
247                    Properties properties = new Properties();
248    
249                    properties.putAll(unicodeProperties);
250    
251                    _addProperties(properties);
252            }
253    
254            private boolean _contains(String key) {
255                    Configuration configuration = _getConfiguration();
256    
257                    return configuration.contains(key);
258            }
259    
260            private String _get(String key) {
261                    Configuration configuration = _getConfiguration();
262    
263                    return configuration.get(key);
264            }
265    
266            private String _get(String key, Filter filter) {
267                    Configuration configuration = _getConfiguration();
268    
269                    return configuration.get(key, filter);
270            }
271    
272            private String[] _getArray(String key) {
273                    Configuration configuration = _getConfiguration();
274    
275                    return configuration.getArray(key);
276            }
277    
278            private String[] _getArray(String key, Filter filter) {
279                    Configuration configuration = _getConfiguration();
280    
281                    return configuration.getArray(key, filter);
282            }
283    
284            private Configuration _getConfiguration() {
285                    return _getConfiguration(CompanyThreadLocal.getCompanyId());
286            }
287    
288            private Configuration _getConfiguration(long companyId) {
289                    if (_configurations == null) {
290                            return _configuration;
291                    }
292    
293                    if (companyId > CompanyConstants.SYSTEM) {
294                            Configuration configuration = _configurations.get(companyId);
295    
296                            if (configuration == null) {
297                                    configuration = new ConfigurationImpl(
298                                            PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
299                                            companyId);
300    
301                                    _configurations.put(companyId, configuration);
302                            }
303    
304                            return configuration;
305                    }
306                    else {
307                            return _configuration;
308                    }
309            }
310    
311            private Configuration _getConfiguration(Company company) {
312                    if (_configurations == null) {
313                            return _configuration;
314                    }
315    
316                    long companyId = company.getCompanyId();
317    
318                    Configuration configuration = _configurations.get(companyId);
319    
320                    if (configuration == null) {
321                            configuration = new ConfigurationImpl(
322                                    PropsUtil.class.getClassLoader(), PropsFiles.PORTAL, companyId,
323                                    company.getWebId());
324    
325                            _configurations.put(companyId, configuration);
326                    }
327    
328                    return configuration;
329            }
330    
331            private String _getDefaultLiferayHome() {
332                    String defaultLiferayHome = null;
333    
334                    if (ServerDetector.isGeronimo()) {
335                            defaultLiferayHome =
336                                    SystemProperties.get("org.apache.geronimo.home.dir") + "/..";
337                    }
338                    else if (ServerDetector.isGlassfish()) {
339                            defaultLiferayHome =
340                                    SystemProperties.get("com.sun.aas.installRoot") + "/..";
341                    }
342                    else if (ServerDetector.isJBoss()) {
343                            defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
344                    }
345                    else if (ServerDetector.isJOnAS()) {
346                            defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
347                    }
348                    else if (ServerDetector.isWebLogic()) {
349                            defaultLiferayHome =
350                                    SystemProperties.get("env.DOMAIN_HOME") + "/..";
351                    }
352                    else if (ServerDetector.isJetty()) {
353                            defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
354                    }
355                    else if (ServerDetector.isResin()) {
356                            defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
357                    }
358                    else if (ServerDetector.isTomcat()) {
359                            defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
360                    }
361                    else {
362                            defaultLiferayHome = SystemProperties.get("user.dir") + "/liferay";
363                    }
364    
365                    defaultLiferayHome = StringUtil.replace(
366                            defaultLiferayHome, CharPool.BACK_SLASH, CharPool.SLASH);
367    
368                    defaultLiferayHome = StringUtil.replace(
369                            defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
370    
371                    if (defaultLiferayHome.endsWith("/..")) {
372                            int pos = defaultLiferayHome.lastIndexOf(
373                                    CharPool.SLASH, defaultLiferayHome.length() - 4);
374    
375                            if (pos != -1) {
376                                    defaultLiferayHome = defaultLiferayHome.substring(0, pos);
377                            }
378                    }
379    
380                    if (_log.isDebugEnabled()) {
381                            _log.debug("Default Liferay home " + defaultLiferayHome);
382                    }
383    
384                    return defaultLiferayHome;
385            }
386    
387            private String _getLibDir(Class<?> clazz) {
388                    String path = ClassUtil.getParentPath(
389                            clazz.getClassLoader(), clazz.getName());
390    
391                    int pos = path.lastIndexOf(".jar!");
392    
393                    if (pos == -1) {
394                            pos = path.lastIndexOf(".jar/");
395                    }
396    
397                    pos = path.lastIndexOf(CharPool.SLASH, pos);
398    
399                    path = path.substring(0, pos + 1);
400    
401                    return path;
402            }
403    
404            private Properties _getProperties() {
405                    Configuration configuration = _getConfiguration();
406    
407                    return configuration.getProperties();
408            }
409    
410            private Properties _getProperties(long companyId) {
411                    Configuration configuration = _getConfiguration(companyId);
412    
413                    return configuration.getProperties();
414            }
415    
416            private Properties _getProperties(String prefix, boolean removePrefix) {
417                    Configuration configuration = _getConfiguration();
418    
419                    return configuration.getProperties(prefix, removePrefix);
420            }
421    
422            private void _removeProperties(Properties properties) {
423                    Configuration configuration = _getConfiguration();
424    
425                    configuration.removeProperties(properties);
426            }
427    
428            private void _set(String key, String value) {
429                    Configuration configuration = _getConfiguration();
430    
431                    configuration.set(key, value);
432            }
433    
434            private static Log _log = LogFactoryUtil.getLog(PropsUtil.class);
435    
436            private static PropsUtil _instance = new PropsUtil();
437    
438            private Configuration _configuration;
439            private Map<Long, Configuration> _configurations;
440    
441    }