1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.configuration.ConfigurationImpl;
26  import com.liferay.portal.kernel.configuration.Configuration;
27  import com.liferay.portal.kernel.configuration.Filter;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.PropsKeys;
32  import com.liferay.portal.kernel.util.ServerDetector;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.model.CompanyConstants;
36  import com.liferay.portal.security.auth.CompanyThreadLocal;
37  import com.liferay.util.SystemProperties;
38  
39  import java.util.HashMap;
40  import java.util.Map;
41  import java.util.Properties;
42  
43  /**
44   * <a href="PropsUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class PropsUtil {
49  
50      public static void addProperties(Properties properties) {
51          _instance._addProperties(properties);
52      }
53  
54      public static boolean contains(String key) {
55          return _instance._contains(key);
56      }
57  
58      public static String get(String key) {
59          return _instance._get(key);
60      }
61  
62      public static String get(String key, Filter filter) {
63          return _instance._get(key, filter);
64      }
65  
66      public static String[] getArray(String key) {
67          return _instance._getArray(key);
68      }
69  
70      public static String[] getArray(String key, Filter filter) {
71          return _instance._getArray(key, filter);
72      }
73  
74      public static Properties getProperties() {
75          return _instance._getProperties();
76      }
77  
78      public static Properties getProperties(
79          String prefix, boolean removePrefix) {
80  
81          return _instance._getProperties(prefix, removePrefix);
82      }
83  
84      public static void removeProperties(Properties properties) {
85          _instance._removeProperties(properties);
86      }
87  
88      public static void set(String key, String value) {
89          _instance._set(key, value);
90      }
91  
92      private PropsUtil() {
93          try {
94              SystemProperties.set(
95                  PropsKeys.DEFAULT_LIFERAY_HOME, _getDefaultLiferayHome());
96  
97              _configuration = new ConfigurationImpl(
98                  PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
99  
100             String liferayHome = _get(PropsKeys.LIFERAY_HOME);
101 
102             SystemProperties.set(PropsKeys.LIFERAY_HOME, liferayHome);
103 
104             SystemProperties.set(
105                 "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
106 
107             if (GetterUtil.getBoolean(
108                     SystemProperties.get("company-id-properties"))) {
109 
110                 _configurations = new HashMap<Long, Configuration>();
111             }
112         }
113         catch (Exception e) {
114             if (_log.isErrorEnabled()) {
115                 _log.error("Unable to initialize PropsUtil", e);
116             }
117         }
118     }
119 
120     private void _addProperties(Properties properties) {
121         _getConfiguration().addProperties(properties);
122     }
123 
124     private boolean _contains(String key) {
125         return _getConfiguration().contains(key);
126     }
127 
128     private String _get(String key) {
129         return _getConfiguration().get(key);
130     }
131 
132     private String _get(String key, Filter filter) {
133         return _getConfiguration().get(key, filter);
134     }
135 
136     private String[] _getArray(String key) {
137         return _getConfiguration().getArray(key);
138     }
139 
140     private String[] _getArray(String key, Filter filter) {
141         return _getConfiguration().getArray(key, filter);
142     }
143 
144     private Configuration _getConfiguration() {
145         if (_configurations == null) {
146             return _configuration;
147         }
148 
149         long companyId = CompanyThreadLocal.getCompanyId();
150 
151         if (companyId > CompanyConstants.SYSTEM) {
152             Configuration configuration = _configurations.get(companyId);
153 
154             if (configuration == null) {
155                 configuration = new ConfigurationImpl(
156                     PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
157                     companyId);
158 
159                 _configurations.put(companyId, configuration);
160             }
161 
162             return configuration;
163         }
164         else {
165             return _configuration;
166         }
167     }
168 
169     private String _getDefaultLiferayHome() {
170         String defaultLiferayHome = null;
171 
172         if (ServerDetector.isGeronimo()) {
173             defaultLiferayHome =
174                 SystemProperties.get("org.apache.geronimo.base.dir") + "/..";
175         }
176         else if (ServerDetector.isGlassfish()) {
177             defaultLiferayHome =
178                 SystemProperties.get("com.sun.aas.installRoot") + "/..";
179         }
180         else if (ServerDetector.isJBoss()) {
181             defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
182         }
183         else if (ServerDetector.isJOnAS()) {
184             defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
185         }
186         else if (ServerDetector.isWebLogic()) {
187             defaultLiferayHome =
188                 SystemProperties.get("env.DOMAIN_HOME") + "/..";
189         }
190         else if (ServerDetector.isJetty()) {
191             defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
192         }
193         else if (ServerDetector.isResin()) {
194             defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
195         }
196         else if (ServerDetector.isTomcat()) {
197             defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
198         }
199         else {
200             defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
201         }
202 
203         defaultLiferayHome = StringUtil.replace(
204             defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
205 
206         defaultLiferayHome = StringUtil.replace(
207             defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
208 
209         if (defaultLiferayHome.endsWith("/..")) {
210             int pos = defaultLiferayHome.lastIndexOf(
211                 StringPool.SLASH, defaultLiferayHome.length() - 4);
212 
213             if (pos != -1) {
214                 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
215             }
216         }
217 
218         return defaultLiferayHome;
219     }
220 
221     private Properties _getProperties() {
222         return _getConfiguration().getProperties();
223     }
224 
225     private Properties _getProperties(String prefix, boolean removePrefix) {
226         return _getConfiguration().getProperties(prefix, removePrefix);
227     }
228 
229     private void _removeProperties(Properties properties) {
230         _getConfiguration().removeProperties(properties);
231     }
232 
233     private void _set(String key, String value) {
234         _getConfiguration().set(key, value);
235     }
236 
237     private static Log _log = LogFactoryUtil.getLog(PropsUtil.class);
238 
239     private static PropsUtil _instance = new PropsUtil();
240 
241     private Configuration _configuration;
242     private Map<Long, Configuration> _configurations;
243 
244 }