PageRenderTime 37ms CodeModel.GetById 28ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/components/camel-guice/src/main/java/org/apache/camel/guice/GuiceCamelContext.java

https://gitlab.com/matticala/apache-camel
Java | 176 lines | 129 code | 23 blank | 24 comment | 3 complexity | cf521b130146f1c72133a56a3bdd3e25 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one or more
  3 * contributor license agreements.  See the NOTICE file distributed with
  4 * this work for additional information regarding copyright ownership.
  5 * The ASF licenses this file to You under the Apache License, Version 2.0
  6 * (the "License"); you may not use this file except in compliance with
  7 * the License.  You may obtain a copy of the License at
  8 *
  9 *      http://www.apache.org/licenses/LICENSE-2.0
 10 *
 11 * Unless required by applicable law or agreed to in writing, software
 12 * distributed under the License is distributed on an "AS IS" BASIS,
 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14 * See the License for the specific language governing permissions and
 15 * limitations under the License.
 16 */
 17package org.apache.camel.guice;
 18
 19import java.util.List;
 20import java.util.Set;
 21
 22import javax.annotation.PostConstruct;
 23import javax.annotation.PreDestroy;
 24import javax.naming.Context;
 25import javax.naming.InitialContext;
 26
 27import com.google.inject.Binding;
 28import com.google.inject.Inject;
 29
 30import org.apache.camel.ErrorHandlerFactory;
 31import org.apache.camel.RoutesBuilder;
 32import org.apache.camel.TypeConverter;
 33import org.apache.camel.guice.impl.GuiceInjector;
 34import org.apache.camel.guice.inject.Injectors;
 35import org.apache.camel.impl.DefaultCamelContext;
 36import org.apache.camel.impl.JndiRegistry;
 37import org.apache.camel.spi.ComponentResolver;
 38import org.apache.camel.spi.Injector;
 39import org.apache.camel.spi.InterceptStrategy;
 40import org.apache.camel.spi.LanguageResolver;
 41import org.apache.camel.spi.LifecycleStrategy;
 42import org.apache.camel.spi.Registry;
 43import org.apache.camel.util.ObjectHelper;
 44
 45/**
 46 * The default CamelContext implementation for working with Guice.
 47 * <p/>
 48 * It is recommended you use this implementation with the
 49 * <a href="http://code.google.com/p/guiceyfruit/wiki/GuiceyJndi">Guicey JNDI Provider</a>
 50 *
 51 * @version 
 52 */
 53public class GuiceCamelContext extends DefaultCamelContext {
 54    private final com.google.inject.Injector injector;
 55
 56    @Inject
 57    public GuiceCamelContext(com.google.inject.Injector injector) {
 58        this.injector = injector;
 59    }
 60
 61    @PostConstruct
 62    @Override
 63    public void start() {
 64        try {
 65            super.start();
 66        } catch (Exception e) {
 67            throw ObjectHelper.wrapRuntimeCamelException(e);
 68        }
 69    }
 70
 71    @PreDestroy
 72    @Override
 73    public void stop() {
 74        try {
 75            super.stop();
 76        } catch (Exception e) {
 77            throw ObjectHelper.wrapRuntimeCamelException(e);
 78        }
 79    }
 80
 81    @Inject
 82    public void setRouteBuilders(Set<RoutesBuilder> routeBuilders) {
 83        for (RoutesBuilder builder : routeBuilders) {
 84            try {
 85                addRoutes(builder);
 86            } catch (Exception e) {
 87                throw ObjectHelper.wrapRuntimeCamelException(e);
 88            }
 89        }
 90    }
 91
 92    @Override
 93    @Inject(optional = true)
 94    public void setRegistry(Registry registry) {
 95        super.setRegistry(registry);
 96    }
 97
 98    @Override
 99    @Inject(optional = true)
100    public void setJndiContext(Context jndiContext) {
101        super.setJndiContext(jndiContext);
102    }
103
104    @Override
105    @Inject(optional = true)
106    public void setInjector(Injector injector) {
107        super.setInjector(injector);
108    }
109
110    @Override
111    @Inject(optional = true)
112    public void setComponentResolver(ComponentResolver componentResolver) {
113        super.setComponentResolver(componentResolver);
114    }
115
116    @Override
117    @Inject(optional = true)
118    public void setAutoCreateComponents(boolean autoCreateComponents) {
119        super.setAutoCreateComponents(autoCreateComponents);
120    }
121
122    @Override
123    @Inject(optional = true)
124    public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) {
125        super.setErrorHandlerBuilder(errorHandlerBuilder);
126    }
127
128    @Override
129    @Inject(optional = true)
130    public void setInterceptStrategies(List<InterceptStrategy> interceptStrategies) {
131        super.setInterceptStrategies(interceptStrategies);
132    }
133
134    @Override
135    @Inject(optional = true)
136    public void setLanguageResolver(LanguageResolver languageResolver) {
137        super.setLanguageResolver(languageResolver);
138    }
139
140    @Override
141    @Inject(optional = true)
142    public void setLifecycleStrategies(List<LifecycleStrategy> lifecycleStrategies) {
143        super.setLifecycleStrategies(lifecycleStrategies);
144    }
145
146    @Override
147    @Inject(optional = true)
148    public void setTypeConverter(TypeConverter typeConverter) {
149        super.setTypeConverter(typeConverter);
150    }
151
152    @Override
153    protected Injector createInjector() {
154        return new GuiceInjector(injector);
155    }
156
157    @Override
158    protected Registry createRegistry() {
159        Context context = createContext();
160        return new JndiRegistry(context);
161    }
162
163    protected Context createContext() {
164        Set<Binding<?>> bindings = Injectors.getBindingsOf(injector, Context.class);
165        try {
166            if (bindings.isEmpty()) {
167                return new InitialContext();
168            } else {
169                return injector.getInstance(Context.class);
170            }
171        } catch (Exception e) {
172            throw ObjectHelper.wrapRuntimeCamelException(e);
173        }
174    }
175
176}