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: 36635 $ 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 a list of all endpoints of the given type 062 * 063 * @param camelContext 064 * @param type the type of the endpoints requested 065 * @return a list which may be empty of all the endpoint instances of the 066 * given type 067 */ 068 public static <T> List<T> getSingletonEndpoints(CamelContext camelContext, Class<T> type) { 069 List<T> answer = new ArrayList<T>(); 070 Collection<Endpoint> endpoints = camelContext.getSingletonEndpoints(); 071 for (Endpoint endpoint : endpoints) { 072 if (type.isInstance(endpoint)) { 073 T value = type.cast(endpoint); 074 answer.add(value); 075 } 076 } 077 return answer; 078 } 079 080 /** 081 * Converts the given value to the requested type 082 */ 083 public static <T> T convertTo(CamelContext context, Class<T> type, Object value) { 084 notNull(context, "camelContext"); 085 return context.getTypeConverter().convertTo(type, value); 086 } 087 088 /** 089 * Converts the given value to the specified type throwing an {@link IllegalArgumentException} 090 * if the value could not be converted to a non null value 091 */ 092 public static <T> T mandatoryConvertTo(CamelContext context, Class<T> type, Object value) { 093 T answer = convertTo(context, type, value); 094 if (answer == null) { 095 throw new IllegalArgumentException("Value " + value + " converted to " + type.getName() + " cannot be null"); 096 } 097 return answer; 098 } 099 100 /** 101 * Creates a new instance of the given type using the {@link Injector} on the given 102 * {@link CamelContext} 103 */ 104 public static <T> T newInstance(CamelContext context, Class<T> beanType) { 105 return context.getInjector().newInstance(beanType); 106 } 107 108 /** 109 * Look up the given named bean in the {@link Registry} on the 110 * {@link CamelContext} 111 */ 112 public static Object lookup(CamelContext context, String name) { 113 return context.getRegistry().lookup(name); 114 } 115 116 /** 117 * Look up the given named bean of the given type in the {@link Registry} on the 118 * {@link CamelContext} 119 */ 120 public static <T> T lookup(CamelContext context, String name, Class<T> beanType) { 121 return context.getRegistry().lookup(name, beanType); 122 } 123 124 /** 125 * Look up the given named bean in the {@link Registry} on the 126 * {@link CamelContext} or throws 127 */ 128 public static Object mandatoryLookup(CamelContext context, String name) { 129 Object answer = lookup(context, name); 130 notNull(answer, "registry entry called " + name); 131 return answer; 132 } 133 134 /** 135 * Look up the given named bean of the given type in the {@link Registry} on the 136 * {@link CamelContext} 137 */ 138 public static <T> T mandatoryLookup(CamelContext context, String name, Class<T> beanType) { 139 T answer = lookup(context, name, beanType); 140 notNull(answer, "registry entry called " + name + " of type " + beanType.getName()); 141 return answer; 142 } 143 144 /** 145 * Resolves the given language name into a {@link Language} or throws an exception if it could not be converted 146 */ 147 public static Language resolveMandatoryLanguage(CamelContext camelContext, String languageName) { 148 notNull(camelContext, "camelContext"); 149 notNull(languageName, "languageName"); 150 151 Language language = camelContext.resolveLanguage(languageName); 152 if (language == null) { 153 throw new IllegalArgumentException("Could not resolve language: " + languageName); 154 } 155 return language; 156 } 157 158 /** 159 * Resolves the mandatory language name and expression text into a {@link Expression} instance 160 * throwing an exception if it could not be created 161 */ 162 public static Expression resolveMandatoryExpression(CamelContext camelContext, String languageName, String expressionText) { 163 notNull(expressionText, "expressionText"); 164 165 Language language = resolveMandatoryLanguage(camelContext, languageName); 166 Expression<Exchange> expression = language.createExpression(expressionText); 167 if (expression == null) { 168 throw new IllegalArgumentException("Could not create expression: " + expressionText + " with language: " + language); 169 } 170 return expression; 171 } 172 }