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