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