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