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.NoTypeConversionAvailableException; 021 import org.apache.camel.component.bean.BeanProcessor; 022 import org.apache.camel.component.event.EventComponent; 023 import org.apache.camel.component.event.EventEndpoint; 024 import org.apache.camel.impl.DefaultCamelContext; 025 import org.apache.camel.impl.ProcessorEndpoint; 026 import org.apache.camel.spi.Injector; 027 import org.apache.camel.spi.Registry; 028 import org.apache.camel.spring.spi.ApplicationContextRegistry; 029 import org.apache.camel.spring.spi.SpringInjector; 030 import org.apache.camel.util.ObjectHelper; 031 import org.apache.commons.logging.Log; 032 import org.apache.commons.logging.LogFactory; 033 import org.springframework.beans.BeansException; 034 import org.springframework.beans.factory.DisposableBean; 035 import org.springframework.beans.factory.InitializingBean; 036 import org.springframework.context.ApplicationContext; 037 import org.springframework.context.ApplicationContextAware; 038 import org.springframework.context.ApplicationEvent; 039 import org.springframework.context.ApplicationListener; 040 import org.springframework.context.ConfigurableApplicationContext; 041 import org.springframework.context.event.ContextRefreshedEvent; 042 import org.springframework.context.support.ClassPathXmlApplicationContext; 043 044 import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException; 045 046 /** 047 * A Spring aware implementation of {@link org.apache.camel.CamelContext} which 048 * will automatically register itself with Springs lifecycle methods plus allows 049 * spring to be used to customize a any <a 050 * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a> 051 * as well as supporting accessing components and beans via the Spring 052 * {@link ApplicationContext} 053 * 054 * @version $Revision: 787932 $ 055 */ 056 public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, 057 ApplicationContextAware, ApplicationListener { 058 059 private static final transient Log LOG = LogFactory.getLog(SpringCamelContext.class); 060 private ApplicationContext applicationContext; 061 private EventEndpoint eventEndpoint; 062 private boolean shouldStartContext = 063 ObjectHelper.getSystemProperty("shouldStartContext", Boolean.TRUE); // start context by default 064 065 public SpringCamelContext() { 066 } 067 068 public SpringCamelContext(ApplicationContext applicationContext) { 069 setApplicationContext(applicationContext); 070 } 071 072 public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception { 073 // lets try and look up a configured camel context in the context 074 String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class); 075 if (names.length == 1) { 076 return (SpringCamelContext)applicationContext.getBean(names[0], SpringCamelContext.class); 077 } 078 SpringCamelContext answer = new SpringCamelContext(); 079 answer.setApplicationContext(applicationContext); 080 answer.afterPropertiesSet(); 081 return answer; 082 } 083 084 public static SpringCamelContext springCamelContext(String configLocations) throws Exception { 085 return springCamelContext(new ClassPathXmlApplicationContext(configLocations)); 086 } 087 088 public void afterPropertiesSet() throws Exception { 089 maybeStart(); 090 } 091 092 private void maybeStart() throws Exception { 093 if (!getShouldStartContext()) { 094 LOG.info("Not starting Apache Camel as property ShouldStartContext is false"); 095 return; 096 } 097 098 if (!isStarted() && !isStarting()) { 099 // Make sure we will not get into the endless loop of calling star 100 LOG.info("Starting Apache Camel as property ShouldStartContext is true"); 101 start(); 102 } else { 103 // ignore as Camel is already started 104 LOG.trace("Ignoring maybeStart() as Apache Camel is already started"); 105 } 106 } 107 108 public void destroy() throws Exception { 109 stop(); 110 } 111 112 public void onApplicationEvent(ApplicationEvent event) { 113 if (LOG.isDebugEnabled()) { 114 LOG.debug("onApplicationEvent: " + event); 115 } 116 117 if (event instanceof ContextRefreshedEvent) { 118 // now lets start the CamelContext so that all its possible 119 // dependencies are initialized 120 try { 121 maybeStart(); 122 } catch (RuntimeException e) { 123 throw e; 124 } catch (Exception e) { 125 throw wrapRuntimeCamelException(e); 126 } 127 } 128 129 if (eventEndpoint != null) { 130 eventEndpoint.onApplicationEvent(event); 131 } else { 132 LOG.warn("No spring-event endpoint enabled to handle event: " + event); 133 } 134 } 135 136 // Properties 137 // ----------------------------------------------------------------------- 138 139 public ApplicationContext getApplicationContext() { 140 return applicationContext; 141 } 142 143 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 144 this.applicationContext = applicationContext; 145 146 if (applicationContext instanceof ConfigurableApplicationContext) { 147 addComponent("spring-event", new EventComponent(applicationContext)); 148 } 149 } 150 151 public EventEndpoint getEventEndpoint() { 152 return eventEndpoint; 153 } 154 155 public void setEventEndpoint(EventEndpoint eventEndpoint) { 156 this.eventEndpoint = eventEndpoint; 157 } 158 159 // Implementation methods 160 // ----------------------------------------------------------------------- 161 162 @Override 163 protected void doStart() throws Exception { 164 maybeDoStart(); 165 } 166 167 protected void maybeDoStart() throws Exception { 168 if (getShouldStartContext()) { 169 super.doStart(); 170 if (eventEndpoint == null) { 171 eventEndpoint = createEventEndpoint(); 172 } 173 } 174 } 175 176 @Override 177 protected Injector createInjector() { 178 if (applicationContext instanceof ConfigurableApplicationContext) { 179 return new SpringInjector((ConfigurableApplicationContext)applicationContext); 180 } else { 181 LOG.warn("Cannot use SpringInjector as applicationContext is not a ConfigurableApplicationContext as its: " 182 + applicationContext); 183 return super.createInjector(); 184 } 185 } 186 187 protected EventEndpoint createEventEndpoint() { 188 return getEndpoint("spring-event:default", EventEndpoint.class); 189 } 190 191 protected Endpoint convertBeanToEndpoint(String uri, Object bean) { 192 // We will use the type convert to build the endpoint first 193 try { 194 Endpoint endpoint = getTypeConverter().convertTo(Endpoint.class, bean); 195 if (endpoint != null) { 196 endpoint.setCamelContext(this); 197 return endpoint; 198 } 199 } catch (NoTypeConversionAvailableException ex) { 200 // ignore, handled below 201 } 202 203 return new ProcessorEndpoint(uri, this, new BeanProcessor(bean, this)); 204 } 205 206 @Override 207 protected Registry createRegistry() { 208 return new ApplicationContextRegistry(getApplicationContext()); 209 } 210 211 public void setShouldStartContext(boolean shouldStartContext) { 212 this.shouldStartContext = shouldStartContext; 213 } 214 215 public boolean getShouldStartContext() { 216 return shouldStartContext; 217 } 218 219 @Override 220 public String toString() { 221 StringBuilder sb = new StringBuilder(); 222 sb.append("SpringCamelContext(").append(getName()).append(")"); 223 return sb.toString(); 224 } 225 226 }