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.Processor;
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.Injector;
028    import org.apache.camel.spi.Registry;
029    import org.apache.camel.spring.spi.ApplicationContextRegistry;
030    import org.apache.camel.spring.spi.SpringInjector;
031    import org.apache.camel.util.ObjectHelper;
032    import org.apache.commons.logging.Log;
033    import org.apache.commons.logging.LogFactory;
034    import org.springframework.beans.BeansException;
035    import org.springframework.beans.factory.DisposableBean;
036    import org.springframework.beans.factory.InitializingBean;
037    import org.springframework.context.ApplicationContext;
038    import org.springframework.context.ApplicationContextAware;
039    import org.springframework.context.ApplicationEvent;
040    import org.springframework.context.ApplicationListener;
041    import org.springframework.context.ConfigurableApplicationContext;
042    import org.springframework.context.event.ContextRefreshedEvent;
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: 705853 $
056     */
057    public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean,
058        ApplicationContextAware, ApplicationListener {
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)
073            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    
086        public static SpringCamelContext springCamelContext(String configLocations) throws Exception {
087            return springCamelContext(new ClassPathXmlApplicationContext(configLocations));
088        }
089    
090        public void afterPropertiesSet() throws Exception {
091            maybeStart();
092        }
093    
094        private void maybeStart() throws Exception {
095            if (getShouldStartContext()) {
096                LOG.debug("Starting the CamelContext now that the ApplicationContext has started");
097                start();
098            } else {
099                LOG.debug("Not starting the CamelContext since shouldStartContext property was true.");
100            }
101        }
102    
103        public void destroy() throws Exception {
104            stop();
105        }
106    
107        public void onApplicationEvent(ApplicationEvent event) {
108            if (LOG.isDebugEnabled()) {
109                LOG.debug("Publishing spring-event: " + event);
110            }
111    
112            if (event instanceof ContextRefreshedEvent) {
113                // now lets start the CamelContext so that all its possible
114                // dependencies are initialized
115                try {
116                    maybeStart();
117                } catch (RuntimeException e) {
118                    throw e;
119                } catch (Exception e) {
120                    throw wrapRuntimeCamelException(e);
121                }
122                if (eventEndpoint != null) {
123                    eventEndpoint.onApplicationEvent(event);
124                }
125            } else {
126                if (eventEndpoint != null) {
127                    eventEndpoint.onApplicationEvent(event);
128                } else {
129                    LOG.warn("No spring-event endpoint enabled for: " + event);
130                }
131            }
132        }
133    
134        // Properties
135        // -----------------------------------------------------------------------
136    
137        public ApplicationContext getApplicationContext() {
138            return applicationContext;
139        }
140    
141        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
142            this.applicationContext = applicationContext;
143    
144            if (applicationContext instanceof ConfigurableApplicationContext) {
145                addComponent("spring-event", new EventComponent(applicationContext));
146            }
147        }
148    
149        public EventEndpoint getEventEndpoint() {
150            return eventEndpoint;
151        }
152    
153        public void setEventEndpoint(EventEndpoint eventEndpoint) {
154            this.eventEndpoint = eventEndpoint;
155        }
156    
157        // Implementation methods
158        // -----------------------------------------------------------------------
159    
160        @Override
161        protected void doStart() throws Exception {
162            maybeDoStart();
163        }
164    
165        protected void maybeDoStart() throws Exception {
166            if (getShouldStartContext()) {
167                super.doStart();
168                if (eventEndpoint == null) {
169                    eventEndpoint = createEventEndpoint();
170                }
171            }
172        }
173    
174        @Override
175        protected Injector createInjector() {
176            if (applicationContext instanceof ConfigurableApplicationContext) {
177                return new SpringInjector((ConfigurableApplicationContext)applicationContext);
178            } else {
179                LOG.warn("Cannot use SpringInjector as applicationContext is not a ConfigurableApplicationContext as its: "
180                          + applicationContext);
181                return super.createInjector();
182            }
183        }
184    
185        protected EventEndpoint createEventEndpoint() {
186            EventEndpoint endpoint = getEndpoint("spring-event:default", EventEndpoint.class);
187            return endpoint;
188        }
189    
190        protected Endpoint convertBeanToEndpoint(String uri, Object bean) {
191            // We will use the type convert to build the endpoint first
192            try {
193                Endpoint endpoint = getTypeConverter().convertTo(Endpoint.class, bean);
194                if (endpoint != null) {
195                    endpoint.setCamelContext(this);
196                    return endpoint;
197                }
198            } catch (NoTypeConversionAvailableException ex) {
199                // ignore, handled below
200            }
201    
202            return new ProcessorEndpoint(uri, this, new BeanProcessor(bean, this));
203        }
204    
205        @Override
206        protected Registry createRegistry() {
207            return new ApplicationContextRegistry(getApplicationContext());
208        }
209    
210        public void setShouldStartContext(boolean shouldStartContext) {
211            this.shouldStartContext = shouldStartContext;
212        }
213    
214        public boolean getShouldStartContext() {
215            return shouldStartContext;
216        }
217    }