View Javadoc
1   /*
2    * Copyright 2010 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.core.config;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  import org.apache.camel.CamelContext;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.openehealth.ipf.commons.core.config.OrderedConfigurer;
28  import org.openehealth.ipf.commons.core.config.Registry;
29  
30  /**
31   * 
32   * Configurer used to autowire all classes extending
33   * the {@link CustomRouteBuilder} abstract class.
34   * 
35   * @author Boris Stanojevic
36   * 
37   */
38  public class CustomRouteBuilderConfigurer<R extends Registry> extends OrderedConfigurer<CustomRouteBuilder, R> 
39         implements Comparator<CustomRouteBuilder> {
40  
41      private CamelContext camelContext;
42      
43      private static final Logger LOG = LoggerFactory.getLogger(CustomRouteBuilderConfigurer.class);
44  
45      @Override
46      public Collection<CustomRouteBuilder> lookup(R registry) {        
47          List<CustomRouteBuilder> list = new ArrayList<>(
48                  registry.beans(CustomRouteBuilder.class).values());
49          Collections.sort(list);
50          return list;
51      }
52  
53      
54      @Override
55      public int compare(CustomRouteBuilder rb1, CustomRouteBuilder rb2) {
56          return rb1.compareTo(rb2);
57      }
58      
59      @Override
60      public void configure(CustomRouteBuilder configuration) throws Exception{
61          if (configuration.getIntercepted() != null) {
62              configuration.getIntercepted().includeRoutes(configuration);
63          } else {
64              camelContext.addRoutes(configuration);
65          }
66          LOG.debug("Custom route builder configured: {}", configuration);
67      }
68  
69      public CamelContext getCamelContext() {
70          return camelContext;
71      }
72  
73      public void setCamelContext(CamelContext camelContext) {
74          this.camelContext = camelContext;
75      }
76  
77  }