001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019import java.util.HashMap;
020import java.util.Map;
021import java.util.Properties;
022
023/**
024 * An {@link OrderedProperties} that also keeps track from which location the properties are sourced from, and default
025 * values.
026 *
027 * This can be used to track all the various sources for configuration that a Camel application uses (properties file,
028 * ENV variables, hardcoded in java, spring-boot, quarkus, camel-k modeline, camel-yaml-dsl etc.
029 *
030 * <b>Important:</b> Use the put method that takes location as argument to store location information, and the put
031 * method that takes default value as argument to store default value information.
032 */
033public final class OrderedLocationProperties extends BaseOrderedProperties {
034
035    private final Map<Object, String> locations = new HashMap<>();
036    private final Map<Object, Object> defaultValues = new HashMap<>();
037
038    public void put(String location, Object key, Object value) {
039        locations.put(key, location);
040        put(key, value);
041    }
042
043    public void put(String location, Object key, Object value, Object defaultValue) {
044        put(location, key, value);
045        if (defaultValue != null) {
046            defaultValues.put(key, defaultValue);
047        }
048    }
049
050    public void putAll(OrderedLocationProperties other) {
051        for (var entry : other.entrySet()) {
052            put(other.getLocation(entry.getKey()), entry.getKey(), entry.getValue(), other.getDefaultValue(entry.getKey()));
053        }
054    }
055
056    public void putAll(String location, Map<Object, Object> map) {
057        for (var entry : map.entrySet()) {
058            put(location, entry.getKey(), entry.getValue());
059        }
060    }
061
062    public void putAll(String location, Properties properties) {
063        for (var entry : properties.entrySet()) {
064            put(location, entry.getKey(), entry.getValue());
065        }
066    }
067
068    /**
069     * Gets the location from where the property was resolved
070     *
071     * @param  key the property key
072     * @return     the location, or <tt>null</tt> if not possible to determine the location.
073     */
074    public String getLocation(Object key) {
075        return locations.get(key);
076    }
077
078    /**
079     * Gets the default value of the property, if a default value exists
080     *
081     * @param  key the property key
082     * @return     the default value, or <tt>null</tt> if not possible to determine the default value.
083     */
084    public Object getDefaultValue(Object key) {
085        return defaultValues.get(key);
086    }
087
088    @Override
089    public synchronized void clear() {
090        locations.clear();
091        defaultValues.clear();
092        super.clear();
093    }
094
095    @Override
096    public synchronized Object remove(Object key) {
097        locations.remove(key);
098        defaultValues.remove(key);
099        return super.remove(key);
100    }
101}