001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.configuration;
016    
017    import com.germinus.easyconf.ComponentConfiguration;
018    import com.germinus.easyconf.ComponentProperties;
019    
020    import com.liferay.portal.configuration.easyconf.ClassLoaderAggregateProperties;
021    import com.liferay.portal.configuration.easyconf.ClassLoaderComponentConfiguration;
022    import com.liferay.portal.kernel.configuration.Filter;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.ArrayUtil;
026    import com.liferay.portal.kernel.util.PropertiesUtil;
027    import com.liferay.portal.kernel.util.StringBundler;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Company;
030    import com.liferay.portal.model.CompanyConstants;
031    import com.liferay.portal.service.CompanyLocalServiceUtil;
032    
033    import java.lang.reflect.Field;
034    
035    import java.util.HashSet;
036    import java.util.Iterator;
037    import java.util.List;
038    import java.util.Map;
039    import java.util.Properties;
040    import java.util.Set;
041    import java.util.concurrent.ConcurrentHashMap;
042    
043    import org.apache.commons.configuration.CompositeConfiguration;
044    import org.apache.commons.configuration.Configuration;
045    import org.apache.commons.configuration.MapConfiguration;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Shuyang Zhou
050     */
051    public class ConfigurationImpl
052            implements com.liferay.portal.kernel.configuration.Configuration {
053    
054            public ConfigurationImpl(ClassLoader classLoader, String name) {
055                    this(classLoader, name, CompanyConstants.SYSTEM);
056            }
057    
058            public ConfigurationImpl(
059                    ClassLoader classLoader, String name, long companyId) {
060    
061                    String webId = null;
062    
063                    if (companyId > CompanyConstants.SYSTEM) {
064                            try {
065                                    Company company = CompanyLocalServiceUtil.getCompanyById(
066                                            companyId);
067    
068                                    webId = company.getWebId();
069                            }
070                            catch (Exception e) {
071                                    _log.error(e, e);
072                            }
073                    }
074    
075                    _componentConfiguration = new ClassLoaderComponentConfiguration(
076                            classLoader, webId, name);
077    
078                    printSources(companyId, webId);
079            }
080    
081            @Override
082            public void addProperties(Properties properties) {
083                    try {
084                            ComponentProperties componentProperties = getComponentProperties();
085    
086                            ClassLoaderAggregateProperties classLoaderAggregateProperties =
087                                    (ClassLoaderAggregateProperties)
088                                            componentProperties.toConfiguration();
089    
090                            Field field1 = CompositeConfiguration.class.getDeclaredField(
091                                    "configList");
092    
093                            field1.setAccessible(true);
094    
095                            // Add to configList of base conf
096    
097                            List<Configuration> configurations =
098                                    (List<Configuration>)field1.get(classLoaderAggregateProperties);
099    
100                            MapConfiguration newConfiguration = new MapConfiguration(
101                                    properties);
102    
103                            configurations.add(0, newConfiguration);
104    
105                            // Add to configList of AggregatedProperties itself
106    
107                            CompositeConfiguration compositeConfiguration =
108                                    classLoaderAggregateProperties.getBaseConfiguration();
109    
110                            configurations = (List<Configuration>)field1.get(
111                                    compositeConfiguration);
112    
113                            configurations.add(0, newConfiguration);
114    
115                            _properties = null;
116    
117                            clearCache();
118                    }
119                    catch (Exception e) {
120                            _log.error("The properties could not be added", e);
121                    }
122            }
123    
124            @Override
125            public void clearCache() {
126                    _values.clear();
127    
128                    _properties = null;
129            }
130    
131            @Override
132            public boolean contains(String key) {
133                    Object value = _values.get(key);
134    
135                    if (value == null) {
136                            ComponentProperties componentProperties = getComponentProperties();
137    
138                            value = componentProperties.getProperty(key);
139    
140                            if (value == null) {
141                                    value = _nullValue;
142                            }
143    
144                            _values.put(key, value);
145                    }
146    
147                    if (value == _nullValue) {
148                            return false;
149                    }
150    
151                    return true;
152            }
153    
154            @Override
155            public String get(String key) {
156                    Object value = _values.get(key);
157    
158                    if (value == null) {
159                            ComponentProperties componentProperties = getComponentProperties();
160    
161                            value = componentProperties.getString(key);
162    
163                            if (value == null) {
164                                    value = _nullValue;
165                            }
166    
167                            _values.put(key, value);
168                    }
169                    else if (_PRINT_DUPLICATE_CALLS_TO_GET) {
170                            System.out.println("Duplicate call to get " + key);
171                    }
172    
173                    if (value instanceof String) {
174                            return (String)value;
175                    }
176    
177                    return null;
178            }
179    
180            @Override
181            public String get(String key, Filter filter) {
182                    String filterCacheKey = buildFilterCacheKey(key, filter, false);
183    
184                    Object value = null;
185    
186                    if (filterCacheKey != null) {
187                            value = _values.get(filterCacheKey);
188                    }
189    
190                    if (value == null) {
191                            ComponentProperties componentProperties = getComponentProperties();
192    
193                            value = componentProperties.getString(
194                                    key, getEasyConfFilter(filter));
195    
196                            if (filterCacheKey != null) {
197                                    if (value == null) {
198                                            value = _nullValue;
199                                    }
200    
201                                    _values.put(filterCacheKey, value);
202                            }
203                    }
204    
205                    if (value instanceof String) {
206                            return (String)value;
207                    }
208    
209                    return null;
210            }
211    
212            @Override
213            public String[] getArray(String key) {
214                    String cacheKey = _ARRAY_KEY_PREFIX.concat(key);
215    
216                    Object value = _values.get(cacheKey);
217    
218                    if (value == null) {
219                            ComponentProperties componentProperties = getComponentProperties();
220    
221                            String[] array = componentProperties.getStringArray(key);
222    
223                            value = fixArrayValue(cacheKey, array);
224                    }
225    
226                    if (value instanceof String[]) {
227                            return (String[])value;
228                    }
229    
230                    return _emptyArray;
231            }
232    
233            @Override
234            public String[] getArray(String key, Filter filter) {
235                    String filterCacheKey = buildFilterCacheKey(key, filter, true);
236    
237                    Object value = null;
238    
239                    if (filterCacheKey != null) {
240                            value = _values.get(filterCacheKey);
241                    }
242    
243                    if (value == null) {
244                            ComponentProperties componentProperties = getComponentProperties();
245    
246                            String[] array = componentProperties.getStringArray(
247                                    key, getEasyConfFilter(filter));
248    
249                            value = fixArrayValue(filterCacheKey, array);
250                    }
251    
252                    if (value instanceof String[]) {
253                            return (String[])value;
254                    }
255    
256                    return _emptyArray;
257            }
258    
259            @Override
260            public Properties getProperties() {
261                    if (_properties != null) {
262                            return _properties;
263                    }
264    
265                    // For some strange reason, componentProperties.getProperties() returns
266                    // values with spaces after commas. So a property setting of "xyz=1,2,3"
267                    // actually returns "xyz=1, 2, 3". This can break applications that
268                    // don't expect that extra space. However, getting the property value
269                    // directly through componentProperties returns the correct value. This
270                    // method fixes the weird behavior by returning properties with the
271                    // correct values.
272    
273                    _properties = new Properties();
274    
275                    ComponentProperties componentProperties = getComponentProperties();
276    
277                    Properties componentPropertiesProperties =
278                            componentProperties.getProperties();
279    
280                    for (String key : componentPropertiesProperties.stringPropertyNames()) {
281                            _properties.setProperty(key, componentProperties.getString(key));
282                    }
283    
284                    return _properties;
285            }
286    
287            @Override
288            public Properties getProperties(String prefix, boolean removePrefix) {
289                    Properties properties = getProperties();
290    
291                    return PropertiesUtil.getProperties(properties, prefix, removePrefix);
292            }
293    
294            @Override
295            public void removeProperties(Properties properties) {
296                    try {
297                            ComponentProperties componentProperties = getComponentProperties();
298    
299                            ClassLoaderAggregateProperties classLoaderAggregateProperties =
300                                    (ClassLoaderAggregateProperties)
301                                            componentProperties.toConfiguration();
302    
303                            CompositeConfiguration compositeConfiguration =
304                                    classLoaderAggregateProperties.getBaseConfiguration();
305    
306                            Field field2 = CompositeConfiguration.class.getDeclaredField(
307                                    "configList");
308    
309                            field2.setAccessible(true);
310    
311                            @SuppressWarnings("unchecked")
312                            List<Configuration> configurations =
313                                    (List<Configuration>)field2.get(compositeConfiguration);
314    
315                            Iterator<Configuration> itr = configurations.iterator();
316    
317                            while (itr.hasNext()) {
318                                    Configuration configuration = itr.next();
319    
320                                    if (!(configuration instanceof MapConfiguration)) {
321                                            break;
322                                    }
323    
324                                    MapConfiguration mapConfiguration =
325                                            (MapConfiguration)configuration;
326    
327                                    if (mapConfiguration.getMap() == properties) {
328                                            itr.remove();
329    
330                                            classLoaderAggregateProperties.removeConfiguration(
331                                                    configuration);
332                                    }
333                            }
334    
335                            _properties = null;
336    
337                            clearCache();
338                    }
339                    catch (Exception e) {
340                            _log.error("The properties could not be removed", e);
341                    }
342            }
343    
344            @Override
345            public void set(String key, String value) {
346                    ComponentProperties componentProperties = getComponentProperties();
347    
348                    componentProperties.setProperty(key, value);
349    
350                    _values.put(key, value);
351            }
352    
353            protected String buildFilterCacheKey(
354                    String key, Filter filter, boolean arrayValue) {
355    
356                    if (filter.getVariables() != null) {
357                            return null;
358                    }
359    
360                    String[] selectors = filter.getSelectors();
361    
362                    int length = 0;
363    
364                    if (arrayValue) {
365                            length = selectors.length + 2;
366                    }
367                    else {
368                            length = selectors.length + 1;
369                    }
370    
371                    StringBundler sb = new StringBundler(length);
372    
373                    if (arrayValue) {
374                            sb.append(_ARRAY_KEY_PREFIX);
375                    }
376    
377                    sb.append(key);
378                    sb.append(selectors);
379    
380                    return sb.toString();
381            }
382    
383            protected Object fixArrayValue(String cacheKey, String[] array) {
384                    if (cacheKey == null) {
385                            return array;
386                    }
387    
388                    Object value = _nullValue;
389    
390                    if (ArrayUtil.isNotEmpty(array)) {
391    
392                            // Commons Configuration parses an empty property into a String
393                            // array with one String containing one space. It also leaves a
394                            // trailing array member if you set a property in more than one
395                            // line.
396    
397                            if (Validator.isNull(array[array.length - 1])) {
398                                    String[] subArray = new String[array.length - 1];
399    
400                                    System.arraycopy(array, 0, subArray, 0, subArray.length);
401    
402                                    array = subArray;
403                            }
404    
405                            if (array.length > 0) {
406                                    value = array;
407                            }
408                    }
409    
410                    _values.put(cacheKey, value);
411    
412                    return value;
413            }
414    
415            protected ComponentProperties getComponentProperties() {
416                    return _componentConfiguration.getProperties();
417            }
418    
419            protected com.germinus.easyconf.Filter getEasyConfFilter(Filter filter) {
420                    com.germinus.easyconf.Filter easyConfFilter =
421                            com.germinus.easyconf.Filter.by(filter.getSelectors());
422    
423                    if (filter.getVariables() != null) {
424                            easyConfFilter.setVariables(filter.getVariables());
425                    }
426    
427                    return easyConfFilter;
428            }
429    
430            protected void printSources(long companyId, String webId) {
431                    ComponentProperties componentProperties = getComponentProperties();
432    
433                    List<String> sources = componentProperties.getLoadedSources();
434    
435                    for (int i = sources.size() - 1; i >= 0; i--) {
436                            String source = sources.get(i);
437    
438                            if (_printedSources.contains(source)) {
439                                    continue;
440                            }
441    
442                            _printedSources.add(source);
443    
444                            String info = "Loading " + source;
445    
446                            if (companyId > CompanyConstants.SYSTEM) {
447                                    info +=
448                                            " for {companyId=" + companyId + ", webId=" + webId + "}";
449                            }
450    
451                            System.out.println(info);
452                    }
453            }
454    
455            private static final String _ARRAY_KEY_PREFIX = "ARRAY_";
456    
457            private static final boolean _PRINT_DUPLICATE_CALLS_TO_GET = false;
458    
459            private static final Log _log = LogFactoryUtil.getLog(
460                    ConfigurationImpl.class);
461    
462            private static final String[] _emptyArray = new String[0];
463            private static final Object _nullValue = new Object();
464    
465            private final ComponentConfiguration _componentConfiguration;
466            private final Set<String> _printedSources = new HashSet<>();
467            private Properties _properties;
468            private final Map<String, Object> _values = new ConcurrentHashMap<>();
469    
470    }