1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.GetterUtil;
29  import com.liferay.portal.kernel.util.ServerDetector;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.model.CompanyConstants;
33  import com.liferay.portal.security.auth.CompanyThreadLocal;
34  import com.liferay.util.SystemProperties;
35  
36  import java.util.HashMap;
37  import java.util.Map;
38  import java.util.Properties;
39  
40  /**
41   * <a href="PropsUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class PropsUtil {
47  
48      public static void addProperties(Properties properties) {
49          _instance._addProperties(properties);
50      }
51  
52      public static boolean contains(String key) {
53          return _instance._contains(key);
54      }
55  
56      public static String get(String key) {
57          return _instance._get(key);
58      }
59  
60      public static String get(String key, Filter filter) {
61          return _instance._get(key, filter);
62      }
63  
64      public static String[] getArray(String key) {
65          return _instance._getArray(key);
66      }
67  
68      public static String[] getArray(String key, Filter filter) {
69          return _instance._getArray(key, filter);
70      }
71  
72      public static Properties getProperties() {
73          return _instance._getProperties();
74      }
75  
76      public static Properties getProperties(
77          String prefix, boolean removePrefix) {
78  
79          return _instance._getProperties(prefix, removePrefix);
80      }
81  
82      public static void removeProperties(Properties properties) {
83          _instance._removeProperties(properties);
84      }
85  
86      public static void set(String key, String value) {
87          _instance._set(key, value);
88      }
89  
90      private PropsUtil() {
91          SystemProperties.set("default.liferay.home", _getDefaultLiferayHome());
92  
93          _configuration = new ConfigurationImpl(
94              PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
95  
96          String liferayHome = _get(PropsKeys.LIFERAY_HOME);
97  
98          SystemProperties.set(
99              "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
100 
101         if (GetterUtil.getBoolean(
102                 SystemProperties.get("company-id-properties"))) {
103 
104             _configurations = new HashMap<Long, Configuration>();
105         }
106     }
107 
108     private void _addProperties(Properties properties) {
109         _getConfiguration().addProperties(properties);
110     }
111 
112     private boolean _contains(String key) {
113         return _getConfiguration().contains(key);
114     }
115 
116     private String _get(String key) {
117         return _getConfiguration().get(key);
118     }
119 
120     private String _get(String key, Filter filter) {
121         return _getConfiguration().get(key, filter);
122     }
123 
124     private String[] _getArray(String key) {
125         return _getConfiguration().getArray(key);
126     }
127 
128     private String[] _getArray(String key, Filter filter) {
129         return _getConfiguration().getArray(key, filter);
130     }
131 
132     private Configuration _getConfiguration() {
133         if (_configurations == null) {
134             return _configuration;
135         }
136 
137         long companyId = CompanyThreadLocal.getCompanyId();
138 
139         if (companyId > CompanyConstants.SYSTEM) {
140             Configuration configuration = _configurations.get(companyId);
141 
142             if (configuration == null) {
143                 configuration = new ConfigurationImpl(
144                     PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
145                     companyId);
146 
147                 _configurations.put(companyId, configuration);
148             }
149 
150             return configuration;
151         }
152         else {
153             return _configuration;
154         }
155     }
156 
157     private String _getDefaultLiferayHome() {
158         String defaultLiferayHome = null;
159 
160         if (ServerDetector.isGeronimo()) {
161             defaultLiferayHome =
162                 SystemProperties.get("org.apache.geronimo.base.dir") + "/..";
163         }
164         else if (ServerDetector.isGlassfish()) {
165             defaultLiferayHome =
166                 SystemProperties.get("com.sun.aas.installRoot") + "/..";
167         }
168         else if (ServerDetector.isJBoss()) {
169             defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
170         }
171         else if (ServerDetector.isJOnAS()) {
172             defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
173         }
174         else if (ServerDetector.isWebLogic()) {
175             defaultLiferayHome =
176                 SystemProperties.get("env.DOMAIN_HOME") + "/..";
177         }
178         else if (ServerDetector.isJetty()) {
179             defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
180         }
181         else if (ServerDetector.isResin()) {
182             defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
183         }
184         else if (ServerDetector.isTomcat()) {
185             defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
186         }
187         else {
188             defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
189         }
190 
191         defaultLiferayHome = StringUtil.replace(
192             defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
193 
194         defaultLiferayHome = StringUtil.replace(
195             defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
196 
197         if (defaultLiferayHome.endsWith("/..")) {
198             int pos = defaultLiferayHome.lastIndexOf(
199                 StringPool.SLASH, defaultLiferayHome.length() - 4);
200 
201             if (pos != -1) {
202                 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
203             }
204         }
205 
206         return defaultLiferayHome;
207     }
208 
209     private Properties _getProperties() {
210         return _getConfiguration().getProperties();
211     }
212 
213     private Properties _getProperties(String prefix, boolean removePrefix) {
214         return _getConfiguration().getProperties(prefix, removePrefix);
215     }
216 
217     private void _removeProperties(Properties properties) {
218         _getConfiguration().removeProperties(properties);
219     }
220 
221     private void _set(String key, String value) {
222         _getConfiguration().set(key, value);
223     }
224 
225     private static PropsUtil _instance = new PropsUtil();
226 
227     private Configuration _configuration;
228     private Map<Long, Configuration> _configurations;
229 
230 }