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.blueprint;
018    
019    import java.lang.reflect.Method;
020    import java.util.ArrayList;
021    import java.util.LinkedHashSet;
022    import java.util.List;
023    import java.util.Properties;
024    import java.util.Set;
025    
026    import org.apache.aries.blueprint.ExtendedBeanMetadata;
027    import org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder;
028    import org.apache.camel.component.properties.DefaultPropertiesParser;
029    import org.apache.camel.component.properties.PropertiesParser;
030    import org.apache.camel.util.ObjectHelper;
031    import org.osgi.service.blueprint.container.BlueprintContainer;
032    import org.osgi.service.blueprint.reflect.ComponentMetadata;
033    
034    /**
035     * Blueprint {@link PropertiesParser} which supports looking up
036     * property placeholders from the Blueprint Property Placeholder Service.
037     * <p/>
038     * This implementation will sit on top of any existing configured
039     * {@link PropertiesParser} and will delegate to those in case Blueprint could not
040     * resolve the property.
041     */
042    public class BlueprintPropertiesParser extends DefaultPropertiesParser {
043    
044        private final BlueprintContainer container;
045        private PropertiesParser delegate;
046        private final Set<AbstractPropertyPlaceholder> placeholders = new LinkedHashSet<AbstractPropertyPlaceholder>();
047        private Method method;
048    
049        public BlueprintPropertiesParser(BlueprintContainer container, PropertiesParser delegate) {
050            this.container = container;
051            this.delegate = delegate;
052        }
053    
054        /**
055         * Lookup the ids of the Blueprint property placeholder services in the
056         * Blueprint container.
057         *
058         * @return the ids, will be an empty array if none found.
059         */
060        public String[] lookupPropertyPlaceholderIds() {
061            List<String> ids = new ArrayList<String>();
062    
063            for (String id : container.getComponentIds()) {
064                ComponentMetadata meta = container.getComponentMetadata(id);
065                if (meta instanceof ExtendedBeanMetadata) {
066                    Class clazz = ((ExtendedBeanMetadata) meta).getRuntimeClass();
067                    if (clazz != null && AbstractPropertyPlaceholder.class.isAssignableFrom(clazz)) {
068                        ids.add(id);
069                    }
070                }
071            }
072    
073            return ids.toArray(new String[ids.size()]);
074        }
075    
076        /**
077         * Adds the given Blueprint property placeholder service with the given id
078         *
079         * @param id id of the Blueprint property placeholder service to add.
080         */
081        public void addPropertyPlaceholder(String id) {
082            Object component = container.getComponentInstance(id);
083    
084            if (component instanceof AbstractPropertyPlaceholder) {
085                AbstractPropertyPlaceholder placeholder = (AbstractPropertyPlaceholder) component;
086                placeholders.add(placeholder);
087    
088                log.debug("Adding Blueprint PropertyPlaceholder: {}", id);
089    
090                if (method == null) {
091                    try {
092                        method = AbstractPropertyPlaceholder.class.getDeclaredMethod("getProperty", String.class);
093                        method.setAccessible(true);
094                    } catch (NoSuchMethodException e) {
095                        throw new IllegalStateException("Cannot add blueprint property placeholder: " + id
096                                + " as the method getProperty is not accessible", e);
097                    }
098                }
099            }
100        }
101    
102        @Override
103        public String parseProperty(String key, String value, Properties properties) {
104            log.trace("Parsing property key: {} with value: {}", key, value);
105    
106            // lookup key in blueprint and return its value
107            if (key != null) {
108                for (AbstractPropertyPlaceholder placeholder : placeholders) {
109                    value = (String) ObjectHelper.invokeMethod(method, placeholder, key);
110                    if (value != null) {
111                        log.debug("Blueprint parsed property key: {} as value: {}", key, value);
112                        break;
113                    }
114                }
115            }
116    
117            if (value == null && delegate != null) {
118                // let delegate have a try since blueprint didn't resolve it
119                value = delegate.parseProperty(key, value, properties);
120            }
121    
122            log.trace("Returning parsed property key: {} as value: {}", key, value);
123            return value;
124        }
125    
126    }