org.atomsphere.management.web.properties.WebSystemPropertiesService.java Source code

Java tutorial

Introduction

Here is the source code for org.atomsphere.management.web.properties.WebSystemPropertiesService.java

Source

/**
 * Atomsphere is an enterprise Test management tool.
 * Copyright (C) 2011 JatakaSource Ltd.
 *
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Atomsphere is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Atomsphere.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.atomsphere.management.web.properties;

import java.util.Locale;

import org.apache.commons.lang.LocaleUtils;
import org.apache.commons.lang.StringUtils;
import org.atomsphere.management.web.parameters.EnvParameterService;
import org.atomsphere.management.web.parameters.EnvParametersKeys;
import org.jatakasource.common.properties.SystemPlaceholderConfigurer;
import org.jatakasource.common.svc.properties.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class WebSystemPropertiesService implements SystemProperties, EnvParameterService {
    private final static String DEFAULT_DIRECTION = "ltr";
    private final static Locale DEFAULT_LOCALE = Locale.ENGLISH;

    @Autowired
    private SystemPlaceholderConfigurer paceholderConfigurer;

    private String get(String key, String defaultValue) {
        return paceholderConfigurer.get(key, defaultValue);
    }

    @Override
    public String getProperty(String key) {
        return get(key, null);
    }

    @Override
    public String getProperty(String key, String defaultValue) {
        return get(key, defaultValue);
    }

    public Integer getInteger(String key, Integer defaultValue) {
        String value = get(key, defaultValue != null ? defaultValue.toString() : null);
        if (StringUtils.isNumeric(value) && StringUtils.isNotEmpty(value)) {
            return Integer.valueOf(value);
        }

        return defaultValue;
    }

    @Override
    public Locale getLocale() {
        Locale locale = null;

        String localeStr = getProperty(
                EnvParametersKeys.class.getName() + "." + EnvParametersKeys.DEFAULT_LOCALE.name());

        if (StringUtils.isNotEmpty(localeStr)) {
            locale = LocaleUtils.toLocale(localeStr);
        }
        return locale == null ? DEFAULT_LOCALE : locale;
    }

    @Override
    public String getDirection() {
        String directionStr = getProperty(
                EnvParametersKeys.class.getName() + "." + EnvParametersKeys.DEFAULT_DIRECTION.name());

        return StringUtils.isNotEmpty(directionStr) ? directionStr : DEFAULT_DIRECTION;
    }

}