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.Processor;
021 import org.apache.camel.RuntimeCamelException;
022 import org.apache.camel.component.bean.BeanProcessor;
023 import org.apache.camel.component.event.EventComponent;
024 import org.apache.camel.component.event.EventEndpoint;
025 import org.apache.camel.impl.DefaultCamelContext;
026 import org.apache.camel.impl.ProcessorEndpoint;
027 import org.apache.camel.spi.ComponentResolver;
028 import org.apache.camel.spi.Injector;
029 import org.apache.camel.spi.Registry;
030 import org.apache.camel.spring.spi.ApplicationContextRegistry;
031 import org.apache.camel.spring.spi.SpringComponentResolver;
032 import org.apache.camel.spring.spi.SpringInjector;
033 import org.apache.commons.logging.Log;
034 import org.apache.commons.logging.LogFactory;
035
036 import org.springframework.beans.BeansException;
037 import org.springframework.beans.factory.DisposableBean;
038 import org.springframework.beans.factory.InitializingBean;
039 import org.springframework.context.ApplicationContext;
040 import org.springframework.context.ApplicationContextAware;
041 import org.springframework.context.ApplicationEvent;
042 import org.springframework.context.ApplicationListener;
043 import org.springframework.context.ConfigurableApplicationContext;
044 import org.springframework.context.event.ContextRefreshedEvent;
045 import org.springframework.context.support.AbstractRefreshableApplicationContext;
046 import org.springframework.context.support.ClassPathXmlApplicationContext;
047
048 /**
049 * A Spring aware implementation of {@link CamelContext} which will
050 * automatically register itself with Springs lifecycle methods plus allows
051 * spring to be used to customize a any <a
052 * href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a>
053 * as well as supporting accessing components and beans via the Spring
054 * {@link ApplicationContext}
055 *
056 * @version $Revision: 563665 $
057 */
058 public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener {
059 private static final transient Log LOG = LogFactory.getLog(SpringCamelContext.class);
060 private ApplicationContext applicationContext;
061 private EventEndpoint eventEndpoint;
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 start();
088 }
089
090 public void destroy() throws Exception {
091 stop();
092 }
093
094 public void onApplicationEvent(ApplicationEvent event) {
095 if (LOG.isDebugEnabled()) {
096 LOG.debug("Publishing event: " + event);
097 }
098
099 if (event instanceof ContextRefreshedEvent) {
100 // now lets start the CamelContext so that all its possible
101 // dependencies are initailized
102 try {
103 LOG.debug("Starting the CamelContext now that the ApplicationContext has started");
104 start();
105 } catch (Exception e) {
106 throw new RuntimeCamelException(e);
107 }
108 if (eventEndpoint != null) {
109 eventEndpoint.onApplicationEvent(event);
110 }
111 } else {
112 if (eventEndpoint != null) {
113 eventEndpoint.onApplicationEvent(event);
114 } else {
115 LOG.warn("No eventEndpoint enabled for event: " + event);
116 }
117 }
118 }
119
120 // Properties
121 // -----------------------------------------------------------------------
122
123 public ApplicationContext getApplicationContext() {
124 return applicationContext;
125 }
126
127 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
128 this.applicationContext = applicationContext;
129
130 if (applicationContext instanceof ConfigurableApplicationContext) {
131 addComponent("event", new EventComponent(applicationContext));
132 }
133 }
134
135 public EventEndpoint getEventEndpoint() {
136 return eventEndpoint;
137 }
138
139 public void setEventEndpoint(EventEndpoint eventEndpoint) {
140 this.eventEndpoint = eventEndpoint;
141 }
142
143 // Implementation methods
144 // -----------------------------------------------------------------------
145
146 @Override
147 protected void doStart() throws Exception {
148 super.doStart();
149 if (eventEndpoint == null) {
150 eventEndpoint = createEventEndpoint();
151 }
152 }
153
154 @Override
155 protected Injector createInjector() {
156 return new SpringInjector((AbstractRefreshableApplicationContext)getApplicationContext());
157 }
158
159 @Override
160 protected ComponentResolver createComponentResolver() {
161 ComponentResolver defaultResolver = super.createComponentResolver();
162 return new SpringComponentResolver(getApplicationContext(), defaultResolver);
163 }
164
165 protected EventEndpoint createEventEndpoint() {
166 EventEndpoint endpoint = getEndpoint("event:default", EventEndpoint.class);
167 return endpoint;
168 }
169
170 protected Endpoint convertBeanToEndpoint(String uri, Object bean) {
171 Processor processor = new BeanProcessor(bean, this);
172 return new ProcessorEndpoint(uri, this, processor);
173 }
174
175 @Override
176 protected Registry createRegistry() {
177 return new ApplicationContextRegistry(getApplicationContext());
178 }
179 }