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 java.util.HashMap;
024import java.util.Map;
025
026import org.apache.commons.lang3.StringUtils;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.runtime.api.Framework;
030import org.nuxeo.runtime.logging.DeprecationLogger;
031import org.nuxeo.runtime.model.SimpleContributionRegistry;
032
033/**
034 * Registry for JSF configuration contributions.
035 *
036 * @since 7.4
037 */
038public class ConfigurationPropertyRegistry extends SimpleContributionRegistry<ConfigurationPropertyDescriptor> {
039
040    private static final Log log = LogFactory.getLog(ConfigurationPropertyRegistry.class);
041
042    protected Map<String, String> properties = new HashMap<>();
043
044    @Override
045    public String getContributionId(ConfigurationPropertyDescriptor contrib) {
046        return contrib.getName();
047    }
048
049    @Override
050    public void contributionUpdated(String key, ConfigurationPropertyDescriptor contrib,
051            ConfigurationPropertyDescriptor newOrigContrib) {
052        String name = contrib.getName();
053        if (StringUtils.isEmpty(name)) {
054            log.error("Cannot register configuration property with an empty name");
055            return;
056        }
057        if (Framework.getProperties().containsKey(key)) {
058            String message = "Property '" + key + "' should now be contributed to extension "
059                    + "point 'org.nuxeo.runtime.ConfigurationService', using target 'configuration'";
060            DeprecationLogger.log(message, "7.4");
061            Framework.getRuntime().getMessageHandler().addWarning(message);
062        }
063        String value = contrib.getValue();
064        properties.put(name, value);
065        log.info("Registered property with name " + name + " and value " + value);
066    }
067
068    @Override
069    public void contributionRemoved(String id, ConfigurationPropertyDescriptor origContrib) {
070        properties.remove(id);
071        log.info("Unregistered property with name " + id);
072    }
073
074    @Override
075    public ConfigurationPropertyDescriptor clone(ConfigurationPropertyDescriptor orig) {
076        return orig.clone();
077    }
078
079    @Override
080    public void merge(ConfigurationPropertyDescriptor src, ConfigurationPropertyDescriptor dst) {
081        dst.merge(src);
082    }
083
084    @Override
085    public boolean isSupportingMerge() {
086        return true;
087    }
088
089    public boolean hasProperty(String key) {
090        return properties.containsKey(key);
091    }
092
093    public String getProperty(String key) {
094        return properties.get(key);
095    }
096}