Coverage Report - org.apache.camel.spring.xml.BuilderAction
 
Classes in this File Line Coverage Branch Coverage Complexity
BuilderAction
71% 
83% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.spring.xml;
 19  
 
 20  
 import org.springframework.beans.SimpleTypeConverter;
 21  
 import org.springframework.beans.factory.BeanFactory;
 22  
 import org.springframework.beans.factory.config.RuntimeBeanReference;
 23  
 
 24  
 import java.lang.reflect.InvocationTargetException;
 25  
 import java.util.HashMap;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 
 29  
 public class BuilderAction {
 30  
     private final MethodInfo methodInfo;
 31  
     private final HashMap<String, Object> parameterValues;
 32  
 
 33  80
     public BuilderAction(MethodInfo methodInfo, HashMap<String, Object> parameterValues) {
 34  80
         this.methodInfo = methodInfo;
 35  80
         this.parameterValues = parameterValues;
 36  80
     }
 37  
 
 38  
     public Object invoke(BeanFactory beanFactory, Object rootBuilder, Object contextBuilder) {
 39  80
         SimpleTypeConverter converter = new SimpleTypeConverter();
 40  80
         Object args[] = new Object[methodInfo.parameters.size()];
 41  80
         int pos = 0;
 42  80
         for (Map.Entry<String, Class> entry : methodInfo.parameters.entrySet()) {
 43  70
             String paramName = entry.getKey();
 44  70
             Class paramClass = entry.getValue();
 45  70
             Object value = parameterValues.get(paramName);
 46  70
             if (value != null) {
 47  70
                 value = replaceBeanReferences(beanFactory, rootBuilder, value);
 48  70
                 args[pos] = converter.convertIfNecessary(value, paramClass);
 49  
             }
 50  70
         }
 51  
 
 52  
         try {
 53  80
             return methodInfo.method.invoke(contextBuilder, args);
 54  
         }
 55  0
         catch (InvocationTargetException e) {
 56  0
             throw new IllegalArgumentException(e.getCause());
 57  
         }
 58  0
         catch (RuntimeException e) {
 59  0
             throw e;
 60  
         }
 61  0
         catch (Throwable e) {
 62  0
             throw new IllegalArgumentException(e);
 63  
         }
 64  
     }
 65  
 
 66  
     protected Object replaceBeanReferences(BeanFactory beanFactory, Object rootBuilder, Object value) {
 67  
         // TODO why not using instanceof??
 68  70
         if (value.getClass() == RuntimeBeanReference.class) {
 69  17
             String beanName = ((RuntimeBeanReference) value).getBeanName();
 70  17
             value = beanFactory.getBean(beanName);
 71  
         }
 72  70
         if (value.getClass() == BuilderStatement.class) {
 73  8
             BuilderStatement bs = (BuilderStatement) value;
 74  8
             value = bs.create(beanFactory, rootBuilder);
 75  
         }
 76  70
         if (value instanceof List) {
 77  0
             List list = (List) value;
 78  0
             for (int i = 0, size = list.size(); i < size; i++) {
 79  0
                 list.set(i, replaceBeanReferences(beanFactory, rootBuilder, list.get(i)));
 80  
             }
 81  
         }
 82  70
         return value;
 83  
     }
 84  
 
 85  
     public String getName() {
 86  0
         return methodInfo.getName();
 87  
     }
 88  
 
 89  
     public MethodInfo getMethodInfo() {
 90  77
         return methodInfo;
 91  
     }
 92  
 }