Coverage Report - org.apache.camel.spring.SpringRouteBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringRouteBuilder
0% 
0% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.spring;
 18  
 
 19  
 import org.apache.camel.CamelContext;
 20  
 import org.apache.camel.builder.RouteBuilder;
 21  
 import org.apache.camel.spring.spi.TransactionInterceptor;
 22  
 
 23  
 import org.springframework.context.ApplicationContext;
 24  
 import org.springframework.transaction.support.TransactionTemplate;
 25  
 
 26  
 /**
 27  
  * An extension of the {@link RouteBuilder} to provide some additional helper
 28  
  * methods
 29  
  * 
 30  
  * @version $Revision: 1.1 $
 31  
  */
 32  0
 public abstract class SpringRouteBuilder extends RouteBuilder {
 33  
     private ApplicationContext applicationContext;
 34  
 
 35  
     public TransactionInterceptor transactionInterceptor() {
 36  0
         return new TransactionInterceptor(bean(TransactionTemplate.class));
 37  
     }
 38  
 
 39  
     /**
 40  
      * Looks up the bean with the given name in the application context and
 41  
      * returns it, or throws an exception if the bean is not present or is not
 42  
      * of the given type
 43  
      * 
 44  
      * @param type the type of the bean
 45  
      * @param beanName the name of the bean in the application context
 46  
      * @return the bean
 47  
      */
 48  
     public <T> T bean(Class<T> type, String beanName) {
 49  0
         ApplicationContext context = getApplicationContext();
 50  0
         return (T)context.getBean(beanName, type);
 51  
     }
 52  
 
 53  
     /**
 54  
      * Looks up the bean with the given type in the application context and
 55  
      * returns it, or throws an exception if the bean is not present or there
 56  
      * are multiple possible beans to choose from for the given type
 57  
      * 
 58  
      * @param type the type of the bean
 59  
      * @return the bean
 60  
      */
 61  
     public <T> T bean(Class<T> type) {
 62  0
         ApplicationContext context = getApplicationContext();
 63  0
         String[] names = context.getBeanNamesForType(type, true, true);
 64  0
         if (names != null) {
 65  0
             int count = names.length;
 66  0
             if (count == 1) {
 67  
                 // lets instantiate the single bean
 68  0
                 return (T)context.getBean(names[0]);
 69  0
             } else if (count > 1) {
 70  0
                 throw new IllegalArgumentException("Too many beans in the application context of type: " + type + ". Found: " + count);
 71  
             }
 72  
         }
 73  0
         throw new IllegalArgumentException("No bean available in the application context of type: " + type);
 74  
     }
 75  
 
 76  
     /**
 77  
      * Returns the application context which has been configured via the
 78  
      * {@link #setApplicationContext(ApplicationContext)} method or from the
 79  
      * underlying {@link SpringCamelContext}
 80  
      * 
 81  
      * @return
 82  
      */
 83  
     public ApplicationContext getApplicationContext() {
 84  0
         if (applicationContext == null) {
 85  0
             CamelContext camelContext = getContext();
 86  0
             if (camelContext instanceof SpringCamelContext) {
 87  0
                 SpringCamelContext springCamelContext = (SpringCamelContext)camelContext;
 88  0
                 return springCamelContext.getApplicationContext();
 89  
             } else {
 90  0
                 throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured");
 91  
             }
 92  
         }
 93  0
         return applicationContext;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Sets the application context to use to lookup beans
 98  
      */
 99  
     public void setApplicationContext(ApplicationContext applicationContext) {
 100  0
         this.applicationContext = applicationContext;
 101  0
     }
 102  
 }