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 (Object componentId : container.getComponentIds()) {
064 String id = (String) componentId;
065 ComponentMetadata meta = container.getComponentMetadata(id);
066 if (meta instanceof ExtendedBeanMetadata) {
067 Class clazz = ((ExtendedBeanMetadata) meta).getRuntimeClass();
068 if (clazz != null && AbstractPropertyPlaceholder.class.isAssignableFrom(clazz)) {
069 ids.add(id);
070 }
071 }
072 }
073
074 return ids.toArray(new String[ids.size()]);
075 }
076
077 /**
078 * Adds the given Blueprint property placeholder service with the given id
079 *
080 * @param id id of the Blueprint property placeholder service to add.
081 */
082 public void addPropertyPlaceholder(String id) {
083 Object component = container.getComponentInstance(id);
084
085 if (component instanceof AbstractPropertyPlaceholder) {
086 AbstractPropertyPlaceholder placeholder = (AbstractPropertyPlaceholder) component;
087 placeholders.add(placeholder);
088
089 log.debug("Adding Blueprint PropertyPlaceholder: {}", id);
090
091 if (method == null) {
092 try {
093 method = AbstractPropertyPlaceholder.class.getDeclaredMethod("getProperty", String.class);
094 method.setAccessible(true);
095 } catch (NoSuchMethodException e) {
096 throw new IllegalStateException("Cannot add blueprint property placeholder: " + id
097 + " as the method getProperty is not accessible", e);
098 }
099 }
100 }
101 }
102
103 @Override
104 public String parseProperty(String key, String value, Properties properties) {
105 log.trace("Parsing property key: {} with value: {}", key, value);
106
107 // lookup key in blueprint and return its value
108 if (key != null) {
109 for (AbstractPropertyPlaceholder placeholder : placeholders) {
110 value = (String) ObjectHelper.invokeMethod(method, placeholder, key);
111 if (value != null) {
112 log.debug("Blueprint parsed property key: {} as value: {}", key, value);
113 break;
114 }
115 }
116 }
117
118 if (value == null && delegate != null) {
119 // let delegate have a try since blueprint didn't resolve it
120 value = delegate.parseProperty(key, value, properties);
121 }
122
123 log.trace("Returning parsed property key: {} as value: {}", key, value);
124 return value;
125 }
126
127 }