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 super(propertiesComponent); 055 this.propertiesComponent = propertiesComponent; 056 this.container = container; 057 this.delegate = delegate; 058 } 059 060 /** 061 * Lookup the ids of the Blueprint property placeholder services in the 062 * Blueprint container. 063 * 064 * @return the ids, will be an empty array if none found. 065 */ 066 public String[] lookupPropertyPlaceholderIds() { 067 List<String> ids = new ArrayList<String>(); 068 069 for (Object componentId : container.getComponentIds()) { 070 String id = (String) componentId; 071 ComponentMetadata meta = container.getComponentMetadata(id); 072 if (meta instanceof ExtendedBeanMetadata) { 073 Class<?> clazz = ((ExtendedBeanMetadata) meta).getRuntimeClass(); 074 if (clazz != null && AbstractPropertyPlaceholder.class.isAssignableFrom(clazz)) { 075 ids.add(id); 076 } 077 } 078 } 079 080 return ids.toArray(new String[ids.size()]); 081 } 082 083 /** 084 * Adds the given Blueprint property placeholder service with the given id 085 * 086 * @param id id of the Blueprint property placeholder service to add. 087 */ 088 public void addPropertyPlaceholder(String id) { 089 Object component = container.getComponentInstance(id); 090 091 if (component instanceof AbstractPropertyPlaceholder) { 092 AbstractPropertyPlaceholder placeholder = (AbstractPropertyPlaceholder) component; 093 placeholders.add(placeholder); 094 095 log.debug("Adding Blueprint PropertyPlaceholder: {}", id); 096 097 if (method == null) { 098 try { 099 method = AbstractPropertyPlaceholder.class.getDeclaredMethod("retrieveValue", String.class); 100 method.setAccessible(true); 101 } catch (NoSuchMethodException e) { 102 throw new IllegalStateException("Cannot add blueprint property placeholder: " + id 103 + " as the method retrieveValue is not accessible", e); 104 } 105 } 106 } 107 } 108 109 @Override 110 public String parseProperty(String key, String value, Properties properties) { 111 log.trace("Parsing property key: {} with value: {}", key, value); 112 113 String answer = null; 114 115 // prefer any override properties 116 // this logic is special for BlueprintPropertiesParser as we otherwise prefer 117 // to use the AbstractPropertyPlaceholder from OSGi blueprint config admins 118 // service to lookup otherwise 119 if (key != null && propertiesComponent.getOverrideProperties() != null) { 120 answer = (String) propertiesComponent.getOverrideProperties().get(key); 121 } 122 123 // lookup key in blueprint and return its value 124 if (answer == null && key != null) { 125 for (AbstractPropertyPlaceholder placeholder : placeholders) { 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 try { 138 String candidate = (String) ObjectHelper.invokeMethod(method, placeholder, key); 139 140 if (candidate != null) { 141 if (answer == null || !isDefault) { 142 log.trace("Blueprint parsed candidate property key: {} as value: {}", key, answer); 143 answer = candidate; 144 } 145 } 146 } catch (Exception ex) { 147 // Here we just catch the exception and try to use other candidate 148 } 149 } 150 log.debug("Blueprint parsed property key: {} as value: {}", key, answer); 151 } 152 153 // if there is a delegate then let it parse the current answer as it may be jasypt which 154 // need to decrypt values 155 if (delegate != null) { 156 String delegateAnswer = delegate.parseProperty(key, answer != null ? answer : value, properties); 157 if (delegateAnswer != null) { 158 answer = delegateAnswer; 159 log.debug("Delegate property parser parsed the property key: {} as value: {}", key, answer); 160 } 161 } 162 163 log.trace("Returning parsed property key: {} as value: {}", key, answer); 164 return answer; 165 } 166 167}