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.kernel.settings;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    
020    import java.lang.reflect.InvocationTargetException;
021    import java.lang.reflect.Method;
022    
023    /**
024     * @author Iv??n Zaera
025     */
026    public class ConfigurationBeanSettings
027            extends BaseSettings implements Settings {
028    
029            public ConfigurationBeanSettings(
030                    LocationVariableResolver locationVariableResolver,
031                    Object configurationBean, Settings parentSettings) {
032    
033                    super(parentSettings);
034    
035                    _locationVariableResolver = locationVariableResolver;
036                    _configurationBean = configurationBean;
037            }
038    
039            @Override
040            protected String doGetValue(String key) {
041                    Object object = _getProperty(key);
042    
043                    if (object == null) {
044                            return null;
045                    }
046    
047                    String value = null;
048    
049                    if (object instanceof LocalizedValuesMap) {
050                            value = ((LocalizedValuesMap)object).getDefaultValue();
051                    }
052                    else {
053                            value = object.toString();
054                    }
055    
056                    if (_locationVariableResolver.isLocationVariable(value)) {
057                            return _locationVariableResolver.resolve(value);
058                    }
059    
060                    return value;
061            }
062    
063            @Override
064            protected String[] doGetValues(String key) {
065                    Object object = _getProperty(key);
066    
067                    if (object == null) {
068                            return null;
069                    }
070    
071                    return GetterUtil.getStringValues(object);
072            }
073    
074            private Object _getProperty(String key) {
075                    if (_configurationBean == null) {
076                            return null;
077                    }
078    
079                    Class<?> clazz = _configurationBean.getClass();
080    
081                    try {
082                            Method method = clazz.getMethod(key);
083    
084                            return method.invoke(_configurationBean);
085                    }
086                    catch (NoSuchMethodException nsme) {
087                            return null;
088                    }
089                    catch (InvocationTargetException ite) {
090                            throw new SystemException("Unable to read property " + key, ite);
091                    }
092                    catch (IllegalAccessException iae) {
093                            throw new SystemException("Unable to read property " + key, iae);
094                    }
095            }
096    
097            private final Object _configurationBean;
098            private final LocationVariableResolver _locationVariableResolver;
099    
100    }