001
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
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 try {
099
100
101
102 SystemProperties.set(
103 PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
104
105
106
107 String globalSharedLibDir = _getLibDir(Servlet.class);
108
109 if (_log.isInfoEnabled()) {
110 _log.info("Global shared lib directory " + globalSharedLibDir);
111 }
112
113 SystemProperties.set(
114 PropsKeys.LIFERAY_LIB_GLOBAL_SHARED_DIR, globalSharedLibDir);
115
116
117
118 String globalLibDir = _getLibDir(ReleaseInfo.class);
119
120 if (_log.isInfoEnabled()) {
121 _log.info("Global lib directory " + globalLibDir);
122 }
123
124 SystemProperties.set(
125 PropsKeys.LIFERAY_LIB_GLOBAL_DIR, globalLibDir);
126
127
128
129 Class<?> clazz = getClass();
130
131 ClassLoader classLoader = clazz.getClassLoader();
132
133 String portalLibDir = WebDirDetector.getLibDir(classLoader);
134
135 String portalLibDirProperty = System.getProperty(
136 PropsKeys.LIFERAY_LIB_PORTAL_DIR);
137
138 if (portalLibDirProperty != null) {
139 if (!portalLibDirProperty.endsWith(StringPool.SLASH)) {
140 portalLibDirProperty += StringPool.SLASH;
141 }
142
143 portalLibDir = portalLibDirProperty;
144 }
145
146 if (_log.isInfoEnabled()) {
147 _log.info("Portal lib directory " + portalLibDir);
148 }
149
150 SystemProperties.set(
151 PropsKeys.LIFERAY_LIB_PORTAL_DIR, portalLibDir);
152
153
154
155 String portalWebDir = WebDirDetector.getRootDir(portalLibDir);
156
157 if (_log.isDebugEnabled()) {
158 _log.debug("Portal web directory " + portalWebDir);
159 }
160
161 SystemProperties.set(
162 PropsKeys.LIFERAY_WEB_PORTAL_DIR, portalWebDir);
163
164
165
166 _configuration = new ConfigurationImpl(
167 PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
168
169 String liferayHome = _get(PropsKeys.LIFERAY_HOME);
170
171 if (_log.isDebugEnabled()) {
172 _log.debug("Configured Liferay home " + liferayHome);
173 }
174
175 SystemProperties.set(PropsKeys.LIFERAY_HOME, liferayHome);
176
177
178
179 SystemProperties.set(
180 "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
181
182 if (GetterUtil.getBoolean(
183 SystemProperties.get("company-id-properties"))) {
184
185 _configurations = new HashMap<Long, Configuration>();
186 }
187 }
188 catch (Exception e) {
189 if (_log.isErrorEnabled()) {
190 _log.error("Unable to initialize PropsUtil", e);
191 }
192 }
193 }
194
195 private void _addProperties(Properties properties) {
196 _getConfiguration().addProperties(properties);
197 }
198
199 private void _addProperties(UnicodeProperties unicodeProperties) {
200 Properties properties = new Properties();
201
202 properties.putAll(unicodeProperties);
203
204 _addProperties(properties);
205 }
206
207 private boolean _contains(String key) {
208 return _getConfiguration().contains(key);
209 }
210
211 private String _get(String key) {
212 return _getConfiguration().get(key);
213 }
214
215 private String _get(String key, Filter filter) {
216 return _getConfiguration().get(key, filter);
217 }
218
219 private String[] _getArray(String key) {
220 return _getConfiguration().getArray(key);
221 }
222
223 private String[] _getArray(String key, Filter filter) {
224 return _getConfiguration().getArray(key, filter);
225 }
226
227 private Configuration _getConfiguration() {
228 if (_configurations == null) {
229 return _configuration;
230 }
231
232 Long companyId = CompanyThreadLocal.getCompanyId();
233
234 if (companyId > CompanyConstants.SYSTEM) {
235 Configuration configuration = _configurations.get(companyId);
236
237 if (configuration == null) {
238 configuration = new ConfigurationImpl(
239 PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
240 companyId);
241
242 _configurations.put(companyId, configuration);
243 }
244
245 return configuration;
246 }
247 else {
248 return _configuration;
249 }
250 }
251
252 private String _getDefaultLiferayHome() {
253 String defaultLiferayHome = null;
254
255 if (ServerDetector.isGeronimo()) {
256 defaultLiferayHome =
257 SystemProperties.get("org.apache.geronimo.home.dir") + "/..";
258 }
259 else if (ServerDetector.isGlassfish()) {
260 defaultLiferayHome =
261 SystemProperties.get("com.sun.aas.installRoot") + "/..";
262 }
263 else if (ServerDetector.isJBoss()) {
264 defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
265 }
266 else if (ServerDetector.isJOnAS()) {
267 defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
268 }
269 else if (ServerDetector.isWebLogic()) {
270 defaultLiferayHome =
271 SystemProperties.get("env.DOMAIN_HOME") + "/..";
272 }
273 else if (ServerDetector.isJetty()) {
274 defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
275 }
276 else if (ServerDetector.isResin()) {
277 defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
278 }
279 else if (ServerDetector.isTomcat()) {
280 defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
281 }
282 else {
283 defaultLiferayHome = SystemProperties.get("user.dir") + "/liferay";
284 }
285
286 defaultLiferayHome = StringUtil.replace(
287 defaultLiferayHome, CharPool.BACK_SLASH, CharPool.SLASH);
288
289 defaultLiferayHome = StringUtil.replace(
290 defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
291
292 if (defaultLiferayHome.endsWith("/..")) {
293 int pos = defaultLiferayHome.lastIndexOf(
294 CharPool.SLASH, defaultLiferayHome.length() - 4);
295
296 if (pos != -1) {
297 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
298 }
299 }
300
301 if (_log.isDebugEnabled()) {
302 _log.debug("Default Liferay home " + defaultLiferayHome);
303 }
304
305 return defaultLiferayHome;
306 }
307
308 private String _getLibDir(Class<?> clazz) {
309 String path = ClassUtil.getParentPath(
310 clazz.getClassLoader(), clazz.getName());
311
312 int pos = path.lastIndexOf(".jar!");
313
314 if (pos == -1) {
315 pos = path.lastIndexOf(".jar/");
316 }
317
318 pos = path.lastIndexOf(CharPool.SLASH, pos);
319
320 path = path.substring(0, pos + 1);
321
322 return path;
323 }
324
325 private Properties _getProperties() {
326 return _getConfiguration().getProperties();
327 }
328
329 private Properties _getProperties(String prefix, boolean removePrefix) {
330 return _getConfiguration().getProperties(prefix, removePrefix);
331 }
332
333 private void _removeProperties(Properties properties) {
334 _getConfiguration().removeProperties(properties);
335 }
336
337 private void _set(String key, String value) {
338 _getConfiguration().set(key, value);
339 }
340
341 private static Log _log = LogFactoryUtil.getLog(PropsUtil.class);
342
343 private static PropsUtil _instance = new PropsUtil();
344
345 private Configuration _configuration;
346 private Map<Long, Configuration> _configurations;
347
348 }