Coverage Report - org.apache.camel.spring.SpringCamelContext
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringCamelContext
52% 
100% 
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;
 19  
 
 20  
 import org.apache.camel.impl.DefaultCamelContext;
 21  
 import org.apache.camel.spi.Injector;
 22  
 import org.apache.camel.spi.ComponentResolver;
 23  
 import org.apache.camel.CamelContext;
 24  
 import org.apache.camel.spring.spi.SpringComponentResolver;
 25  
 import org.apache.camel.spring.spi.SpringInjector;
 26  
 import org.apache.camel.spring.component.BeanComponent;
 27  
 import org.springframework.beans.factory.InitializingBean;
 28  
 import org.springframework.beans.factory.DisposableBean;
 29  
 import org.springframework.beans.BeansException;
 30  
 import org.springframework.context.ApplicationContextAware;
 31  
 import org.springframework.context.ApplicationContext;
 32  
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 33  
 import org.springframework.context.support.AbstractRefreshableApplicationContext;
 34  
 
 35  
 /**
 36  
  * A Spring aware implementation of {@link CamelContext} which will automatically register itself with Springs lifecycle
 37  
  * methods  plus allows spring to be used to customize a any
 38  
  * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a> as well as supporting accessing components
 39  
  * and beans via the Spring {@link ApplicationContext}
 40  
  *
 41  
  * @version $Revision: 538919 $
 42  
  */
 43  
 public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, ApplicationContextAware {
 44  
     private ApplicationContext applicationContext;
 45  
 
 46  0
     public SpringCamelContext() {
 47  0
     }
 48  
 
 49  30
     public SpringCamelContext(ApplicationContext applicationContext) {
 50  30
         setApplicationContext(applicationContext);
 51  30
     }
 52  
 
 53  
     public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception {
 54  
         // lets try and look up a configured camel context in the context
 55  9
         String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class);
 56  9
         if (names.length == 1) {
 57  9
             return (SpringCamelContext) applicationContext.getBean(names[0], SpringCamelContext.class);
 58  
         }
 59  0
         SpringCamelContext answer = new SpringCamelContext();
 60  0
         answer.setApplicationContext(applicationContext);
 61  0
         answer.afterPropertiesSet();
 62  0
         return answer;
 63  
     }
 64  
 
 65  
     public static SpringCamelContext springCamelContext(String configLocations) throws Exception {
 66  0
         return springCamelContext(new ClassPathXmlApplicationContext(configLocations));
 67  
     }
 68  
 
 69  
     public void afterPropertiesSet() throws Exception {
 70  
         // lets force lazy initialisation
 71  0
         getInjector();
 72  
 
 73  0
         start();
 74  0
     }
 75  
 
 76  
     public void destroy() throws Exception {
 77  0
         stop();
 78  0
     }
 79  
 
 80  
     public ApplicationContext getApplicationContext() {
 81  60
         return applicationContext;
 82  
     }
 83  
 
 84  
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 85  30
         this.applicationContext = applicationContext;
 86  30
         addComponent("bean", new BeanComponent(applicationContext));
 87  30
     }
 88  
 
 89  
     @Override
 90  
     protected Injector createInjector() {
 91  30
         return new SpringInjector((AbstractRefreshableApplicationContext) getApplicationContext());
 92  
     }
 93  
 
 94  
     @Override
 95  
     protected ComponentResolver createComponentResolver() {
 96  30
         ComponentResolver defaultResolver = super.createComponentResolver();
 97  30
         return new SpringComponentResolver(getApplicationContext(), defaultResolver);
 98  
     }
 99  
 }