org.grails.config.CompositeConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of grails-core Show documentation
Show all versions of grails-core Show documentation
Grails Web Application Framework
/*
* Copyright 2014 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.config;
import grails.config.Config;
import grails.util.GrailsStringUtils;
import groovy.transform.CompileStatic;
import org.grails.core.exceptions.GrailsConfigurationException;
import org.springframework.util.ClassUtils;
import java.util.*;
/**
* A {@link Config} composed of other Configs
*
* @author Graeme Rocher
* @since 3.0
*/
@CompileStatic
public class CompositeConfig implements Config {
protected Deque configs = new ArrayDeque();
/**
* Adds a config at the highest level of precedence
*
* @param config
*/
public void addFirst(Config config) {
configs.addFirst(config);
}
/**
* Adds a config at the lowest level of precedence
*
* @param config
*/
public void addLast(Config config) {
configs.addLast(config);
}
@Override
@Deprecated
public Map flatten() {
Map flattened = new LinkedHashMap();
for(Config c : configs) {
flattened.putAll(c.flatten());
}
return flattened;
}
@Override
public Properties toProperties() {
Properties properties = new Properties();
for(Config c : configs) {
properties.putAll(c.toProperties());
}
return properties;
}
@Override
public Config merge(Map toMerge) {
throw new UnsupportedOperationException("Config cannot be modified");
}
@Override
public T getProperty(String key, Class targetType, T defaultValue, List allowedValues) {
T v = getProperty(key, targetType, defaultValue);
if(!allowedValues.contains(v)) {
throw new GrailsConfigurationException("Invalid configuration value [$value] for key [${key}]. Possible values $allowedValues");
}
return v;
}
@Override
public Object getAt(Object key) {
for(Config c : configs) {
Object v = c.getAt(key);
if(v != null) return v;
}
return null;
}
@Override
public void setAt(Object key, Object value) {
throw new UnsupportedOperationException("Config cannot be modified");
}
@Override
public Object navigate(String... path) {
for(Config c : configs) {
Object v = c.navigate(path);
if(v != null) return v;
}
return null;
}
@Override
public int size() {
int size = 0;
for (Config config : configs) {
size += config.size();
}
return size;
}
@Override
public boolean isEmpty() {
for (Config config : configs) {
if(!config.isEmpty()) {
return false;
}
}
return true;
}
@Override
public boolean containsKey(Object key) {
for (Config config : configs) {
if(config.containsKey(key)) return true;
}
return false;
}
@Override
public boolean containsValue(Object value) {
for (Config config : configs) {
if(config.containsValue(value)) return true;
}
return false;
}
@Override
public Object get(Object key) {
for (Config config : configs) {
Object v = config.get(key);
if(v != null) return v;
}
return null;
}
@Override
public Object put(String key, Object value) {
throw new UnsupportedOperationException("Config cannot be modified");
}
@Override
public Object remove(Object key) {
throw new UnsupportedOperationException("Config cannot be modified");
}
@Override
public void putAll(Map extends String, ?> m) {
throw new UnsupportedOperationException("Config cannot be modified");
}
@Override
public void clear() {
throw new UnsupportedOperationException("Config cannot be modified");
}
@Override
public Iterator> iterator() {
return entrySet().iterator();
}
@Override
public Set keySet() {
Set entries = new HashSet();
for (Config config : configs) {
entries.addAll(config.keySet());
}
return entries;
}
@Override
public Collection