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