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