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