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.xbean.blueprint.context.impl; 018 019 import org.apache.aries.blueprint.ParserContext; 020 import org.apache.aries.blueprint.mutable.MutableBeanMetadata; 021 import org.apache.aries.blueprint.mutable.MutableValueMetadata; 022 import org.osgi.service.blueprint.container.ComponentDefinitionException; 023 import org.osgi.service.blueprint.reflect.BeanProperty; 024 025 import java.lang.reflect.Constructor; 026 import java.lang.reflect.Method; 027 import java.util.ArrayList; 028 import java.util.Arrays; 029 import java.util.Collections; 030 import java.util.Comparator; 031 import java.util.HashMap; 032 import java.util.HashSet; 033 import java.util.LinkedHashMap; 034 import java.util.LinkedList; 035 import java.util.List; 036 import java.util.Map; 037 import java.util.Set; 038 039 /** 040 * NamedConstructorArgs is a BeanFactoryPostProcessor that converts property declarations into indexed constructor args 041 * based on the the constructor parameter names annotation. This process first selects a constructor and then fills in 042 * the constructor arguments from the properties defined in the bean definition. If a property is not defined in the 043 * bean definition, first the defaultValues map is checked for a value and if a value is not present a Java default 044 * value is provided for the constructor argument (e.g. numbers are assigned 0 and objects are assigned null). 045 * 046 * @author Dain Sundstrom 047 * @version $Id$ 048 * @since 2.0 049 */ 050 public class NamedConstructorArgs { 051 private Map<PropertyKey, String> defaultValues = new HashMap<PropertyKey, String>(); 052 053 /** 054 * Gets the default values that are assigned to constructor arguments without a defined value. 055 * 056 * @return the default values that are assigned to constructor arguments without a defined value 057 */ 058 public List<DefaultProperty> getDefaultValues() { 059 List<DefaultProperty> values = new LinkedList<DefaultProperty>(); 060 for (Map.Entry<PropertyKey, String> entry : defaultValues.entrySet()) { 061 PropertyKey key = entry.getKey(); 062 String value = entry.getValue(); 063 values.add(new DefaultProperty(key.name, key.type, value)); 064 } 065 return values; 066 } 067 068 /** 069 * Sets the default values that are assigned to constructor arguments without a defined value. 070 * 071 * @param defaultValues the values that are assigned to constructor arguments without a defined value 072 */ 073 public void setDefaultValues(List<DefaultProperty> defaultValues) { 074 this.defaultValues.clear(); 075 for (DefaultProperty defaultValue : defaultValues) { 076 addDefaultValue(defaultValue); 077 } 078 } 079 080 /** 081 * Adds a default value for a property with the specified name and type. 082 * 083 * @param name the name of the property 084 * @param type the type of the property 085 * @param value the default value for a property with the specified name and type 086 */ 087 public void addDefaultValue(String name, Class type, String value) { 088 defaultValues.put(new PropertyKey(name, type), value); 089 } 090 091 /** 092 * Adds a defautl value for a property. 093 * 094 * @param defaultProperty the default property information 095 */ 096 private void addDefaultValue(DefaultProperty defaultProperty) { 097 defaultValues.put(new PropertyKey(defaultProperty.getName(), defaultProperty.getType()), defaultProperty.getValue()); 098 } 099 100 public void processParameters(MutableBeanMetadata beanMetadata, MappingMetaData metadata, ParserContext parserContext) { 101 102 // if this bean already has constructor arguments defined, don't mess with them 103 if (beanMetadata.getArguments().size() > 0) { 104 return; 105 } 106 107 // try to get a list of constructor arg names to use 108 ConstructionInfo constructionInfo = selectConstructionMethod(beanMetadata, metadata); 109 if (constructionInfo == null) { 110 return; 111 } 112 113 // remove each named property and add an indexed constructor arg 114 List<BeanProperty> beanProperties = beanMetadata.getProperties(); 115 LinkedHashMap<String, BeanProperty> propMap = new LinkedHashMap<String, BeanProperty>(); 116 for (BeanProperty beanProperty : beanProperties) { 117 propMap.put(beanProperty.getName(), beanProperty); 118 } 119 String[] parameterNames = constructionInfo.parameterNames; 120 Class[] parameterTypes = constructionInfo.parameterTypes; 121 for (int i = 0; i < parameterNames.length; i++) { 122 String parameterName = parameterNames[i]; 123 Class parameterType = parameterTypes[i]; 124 125 BeanProperty beanProperty = propMap.get(parameterName); 126 if (beanProperty != null) { 127 propMap.remove(parameterName); 128 beanMetadata.removeProperty(beanProperty); 129 beanMetadata.addArgument(beanProperty.getValue(), parameterType.getName(), i); 130 } else { 131 String defaultValue = defaultValues.get(new PropertyKey(parameterName, parameterType)); 132 if (defaultValue == null) { 133 defaultValue = DEFAULT_VALUE.get(parameterType); 134 } 135 // if (defaultValue instanceof FactoryBean) { 136 // try { 137 // defaultValue = ((FactoryBean)defaultValue).getObject(); 138 // } catch (Exception e) { 139 // throw new FatalBeanException("Unable to get object value from bean factory", e); 140 // } 141 // } 142 MutableValueMetadata valueMetadata = parserContext.createMetadata(MutableValueMetadata.class); 143 valueMetadata.setStringValue(defaultValue); 144 valueMetadata.setType(parameterType.getName()); 145 beanMetadata.addArgument(valueMetadata, parameterType.getName(), i); 146 } 147 } 148 149 // todo set any usable default values on the bean definition 150 } 151 152 private ConstructionInfo selectConstructionMethod(MutableBeanMetadata beanMetadata, MappingMetaData metadata) { 153 Class beanClass = beanMetadata.getRuntimeClass(); 154 155 // get a set containing the names of the defined properties 156 Set<String> definedProperties = new HashSet<String>(); 157 List<BeanProperty> values = beanMetadata.getProperties(); 158 for (BeanProperty beanProperty : values) { 159 definedProperties.add(beanProperty.getName()); 160 } 161 162 // first check for a factory method 163 if (beanMetadata.getFactoryMethod() != null) { 164 return selectFactory(beanClass, beanMetadata, metadata, definedProperties); 165 } else { 166 return selectConstructor(beanClass, metadata, definedProperties); 167 } 168 } 169 170 private ConstructionInfo selectFactory(Class beanClass, MutableBeanMetadata beanMetadata, MappingMetaData metadata, Set definedProperties) { 171 String factoryMethodName = beanMetadata.getFactoryMethod(); 172 173 // get the factory methods sorted by longest arg length first 174 Method[] methods = beanClass.getMethods(); 175 List<Method> factoryMethods = new ArrayList<Method>(methods.length); 176 for (Method method : methods) { 177 if (method.getName().equals(factoryMethodName)) { 178 factoryMethods.add(method); 179 } 180 } 181 182 Collections.sort(factoryMethods, new MethodArgLengthComparator()); 183 184 // if a factory method has been annotated as the default constructor we always use that constructor 185 for (Method factoryMethod : factoryMethods) { 186 if (metadata.isDefaultFactoryMethod(beanClass, factoryMethod)) { 187 return new ConstructionInfo(beanClass, factoryMethod, metadata); 188 } 189 } 190 191 // try to find a constructor for which we have all of the properties defined 192 for (Method factoryMethod : factoryMethods) { 193 ConstructionInfo constructionInfo = new ConstructionInfo(beanClass, factoryMethod, metadata); 194 if (isUsableConstructor(constructionInfo, definedProperties)) { 195 return constructionInfo; 196 } 197 } 198 return null; 199 } 200 201 private ConstructionInfo selectConstructor(Class beanClass, MappingMetaData metadata, Set definedProperties) { 202 // get the constructors sorted by longest arg length first 203 List<Constructor> constructors = new ArrayList<Constructor>(Arrays.asList(beanClass.getConstructors())); 204 Collections.sort(constructors, new ConstructorArgLengthComparator()); 205 206 // if a constructor has been annotated as the default constructor we always use that constructor 207 for (Constructor constructor : constructors) { 208 if (metadata.isDefaultConstructor(constructor)) { 209 return new ConstructionInfo(constructor, metadata); 210 } 211 } 212 213 // try to find a constructor for which we have all of the properties defined 214 for (Constructor constructor : constructors) { 215 ConstructionInfo constructionInfo = new ConstructionInfo(constructor, metadata); 216 if (isUsableConstructor(constructionInfo, definedProperties)) { 217 return constructionInfo; 218 } 219 } 220 return null; 221 } 222 223 private boolean isUsableConstructor(ConstructionInfo constructionInfo, Set definedProperties) { 224 // if we don't have parameter names this is not the constructor we are looking for 225 String[] parameterNames = constructionInfo.parameterNames; 226 if (parameterNames == null) { 227 return false; 228 } 229 230 Class[] parameterTypes = constructionInfo.parameterTypes; 231 for (int i = 0; i < parameterNames.length; i++) { 232 String parameterName = parameterNames[i]; 233 Class parameterType = parameterTypes[i]; 234 235 // can we satify this property using a defined property or default property 236 if (!definedProperties.contains(parameterName) && !defaultValues.containsKey(new PropertyKey(parameterName, parameterType))) { 237 return false; 238 } 239 } 240 241 return true; 242 } 243 244 private class ConstructionInfo { 245 private final Class[] parameterTypes; 246 private final String[] parameterNames; 247 248 public ConstructionInfo(Constructor constructor, MappingMetaData metadata) { 249 this.parameterTypes = constructor.getParameterTypes(); 250 String[] names = metadata.getParameterNames(constructor); 251 252 // verify that we have enough parameter names 253 int expectedParameterCount = parameterTypes.length; 254 if (names != null && names.length != expectedParameterCount) { 255 throw new ComponentDefinitionException("Excpected " + expectedParameterCount + " parameter names for constructor but only got " + 256 names.length + ": " + constructor.toString()); 257 } 258 if (expectedParameterCount == 0) { 259 names = new String[0]; 260 } 261 262 this.parameterNames = names; 263 } 264 265 public ConstructionInfo(Class beanClass, Method factoryMethod, MappingMetaData metadata) { 266 this.parameterTypes = factoryMethod.getParameterTypes(); 267 268 String[] names = metadata.getParameterNames(beanClass, factoryMethod); 269 270 // verify that we have enough parameter names 271 int expectedParameterCount = parameterTypes.length; 272 if (names != null && names.length != expectedParameterCount) { 273 throw new ComponentDefinitionException("Excpected " + expectedParameterCount + " parameter names for factory method but only got " + 274 names.length + ": " + factoryMethod.toString()); 275 } 276 if (expectedParameterCount == 0) { 277 names = new String[0]; 278 } 279 280 this.parameterNames = names; 281 } 282 } 283 284 private static class MethodArgLengthComparator implements Comparator<Method> { 285 public int compare(Method o1, Method o2) { 286 return getArgLength(o2) - getArgLength(o1); 287 } 288 289 private int getArgLength(Method object) { 290 return object.getParameterTypes().length; 291 } 292 } 293 294 private static class ConstructorArgLengthComparator implements Comparator<Constructor> { 295 public int compare(Constructor o1, Constructor o2) { 296 return getArgLength(o2) - getArgLength(o1); 297 } 298 299 private int getArgLength(Constructor object) { 300 return object.getParameterTypes().length; 301 } 302 } 303 304 private static class PropertyKey { 305 private final String name; 306 private final Class type; 307 308 public PropertyKey(String name, Class type) { 309 this.name = name; 310 this.type = type; 311 } 312 313 public boolean equals(Object object) { 314 if (!(object instanceof PropertyKey)) { 315 return false; 316 } 317 318 PropertyKey defaultProperty = (PropertyKey) object; 319 return name.equals(defaultProperty.name) && type.equals(type); 320 } 321 322 public int hashCode() { 323 int result = 17; 324 result = 37 * result + name.hashCode(); 325 result = 37 * result + type.hashCode(); 326 return result; 327 } 328 329 public String toString() { 330 return "[" + name + " " + type + "]"; 331 } 332 } 333 334 private static final Map<Class, String> DEFAULT_VALUE; 335 336 static { 337 Map<Class, String> temp = new HashMap<Class, String>(); 338 temp.put(Boolean.TYPE, Boolean.FALSE.toString()); 339 temp.put(Byte.TYPE, "0B"); 340 temp.put(Character.TYPE, "\\u000"); 341 temp.put(Short.TYPE, "0S"); 342 temp.put(Integer.TYPE, "0"); 343 temp.put(Long.TYPE, "0L"); 344 temp.put(Float.TYPE, "0F"); 345 temp.put(Double.TYPE, "0D"); 346 347 DEFAULT_VALUE = Collections.unmodifiableMap(temp); 348 } 349 }