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: 56884 $
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 static <T> List<T> getSingletonEndpoints(CamelContext camelContext, Class<T> type) {
083            List<T> answer = new ArrayList<T>();
084            Collection<Endpoint> endpoints = camelContext.getSingletonEndpoints();
085            for (Endpoint endpoint : endpoints) {
086                if (type.isInstance(endpoint)) {
087                    T value = type.cast(endpoint);
088                    answer.add(value);
089                }
090            }
091            return answer;
092        }
093    
094        /**
095         * Converts the given value to the requested type
096         */
097        public static <T> T convertTo(CamelContext context, Class<T> type, Object value) {
098            notNull(context, "camelContext");
099            return context.getTypeConverter().convertTo(type, value);
100        }
101    
102        /**
103         * Converts the given value to the specified type throwing an {@link IllegalArgumentException}
104         * if the value could not be converted to a non null value
105         */
106        public static <T> T mandatoryConvertTo(CamelContext context, Class<T> type, Object value) {
107            T answer = convertTo(context, type, value);
108            if (answer == null) {
109                throw new IllegalArgumentException("Value " + value + " converted to " + type.getName() + " cannot be null");
110            }
111            return answer;
112        }
113    
114        /**
115         * Creates a new instance of the given type using the {@link Injector} on the given
116         * {@link CamelContext}
117         */
118        public static <T> T newInstance(CamelContext context, Class<T> beanType) {
119            return context.getInjector().newInstance(beanType);
120        }
121    
122        /**
123         * Look up the given named bean in the {@link Registry} on the
124         * {@link CamelContext}
125         */
126        public static Object lookup(CamelContext context, String name) {
127            return context.getRegistry().lookup(name);
128        }
129    
130        /**
131         * Look up the given named bean of the given type in the {@link Registry} on the
132         * {@link CamelContext}
133         */
134        public static <T> T lookup(CamelContext context, String name, Class<T> beanType) {
135            return context.getRegistry().lookup(name, beanType);
136        }
137    
138        /**
139         * Look up the given named bean in the {@link Registry} on the
140         * {@link CamelContext} or throws
141         */
142        public static Object mandatoryLookup(CamelContext context, String name) {
143            Object answer = lookup(context, name);
144            notNull(answer, "registry entry called " + name);
145            return answer;
146        }
147    
148        /**
149         * Look up the given named bean of the given type in the {@link Registry} on the
150         * {@link CamelContext}
151         */
152        public static <T> T mandatoryLookup(CamelContext context, String name, Class<T> beanType) {
153            T answer = lookup(context, name, beanType);
154            notNull(answer, "registry entry called " + name + " of type " + beanType.getName());
155            return answer;
156        }
157    
158        /**
159         * Resolves the given language name into a {@link Language} or throws an exception if it could not be converted
160         */
161        public static Language resolveMandatoryLanguage(CamelContext camelContext, String languageName) {
162            notNull(camelContext, "camelContext");
163            notNull(languageName, "languageName");
164    
165            Language language = camelContext.resolveLanguage(languageName);
166            if (language == null) {
167                throw new IllegalArgumentException("Could not resolve language: " + languageName);
168            }
169            return language;
170        }
171    
172        /**
173         * Resolves the mandatory language name and expression text into a {@link Expression} instance
174         * throwing an exception if it could not be created
175         */
176        public static Expression resolveMandatoryExpression(CamelContext camelContext, String languageName, String expressionText) {
177            notNull(expressionText, "expressionText");
178    
179            Language language = resolveMandatoryLanguage(camelContext, languageName);
180            Expression<Exchange> expression = language.createExpression(expressionText);
181            if (expression == null) {
182                throw new IllegalArgumentException("Could not create expression: " + expressionText + " with language: " + language);
183            }
184            return expression;
185        }
186    
187        /**
188         * Evaluates the @EndpointInject annotation using the given context
189         */
190        public static Endpoint getEndpointInjection(CamelContext camelContext, String uri, String name, String injectionPointName, boolean mandatory) {
191            Endpoint endpoint = null;
192            if (isNotNullAndNonEmpty(uri)) {
193                endpoint = camelContext.getEndpoint(uri);
194            } else {
195                if (isNullOrBlank(name)) {
196                    name = injectionPointName;
197                }
198                if (mandatory) {
199                    endpoint = mandatoryLookup(camelContext, name, Endpoint.class);
200                } else {
201                    endpoint = lookup(camelContext, name, Endpoint.class);
202                }
203            }
204            return endpoint;
205        }
206    
207    }