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.util; 018 019 020 import java.util.ArrayList; 021 import java.util.Collection; 022 import java.util.List; 023 024 import org.apache.camel.CamelContext; 025 import org.apache.camel.Endpoint; 026 import org.apache.camel.Exchange; 027 import org.apache.camel.Expression; 028 import org.apache.camel.NoSuchEndpointException; 029 import org.apache.camel.spi.Injector; 030 import org.apache.camel.spi.Language; 031 import org.apache.camel.spi.Registry; 032 import static org.apache.camel.util.ObjectHelper.notNull; 033 034 /** 035 * A number of helper methods 036 * 037 * @version $Revision: 47152 $ 038 */ 039 public final class CamelContextHelper { 040 /** 041 * Utility classes should not have a public constructor. 042 */ 043 private CamelContextHelper() { 044 } 045 046 /** 047 * Returns the mandatory endpoint for the given URI or the 048 * {@link org.apache.camel.NoSuchEndpointException} is thrown 049 */ 050 public static Endpoint getMandatoryEndpoint(CamelContext camelContext, String uri) 051 throws NoSuchEndpointException { 052 Endpoint endpoint = camelContext.getEndpoint(uri); 053 if (endpoint == null) { 054 throw new NoSuchEndpointException(uri); 055 } else { 056 return endpoint; 057 } 058 } 059 060 /** 061 * Returns the mandatory endpoint for the given URI and type or the 062 * {@link org.apache.camel.NoSuchEndpointException} is thrown 063 */ 064 public static <T extends Endpoint> T getMandatoryEndpoint(CamelContext camelContext, String uri, Class<T> type) { 065 Endpoint endpoint = getMandatoryEndpoint(camelContext, uri); 066 return ObjectHelper.cast(type, endpoint); 067 } 068 069 /** 070 * Returns a list of all endpoints of the given type 071 * 072 * @param camelContext the camel context 073 * @param type the type of the endpoints requested 074 * @return a list which may be empty of all the endpoint instances of the 075 * given type 076 */ 077 public static <T> List<T> getSingletonEndpoints(CamelContext camelContext, Class<T> type) { 078 List<T> answer = new ArrayList<T>(); 079 Collection<Endpoint> endpoints = camelContext.getSingletonEndpoints(); 080 for (Endpoint endpoint : endpoints) { 081 if (type.isInstance(endpoint)) { 082 T value = type.cast(endpoint); 083 answer.add(value); 084 } 085 } 086 return answer; 087 } 088 089 /** 090 * Converts the given value to the requested type 091 */ 092 public static <T> T convertTo(CamelContext context, Class<T> type, Object value) { 093 notNull(context, "camelContext"); 094 return context.getTypeConverter().convertTo(type, value); 095 } 096 097 /** 098 * Converts the given value to the specified type throwing an {@link IllegalArgumentException} 099 * if the value could not be converted to a non null value 100 */ 101 public static <T> T mandatoryConvertTo(CamelContext context, Class<T> type, Object value) { 102 T answer = convertTo(context, type, value); 103 if (answer == null) { 104 throw new IllegalArgumentException("Value " + value + " converted to " + type.getName() + " cannot be null"); 105 } 106 return answer; 107 } 108 109 /** 110 * Creates a new instance of the given type using the {@link Injector} on the given 111 * {@link CamelContext} 112 */ 113 public static <T> T newInstance(CamelContext context, Class<T> beanType) { 114 return context.getInjector().newInstance(beanType); 115 } 116 117 /** 118 * Look up the given named bean in the {@link Registry} on the 119 * {@link CamelContext} 120 */ 121 public static Object lookup(CamelContext context, String name) { 122 return context.getRegistry().lookup(name); 123 } 124 125 /** 126 * Look up the given named bean of the given type in the {@link Registry} on the 127 * {@link CamelContext} 128 */ 129 public static <T> T lookup(CamelContext context, String name, Class<T> beanType) { 130 return context.getRegistry().lookup(name, beanType); 131 } 132 133 /** 134 * Look up the given named bean in the {@link Registry} on the 135 * {@link CamelContext} or throws 136 */ 137 public static Object mandatoryLookup(CamelContext context, String name) { 138 Object answer = lookup(context, name); 139 notNull(answer, "registry entry called " + name); 140 return answer; 141 } 142 143 /** 144 * Look up the given named bean of the given type in the {@link Registry} on the 145 * {@link CamelContext} 146 */ 147 public static <T> T mandatoryLookup(CamelContext context, String name, Class<T> beanType) { 148 T answer = lookup(context, name, beanType); 149 notNull(answer, "registry entry called " + name + " of type " + beanType.getName()); 150 return answer; 151 } 152 153 /** 154 * Resolves the given language name into a {@link Language} or throws an exception if it could not be converted 155 */ 156 public static Language resolveMandatoryLanguage(CamelContext camelContext, String languageName) { 157 notNull(camelContext, "camelContext"); 158 notNull(languageName, "languageName"); 159 160 Language language = camelContext.resolveLanguage(languageName); 161 if (language == null) { 162 throw new IllegalArgumentException("Could not resolve language: " + languageName); 163 } 164 return language; 165 } 166 167 /** 168 * Resolves the mandatory language name and expression text into a {@link Expression} instance 169 * throwing an exception if it could not be created 170 */ 171 public static Expression resolveMandatoryExpression(CamelContext camelContext, String languageName, String expressionText) { 172 notNull(expressionText, "expressionText"); 173 174 Language language = resolveMandatoryLanguage(camelContext, languageName); 175 Expression<Exchange> expression = language.createExpression(expressionText); 176 if (expression == null) { 177 throw new IllegalArgumentException("Could not create expression: " + expressionText + " with language: " + language); 178 } 179 return expression; 180 } 181 }