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.HashSet;
020 import java.util.Set;
021
022 import com.google.inject.Injector;
023 import com.google.inject.Provides;
024 import com.google.inject.internal.Sets;
025
026 import org.apache.camel.RoutesBuilder;
027
028 /**
029 * A Guice Module which injects the CamelContext with the specified {@link org.apache.camel.RoutesBuilder} types - which are then injected by Guice.
030 * <p>
031 * If you wish to bind all of the bound {@link org.apache.camel.RoutesBuilder} implementations available - maybe with some filter applied - then
032 * please use the {@link org.apache.camel.guice.CamelModuleWithMatchingRoutes}.
033 * <p>
034 * Or if you would like to specify exactly which {@link org.apache.camel.RoutesBuilder} to bind then use the {@link CamelModule} and create a provider
035 * method annotated with @Provides and returning Set<Routes> such as
036 * <code><pre>
037 * public class MyModule extends CamelModule {
038 * @Provides
039 * Set<Routes> routes(Injector injector) { ... }
040 * }
041 * </pre></code>
042 *
043 *
044 * @version $Revision: 13679 $
045 */
046 public class CamelModuleWithRouteTypes extends CamelModule {
047 private Set<Class<? extends RoutesBuilder>> routes;
048
049 public CamelModuleWithRouteTypes(Class<? extends RoutesBuilder>... routeTypes) {
050 this.routes = new HashSet<Class<? extends RoutesBuilder>>();
051 for (Class<? extends RoutesBuilder> route : routeTypes) {
052 routes.add(route);
053 }
054 }
055
056 public CamelModuleWithRouteTypes(Set<Class<? extends RoutesBuilder>> routes) {
057 this.routes = routes;
058 }
059
060 @Provides
061 Set<RoutesBuilder> routes(Injector injector) {
062 Set<RoutesBuilder> answer = Sets.newHashSet();
063 for (Class<? extends RoutesBuilder> type : routes) {
064 RoutesBuilder route = injector.getInstance(type);
065 answer.add(route);
066 }
067 return answer;
068 }
069 }