Coverage Report - org.apache.camel.spring.spi.SpringComponentResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringComponentResolver
69% 
67% 
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.spi;
 19  
 
 20  
 import static org.apache.camel.util.ObjectHelper.notNull;
 21  
 
 22  
 import org.apache.camel.CamelContext;
 23  
 import org.apache.camel.Component;
 24  
 import org.apache.camel.spi.ComponentResolver;
 25  
 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
 26  
 import org.springframework.context.ApplicationContext;
 27  
 
 28  
 /**
 29  
  * An implementation of {@link ComponentResolver} which tries to find a Camel {@link Component}
 30  
  * in the Spring {@link ApplicationContext} first; if its not there it defaults to the auto-discovery mechanism.
 31  
  *
 32  
  * @version $Revision: 530555 $
 33  
  */
 34  
 public class SpringComponentResolver implements ComponentResolver {
 35  
     private final ApplicationContext applicationContext;
 36  
     private final ComponentResolver nextResolver;
 37  
 
 38  30
     public SpringComponentResolver(ApplicationContext applicationContext, ComponentResolver nextResolver) {
 39  30
         notNull(applicationContext, "applicationContext");
 40  30
         this.applicationContext = applicationContext;
 41  30
         this.nextResolver = nextResolver;
 42  30
     }
 43  
 
 44  
     public Component resolveComponent(String name, CamelContext context) throws Exception {
 45  39
         Object bean = null;
 46  
         try {
 47  39
             bean = applicationContext.getBean(name);
 48  
         }
 49  39
         catch (NoSuchBeanDefinitionException e) {
 50  
             // ignore its not an error
 51  0
         }
 52  39
         if (bean != null) {
 53  0
             if (bean instanceof Component) {
 54  0
                 return (Component) bean;
 55  
             }
 56  
             else {
 57  0
                 throw new IllegalArgumentException("Bean with name: " + name + " in spring context is not a Component: " + bean);
 58  
             }
 59  
         }
 60  39
         if (nextResolver == null) {
 61  0
             return null;
 62  
         }
 63  39
         return nextResolver.resolveComponent(name, context);
 64  
     }
 65  
 }