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.lang.reflect.Method;
020import java.util.ArrayList;
021import java.util.LinkedHashSet;
022import java.util.List;
023import java.util.Map;
024import java.util.Properties;
025import java.util.Set;
026
027import org.apache.aries.blueprint.ExtendedBeanMetadata;
028import org.apache.aries.blueprint.ext.AbstractPropertyPlaceholder;
029import org.apache.aries.blueprint.ext.PropertyPlaceholder;
030import org.apache.camel.component.properties.DefaultPropertiesParser;
031import org.apache.camel.component.properties.PropertiesComponent;
032import org.apache.camel.component.properties.PropertiesParser;
033import org.apache.camel.util.ObjectHelper;
034import org.osgi.service.blueprint.container.BlueprintContainer;
035import org.osgi.service.blueprint.reflect.ComponentMetadata;
036
037/**
038 * Blueprint {@link PropertiesParser} which supports looking up
039 * property placeholders from the Blueprint Property Placeholder Service.
040 * <p/>
041 * This implementation will sit on top of any existing configured
042 * {@link PropertiesParser} and will delegate to those in case Blueprint could not
043 * resolve the property.
044 */
045public class BlueprintPropertiesParser extends DefaultPropertiesParser {
046
047    private final PropertiesComponent propertiesComponent;
048    private final BlueprintContainer container;
049    private final PropertiesParser delegate;
050    private final Set<AbstractPropertyPlaceholder> placeholders = new LinkedHashSet<AbstractPropertyPlaceholder>();
051    private Method method;
052
053    public BlueprintPropertiesParser(PropertiesComponent propertiesComponent, BlueprintContainer container, PropertiesParser delegate) {
054        this.propertiesComponent = propertiesComponent;
055        this.container = container;
056        this.delegate = delegate;
057    }
058
059    /**
060     * Lookup the ids of the Blueprint property placeholder services in the
061     * Blueprint container.
062     *
063     * @return the ids, will be an empty array if none found.
064     */
065    public String[] lookupPropertyPlaceholderIds() {
066        List<String> ids = new ArrayList<String>();
067
068        for (Object componentId : container.getComponentIds()) {
069            String id = (String) componentId;
070            ComponentMetadata meta = container.getComponentMetadata(id);
071            if (meta instanceof ExtendedBeanMetadata) {
072                Class<?> clazz = ((ExtendedBeanMetadata) meta).getRuntimeClass();
073                if (clazz != null && AbstractPropertyPlaceholder.class.isAssignableFrom(clazz)) {
074                    ids.add(id);
075                }
076            }
077        }
078
079        return ids.toArray(new String[ids.size()]);
080    }
081
082    /**
083     * Adds the given Blueprint property placeholder service with the given id
084     *
085     * @param id id of the Blueprint property placeholder service to add.
086     */
087    public void addPropertyPlaceholder(String id) {
088        Object component = container.getComponentInstance(id);
089
090        if (component instanceof AbstractPropertyPlaceholder) {
091            AbstractPropertyPlaceholder placeholder = (AbstractPropertyPlaceholder) component;
092            placeholders.add(placeholder);
093
094            log.debug("Adding Blueprint PropertyPlaceholder: {}", id);
095
096            if (method == null) {
097                try {
098                    method = AbstractPropertyPlaceholder.class.getDeclaredMethod("getProperty", String.class);
099                    method.setAccessible(true);
100                } catch (NoSuchMethodException e) {
101                    throw new IllegalStateException("Cannot add blueprint property placeholder: " + id
102                            + " as the method getProperty is not accessible", e);
103                }
104            }
105        }
106    }
107
108    @Override
109    public String parseProperty(String key, String value, Properties properties) {
110        log.trace("Parsing property key: {} with value: {}", key, value);
111
112        String answer = null;
113
114        // prefer any override properties
115        // this logic is special for BlueprintPropertiesParser as we otherwise prefer
116        // to use the AbstractPropertyPlaceholder from OSGi blueprint config admins
117        // service to lookup otherwise
118        if (key != null && propertiesComponent.getOverrideProperties() != null) {
119            answer = (String) propertiesComponent.getOverrideProperties().get(key);
120        }
121
122        // lookup key in blueprint and return its value
123        if (answer == null && key != null) {
124            for (AbstractPropertyPlaceholder placeholder : placeholders) {
125
126                boolean isDefault = false;
127                if (placeholders.size() > 1) {
128                    // okay we have multiple placeholders and we want to return the answer that
129                    // is not the default placeholder if there is multiple keys
130                    if (placeholder instanceof PropertyPlaceholder) {
131                        Map map = ((PropertyPlaceholder) placeholder).getDefaultProperties();
132                        isDefault = map != null && map.containsKey(key);
133                    }
134                    log.trace("Blueprint property key: {} is part of default properties: {}", key, isDefault);
135                }
136
137                String candidate = (String) ObjectHelper.invokeMethod(method, placeholder, key);
138
139                if (candidate != null) {
140                    if (answer == null || !isDefault) {
141                        log.trace("Blueprint parsed candidate property key: {} as value: {}", key, answer);
142                        answer = candidate;
143                    }
144                }
145            }
146
147            log.debug("Blueprint parsed property key: {} as value: {}", key, answer);
148        }
149
150        // if there is a delegate then let it parse the current answer as it may be jasypt which
151        // need to decrypt values
152        if (delegate != null) {
153            String delegateAnswer = delegate.parseProperty(key, answer != null ? answer : value, properties);
154            if (delegateAnswer != null) {
155                answer = delegateAnswer;
156            }
157        }
158
159        log.trace("Returning parsed property key: {} as value: {}", key, answer);
160        return answer;
161    }
162
163}