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