| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| SpringRouteBuilder |
|
| 0.0;0 |
| 1 | /** |
|
| 2 | * |
|
| 3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
| 4 | * contributor license agreements. See the NOTICE file distributed with |
|
| 5 | * this work for additional information regarding copyright ownership. |
|
| 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
| 7 | * (the "License"); you may not use this file except in compliance with |
|
| 8 | * the License. You may obtain a copy of the License at |
|
| 9 | * |
|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 11 | * |
|
| 12 | * Unless required by applicable law or agreed to in writing, software |
|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 15 | * See the License for the specific language governing permissions and |
|
| 16 | * limitations under the License. |
|
| 17 | */ |
|
| 18 | package org.apache.camel.spring; |
|
| 19 | ||
| 20 | import org.apache.camel.CamelContext; |
|
| 21 | import org.apache.camel.Exchange; |
|
| 22 | import org.apache.camel.builder.RouteBuilder; |
|
| 23 | import org.springframework.context.ApplicationContext; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * An extension of the {@link RouteBuilder} to provide some additional helper methods |
|
| 27 | * |
|
| 28 | * @version $Revision: 1.1 $ |
|
| 29 | */ |
|
| 30 | 0 | public abstract class SpringRouteBuilder extends RouteBuilder { |
| 31 | private ApplicationContext applicationContext; |
|
| 32 | ||
| 33 | /** |
|
| 34 | * Looks up the bean with the given name in the application context and returns it, or throws an exception if the |
|
| 35 | * bean is not present or is not of the given type |
|
| 36 | * |
|
| 37 | * @param type the type of the bean |
|
| 38 | * @param beanName the name of the bean in the application context |
|
| 39 | * @return the bean |
|
| 40 | */ |
|
| 41 | public <T> T bean(Class<T> type, String beanName) { |
|
| 42 | 0 | ApplicationContext context = getApplicationContext(); |
| 43 | 0 | return (T) context.getBean(beanName, type); |
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * Looks up the bean with the given type in the application context and returns it, or throws an exception if the |
|
| 48 | * bean is not present or there are multiple possible beans to choose from for the given type |
|
| 49 | * |
|
| 50 | * @param type the type of the bean |
|
| 51 | * @return the bean |
|
| 52 | */ |
|
| 53 | public <T> T bean(Class<T> type) { |
|
| 54 | 0 | ApplicationContext context = getApplicationContext(); |
| 55 | 0 | String[] names = context.getBeanNamesForType(type, true, true); |
| 56 | 0 | if (names != null) { |
| 57 | 0 | int count = names.length; |
| 58 | 0 | if (count == 1) { |
| 59 | // lets instantiate the single bean |
|
| 60 | 0 | return (T) context.getBean(names[0]); |
| 61 | } |
|
| 62 | 0 | else if (count > 1) { |
| 63 | 0 | throw new IllegalArgumentException("Too many beans in the application context of type: " + type + ". Found: " + count); |
| 64 | } |
|
| 65 | } |
|
| 66 | 0 | throw new IllegalArgumentException("No bean available in the application context of type: " + type); |
| 67 | } |
|
| 68 | ||
| 69 | /** |
|
| 70 | * Returns the application context which has been configured via the {@link #setApplicationContext(ApplicationContext)} |
|
| 71 | * method or from the underlying {@link SpringCamelContext} |
|
| 72 | * |
|
| 73 | * @return |
|
| 74 | */ |
|
| 75 | public ApplicationContext getApplicationContext() { |
|
| 76 | 0 | if (applicationContext == null) { |
| 77 | 0 | CamelContext camelContext = getContext(); |
| 78 | 0 | if (camelContext instanceof SpringCamelContext) { |
| 79 | 0 | SpringCamelContext springCamelContext = (SpringCamelContext) camelContext; |
| 80 | 0 | return springCamelContext.getApplicationContext(); |
| 81 | } |
|
| 82 | else { |
|
| 83 | 0 | throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured"); |
| 84 | } |
|
| 85 | } |
|
| 86 | 0 | return applicationContext; |
| 87 | } |
|
| 88 | ||
| 89 | /** |
|
| 90 | * Sets the application context to use to lookup beans |
|
| 91 | */ |
|
| 92 | public void setApplicationContext(ApplicationContext applicationContext) { |
|
| 93 | 0 | this.applicationContext = applicationContext; |
| 94 | 0 | } |
| 95 | } |