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.
025 *
026 * This can be used to track all the various sources for configuration that a Camel application uses (properties file,
027 * ENV variables, hardcoded in java, spring-boot, quarkus, camel-k modeline, camel-yaml-dsl etc.
028 *
029 * <b>Important:</b> Use the put method that takes location as argument to store location information.
030 */
031public final class OrderedLocationProperties extends BaseOrderedProperties {
032
033    private final Map<Object, String> locations = new HashMap<>();
034
035    public void put(String location, Object key, Object value) {
036        locations.put(key, location);
037        put(key, value);
038    }
039
040    public void putAll(OrderedLocationProperties other) {
041        for (var entry : other.entrySet()) {
042            put(other.getLocation(entry.getKey()), entry.getKey(), entry.getValue());
043        }
044    }
045
046    public void putAll(String location, Map<Object, Object> map) {
047        for (var entry : map.entrySet()) {
048            put(location, entry.getKey(), entry.getValue());
049        }
050    }
051
052    public void putAll(String location, Properties properties) {
053        for (var entry : properties.entrySet()) {
054            put(location, entry.getKey(), entry.getValue());
055        }
056    }
057
058    public String getLocation(Object key) {
059        return locations.get(key);
060    }
061
062    @Override
063    public synchronized void clear() {
064        locations.clear();
065        super.clear();
066    }
067
068    @Override
069    public synchronized Object remove(Object key) {
070        locations.remove(key);
071        return super.remove(key);
072    }
073}