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