001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.spring;
019
020 import org.apache.camel.impl.DefaultCamelContext;
021 import org.apache.camel.spi.Injector;
022 import org.apache.camel.spi.ComponentResolver;
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.spring.spi.SpringComponentResolver;
025 import org.apache.camel.spring.spi.SpringInjector;
026 import org.apache.camel.spring.component.BeanComponent;
027 import org.springframework.beans.factory.InitializingBean;
028 import org.springframework.beans.factory.DisposableBean;
029 import org.springframework.beans.BeansException;
030 import org.springframework.context.ApplicationContextAware;
031 import org.springframework.context.ApplicationContext;
032 import org.springframework.context.support.ClassPathXmlApplicationContext;
033 import org.springframework.context.support.AbstractRefreshableApplicationContext;
034
035 /**
036 * A Spring aware implementation of {@link CamelContext} which will automatically register itself with Springs lifecycle
037 * methods plus allows spring to be used to customize a any
038 * <a href="http://activemq.apache.org/camel/type-converter.html">Type Converters</a> as well as supporting accessing components
039 * and beans via the Spring {@link ApplicationContext}
040 *
041 * @version $Revision: 538919 $
042 */
043 public class SpringCamelContext extends DefaultCamelContext implements InitializingBean, DisposableBean, ApplicationContextAware {
044 private ApplicationContext applicationContext;
045
046 public SpringCamelContext() {
047 }
048
049 public SpringCamelContext(ApplicationContext applicationContext) {
050 setApplicationContext(applicationContext);
051 }
052
053 public static SpringCamelContext springCamelContext(ApplicationContext applicationContext) throws Exception {
054 // lets try and look up a configured camel context in the context
055 String[] names = applicationContext.getBeanNamesForType(SpringCamelContext.class);
056 if (names.length == 1) {
057 return (SpringCamelContext) applicationContext.getBean(names[0], SpringCamelContext.class);
058 }
059 SpringCamelContext answer = new SpringCamelContext();
060 answer.setApplicationContext(applicationContext);
061 answer.afterPropertiesSet();
062 return answer;
063 }
064
065 public static SpringCamelContext springCamelContext(String configLocations) throws Exception {
066 return springCamelContext(new ClassPathXmlApplicationContext(configLocations));
067 }
068
069 public void afterPropertiesSet() throws Exception {
070 // lets force lazy initialisation
071 getInjector();
072
073 start();
074 }
075
076 public void destroy() throws Exception {
077 stop();
078 }
079
080 public ApplicationContext getApplicationContext() {
081 return applicationContext;
082 }
083
084 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
085 this.applicationContext = applicationContext;
086 addComponent("bean", new BeanComponent(applicationContext));
087 }
088
089 @Override
090 protected Injector createInjector() {
091 return new SpringInjector((AbstractRefreshableApplicationContext) getApplicationContext());
092 }
093
094 @Override
095 protected ComponentResolver createComponentResolver() {
096 ComponentResolver defaultResolver = super.createComponentResolver();
097 return new SpringComponentResolver(getApplicationContext(), defaultResolver);
098 }
099 }