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