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.GetterUtil;
23  import com.liferay.portal.kernel.util.PropsKeys;
24  import com.liferay.portal.model.CompanyConstants;
25  import com.liferay.portal.security.auth.CompanyThreadLocal;
26  import com.liferay.util.SystemProperties;
27  
28  import java.util.HashMap;
29  import java.util.Map;
30  import java.util.Properties;
31  
32  /**
33   * <a href="PropsUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class PropsUtil {
38  
39      public static void addProperties(Properties properties) {
40          _instance._addProperties(properties);
41      }
42  
43      public static boolean contains(String key) {
44          return _instance._contains(key);
45      }
46  
47      public static String get(String key) {
48          return _instance._get(key);
49      }
50  
51      public static String get(String key, Filter filter) {
52          return _instance._get(key, filter);
53      }
54  
55      public static String[] getArray(String key) {
56          return _instance._getArray(key);
57      }
58  
59      public static String[] getArray(String key, Filter filter) {
60          return _instance._getArray(key, filter);
61      }
62  
63      public static Properties getProperties() {
64          return _instance._getProperties();
65      }
66  
67      public static Properties getProperties(
68          String prefix, boolean removePrefix) {
69  
70          return _instance._getProperties(prefix, removePrefix);
71      }
72  
73      public static void removeProperties(Properties properties) {
74          _instance._removeProperties(properties);
75      }
76  
77      public static void set(String key, String value) {
78          _instance._set(key, value);
79      }
80  
81      private PropsUtil() {
82          try {
83              _configuration = new ConfigurationImpl(
84                  PropsUtil.class.getClassLoader(), PropsFiles.PORTAL);
85  
86              // Set the portal property "resource.repositories.root" and
87              // "ehcache.disk.store.dir" as a system property as well so it can
88              // be referenced by Ehcache. The property
89              // "resource.repositories.root" is deprecated. Please use
90              // "ehcache.disk.store.dir" instead.
91  
92              SystemProperties.set(
93                  PropsKeys.RESOURCE_REPOSITORIES_ROOT,
94                  _get(PropsKeys.RESOURCE_REPOSITORIES_ROOT));
95  
96              SystemProperties.set(
97                  "ehcache.disk.store.dir",
98                  _get(PropsKeys.RESOURCE_REPOSITORIES_ROOT));
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 Properties _getProperties() {
163         return _getConfiguration().getProperties();
164     }
165 
166     private Properties _getProperties(String prefix, boolean removePrefix) {
167         return _getConfiguration().getProperties(prefix, removePrefix);
168     }
169 
170     private void _removeProperties(Properties properties) {
171         _getConfiguration().removeProperties(properties);
172     }
173 
174     private void _set(String key, String value) {
175         _getConfiguration().set(key, value);
176     }
177 
178     private static Log _log = LogFactoryUtil.getLog(PropsUtil.class);
179 
180     private static PropsUtil _instance = new PropsUtil();
181 
182     private Configuration _configuration;
183     private Map<Long, Configuration> _configurations;
184 
185 }