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 org.apache.camel.CamelContext;
020 import org.apache.camel.Endpoint;
021 import org.apache.camel.NoSuchEndpointException;
022
023 import static org.apache.camel.util.ObjectHelper.isEmpty;
024 import static org.apache.camel.util.ObjectHelper.isNotEmpty;
025 import static org.apache.camel.util.ObjectHelper.notNull;
026
027 /**
028 * A number of helper methods
029 *
030 * @version $Revision: 891577 $
031 */
032 public final class CamelContextHelper {
033
034 /**
035 * Utility classes should not have a public constructor.
036 */
037 private CamelContextHelper() {
038 }
039
040 /**
041 * Returns the mandatory endpoint for the given URI or the
042 * {@link org.apache.camel.NoSuchEndpointException} is thrown
043 */
044 public static Endpoint getMandatoryEndpoint(CamelContext camelContext, String uri)
045 throws NoSuchEndpointException {
046 Endpoint endpoint = camelContext.getEndpoint(uri);
047 if (endpoint == null) {
048 throw new NoSuchEndpointException(uri);
049 } else {
050 return endpoint;
051 }
052 }
053
054 /**
055 * Returns the mandatory endpoint for the given URI and type or the
056 * {@link org.apache.camel.NoSuchEndpointException} is thrown
057 */
058 public static <T extends Endpoint> T getMandatoryEndpoint(CamelContext camelContext, String uri, Class<T> type) {
059 Endpoint endpoint = getMandatoryEndpoint(camelContext, uri);
060 return ObjectHelper.cast(type, endpoint);
061 }
062
063 /**
064 * Converts the given value to the requested type
065 */
066 public static <T> T convertTo(CamelContext context, Class<T> type, Object value) {
067 notNull(context, "camelContext");
068 return context.getTypeConverter().convertTo(type, value);
069 }
070
071 /**
072 * Converts the given value to the specified type throwing an {@link IllegalArgumentException}
073 * if the value could not be converted to a non null value
074 */
075 public static <T> T mandatoryConvertTo(CamelContext context, Class<T> type, Object value) {
076 T answer = convertTo(context, type, value);
077 if (answer == null) {
078 throw new IllegalArgumentException("Value " + value + " converted to " + type.getName() + " cannot be null");
079 }
080 return answer;
081 }
082
083 /**
084 * Creates a new instance of the given type using the {@link org.apache.camel.spi.Injector} on the given
085 * {@link CamelContext}
086 */
087 public static <T> T newInstance(CamelContext context, Class<T> beanType) {
088 return context.getInjector().newInstance(beanType);
089 }
090
091 /**
092 * Look up the given named bean in the {@link org.apache.camel.spi.Registry} on the
093 * {@link CamelContext}
094 */
095 public static Object lookup(CamelContext context, String name) {
096 return context.getRegistry().lookup(name);
097 }
098
099 /**
100 * Look up the given named bean of the given type in the {@link org.apache.camel.spi.Registry} on the
101 * {@link CamelContext}
102 */
103 public static <T> T lookup(CamelContext context, String name, Class<T> beanType) {
104 return context.getRegistry().lookup(name, beanType);
105 }
106
107 /**
108 * Look up the given named bean in the {@link org.apache.camel.spi.Registry} on the
109 * {@link CamelContext} or throws IllegalArgumentException if not found.
110 */
111 public static Object mandatoryLookup(CamelContext context, String name) {
112 Object answer = lookup(context, name);
113 notNull(answer, "registry entry called " + name);
114 return answer;
115 }
116
117 /**
118 * Look up the given named bean of the given type in the {@link org.apache.camel.spi.Registry} on the
119 * {@link CamelContext} or throws IllegalArgumentException if not found.
120 */
121 public static <T> T mandatoryLookup(CamelContext context, String name, Class<T> beanType) {
122 T answer = lookup(context, name, beanType);
123 notNull(answer, "registry entry called " + name + " of type " + beanType.getName());
124 return answer;
125 }
126
127 /**
128 * Evaluates the @EndpointInject annotation using the given context
129 */
130 public static Endpoint getEndpointInjection(CamelContext camelContext, String uri, String name, String injectionPointName, boolean mandatory) {
131 Endpoint endpoint;
132 if (isNotEmpty(uri)) {
133 endpoint = camelContext.getEndpoint(uri);
134 } else {
135 // if a name is given then it should be possible to lookup
136 // otherwise we do not catch situations where there is a typo etc
137 if (isNotEmpty(name)) {
138 endpoint = mandatoryLookup(camelContext, name, Endpoint.class);
139 } else {
140 if (isEmpty(name)) {
141 name = injectionPointName;
142 }
143 if (mandatory) {
144 endpoint = mandatoryLookup(camelContext, name, Endpoint.class);
145 } else {
146 endpoint = lookup(camelContext, name, Endpoint.class);
147 }
148 }
149 }
150 return endpoint;
151 }
152
153 }