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.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          _configuration = new ConfigurationImpl(
89              PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
90  
91          // Set the portal property "resource.repositories.root" and
92          // "ehcache.disk.store.dir" as a system property as well so it can be
93          // referenced by Ehcache. The property "resource.repositories.root" is
94          // deprecated. Please use "ehcache.disk.store.dir" instead.
95  
96          SystemProperties.set(
97              PropsKeys.RESOURCE_REPOSITORIES_ROOT,
98              _get(PropsKeys.RESOURCE_REPOSITORIES_ROOT));
99  
100         SystemProperties.set(
101             "ehcache.disk.store.dir",
102             _get(PropsKeys.RESOURCE_REPOSITORIES_ROOT));
103 
104         if (GetterUtil.getBoolean(
105                 SystemProperties.get("company-id-properties"))) {
106 
107             _configurations = new HashMap<Long, Configuration>();
108         }
109     }
110 
111     private void _addProperties(Properties properties) {
112         _getConfiguration().addProperties(properties);
113     }
114 
115     private boolean _contains(String key) {
116         return _getConfiguration().contains(key);
117     }
118 
119     private String _get(String key) {
120         return _getConfiguration().get(key);
121     }
122 
123     private String _get(String key, Filter filter) {
124         return _getConfiguration().get(key, filter);
125     }
126 
127     private String[] _getArray(String key) {
128         return _getConfiguration().getArray(key);
129     }
130 
131     private String[] _getArray(String key, Filter filter) {
132         return _getConfiguration().getArray(key, filter);
133     }
134 
135     private Configuration _getConfiguration() {
136         if (_configurations == null) {
137             return _configuration;
138         }
139 
140         long companyId = CompanyThreadLocal.getCompanyId();
141 
142         if (companyId > CompanyConstants.SYSTEM) {
143             Configuration configuration = _configurations.get(companyId);
144 
145             if (configuration == null) {
146                 configuration = new ConfigurationImpl(
147                     PropsUtil.class.getClassLoader(), PropsFiles.PORTAL,
148                     companyId);
149 
150                 _configurations.put(companyId, configuration);
151             }
152 
153             return configuration;
154         }
155         else {
156             return _configuration;
157         }
158     }
159 
160     private Properties _getProperties() {
161         return _getConfiguration().getProperties();
162     }
163 
164     private Properties _getProperties(String prefix, boolean removePrefix) {
165         return _getConfiguration().getProperties(prefix, removePrefix);
166     }
167 
168     private void _removeProperties(Properties properties) {
169         _getConfiguration().removeProperties(properties);
170     }
171 
172     private void _set(String key, String value) {
173         _getConfiguration().set(key, value);
174     }
175 
176     private static PropsUtil _instance = new PropsUtil();
177 
178     private Configuration _configuration;
179     private Map<Long, Configuration> _configurations;
180 
181 }