org.sonar.server.settings.ChildSettings Maven / Gradle / Ivy
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.settings;
import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.Settings;
import org.sonar.api.config.internal.ConfigurationBridge;
import static java.util.Objects.requireNonNull;
public class ChildSettings extends Settings {
private final Settings parentSettings;
private final Map localProperties = new HashMap<>();
public ChildSettings(Settings parentSettings) {
super(parentSettings.getDefinitions(), parentSettings.getEncryption());
this.parentSettings = parentSettings;
}
@Override
protected Optional get(String key) {
String value = localProperties.get(key);
if (value != null) {
return Optional.of(value);
}
return parentSettings.getRawString(key);
}
@Override
protected void set(String key, String value) {
localProperties.put(
requireNonNull(key, "key can't be null"),
requireNonNull(value, "value can't be null").trim());
}
@Override
protected void remove(String key) {
localProperties.remove(key);
}
/**
* Only returns the currently loaded properties.
*
*
* On the Web Server, global properties are loaded lazily when requested by name. Therefor,
* this will return only global properties which have been requested using
* {@link #get(String)} at least once prior to this call.
*/
@Override
public Map getProperties() {
// order is important. local properties override parent properties.
ImmutableMap.Builder builder = ImmutableMap.builder();
builder.putAll(parentSettings.getProperties());
builder.putAll(localProperties);
return builder.build();
}
public Configuration asConfiguration() {
return new ConfigurationBridge(this);
}
}