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;
018
019 import java.util.Collection;
020 import java.util.List;
021 import java.util.Map;
022 import java.util.concurrent.Callable;
023
024 import org.apache.camel.builder.ErrorHandlerBuilder;
025 import org.apache.camel.model.RouteType;
026 import org.apache.camel.model.dataformat.DataFormatType;
027 import org.apache.camel.spi.ExchangeConverter;
028 import org.apache.camel.spi.Injector;
029 import org.apache.camel.spi.InterceptStrategy;
030 import org.apache.camel.spi.Language;
031 import org.apache.camel.spi.LifecycleStrategy;
032 import org.apache.camel.spi.Registry;
033 import org.apache.camel.util.FactoryFinder;
034
035 /**
036 * Interface used to represent the context used to configure routes and the
037 * policies to use during message exchanges between endpoints.
038 *
039 * @version $Revision: 752763 $
040 */
041 public interface CamelContext extends Service {
042
043 /**
044 * Gets the name of the this context.
045 *
046 * @return the name
047 */
048 String getName();
049
050 // Component Management Methods
051 //-----------------------------------------------------------------------
052
053 /**
054 * Adds a component to the context.
055 *
056 * @param componentName the name the component is registered as
057 * @param component the component
058 */
059 void addComponent(String componentName, Component component);
060
061 /**
062 * Gets a component from the context by name.
063 *
064 * @param componentName the name of the component
065 * @return the component
066 */
067 Component getComponent(String componentName);
068
069 /**
070 * Gets a component from the context by name and specifying the expected type of component.
071 *
072 * @param name the name to lookup
073 * @param componentType the expected type
074 * @return the component
075 */
076 <T extends Component> T getComponent(String name, Class<T> componentType);
077
078 /**
079 * Removes a previously added component.
080 *
081 * @param componentName the component name to remove
082 * @return the previously added component or null if it had not been previously added.
083 */
084 Component removeComponent(String componentName);
085
086 /**
087 * Gets the a previously added component by name or lazily creates the component
088 * using the factory Callback.
089 *
090 * @param componentName the name of the component
091 * @param factory used to create a new component instance if the component was not previously added.
092 * @return the component
093 */
094 Component getOrCreateComponent(String componentName, Callable<Component> factory);
095
096 // Endpoint Management Methods
097 //-----------------------------------------------------------------------
098
099 /**
100 * Resolves the given URI to an {@link Endpoint}. If the URI has a singleton endpoint
101 * registered, then the singleton is returned. Otherwise, a new {@link Endpoint} is created
102 * and if the endpoint is a singleton it is registered as a singleton endpoint.
103 *
104 * @param uri the URI of the endpoint
105 * @return the endpoint
106 */
107 Endpoint getEndpoint(String uri);
108
109 /**
110 * Resolves the given name to an {@link Endpoint} of the specified type.
111 * If the name has a singleton endpoint registered, then the singleton is returned.
112 * Otherwise, a new {@link Endpoint} is created and if the endpoint is a
113 * singleton it is registered as a singleton endpoint.
114 *
115 * @param name the name of the endpoint
116 * @param endpointType the expected type
117 * @return the endpoint
118 */
119 <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType);
120
121 /**
122 * Returns the collection of all registered endpoints.
123 *
124 * @return all endpoints
125 */
126 Collection<Endpoint> getEndpoints();
127
128 /**
129 * Returns a new Map containing all of the active endpoints with the key of the map being their
130 * unique key
131 */
132 Map<String, Endpoint> getEndpointMap();
133
134 /**
135 * Returns the collection of all registered endpoints for a uri or an empty collection.
136 * For a singleton endpoint the collection will contain exactly one element.
137 *
138 * @param uri the URI of the endpoints
139 * @return collection of endpoints
140 */
141 Collection<Endpoint> getEndpoints(String uri);
142
143 /**
144 * Returns the collection of all registered singleton endpoints.
145 *
146 * @return all the singleton endpoints
147 */
148 Collection<Endpoint> getSingletonEndpoints();
149
150 /**
151 * Adds the endpoint to the context using the given URI.
152 *
153 * @param uri the URI to be used to resolve this endpoint
154 * @param endpoint the endpoint to be added to the context
155 * @return the old endpoint that was previously registered to the context if
156 * there was already an singleton endpoint for that URI or null
157 * @throws Exception if the new endpoint could not be started or the old
158 * singleton endpoint could not be stopped
159 */
160 Endpoint addEndpoint(String uri, Endpoint endpoint) throws Exception;
161
162 /**
163 * Removes all endpoints with the given URI
164 *
165 * @param uri the URI to be used to remove
166 * @return a collection of endpoints removed or null if there are no endpoints for this URI
167 * @throws Exception if at least one endpoint could not be stopped
168 */
169 Collection<Endpoint> removeEndpoints(String uri) throws Exception;
170
171 /**
172 * Adds the endpoint to the context using the given URI. The endpoint will be registered as a singleton.
173 *
174 * @param uri the URI to be used to resolve this endpoint
175 * @param endpoint the endpoint to be added to the context
176 * @return the old endpoint that was previously registered to the context if there was
177 * already an endpoint for that URI
178 * @throws Exception if the new endpoint could not be started or the old endpoint could not be stopped
179 */
180 @Deprecated
181 Endpoint addSingletonEndpoint(String uri, Endpoint endpoint) throws Exception;
182
183 /**
184 * Removes the singleton endpoint with the given URI
185 *
186 * @param uri the URI to be used to remove
187 * @return the endpoint that was removed or null if there is no endpoint for this URI
188 * @throws Exception if endpoint could not be stopped
189 */
190 @Deprecated
191 Endpoint removeSingletonEndpoint(String uri) throws Exception;
192
193
194 // Route Management Methods
195 //-----------------------------------------------------------------------
196
197 /**
198 * Returns a list of the current route definitions
199 *
200 * @return list of the current route definitions
201 */
202 List<RouteType> getRouteDefinitions();
203
204 /**
205 * Returns the current routes in this context
206 *
207 * @return the current routes
208 */
209 List<Route> getRoutes();
210
211 /**
212 * Sets the routes for this context, replacing any current routes
213 *
214 * @param routes the new routes to use
215 * @deprecated is considered for deprecation, use addRoutes instead, could be removed in Camel 2.0
216 */
217 @Deprecated
218 void setRoutes(List<Route> routes);
219
220 /**
221 * Adds a collection of routes to this context
222 *
223 * @param routes the routes to add
224 * @throws Exception if the routes could not be created for whatever reason
225 */
226 void addRoutes(Collection<Route> routes) throws Exception;
227
228 /**
229 * Adds a collection of routes to this context using the given builder
230 * to build them
231 *
232 * @param builder the builder which will create the routes and add them to this context
233 * @throws Exception if the routes could not be created for whatever reason
234 */
235 void addRoutes(Routes builder) throws Exception;
236
237 /**
238 * Adds a collection of route definitions to the context
239 *
240 * @param routeDefinitions the route definitions to add
241 * @throws Exception if the route definition could not be created for whatever reason
242 */
243 void addRouteDefinitions(Collection<RouteType> routeDefinitions) throws Exception;
244
245
246 // Properties
247 //-----------------------------------------------------------------------
248
249 /**
250 * Returns the converter of exchanges from one type to another
251 *
252 * @return the converter
253 */
254 ExchangeConverter getExchangeConverter();
255
256 /**
257 * Returns the type converter used to coerce types from one type to another
258 *
259 * @return the converter
260 */
261 TypeConverter getTypeConverter();
262
263 /**
264 * Returns the registry used to lookup components by name and type such as the Spring ApplicationContext,
265 * JNDI or the OSGi Service Registry
266 *
267 * @return the registry
268 */
269 Registry getRegistry();
270
271 /**
272 * Returns the injector used to instantiate objects by type
273 *
274 * @return the injector
275 */
276 Injector getInjector();
277
278 /**
279 * Returns the lifecycle strategy used to handle lifecycle notification
280 *
281 * @return the lifecycle strategy
282 */
283 LifecycleStrategy getLifecycleStrategy();
284
285 /**
286 * Resolves a language for creating expressions
287 *
288 * @param language name of the language
289 * @return the resolved language
290 */
291 Language resolveLanguage(String language);
292
293 /**
294 * Creates a new ProducerTemplate.
295 * <p/>
296 * See this FAQ before use: <a href="http://activemq.apache.org/camel/why-does-camel-use-too-many-threads-with-producertemplate.html">
297 * Why does Camel use too many threads with ProducerTemplate?</a>
298 *
299 * @return the template
300 */
301 <E extends Exchange> ProducerTemplate<E> createProducerTemplate();
302
303 /**
304 * Adds the given interceptor strategy
305 *
306 * @param interceptStrategy the strategy
307 */
308 void addInterceptStrategy(InterceptStrategy interceptStrategy);
309
310 /**
311 * Gets the default error handler builder which is inherited by the routes
312 *
313 * @return the builder
314 */
315 ErrorHandlerBuilder getErrorHandlerBuilder();
316
317 /**
318 * Sets the default error handler builder which is inherited by the routes
319 *
320 * @param errorHandlerBuilder the builder
321 */
322 void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder);
323
324 /**
325 * Sets the data formats that can be referenced in the routes.
326 * @param dataFormats the data formats
327 */
328 void setDataFormats(Map<String, DataFormatType> dataFormats);
329
330 /**
331 * Gets the data formats that can be referenced in the routes.
332 *
333 * @return the data formats available
334 */
335 Map<String, DataFormatType> getDataFormats();
336
337 /**
338 * Sets the properties that can be referenced in the camel context
339 */
340 void setProperties(Map<String, String> properties);
341
342 /**
343 * Gets the properties that can be referenced in the camel context
344 */
345 Map<String, String> getProperties();
346
347 /**
348 * Create a FactoryFinder which will be used for the loading the factory class from META-INF
349 * @return the factory finder
350 */
351 FactoryFinder createFactoryFinder();
352
353 /**
354 * Create a FactoryFinder which will be used for the loading the factory class from META-INF
355 * @param path the META-INF path
356 * @return the factory finder
357 */
358 FactoryFinder createFactoryFinder(String path);
359 }