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