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 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.List;
022
023 import org.apache.camel.CamelContext;
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.Expression;
026 import org.apache.camel.NoSuchEndpointException;
027 import org.apache.camel.spi.Language;
028
029 import static org.apache.camel.util.ObjectHelper.isEmpty;
030 import static org.apache.camel.util.ObjectHelper.isNotEmpty;
031 import static org.apache.camel.util.ObjectHelper.notNull;
032
033 /**
034 * A number of helper methods
035 *
036 * @version $Revision: 751221 $
037 */
038 public final class CamelContextHelper {
039
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 <T> List<T> getEndpoints(CamelContext camelContext, Class<T> type) {
078 return getEndpointsImpl(camelContext, type, false);
079 }
080
081 /**
082 * Returns a list of all singleton endpoints of the given type
083 *
084 * @param camelContext the camel context
085 * @param type the type of the endpoints requested
086 * @return a list which may be empty of all the endpoint instances of the
087 * given type
088 */
089 public static <T> List<T> getSingletonEndpoints(CamelContext camelContext, Class<T> type) {
090 return getEndpointsImpl(camelContext, type, true);
091 }
092
093 /**
094 * Returns a list of all singleton or regular endpoints of the given type
095 */
096 private static <T> List<T> getEndpointsImpl(CamelContext camelContext, Class<T> type, boolean singleton) {
097 List<T> answer = new ArrayList<T>();
098 Collection<Endpoint> endpoints = singleton ? camelContext.getSingletonEndpoints() : camelContext.getEndpoints();
099 for (Endpoint endpoint : endpoints) {
100 if (type.isInstance(endpoint)) {
101 T value = type.cast(endpoint);
102 answer.add(value);
103 }
104 }
105 return answer;
106 }
107
108 /**
109 * Converts the given value to the requested type
110 */
111 public static <T> T convertTo(CamelContext context, Class<T> type, Object value) {
112 notNull(context, "camelContext");
113 return context.getTypeConverter().convertTo(type, value);
114 }
115
116 /**
117 * Converts the given value to the specified type throwing an {@link IllegalArgumentException}
118 * if the value could not be converted to a non null value
119 */
120 public static <T> T mandatoryConvertTo(CamelContext context, Class<T> type, Object value) {
121 T answer = convertTo(context, type, value);
122 if (answer == null) {
123 throw new IllegalArgumentException("Value " + value + " converted to " + type.getName() + " cannot be null");
124 }
125 return answer;
126 }
127
128 /**
129 * Creates a new instance of the given type using the {@link org.apache.camel.spi.Injector} on the given
130 * {@link CamelContext}
131 */
132 public static <T> T newInstance(CamelContext context, Class<T> beanType) {
133 return context.getInjector().newInstance(beanType);
134 }
135
136 /**
137 * Look up the given named bean in the {@link org.apache.camel.spi.Registry} on the
138 * {@link CamelContext}
139 */
140 public static Object lookup(CamelContext context, String name) {
141 return context.getRegistry().lookup(name);
142 }
143
144 /**
145 * Look up the given named bean of the given type in the {@link org.apache.camel.spi.Registry} on the
146 * {@link CamelContext}
147 */
148 public static <T> T lookup(CamelContext context, String name, Class<T> beanType) {
149 return context.getRegistry().lookup(name, beanType);
150 }
151
152 /**
153 * Look up the given named bean in the {@link org.apache.camel.spi.Registry} on the
154 * {@link CamelContext} or throws IllegalArgumentException if not found.
155 */
156 public static Object mandatoryLookup(CamelContext context, String name) {
157 Object answer = lookup(context, name);
158 notNull(answer, "registry entry called " + name);
159 return answer;
160 }
161
162 /**
163 * Look up the given named bean of the given type in the {@link org.apache.camel.spi.Registry} on the
164 * {@link CamelContext} or throws IllegalArgumentException if not found.
165 */
166 public static <T> T mandatoryLookup(CamelContext context, String name, Class<T> beanType) {
167 T answer = lookup(context, name, beanType);
168 notNull(answer, "registry entry called " + name + " of type " + beanType.getName());
169 return answer;
170 }
171
172 /**
173 * Resolves the given language name into a {@link Language} or throws an exception if it could not be converted
174 */
175 public static Language resolveMandatoryLanguage(CamelContext camelContext, String languageName) {
176 notNull(camelContext, "camelContext");
177 notNull(languageName, "languageName");
178
179 Language language = camelContext.resolveLanguage(languageName);
180 if (language == null) {
181 throw new IllegalArgumentException("Could not resolve language: " + languageName);
182 }
183 return language;
184 }
185
186 /**
187 * Resolves the mandatory language name and expression text into a {@link Expression} instance
188 * throwing an exception if it could not be created
189 */
190 public static Expression resolveMandatoryExpression(CamelContext camelContext, String languageName, String expressionText) {
191 notNull(expressionText, "expressionText");
192
193 Language language = resolveMandatoryLanguage(camelContext, languageName);
194 Expression expression = language.createExpression(expressionText);
195 if (expression == null) {
196 throw new IllegalArgumentException("Could not create expression: " + expressionText + " with language: " + language);
197 }
198 return expression;
199 }
200
201 /**
202 * Evaluates the @EndpointInject annotation using the given context
203 */
204 public static Endpoint getEndpointInjection(CamelContext camelContext, String uri, String name, String injectionPointName, boolean mandatory) {
205 Endpoint endpoint = null;
206 if (isNotEmpty(uri)) {
207 endpoint = camelContext.getEndpoint(uri);
208 } else {
209 if (isEmpty(name)) {
210 name = injectionPointName;
211 }
212 if (mandatory) {
213 endpoint = mandatoryLookup(camelContext, name, Endpoint.class);
214 } else {
215 endpoint = lookup(camelContext, name, Endpoint.class);
216 }
217 }
218 return endpoint;
219 }
220
221 }