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