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.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  public class PropsUtil {
46  
47      public static void addProperties(Properties properties) {
48          _instance._addProperties(properties);
49      }
50  
51      public static boolean contains(String key) {
52          return _instance._contains(key);
53      }
54  
55      public static String get(String key) {
56          return _instance._get(key);
57      }
58  
59      public static String get(String key, Filter filter) {
60          return _instance._get(key, filter);
61      }
62  
63      public static String[] getArray(String key) {
64          return _instance._getArray(key);
65      }
66  
67      public static String[] getArray(String key, Filter filter) {
68          return _instance._getArray(key, filter);
69      }
70  
71      public static Properties getProperties() {
72          return _instance._getProperties();
73      }
74  
75      public static Properties getProperties(
76          String prefix, boolean removePrefix) {
77  
78          return _instance._getProperties(prefix, removePrefix);
79      }
80  
81      public static void removeProperties(Properties properties) {
82          _instance._removeProperties(properties);
83      }
84  
85      public static void set(String key, String value) {
86          _instance._set(key, value);
87      }
88  
89      private PropsUtil() {
90          SystemProperties.set(
91              PropsKeys.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(PropsKeys.LIFERAY_HOME, liferayHome);
99  
100         SystemProperties.set(
101             "ehcache.disk.store.dir", liferayHome + "/data/ehcache");
102 
103         if (GetterUtil.getBoolean(
104                 SystemProperties.get("company-id-properties"))) {
105 
106             _configurations = new HashMap<Long, Configuration>();
107         }
108     }
109 
110     private void _addProperties(Properties properties) {
111         _getConfiguration().addProperties(properties);
112     }
113 
114     private boolean _contains(String key) {
115         return _getConfiguration().contains(key);
116     }
117 
118     private String _get(String key) {
119         return _getConfiguration().get(key);
120     }
121 
122     private String _get(String key, Filter filter) {
123         return _getConfiguration().get(key, filter);
124     }
125 
126     private String[] _getArray(String key) {
127         return _getConfiguration().getArray(key);
128     }
129 
130     private String[] _getArray(String key, Filter filter) {
131         return _getConfiguration().getArray(key, filter);
132     }
133 
134     private Configuration _getConfiguration() {
135         if (_configurations == null) {
136             return _configuration;
137         }
138 
139         long companyId = CompanyThreadLocal.getCompanyId();
140 
141         if (companyId > CompanyConstants.SYSTEM) {
142             Configuration configuration = _configurations.get(companyId);
143 
144             if (configuration == null) {
145                 configuration = new ConfigurationImpl(
146                     PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
147                     companyId);
148 
149                 _configurations.put(companyId, configuration);
150             }
151 
152             return configuration;
153         }
154         else {
155             return _configuration;
156         }
157     }
158 
159     private String _getDefaultLiferayHome() {
160         String defaultLiferayHome = null;
161 
162         if (ServerDetector.isGeronimo()) {
163             defaultLiferayHome =
164                 SystemProperties.get("org.apache.geronimo.base.dir") + "/..";
165         }
166         else if (ServerDetector.isGlassfish()) {
167             defaultLiferayHome =
168                 SystemProperties.get("com.sun.aas.installRoot") + "/..";
169         }
170         else if (ServerDetector.isJBoss()) {
171             defaultLiferayHome = SystemProperties.get("jboss.home.dir") + "/..";
172         }
173         else if (ServerDetector.isJOnAS()) {
174             defaultLiferayHome = SystemProperties.get("jonas.base") + "/..";
175         }
176         else if (ServerDetector.isWebLogic()) {
177             defaultLiferayHome =
178                 SystemProperties.get("env.DOMAIN_HOME") + "/..";
179         }
180         else if (ServerDetector.isJetty()) {
181             defaultLiferayHome = SystemProperties.get("jetty.home") + "/..";
182         }
183         else if (ServerDetector.isResin()) {
184             defaultLiferayHome = SystemProperties.get("resin.home") + "/..";
185         }
186         else if (ServerDetector.isTomcat()) {
187             defaultLiferayHome = SystemProperties.get("catalina.base") + "/..";
188         }
189         else {
190             defaultLiferayHome = SystemProperties.get("user.home") + "/liferay";
191         }
192 
193         defaultLiferayHome = StringUtil.replace(
194             defaultLiferayHome, StringPool.BACK_SLASH, StringPool.SLASH);
195 
196         defaultLiferayHome = StringUtil.replace(
197             defaultLiferayHome, StringPool.DOUBLE_SLASH, StringPool.SLASH);
198 
199         if (defaultLiferayHome.endsWith("/..")) {
200             int pos = defaultLiferayHome.lastIndexOf(
201                 StringPool.SLASH, defaultLiferayHome.length() - 4);
202 
203             if (pos != -1) {
204                 defaultLiferayHome = defaultLiferayHome.substring(0, pos);
205             }
206         }
207 
208         return defaultLiferayHome;
209     }
210 
211     private Properties _getProperties() {
212         return _getConfiguration().getProperties();
213     }
214 
215     private Properties _getProperties(String prefix, boolean removePrefix) {
216         return _getConfiguration().getProperties(prefix, removePrefix);
217     }
218 
219     private void _removeProperties(Properties properties) {
220         _getConfiguration().removeProperties(properties);
221     }
222 
223     private void _set(String key, String value) {
224         _getConfiguration().set(key, value);
225     }
226 
227     private static PropsUtil _instance = new PropsUtil();
228 
229     private Configuration _configuration;
230     private Map<Long, Configuration> _configurations;
231 
232 }