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.guice;
018    
019    import java.util.List;
020    import java.util.Set;
021    
022    import javax.annotation.PostConstruct;
023    import javax.annotation.PreDestroy;
024    import javax.naming.Context;
025    import javax.naming.InitialContext;
026    
027    import com.google.inject.Binding;
028    import com.google.inject.Inject;
029    
030    import org.apache.camel.Route;
031    import org.apache.camel.RoutesBuilder;
032    import org.apache.camel.RuntimeCamelException;
033    import org.apache.camel.TypeConverter;
034    import org.apache.camel.builder.ErrorHandlerBuilder;
035    import org.apache.camel.guice.impl.GuiceInjector;
036    import org.apache.camel.impl.DefaultCamelContext;
037    import org.apache.camel.impl.JndiRegistry;
038    import org.apache.camel.spi.ComponentResolver;
039    import org.apache.camel.spi.Injector;
040    import org.apache.camel.spi.InterceptStrategy;
041    import org.apache.camel.spi.LanguageResolver;
042    import org.apache.camel.spi.LifecycleStrategy;
043    import org.apache.camel.spi.Registry;
044    import org.guiceyfruit.Injectors;
045    
046    /**
047     * The default CamelContext implementation for working with Guice.
048     * <p/>
049     * It is recommended you use this implementation with the
050     * <a href="http://code.google.com/p/guiceyfruit/wiki/GuiceyJndi">Guicey JNDI Provider</a>
051     *
052     * @version $Revision: 21807 $
053     */
054    public class GuiceCamelContext extends DefaultCamelContext {
055        private final com.google.inject.Injector injector;
056    
057        @Inject
058        public GuiceCamelContext(com.google.inject.Injector injector) {
059            this.injector = injector;
060        }
061    
062        @PostConstruct
063        @Override
064        public void start() throws Exception {
065            super.start();
066        }
067    
068        @PreDestroy
069        @Override
070        public void stop() throws Exception {
071            super.stop();
072        }
073    
074        @Inject
075        public void setRouteBuilders(Set<RoutesBuilder> routeBuilders) throws Exception {
076            for (RoutesBuilder builder : routeBuilders) {
077                addRoutes(builder);
078            }
079        }
080    
081        @Override
082        @Inject(optional = true)
083        public void setRegistry(Registry registry) {
084            super.setRegistry(registry);
085        }
086    
087        @Override
088        @Inject(optional = true)
089        public void setJndiContext(Context jndiContext) {
090            super.setJndiContext(jndiContext);
091        }
092    
093        @Override
094        @Inject(optional = true)
095        public void setInjector(Injector injector) {
096            super.setInjector(injector);
097        }
098    
099        @Override
100        @Inject(optional = true)
101        public void setComponentResolver(ComponentResolver componentResolver) {
102            super.setComponentResolver(componentResolver);
103        }
104    
105        @Override
106        @Inject(optional = true)
107        public void setAutoCreateComponents(boolean autoCreateComponents) {
108            super.setAutoCreateComponents(autoCreateComponents);
109        }
110    
111        @Override
112        @Inject(optional = true)
113        public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
114            super.setErrorHandlerBuilder(errorHandlerBuilder);
115        }
116    
117        @Override
118        @Inject(optional = true)
119        public void setInterceptStrategies(List<InterceptStrategy> interceptStrategies) {
120            super.setInterceptStrategies(interceptStrategies);
121        }
122    
123        @Override
124        @Inject(optional = true)
125        public void setLanguageResolver(LanguageResolver languageResolver) {
126            super.setLanguageResolver(languageResolver);
127        }
128    
129        @Override
130        @Inject(optional = true)
131        public void setLifecycleStrategies(List<LifecycleStrategy> lifecycleStrategies) {
132            super.setLifecycleStrategies(lifecycleStrategies);
133        }
134    
135        @Override
136        @Inject(optional = true)
137        public void setTypeConverter(TypeConverter typeConverter) {
138            super.setTypeConverter(typeConverter);
139        }
140    
141        @Override
142        protected Injector createInjector() {
143            return new GuiceInjector(injector);
144        }
145    
146        @Override
147        protected Registry createRegistry() {
148            Context context = createContext();
149            return new JndiRegistry(context);
150        }
151    
152        protected Context createContext() {
153            Set<Binding<?>> bindings = Injectors.getBindingsOf(injector, Context.class);
154            try {
155                if (bindings.isEmpty()) {
156                    return new InitialContext();
157                } else {
158                    return injector.getInstance(Context.class);
159                }
160            } catch (Exception e) {
161                throw new RuntimeCamelException(e);
162            }
163        }
164    
165    }