001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *      Andre Justo
016 *      Anahide Tchertchian
017 */
018package org.nuxeo.runtime.services.config;
019
020import org.apache.commons.lang.StringUtils;
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.runtime.api.Framework;
024import org.nuxeo.runtime.model.ComponentInstance;
025import org.nuxeo.runtime.model.DefaultComponent;
026
027/**
028 * @since 7.4
029 */
030public class ConfigurationServiceImpl extends DefaultComponent implements ConfigurationService {
031
032    protected static final Log log = LogFactory.getLog(ConfigurationServiceImpl.class);
033
034    public static final String CONFIGURATION_EP = "configuration";
035
036    protected ConfigurationPropertyRegistry registry = new ConfigurationPropertyRegistry();
037
038    @Override
039    public String getProperty(String key) {
040        return getProperty(key, null);
041    }
042
043    @Override
044    public String getProperty(String key, String defaultValue) {
045        if (registry.hasProperty(key)) {
046            return registry.getProperty(key);
047        }
048        return defaultValue;
049    }
050
051    @Override
052    public boolean isBooleanPropertyTrue(String key) {
053        String value = getProperty(key);
054        return Boolean.parseBoolean(value);
055    }
056
057    @Override
058    public boolean isBooleanPropertyFalse(String key) {
059        String value = getProperty(key);
060        if (StringUtils.isBlank(value)) {
061            return false;
062        }
063        return !Boolean.parseBoolean(value);
064    }
065
066    @Override
067    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
068        if (CONFIGURATION_EP.equals(extensionPoint)) {
069            registry.addContribution((ConfigurationPropertyDescriptor) contribution);
070        }
071    }
072
073    @Override
074    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
075        if (CONFIGURATION_EP.equals(extensionPoint)) {
076            registry.removeContribution((ConfigurationPropertyDescriptor) contribution);
077        }
078    }
079
080}