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.DataFormat;
029    import org.apache.camel.spi.EndpointStrategy;
030    import org.apache.camel.spi.FactoryFinder;
031    import org.apache.camel.spi.FactoryFinderResolver;
032    import org.apache.camel.spi.InflightRepository;
033    import org.apache.camel.spi.Injector;
034    import org.apache.camel.spi.InterceptStrategy;
035    import org.apache.camel.spi.Language;
036    import org.apache.camel.spi.LifecycleStrategy;
037    import org.apache.camel.spi.ManagementStrategy;
038    import org.apache.camel.spi.NodeIdFactory;
039    import org.apache.camel.spi.PackageScanClassResolver;
040    import org.apache.camel.spi.Registry;
041    import org.apache.camel.spi.ServicePool;
042    import org.apache.camel.spi.TypeConverterRegistry;
043    
044    /**
045     * Interface used to represent the context used to configure routes and the
046     * policies to use during message exchanges between endpoints.
047     *
048     * @version $Revision: 883271 $
049     */
050    public interface CamelContext extends Service, RuntimeConfiguration {
051    
052        /**
053         * Gets the name of the this context.
054         *
055         * @return the name
056         */
057        String getName();
058    
059        /**
060         * Gets the version of the this context.
061         *
062         * @return the version
063         */
064        String getVersion();
065    
066        // Service Methods
067        //-----------------------------------------------------------------------
068    
069        /**
070         * Adds a service, starting it so that it will be stopped with this context
071         *
072         * @param object the service
073         * @throws Exception can be thrown when starting the service
074         */
075        void addService(Object object) throws Exception;
076    
077        /**
078         * Has the given service already been added?
079         *
080         * @param object the service
081         * @return <tt>true</tt> if already added, <tt>false</tt> if not.
082         */
083        boolean hasService(Object object);
084    
085        // Component Management Methods
086        //-----------------------------------------------------------------------
087    
088        /**
089         * Adds a component to the context.
090         *
091         * @param componentName  the name the component is registered as
092         * @param component      the component
093         */
094        void addComponent(String componentName, Component component);
095    
096        /**
097         * Is the given component already registered?
098         *
099         * @param componentName the name of the component
100         * @return the registered Component or <tt>null</tt> if not registered
101         */
102        Component hasComponent(String componentName);
103    
104        /**
105         * Gets a component from the context by name.
106         *
107         * @param componentName the name of the component
108         * @return the component
109         */
110        Component getComponent(String componentName);
111    
112        /**
113         * Gets a component from the context by name and specifying the expected type of component.
114         *
115         * @param name  the name to lookup
116         * @param componentType  the expected type
117         * @return the component
118         */
119        <T extends Component> T getComponent(String name, Class<T> componentType);
120    
121        /**
122         * Gets a readonly list of names of the components currently registered
123         *
124         * @return a readonly list with the names of the the components
125         */
126        List<String> getComponentNames();
127    
128        /**
129         * Removes a previously added component.
130         *
131         * @param componentName the component name to remove
132         * @return the previously added component or null if it had not been previously added.
133         */
134        Component removeComponent(String componentName);
135    
136        /**
137         * Gets the a previously added component by name or lazily creates the component
138         * using the factory Callback.
139         *
140         * @param componentName the name of the component
141         * @param factory       used to create a new component instance if the component was not previously added.
142         * @return the component
143         * @deprecated will be removed in Camel 2.3.
144         */
145        @Deprecated
146        Component getOrCreateComponent(String componentName, Callable<Component> factory);
147    
148        // Endpoint Management Methods
149        //-----------------------------------------------------------------------
150    
151        /**
152         * Resolves the given name to an {@link Endpoint} of the specified type.
153         * If the name has a singleton endpoint registered, then the singleton is returned.
154         * Otherwise, a new {@link Endpoint} is created and registered.
155         *
156         * @param uri  the URI of the endpoint
157         * @return  the endpoint
158         */
159        Endpoint getEndpoint(String uri);
160    
161        /**
162         * Resolves the given name to an {@link Endpoint} of the specified type.
163         * If the name has a singleton endpoint registered, then the singleton is returned.
164         * Otherwise, a new {@link Endpoint} is created and registered.
165         *
166         * @param name  the name of the endpoint
167         * @param endpointType  the expected type
168         * @return the endpoint
169         */
170        <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType);
171    
172        /**
173         * Returns the collection of all registered endpoints.
174         *
175         * @return  all endpoints
176         */
177        Collection<Endpoint> getEndpoints();
178    
179        /**
180         * Returns a new Map containing all of the active endpoints with the key of the map being their
181         * unique key.
182         *
183         * @return map of active endpoints
184         */
185        Map<String, Endpoint> getEndpointMap();
186    
187        /**
188         * Is the given endpoint already registered?
189         *
190         * @param uri  the URI of the endpoint
191         * @return the registered endpoint or <tt>null</tt> if not registered
192         */
193        Endpoint hasEndpoint(String uri);
194    
195        /**
196         * Returns the collection of all registered endpoints for a uri or an empty collection.
197         * For a singleton endpoint the collection will contain exactly one element.
198         *
199         * @param uri  the URI of the endpoints
200         * @return  collection of endpoints
201         * @deprecated not used will be removed in Camel 2.3.
202         */
203        @Deprecated
204        Collection<Endpoint> getEndpoints(String uri);
205    
206        /**
207         * Returns the collection of all registered singleton endpoints.
208         *
209         * @return  all the singleton endpoints
210         * @deprecated not used will be removed in Camel 2.3.
211         */
212        @Deprecated
213        Collection<Endpoint> getSingletonEndpoints();
214    
215        /**
216         * Adds the endpoint to the context using the given URI.
217         *
218         * @param uri the URI to be used to resolve this endpoint
219         * @param endpoint the endpoint to be added to the context
220         * @return the old endpoint that was previously registered or <tt>null</tt> if none was registered
221         * @throws Exception if the new endpoint could not be started or the old endpoint could not be stopped
222         */
223        Endpoint addEndpoint(String uri, Endpoint endpoint) throws Exception;
224    
225        /**
226         * Removes all endpoints with the given URI
227         *
228         * @param uri the URI to be used to remove
229         * @return a collection of endpoints removed or null if there are no endpoints for this URI
230         * @throws Exception if at least one endpoint could not be stopped
231         * @deprecated not used will be removed in Camel 2.3.
232         */
233        @Deprecated
234        Collection<Endpoint> removeEndpoints(String uri) throws Exception;
235    
236        /**
237         * Registers a {@link org.apache.camel.spi.EndpointStrategy callback} to allow you to do custom
238         * logic when an {@link Endpoint} is about to be registered to the {@link CamelContext} endpoint registry.
239         * <p/>
240         * When a callback is added it will be executed on the already registered endpoints allowing you to catch-up
241         *
242         * @param strategy  callback to be invoked
243         */
244        void addRegisterEndpointCallback(EndpointStrategy strategy);
245    
246        // Route Management Methods
247        //-----------------------------------------------------------------------
248    
249        /**
250         * Returns a list of the current route definitions
251         *
252         * @return list of the current route definitions
253         */
254        List<RouteDefinition> getRouteDefinitions();
255    
256        /**
257         * Gets the route definition with the given id
258         *
259         * @param id id of the route
260         * @return the route definition or <tt>null</tt> if not found
261         */
262        RouteDefinition getRouteDefinition(String id);
263    
264        /**
265         * Returns the current routes in this context
266         *
267         * @return the current routes
268         */
269        List<Route> getRoutes();
270    
271        /**
272         * Adds a collection of routes to this context using the given builder
273         * to build them
274         *
275         * @param builder the builder which will create the routes and add them to this context
276         * @throws Exception if the routes could not be created for whatever reason
277         */
278        void addRoutes(RoutesBuilder builder) throws Exception;
279    
280        /**
281         * Adds a collection of route definitions to the context
282         *
283         * @param routeDefinitions the route definitions to add
284         * @throws Exception if the route definition could not be created for whatever reason
285         */
286        void addRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception;
287    
288        /**
289         * Removes a collection of route definitions from the context - stopping any previously running
290         * routes if any of them are actively running
291         *
292         * @param routeDefinitions route definitions
293         * @throws Exception if the route definition could not be removed for whatever reason
294         */
295        void removeRouteDefinitions(Collection<RouteDefinition> routeDefinitions) throws Exception;
296    
297        /**
298         * Starts the given route if it has been previously stopped
299         *
300         * @param route the route to start
301         * @throws Exception is thrown if the route could not be started for whatever reason
302         */
303        void startRoute(RouteDefinition route) throws Exception;
304    
305        /**
306         * Starts the given route if it has been previously stopped
307         *
308         * @param routeId the route id
309         * @throws Exception is thrown if the route could not be started for whatever reason
310         */
311        void startRoute(String routeId) throws Exception;
312    
313        /**
314         * Stops the given route. It will remain in the list of route definitions return by {@link #getRouteDefinitions()}
315         * unless you use the {@link #removeRouteDefinitions(java.util.Collection)}
316         *
317         * @param route the route to stop
318         * @throws Exception is thrown if the route could not be stopped for whatever reason
319         */
320        void stopRoute(RouteDefinition route) throws Exception;
321    
322        /**
323         * Stops the given route. It will remain in the list of route definitions return by {@link #getRouteDefinitions()}
324         * unless you use the {@link #removeRouteDefinitions(java.util.Collection)}
325         *
326         * @param routeId the route id
327         * @throws Exception is thrown if the route could not be stopped for whatever reason
328         */
329        void stopRoute(String routeId) throws Exception;
330    
331        /**
332         * Returns the current status of the given route
333         *
334         * @param routeId the route id
335         * @return the status for the route
336         */
337        ServiceStatus getRouteStatus(String routeId);
338    
339        /**
340         * Returns the current status of the given route
341         *
342         * @param route the route
343         * @return the status for the route
344         * @deprecated will be removed in Camel 2.3.
345         */
346        @Deprecated
347        ServiceStatus getRouteStatus(RouteDefinition route);
348    
349        // Properties
350        //-----------------------------------------------------------------------
351    
352        /**
353         * Returns the type converter used to coerce types from one type to another
354         *
355         * @return the converter
356         */
357        TypeConverter getTypeConverter();
358    
359        /**
360         * Returns the type converter registry where type converters can be added or looked up
361         *
362         * @return the type converter registry
363         */
364        TypeConverterRegistry getTypeConverterRegistry();
365        
366        /**
367         * Returns the registry used to lookup components by name and type such as the Spring ApplicationContext,
368         * JNDI or the OSGi Service Registry
369         *
370         * @return the registry
371         */
372        Registry getRegistry();
373    
374        /**
375         * Returns the injector used to instantiate objects by type
376         *
377         * @return the injector
378         */
379        Injector getInjector();
380    
381        /**
382         * Returns the lifecycle strategies used to handle lifecycle notifications
383         *
384         * @return the lifecycle strategies
385         */
386        List<LifecycleStrategy> getLifecycleStrategies();
387    
388        /**
389         * Adds the given lifecycle strategy to be used.
390         *
391         * @param lifecycleStrategy  the strategy
392         */
393        void addLifecycleStrategy(LifecycleStrategy lifecycleStrategy);
394    
395        /**
396         * Resolves a language for creating expressions
397         *
398         * @param language  name of the language
399         * @return the resolved language
400         */
401        Language resolveLanguage(String language);
402    
403        /**
404         * Gets a readonly list with the names of the languages currently registered.
405         *
406         * @return a readonly list with the names of the the languages
407         */
408        List<String> getLanguageNames();
409    
410        /**
411         * Creates a new ProducerTemplate.
412         * <p/>
413         * See this FAQ before use: <a href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html">
414         * Why does Camel use too many threads with ProducerTemplate?</a>
415         *
416         * @return the template
417         */
418        ProducerTemplate createProducerTemplate();
419    
420        /**
421         * Creates a new ConsumerTemplate.
422         * <p/>
423         * See this FAQ before use: <a href="http://camel.apache.org/why-does-camel-use-too-many-threads-with-producertemplate.html">
424         * Why does Camel use too many threads with ProducerTemplate?</a> as it also applies for ConsumerTemplate.
425         *
426         * @return the template
427         */
428        ConsumerTemplate createConsumerTemplate();
429    
430        /**
431         * Adds the given interceptor strategy
432         *
433         * @param interceptStrategy the strategy
434         */
435        void addInterceptStrategy(InterceptStrategy interceptStrategy);
436    
437        /**
438         * Gets the interceptor strategies
439         *
440         * @return the list of current interceptor strategies
441         */
442        List<InterceptStrategy> getInterceptStrategies();
443    
444        /**
445         * Gets the default error handler builder which is inherited by the routes
446         *
447         * @return the builder
448         */
449        ErrorHandlerBuilder getErrorHandlerBuilder();
450    
451        /**
452         * Sets the default error handler builder which is inherited by the routes
453         *
454         * @param errorHandlerBuilder  the builder
455         */
456        void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder);
457    
458        /**
459         * Sets the data formats that can be referenced in the routes.
460         *
461         * @param dataFormats the data formats
462         */
463        void setDataFormats(Map<String, DataFormatDefinition> dataFormats);
464    
465        /**
466         * Gets the data formats that can be referenced in the routes.
467         *
468         * @return the data formats available
469         */
470        Map<String, DataFormatDefinition> getDataFormats();
471        
472        /**
473         * Resolve a data format given its name
474         *
475         * @param name the data format name or a reference to it in the {@link Registry}
476         * @return the resolved data format, or <tt>null</tt> if not found
477         */
478        DataFormat resolveDataFormat(String name);
479    
480        /**
481         * Resolve a data format definition given its name
482         *
483         * @param name the data format definition name or a reference to it in the {@link Registry}
484         * @return the resolved data format definition, or <tt>null</tt> if not found
485         */
486        DataFormatDefinition resolveDataFormatDefinition(String name);
487    
488        /**
489         * Sets the properties that can be referenced in the camel context
490         *
491         * @param properties properties
492         */
493        void setProperties(Map<String, String> properties);
494        
495        /**
496         * Gets the properties that can be referenced in the camel context
497         *
498         * @return the properties
499         */
500        Map<String, String> getProperties();
501        
502        /**
503         * Gets the default FactoryFinder which will be used for the loading the factory class from META-INF
504         *
505         * @return the default factory finder
506         */
507        FactoryFinder getDefaultFactoryFinder();
508    
509        /**
510         * Sets the factory finder resolver to use.
511         *
512         * @param resolver the factory finder resolver
513         */
514        void setFactoryFinderResolver(FactoryFinderResolver resolver);
515        
516        /**
517         * Gets the FactoryFinder which will be used for the loading the factory class from META-INF in the given path
518         *
519         * @param path the META-INF path
520         * @return the factory finder
521         * @throws NoFactoryAvailableException is thrown if a factory could not be found
522         */
523        FactoryFinder getFactoryFinder(String path) throws NoFactoryAvailableException;
524    
525        /**
526         * Returns the class resolver to be used for loading/lookup of classes.
527         *
528         * @return the resolver
529         */
530        ClassResolver getClassResolver();
531    
532        /**
533         * Returns the package scanning class resolver
534         *
535         * @return the resolver
536         */
537        PackageScanClassResolver getPackageScanClassResolver();
538    
539        /**
540         * Sets the class resolver to be use
541         *
542         * @param resolver the resolver
543         */
544        void setClassResolver(ClassResolver resolver);
545    
546        /**
547         * Sets the package scanning class resolver to use
548         *
549         * @param resolver the resolver
550         */
551        void setPackageScanClassResolver(PackageScanClassResolver resolver);
552    
553        /**
554         * Sets a pluggable service pool to use for {@link Producer} pooling.
555         *
556         * @param servicePool the pool
557         */
558        void setProducerServicePool(ServicePool<Endpoint, Producer> servicePool);
559    
560        /**
561         * Gets the service pool for {@link Producer} pooling.
562         *
563         * @return the service pool
564         */
565        ServicePool<Endpoint, Producer> getProducerServicePool();
566    
567        /**
568         * Uses a custom node id factory when generating auto assigned ids to the nodes in the route definitions
569         *
570         * @param factory  custom factory to use
571         */
572        void setNodeIdFactory(NodeIdFactory factory);
573    
574        /**
575         * Gets the node id factory
576         *
577         * @return  the node id factory
578         */
579        NodeIdFactory getNodeIdFactory();
580    
581        /**
582         * Gets the management strategy
583         *
584         * @return the management strategy
585         */
586        ManagementStrategy getManagementStrategy();
587    
588        /**
589         * Sets the management strategy to use
590         *
591         * @param strategy the management strategy
592         */
593        void setManagementStrategy(ManagementStrategy strategy);
594    
595        /**
596         * Gets the default tracer
597         *
598         * @return the default tracer
599         */
600        InterceptStrategy getDefaultTracer();
601    
602        /**
603         * Disables using JMX as {@link org.apache.camel.spi.ManagementStrategy}.
604         */
605        void disableJMX();
606    
607        /**
608         * Gets the inflight repository
609         *
610         * @return the repository
611         */
612        InflightRepository getInflightRepository();
613    
614        /**
615         * Sets a custom inflight repository to use
616         *
617         * @param repository the repository
618         */
619        void setInflightRepository(InflightRepository repository);
620        
621        /**
622         * Gets the the application context class loader which may be helpful for running camel in other containers
623         *
624         * @return the application context class loader
625         */
626        ClassLoader getApplicationContextClassLoader();
627    
628        /**
629         * Sets the application context class loader
630         *
631         * @param classLoader the class loader
632         */
633        void setApplicationContextClassLoader(ClassLoader classLoader);
634    }