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.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    import org.springframework.beans.BeansException;
032    import org.springframework.beans.factory.DisposableBean;
033    import org.springframework.beans.factory.InitializingBean;
034    import org.springframework.context.ApplicationContext;
035    import org.springframework.context.ApplicationContextAware;
036    import org.springframework.context.ApplicationEvent;
037    import org.springframework.context.ConfigurableApplicationContext;
038    import org.springframework.context.event.ContextRefreshedEvent;
039    import org.springframework.context.event.ContextStoppedEvent;
040    import org.springframework.context.support.ClassPathXmlApplicationContext;
041    
042    import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
043    
044    /**
045     * A Spring aware implementation of {@link org.apache.camel.CamelContext} which
046     * will automatically register itself with Springs lifecycle methods plus allows
047     * spring to be used to customize a any <a
048     * href="http://camel.apache.org/type-converter.html">Type Converters</a>
049     * as well as supporting accessing components and beans via the Spring
050     * {@link ApplicationContext}
051     *
052     * @version 
053     */
054    public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean,
055            ApplicationContextAware {
056    
057        private static final transient Logger LOG = LoggerFactory.getLogger(SpringCamelContext.class);
058        private ApplicationContext applicationContext;
059        private EventEndpoint eventEndpoint;
060    
061        public SpringCamelContext() {
062        }
063    
064        public SpringCamelContext(ApplicationContext applicationContext) {
065            setApplicationContext(applicationContext);
066        }
067    
068        public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception {
069            // lets try and look up a configured camel context in the context
070            String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class);
071            if (names.length == 1) {
072                return (SpringCamelContext)applicationContext.getBean(names[0], SpringCamelContext.class);
073            }
074            SpringCamelContext answer = new SpringCamelContext();
075            answer.setApplicationContext(applicationContext);
076            answer.afterPropertiesSet();
077            return answer;
078        }
079    
080        public static SpringCamelContext springCamelContext(String configLocations) throws Exception {
081            return springCamelContext(new ClassPathXmlApplicationContext(configLocations));
082        }
083    
084        public void afterPropertiesSet() throws Exception {
085            maybeStart();
086        }
087    
088        public void destroy() throws Exception {
089            stop();
090        }
091    
092        public void onApplicationEvent(ApplicationEvent event) {
093            if (LOG.isDebugEnabled()) {
094                LOG.debug("onApplicationEvent: " + event);
095            }
096    
097            if (event instanceof ContextRefreshedEvent) {
098                // now lets start the CamelContext so that all its possible
099                // dependencies are initialized
100                try {
101                    maybeStart();
102                } catch (Exception e) {
103                    throw wrapRuntimeCamelException(e);
104                }
105            } else if (event instanceof ContextStoppedEvent) {
106                try {
107                    maybeStop();
108                } catch (Exception e) {
109                    throw wrapRuntimeCamelException(e);
110                }
111            }
112    
113            if (eventEndpoint != null) {
114                eventEndpoint.onApplicationEvent(event);
115            } else {
116                LOG.info("No spring-event endpoint enabled to handle event: " + event);
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            ClassLoader cl;
130    
131            // set the application context classloader
132            if (applicationContext != null && applicationContext.getClassLoader() != null) {
133                cl = applicationContext.getClassLoader();
134            } else {
135                LOG.warn("Cannot find the class loader from application context, using the thread context class loader instead");
136                cl = Thread.currentThread().getContextClassLoader();
137            }
138            if (LOG.isDebugEnabled()) {
139                LOG.debug("Set the application context classloader to: " + cl);
140            }
141            this.setApplicationContextClassLoader(cl);
142    
143            if (applicationContext instanceof ConfigurableApplicationContext) {
144                // only add if not already added
145                if (hasComponent("spring-event") == null) {
146                    addComponent("spring-event", new EventComponent(applicationContext));
147                }
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            super.doStart();
165            if (eventEndpoint == null) {
166                eventEndpoint = createEventEndpoint();
167            }
168        }
169    
170        @Override
171        protected Injector createInjector() {
172            if (applicationContext instanceof ConfigurableApplicationContext) {
173                return new SpringInjector((ConfigurableApplicationContext)applicationContext);
174            } else {
175                LOG.warn("Cannot use SpringInjector as applicationContext is not a ConfigurableApplicationContext as its: "
176                          + applicationContext);
177                return super.createInjector();
178            }
179        }
180    
181        protected EventEndpoint createEventEndpoint() {
182            return getEndpoint("spring-event:default", EventEndpoint.class);
183        }
184    
185        protected Endpoint convertBeanToEndpoint(String uri, Object bean) {
186            // We will use the type convert to build the endpoint first
187            Endpoint endpoint = getTypeConverter().convertTo(Endpoint.class, bean);
188            if (endpoint != null) {
189                endpoint.setCamelContext(this);
190                return endpoint;
191            }
192    
193            return new ProcessorEndpoint(uri, this, new BeanProcessor(bean, this));
194        }
195    
196        @Override
197        protected Registry createRegistry() {
198            return new ApplicationContextRegistry(getApplicationContext());
199        }
200    
201        private void maybeStart() throws Exception {
202            if (!isStarted() && !isStarting()) {
203                start();
204            } else {
205                // ignore as Camel is already started
206                LOG.trace("Ignoring maybeStart() as Apache Camel is already started");
207            }
208        }
209    
210        private void maybeStop() throws Exception {
211            if (!isStopping() && !isStopped()) {
212                stop();
213            } else {
214                // ignore as Camel is already stopped
215                LOG.trace("Ignoring maybeStop() as Apache Camel is already stopped");
216            }
217        }
218    
219        @Override
220        public String toString() {
221            StringBuilder sb = new StringBuilder();
222            sb.append("SpringCamelContext(").append(getName()).append(")");
223            if (applicationContext != null) {
224                sb.append(" with spring id ").append(applicationContext.getId());
225            }
226            return sb.toString();
227        }
228    
229    }