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