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