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.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  public class PropsUtil {
43  
44      public static void addProperties(Properties properties) {
45          _instance._addProperties(properties);
46      }
47  
48      public static boolean contains(String key) {
49          return _instance._contains(key);
50      }
51  
52      public static String get(String key) {
53          return _instance._get(key);
54      }
55  
56      public static String get(String key, Filter filter) {
57          return _instance._get(key, filter);
58      }
59  
60      public static String[] getArray(String key) {
61          return _instance._getArray(key);
62      }
63  
64      public static String[] getArray(String key, Filter filter) {
65          return _instance._getArray(key, filter);
66      }
67  
68      public static Properties getProperties() {
69          return _instance._getProperties();
70      }
71  
72      public static Properties getProperties(
73          String prefix, boolean removePrefix) {
74  
75          return _instance._getProperties(prefix, removePrefix);
76      }
77  
78      public static void removeProperties(Properties properties) {
79          _instance._removeProperties(properties);
80      }
81  
82      public static void set(String key, String value) {
83          _instance._set(key, value);
84      }
85  
86      private PropsUtil() {
87          _configuration = new ConfigurationImpl(
88              PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
89  
90          // Set the portal property "resource.repositories.root" and
91          // "ehcache.disk.store.dir" as a system property as well so it can be
92          // referenced by Ehcache. The property "resource.repositories.root" is
93          // deprecated. Please use "ehcache.disk.store.dir" instead.
94  
95          SystemProperties.set(
96              PropsKeys.RESOURCE_REPOSITORIES_ROOT,
97              _get(PropsKeys.RESOURCE_REPOSITORIES_ROOT));
98  
99          SystemProperties.set(
100             "ehcache.disk.store.dir",
101             _get(PropsKeys.RESOURCE_REPOSITORIES_ROOT));
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 Properties _getProperties() {
160         return _getConfiguration().getProperties();
161     }
162 
163     private Properties _getProperties(String prefix, boolean removePrefix) {
164         return _getConfiguration().getProperties(prefix, removePrefix);
165     }
166 
167     private void _removeProperties(Properties properties) {
168         _getConfiguration().removeProperties(properties);
169     }
170 
171     private void _set(String key, String value) {
172         _getConfiguration().set(key, value);
173     }
174 
175     private static PropsUtil _instance = new PropsUtil();
176 
177     private Configuration _configuration;
178     private Map<Long, Configuration> _configurations;
179 
180 }