1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.configuration.ConfigurationImpl;
18  import com.liferay.portal.kernel.configuration.Configuration;
19  import com.liferay.portal.kernel.configuration.Filter;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.PropsKeys;
24  import com.liferay.portal.kernel.util.ServerDetector;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.model.CompanyConstants;
28  import com.liferay.portal.security.auth.CompanyThreadLocal;
29  import com.liferay.util.SystemProperties;
30  
31  import java.util.HashMap;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  /**
36   * <a href="PropsUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class PropsUtil {
41  
42      public static void addProperties(Properties properties) {
43          _instance._addProperties(properties);
44      }
45  
46      public static boolean contains(String key) {
47          return _instance._contains(key);
48      }
49  
50      public static String get(String key) {
51          return _instance._get(key);
52      }
53  
54      public static String get(String key, Filter filter) {
55          return _instance._get(key, filter);
56      }
57  
58      public static String[] getArray(String key) {
59          return _instance._getArray(key);
60      }
61  
62      public static String[] getArray(String key, Filter filter) {
63          return _instance._getArray(key, filter);
64      }
65  
66      public static Properties getProperties() {
67          return _instance._getProperties();
68      }
69  
70      public static Properties getProperties(
71          String prefix, boolean removePrefix) {
72  
73          return _instance._getProperties(prefix, removePrefix);
74      }
75  
76      public static void removeProperties(Properties properties) {
77          _instance._removeProperties(properties);
78      }
79  
80      public static void set(String key, String value) {
81          _instance._set(key, value);
82      }
83  
84      private PropsUtil() {
85          try {
86              SystemProperties.set(
87                  PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
88  
89              _configuration = new ConfigurationImpl(
90                  PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
91  
92              String liferayHome = _get(PropsKeys.LIFERAY_HOME);
93  
94              SystemProperties.set(PropsKeys.LIFERAY_HOME, liferayHome);
95  
96              SystemProperties.set(
97                  "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
98  
99              if (GetterUtil.getBoolean(
100                     SystemProperties.get("company-id-properties"))) {
101 
102                 _configurations = new HashMap<Long, Configuration>();
103             }
104         }
105         catch (Exception e) {
106             if (_log.isErrorEnabled()) {
107                 _log.error("Unable to initialize PropsUtil", e);
108             }
109         }
110     }
111 
112     private void _addProperties(Properties properties) {
113         _getConfiguration().addProperties(properties);
114     }
115 
116     private boolean _contains(String key) {
117         return _getConfiguration().contains(key);
118     }
119 
120     private String _get(String key) {
121         return _getConfiguration().get(key);
122     }
123 
124     private String _get(String key, Filter filter) {
125         return _getConfiguration().get(key, filter);
126     }
127 
128     private String[] _getArray(String key) {
129         return _getConfiguration().getArray(key);
130     }
131 
132     private String[] _getArray(String key, Filter filter) {
133         return _getConfiguration().getArray(key, filter);
134     }
135 
136     private Configuration _getConfiguration() {
137         if (_configurations == null) {
138             return _configuration;
139         }
140 
141         long companyId = CompanyThreadLocal.getCompanyId();
142 
143         if (companyId > CompanyConstants.SYSTEM) {
144             Configuration configuration = _configurations.get(companyId);
145 
146             if (configuration == null) {
147                 configuration = new ConfigurationImpl(
148                     PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
149                     companyId);
150 
151                 _configurations.put(companyId, configuration);
152             }
153 
154             return configuration;
155         }
156         else {
157             return _configuration;
158         }
159     }
160 
161     private String _getDefaultLiferayHome() {
162         String defaultLiferayHome = null;
163 
164         if (ServerDetector.isGeronimo()) {
165             defaultLiferayHome =
166                 SystemProperties.get("org.apache.geronimo.home.dir") + "/..";
167         }
168         else if (ServerDetector.isGlassfish()) {
169             defaultLiferayHome =
170                 SystemProperties.get("com.sun.aas.installRoot") + "/..";
171         }
172         else if (ServerDetector.isJBoss()) {
173             defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
174         }
175         else if (ServerDetector.isJOnAS()) {
176             defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
177         }
178         else if (ServerDetector.isWebLogic()) {
179             defaultLiferayHome =
180                 SystemProperties.get("env.DOMAIN_HOME") + "/..";
181         }
182         else if (ServerDetector.isJetty()) {
183             defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
184         }
185         else if (ServerDetector.isResin()) {
186             defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
187         }
188         else if (ServerDetector.isTomcat()) {
189             defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
190         }
191         else {
192             defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
193         }
194 
195         defaultLiferayHome = StringUtil.replace(
196             defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
197 
198         defaultLiferayHome = StringUtil.replace(
199             defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
200 
201         if (defaultLiferayHome.endsWith("/..")) {
202             int pos = defaultLiferayHome.lastIndexOf(
203                 StringPool.SLASH, defaultLiferayHome.length() - 4);
204 
205             if (pos != -1) {
206                 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
207             }
208         }
209 
210         return defaultLiferayHome;
211     }
212 
213     private Properties _getProperties() {
214         return _getConfiguration().getProperties();
215     }
216 
217     private Properties _getProperties(String prefix, boolean removePrefix) {
218         return _getConfiguration().getProperties(prefix, removePrefix);
219     }
220 
221     private void _removeProperties(Properties properties) {
222         _getConfiguration().removeProperties(properties);
223     }
224 
225     private void _set(String key, String value) {
226         _getConfiguration().set(key, value);
227     }
228 
229     private static Log _log = LogFactoryUtil.getLog(PropsUtil.class);
230 
231     private static PropsUtil _instance = new PropsUtil();
232 
233     private Configuration _configuration;
234     private Map<Long, Configuration> _configurations;
235 
236 }