Coverage Report - org.apache.camel.spring.SpringCamelContext
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringCamelContext
67% 
100% 
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.Endpoint;
 20  
 import org.apache.camel.Processor;
 21  
 import org.apache.camel.RuntimeCamelException;
 22  
 import org.apache.camel.component.bean.BeanProcessor;
 23  
 import org.apache.camel.component.event.EventComponent;
 24  
 import org.apache.camel.component.event.EventEndpoint;
 25  
 import org.apache.camel.impl.DefaultCamelContext;
 26  
 import org.apache.camel.impl.ProcessorEndpoint;
 27  
 import org.apache.camel.spi.ComponentResolver;
 28  
 import org.apache.camel.spi.Injector;
 29  
 import org.apache.camel.spi.Registry;
 30  
 import org.apache.camel.spring.spi.ApplicationContextRegistry;
 31  
 import org.apache.camel.spring.spi.SpringComponentResolver;
 32  
 import org.apache.camel.spring.spi.SpringInjector;
 33  
 import org.apache.commons.logging.Log;
 34  
 import org.apache.commons.logging.LogFactory;
 35  
 
 36  
 import org.springframework.beans.BeansException;
 37  
 import org.springframework.beans.factory.DisposableBean;
 38  
 import org.springframework.beans.factory.InitializingBean;
 39  
 import org.springframework.context.ApplicationContext;
 40  
 import org.springframework.context.ApplicationContextAware;
 41  
 import org.springframework.context.ApplicationEvent;
 42  
 import org.springframework.context.ApplicationListener;
 43  
 import org.springframework.context.ConfigurableApplicationContext;
 44  
 import org.springframework.context.event.ContextRefreshedEvent;
 45  
 import org.springframework.context.support.AbstractRefreshableApplicationContext;
 46  
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 47  
 
 48  
 /**
 49  
  * A Spring aware implementation of {@link CamelContext} which will
 50  
  * automatically register itself with Springs lifecycle methods plus allows
 51  
  * spring to be used to customize a any <a
 52  
  * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
 53  
  * as well as supporting accessing components and beans via the Spring
 54  
  * {@link ApplicationContext}
 55  
  * 
 56  
  * @version $Revision: 563665 $
 57  
  */
 58  
 public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener {
 59  1
     private static final transient Log LOG = LogFactory.getLog(SpringCamelContext.class);
 60  
     private ApplicationContext applicationContext;
 61  
     private EventEndpoint eventEndpoint;
 62  
 
 63  0
     public SpringCamelContext() {
 64  0
     }
 65  
 
 66  56
     public SpringCamelContext(ApplicationContext applicationContext) {
 67  56
         setApplicationContext(applicationContext);
 68  56
     }
 69  
 
 70  
     public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception {
 71  
         // lets try and look up a configured camel context in the context
 72  31
         String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class);
 73  31
         if (names.length == 1) {
 74  31
             return (SpringCamelContext)applicationContext.getBean(names[0], SpringCamelContext.class);
 75  
         }
 76  0
         SpringCamelContext answer = new SpringCamelContext();
 77  0
         answer.setApplicationContext(applicationContext);
 78  0
         answer.afterPropertiesSet();
 79  0
         return answer;
 80  
     }
 81  
 
 82  
     public static SpringCamelContext springCamelContext(String configLocations) throws Exception {
 83  0
         return springCamelContext(new ClassPathXmlApplicationContext(configLocations));
 84  
     }
 85  
 
 86  
     public void afterPropertiesSet() throws Exception {
 87  0
         start();
 88  0
     }
 89  
 
 90  
     public void destroy() throws Exception {
 91  0
         stop();
 92  0
     }
 93  
 
 94  
     public void onApplicationEvent(ApplicationEvent event) {
 95  82
         if (LOG.isDebugEnabled()) {
 96  0
             LOG.debug("Publishing event: " + event);
 97  
         }
 98  
 
 99  82
         if (event instanceof ContextRefreshedEvent) {
 100  
             // now lets start the CamelContext so that all its possible
 101  
             // dependencies are initailized
 102  
             try {
 103  53
                 LOG.debug("Starting the CamelContext now that the ApplicationContext has started");
 104  53
                 start();
 105  0
             } catch (Exception e) {
 106  0
                 throw new RuntimeCamelException(e);
 107  53
             }
 108  53
             if (eventEndpoint != null) {
 109  53
                 eventEndpoint.onApplicationEvent(event);
 110  53
             }
 111  
         } else {
 112  29
             if (eventEndpoint != null) {
 113  29
                 eventEndpoint.onApplicationEvent(event);
 114  29
             } else {
 115  0
                 LOG.warn("No eventEndpoint enabled for event: " + event);
 116  
             }
 117  
         }
 118  82
     }
 119  
 
 120  
     // Properties
 121  
     // -----------------------------------------------------------------------
 122  
 
 123  
     public ApplicationContext getApplicationContext() {
 124  136
         return applicationContext;
 125  
     }
 126  
 
 127  
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 128  56
         this.applicationContext = applicationContext;
 129  
 
 130  56
         if (applicationContext instanceof ConfigurableApplicationContext) {
 131  56
             addComponent("event", new EventComponent(applicationContext));
 132  
         }
 133  56
     }
 134  
 
 135  
     public EventEndpoint getEventEndpoint() {
 136  0
         return eventEndpoint;
 137  
     }
 138  
 
 139  
     public void setEventEndpoint(EventEndpoint eventEndpoint) {
 140  0
         this.eventEndpoint = eventEndpoint;
 141  0
     }
 142  
 
 143  
     // Implementation methods
 144  
     // -----------------------------------------------------------------------
 145  
 
 146  
     @Override
 147  
     protected void doStart() throws Exception {
 148  56
         super.doStart();
 149  56
         if (eventEndpoint == null) {
 150  56
             eventEndpoint = createEventEndpoint();
 151  
         }
 152  56
     }
 153  
 
 154  
     @Override
 155  
     protected Injector createInjector() {
 156  56
         return new SpringInjector((AbstractRefreshableApplicationContext)getApplicationContext());
 157  
     }
 158  
 
 159  
     @Override
 160  
     protected ComponentResolver createComponentResolver() {
 161  53
         ComponentResolver defaultResolver = super.createComponentResolver();
 162  53
         return new SpringComponentResolver(getApplicationContext(), defaultResolver);
 163  
     }
 164  
 
 165  
     protected EventEndpoint createEventEndpoint() {
 166  56
         EventEndpoint endpoint = getEndpoint("event:default", EventEndpoint.class);
 167  56
         return endpoint;
 168  
     }
 169  
 
 170  
     protected Endpoint convertBeanToEndpoint(String uri, Object bean) {
 171  1
         Processor processor = new BeanProcessor(bean, this);
 172  1
         return new ProcessorEndpoint(uri, this, processor);
 173  
     }
 174  
 
 175  
     @Override
 176  
     protected Registry createRegistry() {
 177  20
         return new ApplicationContextRegistry(getApplicationContext());
 178  
     }
 179  
 }