001/*
002 * (C) Copyright 2015 Nuxeo SA (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 */
020package org.nuxeo.runtime.services.config;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.runtime.model.ComponentInstance;
026import org.nuxeo.runtime.model.DefaultComponent;
027
028/**
029 * @since 7.4
030 */
031public class ConfigurationServiceImpl extends DefaultComponent implements ConfigurationService {
032
033    protected static final Log log = LogFactory.getLog(ConfigurationServiceImpl.class);
034
035    public static final String CONFIGURATION_EP = "configuration";
036
037    protected ConfigurationPropertyRegistry registry = new ConfigurationPropertyRegistry();
038
039    @Override
040    public String getProperty(String key) {
041        return getProperty(key, null);
042    }
043
044    @Override
045    public String getProperty(String key, String defaultValue) {
046        if (registry.hasProperty(key)) {
047            return registry.getProperty(key);
048        }
049        return defaultValue;
050    }
051
052    @Override
053    public boolean isBooleanPropertyTrue(String key) {
054        String value = getProperty(key);
055        return Boolean.parseBoolean(value);
056    }
057
058    @Override
059    public boolean isBooleanPropertyFalse(String key) {
060        String value = getProperty(key);
061        if (StringUtils.isBlank(value)) {
062            return false;
063        }
064        return !Boolean.parseBoolean(value);
065    }
066
067    @Override
068    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
069        if (CONFIGURATION_EP.equals(extensionPoint)) {
070            registry.addContribution((ConfigurationPropertyDescriptor) contribution);
071        }
072    }
073
074    @Override
075    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
076        if (CONFIGURATION_EP.equals(extensionPoint)) {
077            registry.removeContribution((ConfigurationPropertyDescriptor) contribution);
078        }
079    }
080
081}