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.DataFormatDefinition;
026 import org.apache.camel.model.RouteDefinition;
027 import org.apache.camel.spi.ClassResolver;
028 import org.apache.camel.spi.EndpointStrategy;
029 import org.apache.camel.spi.FactoryFinder;
030 import org.apache.camel.spi.FactoryFinderResolver;
031 import org.apache.camel.spi.Injector;
032 import org.apache.camel.spi.InterceptStrategy;
033 import org.apache.camel.spi.Language;
034 import org.apache.camel.spi.LifecycleStrategy;
035 import org.apache.camel.spi.NodeIdFactory;
036 import org.apache.camel.spi.PackageScanClassResolver;
037 import org.apache.camel.spi.Registry;
038 import org.apache.camel.spi.ServicePool;
039 import org.apache.camel.spi.TypeConverterRegistry;
040
041 /**
042 * Interface used to represent the context used to configure routes and the
043 * policies to use during message exchanges between endpoints.
044 *
045 * @version $Revision: 800180 $
046 */
047 public interface CamelContext extends Service, RuntimeConfiguration {
048
049 /**
050 * Gets the name of the this context.
051 *
052 * @return the name
053 */
054 String getName();
055
056 // Component Management Methods
057 //-----------------------------------------------------------------------
058
059 /**
060 * Adds a component to the context.
061 *
062 * @param componentName the name the component is registered as
063 * @param component the component
064 */
065 void addComponent(String componentName, Component component);
066
067 /**
068 * Gets a component from the context by name.
069 *
070 * @param componentName the name of the component
071 * @return the component
072 */
073 Component getComponent(String componentName);
074
075 /**
076 * Gets a component from the context by name and specifying the expected type of component.
077 *
078 * @param name the name to lookup
079 * @param componentType the expected type
080 * @return the component
081 */
082 <T extends Component> T getComponent(String name, Class<T> componentType);
083
084 /**
085 * Gets a readonly list of names of the components currently registered
086 *
087 * @return a readonly list with the names of the the components
088 */
089 List<String> getComponentNames();
090
091 /**
092 * Removes a previously added component.
093 *
094 * @param componentName the component name to remove
095 * @return the previously added component or null if it had not been previously added.
096 */
097 Component removeComponent(String componentName);
098
099 /**
100 * Gets the a previously added component by name or lazily creates the component
101 * using the factory Callback.
102 *
103 * @param componentName the name of the component
104 * @param factory used to create a new component instance if the component was not previously added.
105 * @return the component
106 */
107 Component getOrCreateComponent(String componentName, Callable<Component> factory);
108
109 // Endpoint Management Methods
110 //-----------------------------------------------------------------------
111
112 /**
113 * Resolves the given URI to an {@link Endpoint}. If the URI has a singleton endpoint
114 * registered, then the singleton is returned. Otherwise, a new {@link Endpoint} is created
115 * and if the endpoint is a singleton it is registered as a singleton endpoint.
116 *
117 * @param uri the URI of the endpoint
118 * @return the endpoint
119 */
120 Endpoint getEndpoint(String uri);
121
122 /**
123 * Resolves the given name to an {@link Endpoint} of the specified type.
124 * If the name has a singleton endpoint registered, then the singleton is returned.
125 * Otherwise, a new {@link Endpoint} is created and if the endpoint is a
126 * singleton it is registered as a singleton endpoint.
127 *
128 * @param name the name of the endpoint
129 * @param endpointType the expected type
130 * @return the endpoint
131 */
132 <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType);
133
134 /**
135 * Returns the collection of all registered endpoints.
136 *
137 * @return all endpoints
138 */
139 Collection<Endpoint> getEndpoints();
140
141 /**
142 * Returns a new Map containing all of the active endpoints with the key of the map being their
143 * unique key.
144 *
145 * @return map of active endpoints
146 */
147 Map<String, Endpoint> getEndpointMap();
148
149 /**
150 * Is the given endpoint already registered?
151 *
152 * @param uri the URI of the endpoint
153 * @return the registered endpoint or <tt>null</tt> if not registered
154 */
155 Endpoint hasEndpoint(String uri);
156
157 /**
158 * Returns the collection of all registered endpoints for a uri or an empty collection.
159 * For a singleton endpoint the collection will contain exactly one element.
160 *
161 * @param uri the URI of the endpoints
162 * @return collection of endpoints
163 */
164 Collection<Endpoint> getEndpoints(String uri);
165
166 /**
167 * Returns the collection of all registered singleton endpoints.
168 *
169 * @return all the singleton endpoints
170 */
171 Collection<Endpoint> getSingletonEndpoints();
172
173 /**
174 * Adds the endpoint to the context using the given URI.
175 *
176 * @param uri the URI to be used to resolve this endpoint
177 * @param endpoint the endpoint to be added to the context
178 * @return the old endpoint that was previously registered to the context if
179 * there was already an singleton endpoint for that URI or null
180 * @throws Exception if the new endpoint could not be started or the old
181 * singleton endpoint could not be stopped
182 */
183 Endpoint addEndpoint(String uri, Endpoint endpoint) throws Exception;
184
185 /**
186 * Removes all endpoints with the given URI
187 *
188 * @param uri the URI to be used to remove
189 * @return a collection of endpoints removed or null if there are no endpoints for this URI
190 * @throws Exception if at least one endpoint could not be stopped
191 */
192 Collection<Endpoint> removeEndpoints(String uri) throws Exception;
193
194 /**
195 * Registers a {@link org.apache.camel.spi.EndpointStrategy callback} to allow you to do custom
196 * logic when an {@link Endpoint} is about to be registered to the {@link CamelContext} endpoint registry.
197 * <p/>
198 * When a callback is added it will be executed on the already registered endpoints allowing you to catch-up
199 *
200 * @param strategy callback to be invoked
201 */
202 void addRegisterEndpointCallback(EndpointStrategy strategy);
203
204 // Route Management Methods
205 //-----------------------------------------------------------------------
206
207 /**
208 * Returns a list of the current route definitions
209 *
210 * @return list of the current route definitions
211 */
212 List<RouteDefinition> getRouteDefinitions();
213
214 /**
215 * Returns the current routes in this context
216 *
217 * @return the current routes
218 */
219 List<Route> getRoutes();
220
221 /**
222 * Adds a collection of routes to this context using the given builder
223 * to build them
224 *
225 * @param builder the builder which will create the routes and add them to this context
226 * @throws Exception if the routes could not be created for whatever reason
227 */
228 void addRoutes(RoutesBuilder builder) throws Exception;
229
230 /**
231 * Adds a collection of route definitions to the context
232 *
233 * @param routeDefinitions the route definitions to add
234 * @throws Exception if the route definition could not be created for whatever reason
235 */
236 void addRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception;
237
238 /**
239 * Removes a collection of route definitions from the context - stopping any previously running
240 * routes if any of them are actively running
241 *
242 * @param routeDefinitions route definitions
243 * @throws Exception if the route definition could not be removed for whatever reason
244 */
245 void removeRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception;
246
247 /**
248 * Starts the given route if it has been previously stopped
249 *
250 * @param route the route to start
251 * @throws Exception is thrown if the route could not be started for whatever reason
252 */
253 void startRoute(RouteDefinition route) throws Exception;
254
255 /**
256 * Stops the given route. It will remain in the list of route definitions return by {@link #getRouteDefinitions()}
257 * unless you use the {@link #removeRouteDefinitions(java.util.Collection)}
258 *
259 * @param route the route to stop
260 * @throws Exception is thrown if the route could not be stopped for whatever reason
261 */
262 void stopRoute(RouteDefinition route) throws Exception;
263
264
265 // Properties
266 //-----------------------------------------------------------------------
267
268 /**
269 * Returns the type converter used to coerce types from one type to another
270 *
271 * @return the converter
272 */
273 TypeConverter getTypeConverter();
274
275 /**
276 * Returns the type converter registry where type converters can be added or looked up
277 *
278 * @return the type converter registry
279 */
280 TypeConverterRegistry getTypeConverterRegistry();
281
282 /**
283 * Returns the registry used to lookup components by name and type such as the Spring ApplicationContext,
284 * JNDI or the OSGi Service Registry
285 *
286 * @return the registry
287 */
288 Registry getRegistry();
289
290 /**
291 * Returns the injector used to instantiate objects by type
292 *
293 * @return the injector
294 */
295 Injector getInjector();
296
297 /**
298 * Returns the lifecycle strategy used to handle lifecycle notification
299 *
300 * @return the lifecycle strategy
301 */
302 LifecycleStrategy getLifecycleStrategy();
303
304 /**
305 * Resolves a language for creating expressions
306 *
307 * @param language name of the language
308 * @return the resolved language
309 */
310 Language resolveLanguage(String language);
311
312 /**
313 * Gets a readonly list with the names of the languages currently registered.
314 *
315 * @return a readonly list with the names of the the languages
316 */
317 List<String> getLanguageNames();
318
319 /**
320 * Creates a new ProducerTemplate.
321 * <p/>
322 * See this FAQ before use: <a href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html">
323 * Why does Camel use too many threads with ProducerTemplate?</a>
324 *
325 * @return the template
326 */
327 ProducerTemplate createProducerTemplate();
328
329 /**
330 * Creates a new ConsumerTemplate.
331 * <p/>
332 * See this FAQ before use: <a href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html">
333 * Why does Camel use too many threads with ProducerTemplate?</a> as it also applies for ConsumerTemplate.
334 *
335 * @return the template
336 */
337 ConsumerTemplate createConsumerTemplate();
338
339 /**
340 * Adds the given interceptor strategy
341 *
342 * @param interceptStrategy the strategy
343 */
344 void addInterceptStrategy(InterceptStrategy interceptStrategy);
345
346 /**
347 * Gets the interceptor strategies
348 *
349 * @return the list of current interceptor strategies
350 */
351 List<InterceptStrategy> getInterceptStrategies();
352
353 /**
354 * Gets the default error handler builder which is inherited by the routes
355 *
356 * @return the builder
357 */
358 ErrorHandlerBuilder getErrorHandlerBuilder();
359
360 /**
361 * Sets the default error handler builder which is inherited by the routes
362 *
363 * @param errorHandlerBuilder the builder
364 */
365 void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder);
366
367 /**
368 * Sets the data formats that can be referenced in the routes.
369 * @param dataFormats the data formats
370 */
371 void setDataFormats(Map<String, DataFormatDefinition> dataFormats);
372
373 /**
374 * Gets the data formats that can be referenced in the routes.
375 *
376 * @return the data formats available
377 */
378 Map<String, DataFormatDefinition> getDataFormats();
379
380 /**
381 * Sets the properties that can be referenced in the camel context
382 *
383 * @param properties properties
384 */
385 void setProperties(Map<String, String> properties);
386
387 /**
388 * Gets the properties that can be referenced in the camel context
389 *
390 * @return the properties
391 */
392 Map<String, String> getProperties();
393
394 /**
395 * Gets the default FactoryFinder which will be used for the loading the factory class from META-INF
396 *
397 * @return the default factory finder
398 */
399 FactoryFinder getDefaultFactoryFinder();
400
401 /**
402 * Sets the factory finder resolver to use.
403 *
404 * @param resolver the factory finder resolver
405 */
406 void setFactoryFinderResolver(FactoryFinderResolver resolver);
407
408 /**
409 * Gets the FactoryFinder which will be used for the loading the factory class from META-INF in the given path
410 *
411 * @param path the META-INF path
412 * @return the factory finder
413 * @throws NoFactoryAvailableException is thrown if a factory could not be found
414 */
415 FactoryFinder getFactoryFinder(String path) throws NoFactoryAvailableException;
416
417 /**
418 * Returns the current status of the given route
419 *
420 * @param route the route
421 * @return the status for the route
422 */
423 ServiceStatus getRouteStatus(RouteDefinition route);
424
425 /**
426 * Returns the class resolver to be used for loading/lookup of classes.
427 *
428 * @return the resolver
429 */
430 ClassResolver getClassResolver();
431
432 /**
433 * Returns the package scanning class resolver
434 *
435 * @return the resolver
436 */
437 PackageScanClassResolver getPackageScanClassResolver();
438
439 /**
440 * Sets the class resolver to be use
441 *
442 * @param resolver the resolver
443 */
444 void setClassResolver(ClassResolver resolver);
445
446 /**
447 * Sets the package scanning class resolver to use
448 *
449 * @param resolver the resolver
450 */
451 void setPackageScanClassResolver(PackageScanClassResolver resolver);
452
453 /**
454 * Sets a pluggable service pool to use for {@link Producer} pooling.
455 *
456 * @param servicePool the pool
457 */
458 void setProducerServicePool(ServicePool<Endpoint, Producer> servicePool);
459
460 /**
461 * Gets the service pool for {@link Producer} pooling.
462 *
463 * @return the service pool
464 */
465 ServicePool<Endpoint, Producer> getProducerServicePool();
466
467 /**
468 * Uses a custom node id factory when generating auto assigned ids to the nodes in the route definitions
469 *
470 * @param factory custom factory to use
471 */
472 void setNodeIdFactory(NodeIdFactory factory);
473
474 /**
475 * Gets the node id factory
476 *
477 * @return the node id factory
478 */
479 NodeIdFactory getNodeIdFactory();
480
481 }