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.blueprint;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.Properties;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.component.properties.PropertiesLocation;
025import org.apache.camel.component.properties.PropertiesResolver;
026
027/**
028 * A {@link PropertiesResolver} which supports the <tt>blueprint</tt> scheme.
029 * <p/>
030 * This implementation will sit on top of any existing configured
031 * {@link org.apache.camel.component.properties.PropertiesResolver} and will delegate
032 * to any non <tt>blueprint</tt> schemes.
033 */
034public class BlueprintPropertiesResolver implements PropertiesResolver {
035
036    private final PropertiesResolver delegate;
037    private final BlueprintPropertiesParser blueprint;
038
039    public BlueprintPropertiesResolver(PropertiesResolver delegate, BlueprintPropertiesParser blueprint) {
040        this.delegate = delegate;
041        this.blueprint = blueprint;
042    }
043
044    @Override
045    public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, List<PropertiesLocation> locations) throws Exception {
046        Properties answer = new Properties();
047
048        boolean explicit = false;
049
050        for (PropertiesLocation location : locations) {
051            if ("blueprint".equals(location.getResolver())) {
052                blueprint.addPropertyPlaceholder(location.getPath());
053                // indicate an explicit blueprint id was configured
054                explicit = true;
055            } else {
056                // delegate the url
057                answer.putAll(delegate.resolveProperties(context, ignoreMissingLocation, Collections.singletonList(location)));
058            }
059        }
060
061        if (!explicit) {
062            // auto lookup blueprint property placeholders to use if none explicit was configured
063            // this is convention over configuration
064            for (String id : blueprint.lookupPropertyPlaceholderIds()) {
065                blueprint.addPropertyPlaceholder(id);
066            }
067        }
068
069        return answer;
070    }
071
072}