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