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     */
017    package org.apache.camel.spring.spi;
018    
019    import java.util.Properties;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.component.properties.PropertiesResolver;
023    import org.springframework.beans.BeansException;
024    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
025    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
026    
027    /**
028     * A {@link PropertyPlaceholderConfigurer} that bridges Camel's <a href="http://camel.apache.org/using-propertyplaceholder.html">
029     * property placeholder</a> with the Spring property placeholder mechanism.
030     */
031    public class BridgePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer implements PropertiesResolver {
032    
033        // NOTE: this class must be in the spi package as if its in the root package, then Spring fails to parse the XML
034        // files due some weird spring issue. But that is okay as having this class in the spi package is fine anyway.
035    
036        private final Properties properties = new Properties();
037        private PropertiesResolver resolver;
038        private String id;
039    
040        @Override
041        protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
042            // store all the spring properties so we can refer to them later
043            properties.putAll(props);
044            super.processProperties(beanFactoryToProcess, props);
045        }
046    
047        @Override
048        public void setBeanName(String beanName) {
049            this.id = beanName;
050            super.setBeanName(beanName);
051        }
052    
053        @Override
054        public Properties resolveProperties(CamelContext context, boolean ignoreMissingLocation, String... uri) throws Exception {
055            // return the spring properties, if it
056            Properties answer = new Properties();
057            for (String u : uri) {
058                String ref = "ref:" + id;
059                if (ref.equals(u)) {
060                    answer.putAll(properties);
061                } else if (resolver != null) {
062                    Properties p = resolver.resolveProperties(context, ignoreMissingLocation, u);
063                    if (p != null) {
064                        answer.putAll(p);
065                    }
066                }
067            }
068            // must not return null
069            return answer;
070        }
071    
072        public void setResolver(PropertiesResolver resolver) {
073            this.resolver = resolver;
074        }
075    }