001    /**
002     * Copyright (c) 2000-2010 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.util.GetterUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.ServerDetector;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.model.CompanyConstants;
028    import com.liferay.portal.security.auth.CompanyThreadLocal;
029    import com.liferay.util.SystemProperties;
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    import java.util.Properties;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class PropsUtil {
039    
040            public static void addProperties(Properties properties) {
041                    _instance._addProperties(properties);
042            }
043    
044            public static boolean contains(String key) {
045                    return _instance._contains(key);
046            }
047    
048            public static String get(String key) {
049                    return _instance._get(key);
050            }
051    
052            public static String get(String key, Filter filter) {
053                    return _instance._get(key, filter);
054            }
055    
056            public static String[] getArray(String key) {
057                    return _instance._getArray(key);
058            }
059    
060            public static String[] getArray(String key, Filter filter) {
061                    return _instance._getArray(key, filter);
062            }
063    
064            public static Properties getProperties() {
065                    return _instance._getProperties();
066            }
067    
068            public static Properties getProperties(
069                    String prefix, boolean removePrefix) {
070    
071                    return _instance._getProperties(prefix, removePrefix);
072            }
073    
074            public static void removeProperties(Properties properties) {
075                    _instance._removeProperties(properties);
076            }
077    
078            public static void set(String key, String value) {
079                    _instance._set(key, value);
080            }
081    
082            private PropsUtil() {
083                    try {
084                            SystemProperties.set(
085                                    PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
086    
087                            _configuration = new ConfigurationImpl(
088                                    PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
089    
090                            String liferayHome = _get(PropsKeys.LIFERAY_HOME);
091    
092                            SystemProperties.set(PropsKeys.LIFERAY_HOME, liferayHome);
093    
094                            SystemProperties.set(
095                                    "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
096    
097                            if (GetterUtil.getBoolean(
098                                            SystemProperties.get("company-id-properties"))) {
099    
100                                    _configurations = new HashMap<Long, Configuration>();
101                            }
102                    }
103                    catch (Exception e) {
104                            if (_log.isErrorEnabled()) {
105                                    _log.error("Unable to initialize PropsUtil", e);
106                            }
107                    }
108            }
109    
110            private void _addProperties(Properties properties) {
111                    _getConfiguration().addProperties(properties);
112            }
113    
114            private boolean _contains(String key) {
115                    return _getConfiguration().contains(key);
116            }
117    
118            private String _get(String key) {
119                    return _getConfiguration().get(key);
120            }
121    
122            private String _get(String key, Filter filter) {
123                    return _getConfiguration().get(key, filter);
124            }
125    
126            private String[] _getArray(String key) {
127                    return _getConfiguration().getArray(key);
128            }
129    
130            private String[] _getArray(String key, Filter filter) {
131                    return _getConfiguration().getArray(key, filter);
132            }
133    
134            private Configuration _getConfiguration() {
135                    if (_configurations == null) {
136                            return _configuration;
137                    }
138    
139                    long companyId = CompanyThreadLocal.getCompanyId();
140    
141                    if (companyId > CompanyConstants.SYSTEM) {
142                            Configuration configuration = _configurations.get(companyId);
143    
144                            if (configuration == null) {
145                                    configuration = new ConfigurationImpl(
146                                            PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
147                                            companyId);
148    
149                                    _configurations.put(companyId, configuration);
150                            }
151    
152                            return configuration;
153                    }
154                    else {
155                            return _configuration;
156                    }
157            }
158    
159            private String _getDefaultLiferayHome() {
160                    String defaultLiferayHome = null;
161    
162                    if (ServerDetector.isGeronimo()) {
163                            defaultLiferayHome =
164                                    SystemProperties.get("org.apache.geronimo.home.dir") + "/..";
165                    }
166                    else if (ServerDetector.isGlassfish()) {
167                            defaultLiferayHome =
168                                    SystemProperties.get("com.sun.aas.installRoot") + "/..";
169                    }
170                    else if (ServerDetector.isJBoss()) {
171                            defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
172                    }
173                    else if (ServerDetector.isJOnAS()) {
174                            defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
175                    }
176                    else if (ServerDetector.isWebLogic()) {
177                            defaultLiferayHome =
178                                    SystemProperties.get("env.DOMAIN_HOME") + "/..";
179                    }
180                    else if (ServerDetector.isJetty()) {
181                            defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
182                    }
183                    else if (ServerDetector.isResin()) {
184                            defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
185                    }
186                    else if (ServerDetector.isTomcat()) {
187                            defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
188                    }
189                    else {
190                            defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
191                    }
192    
193                    defaultLiferayHome = StringUtil.replace(
194                            defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
195    
196                    defaultLiferayHome = StringUtil.replace(
197                            defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
198    
199                    if (defaultLiferayHome.endsWith("/..")) {
200                            int pos = defaultLiferayHome.lastIndexOf(
201                                    StringPool.SLASH, defaultLiferayHome.length() - 4);
202    
203                            if (pos != -1) {
204                                    defaultLiferayHome = defaultLiferayHome.substring(0, pos);
205                            }
206                    }
207    
208                    return defaultLiferayHome;
209            }
210    
211            private Properties _getProperties() {
212                    return _getConfiguration().getProperties();
213            }
214    
215            private Properties _getProperties(String prefix, boolean removePrefix) {
216                    return _getConfiguration().getProperties(prefix, removePrefix);
217            }
218    
219            private void _removeProperties(Properties properties) {
220                    _getConfiguration().removeProperties(properties);
221            }
222    
223            private void _set(String key, String value) {
224                    _getConfiguration().set(key, value);
225            }
226    
227            private static Log _log = LogFactoryUtil.getLog(PropsUtil.class);
228    
229            private static PropsUtil _instance = new PropsUtil();
230    
231            private Configuration _configuration;
232            private Map<Long, Configuration> _configurations;
233    
234    }