001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.spring; 018 019 import org.apache.camel.Endpoint; 020 import org.apache.camel.component.bean.BeanProcessor; 021 import org.apache.camel.component.event.EventComponent; 022 import org.apache.camel.component.event.EventEndpoint; 023 import org.apache.camel.impl.DefaultCamelContext; 024 import org.apache.camel.impl.ProcessorEndpoint; 025 import org.apache.camel.spi.Injector; 026 import org.apache.camel.spi.Registry; 027 import org.apache.camel.spring.spi.ApplicationContextRegistry; 028 import org.apache.camel.spring.spi.SpringInjector; 029 import org.apache.commons.logging.Log; 030 import org.apache.commons.logging.LogFactory; 031 import org.springframework.beans.BeansException; 032 import org.springframework.beans.factory.DisposableBean; 033 import org.springframework.beans.factory.InitializingBean; 034 import org.springframework.context.ApplicationContext; 035 import org.springframework.context.ApplicationContextAware; 036 import org.springframework.context.ApplicationEvent; 037 import org.springframework.context.ApplicationListener; 038 import org.springframework.context.ConfigurableApplicationContext; 039 import org.springframework.context.event.ContextRefreshedEvent; 040 import org.springframework.context.support.ClassPathXmlApplicationContext; 041 042 import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException; 043 044 /** 045 * A Spring aware implementation of {@link org.apache.camel.CamelContext} which 046 * will automatically register itself with Springs lifecycle methods plus allows 047 * spring to be used to customize a any <a 048 * href="http://camel.apache.org/type-converter.html">Type Converters</a> 049 * as well as supporting accessing components and beans via the Spring 050 * {@link ApplicationContext} 051 * 052 * @version $Revision: 833859 $ 053 */ 054 public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, 055 ApplicationContextAware, ApplicationListener { 056 057 private static final transient Log LOG = LogFactory.getLog(SpringCamelContext.class); 058 private ApplicationContext applicationContext; 059 private EventEndpoint eventEndpoint; 060 061 public SpringCamelContext() { 062 } 063 064 public SpringCamelContext(ApplicationContext applicationContext) { 065 setApplicationContext(applicationContext); 066 } 067 068 public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception { 069 // lets try and look up a configured camel context in the context 070 String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class); 071 if (names.length == 1) { 072 return (SpringCamelContext)applicationContext.getBean(names[0], SpringCamelContext.class); 073 } 074 SpringCamelContext answer = new SpringCamelContext(); 075 answer.setApplicationContext(applicationContext); 076 answer.afterPropertiesSet(); 077 return answer; 078 } 079 080 public static SpringCamelContext springCamelContext(String configLocations) throws Exception { 081 return springCamelContext(new ClassPathXmlApplicationContext(configLocations)); 082 } 083 084 public void afterPropertiesSet() throws Exception { 085 maybeStart(); 086 } 087 088 public void destroy() throws Exception { 089 stop(); 090 } 091 092 public void onApplicationEvent(ApplicationEvent event) { 093 if (LOG.isDebugEnabled()) { 094 LOG.debug("onApplicationEvent: " + event); 095 } 096 097 if (event instanceof ContextRefreshedEvent) { 098 // now lets start the CamelContext so that all its possible 099 // dependencies are initialized 100 try { 101 maybeStart(); 102 } catch (Exception e) { 103 throw wrapRuntimeCamelException(e); 104 } 105 } 106 107 if (eventEndpoint != null) { 108 eventEndpoint.onApplicationEvent(event); 109 } else { 110 LOG.info("No spring-event endpoint enabled to handle event: " + event); 111 } 112 } 113 114 // Properties 115 // ----------------------------------------------------------------------- 116 117 public ApplicationContext getApplicationContext() { 118 return applicationContext; 119 } 120 121 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 122 this.applicationContext = applicationContext; 123 ClassLoader cl; 124 125 // set the application context classloader 126 if (applicationContext != null && applicationContext.getClassLoader() != null) { 127 cl = applicationContext.getClassLoader(); 128 } else { 129 LOG.warn("Cannot find the class loader from application context, using the thread context class loader instead"); 130 cl = Thread.currentThread().getContextClassLoader(); 131 } 132 if (LOG.isDebugEnabled()) { 133 LOG.debug("Set the application context classloader to: " + cl); 134 } 135 this.setApplicationContextClassLoader(cl); 136 137 if (applicationContext instanceof ConfigurableApplicationContext) { 138 // only add if not already added 139 if (hasComponent("spring-event") == null) { 140 addComponent("spring-event", new EventComponent(applicationContext)); 141 } 142 } 143 } 144 145 public EventEndpoint getEventEndpoint() { 146 return eventEndpoint; 147 } 148 149 public void setEventEndpoint(EventEndpoint eventEndpoint) { 150 this.eventEndpoint = eventEndpoint; 151 } 152 153 // Implementation methods 154 // ----------------------------------------------------------------------- 155 156 @Override 157 protected void doStart() throws Exception { 158 super.doStart(); 159 if (eventEndpoint == null) { 160 eventEndpoint = createEventEndpoint(); 161 } 162 } 163 164 @Override 165 protected Injector createInjector() { 166 if (applicationContext instanceof ConfigurableApplicationContext) { 167 return new SpringInjector((ConfigurableApplicationContext)applicationContext); 168 } else { 169 LOG.warn("Cannot use SpringInjector as applicationContext is not a ConfigurableApplicationContext as its: " 170 + applicationContext); 171 return super.createInjector(); 172 } 173 } 174 175 protected EventEndpoint createEventEndpoint() { 176 return getEndpoint("spring-event:default", EventEndpoint.class); 177 } 178 179 protected Endpoint convertBeanToEndpoint(String uri, Object bean) { 180 // We will use the type convert to build the endpoint first 181 Endpoint endpoint = getTypeConverter().convertTo(Endpoint.class, bean); 182 if (endpoint != null) { 183 endpoint.setCamelContext(this); 184 return endpoint; 185 } 186 187 return new ProcessorEndpoint(uri, this, new BeanProcessor(bean, this)); 188 } 189 190 @Override 191 protected Registry createRegistry() { 192 return new ApplicationContextRegistry(getApplicationContext()); 193 } 194 195 private void maybeStart() throws Exception { 196 if (!isStarted() && !isStarting()) { 197 start(); 198 } else { 199 // ignore as Camel is already started 200 LOG.trace("Ignoring maybeStart() as Apache Camel is already started"); 201 } 202 } 203 204 @Override 205 public String toString() { 206 StringBuilder sb = new StringBuilder(); 207 sb.append("SpringCamelContext(").append(getName()).append(")"); 208 if (applicationContext != null) { 209 sb.append(" with spring id ").append(applicationContext.getId()); 210 } 211 return sb.toString(); 212 } 213 214 }