001/*
002 * (C) Copyright 2015-2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *      Andre Justo
018 *      Anahide Tchertchian
019 *      Kevin Leturc <kleturc@nuxeo.com>
020 */
021package org.nuxeo.runtime.services.config;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.runtime.model.ComponentInstance;
027import org.nuxeo.runtime.model.DefaultComponent;
028
029/**
030 * @since 7.4
031 */
032public class ConfigurationServiceImpl extends DefaultComponent implements ConfigurationService {
033
034    protected static final Log log = LogFactory.getLog(ConfigurationServiceImpl.class);
035
036    public static final String CONFIGURATION_EP = "configuration";
037
038    protected ConfigurationPropertyRegistry registry = new ConfigurationPropertyRegistry();
039
040    @Override
041    public String getProperty(String key) {
042        return getProperty(key, null);
043    }
044
045    @Override
046    public String getProperty(String key, String defaultValue) {
047        if (registry.hasProperty(key)) {
048            return registry.getProperty(key);
049        }
050        return defaultValue;
051    }
052
053    @Override
054    public boolean isBooleanPropertyTrue(String key) {
055        String value = getProperty(key);
056        return Boolean.parseBoolean(value);
057    }
058
059    @Override
060    public boolean isBooleanPropertyFalse(String key) {
061        String value = getProperty(key);
062        return StringUtils.isNotBlank(value) && !Boolean.parseBoolean(value);
063    }
064
065    @Override
066    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
067        if (CONFIGURATION_EP.equals(extensionPoint)) {
068            registry.addContribution((ConfigurationPropertyDescriptor) contribution);
069        }
070    }
071
072    @Override
073    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
074        if (CONFIGURATION_EP.equals(extensionPoint)) {
075            registry.removeContribution((ConfigurationPropertyDescriptor) contribution);
076        }
077    }
078
079}