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.Set;
020    
021    import com.google.inject.Injector;
022    import com.google.inject.Provides;
023    import com.google.inject.matcher.Matcher;
024    import com.google.inject.matcher.Matchers;
025    import org.apache.camel.RoutesBuilder;
026    import org.guiceyfruit.Injectors;
027    
028    /**
029     * A Guice Module which injects the CamelContext with all available implementations
030     * of {@link org.apache.camel.RoutesBuilder} which are bound to Guice with an optional {@link Matcher} to filter out the classes required.
031     * <p>
032     * 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
033     * method annotated with @Provides and returning Set<Routes> such as
034     * <code><pre>
035     * public class MyModule extends CamelModule {
036     *   &#64;Provides
037     *   Set&lt;Routes&gt; routes(Injector injector) { ... }
038     * }
039     * </pre></code>
040     *
041     * @version $Revision: 13679 $
042     */
043    public class CamelModuleWithMatchingRoutes extends CamelModule {
044        private final Matcher<Class> matcher;
045    
046        public CamelModuleWithMatchingRoutes() {
047            this(Matchers.subclassesOf(RoutesBuilder.class));
048        }
049    
050        public CamelModuleWithMatchingRoutes(Matcher<Class> matcher) {
051            this.matcher = matcher;
052        }
053    
054        @Provides
055        Set<RoutesBuilder> routes(Injector injector) {
056            return Injectors.getInstancesOf(injector, matcher);
057        }
058    }