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.concurrent.Callable;
022    
023    import org.apache.camel.builder.ErrorHandlerBuilder;
024    import org.apache.camel.model.RouteType;
025    import org.apache.camel.spi.ExchangeConverter;
026    import org.apache.camel.spi.Injector;
027    import org.apache.camel.spi.InterceptStrategy;
028    import org.apache.camel.spi.Language;
029    import org.apache.camel.spi.LifecycleStrategy;
030    import org.apache.camel.spi.Registry;
031    
032    /**
033     * Interface used to represent the context used to configure routes and the
034     * policies to use during message exchanges between endpoints.
035     *
036     * @version $Revision: 45030 $
037     */
038    public interface CamelContext extends Service {
039    
040        /**
041         * Gets the name of the this context.
042         */
043        String getName();
044    
045        // Component Management Methods
046        //-----------------------------------------------------------------------
047    
048        /**
049         * Adds a component to the context.
050         */
051        void addComponent(String componentName, Component component);
052    
053        /**
054         * Gets a component from the context by name.
055         */
056        Component getComponent(String componentName);
057    
058        /**
059         * Gets a component from the context by name and specifying the expected type of component.
060         */
061        <T extends Component> T getComponent(String name, Class<T> componentType);
062    
063        /**
064         * Removes a previously added component.
065         *
066         * @param componentName
067         * @return the previously added component or null if it had not been previously added.
068         */
069        Component removeComponent(String componentName);
070    
071        /**
072         * Gets the a previously added component by name or lazily creates the component
073         * using the factory Callback.
074         *
075         * @param componentName the name of the component
076         * @param factory       used to create a new component instance if the component was not previously added.
077         * @return the component
078         */
079        Component getOrCreateComponent(String componentName, Callable<Component> factory);
080    
081        // Endpoint Management Methods
082        //-----------------------------------------------------------------------
083    
084        /**
085         * Resolves the given URI to an {@link Endpoint}.  If the URI has a singleton endpoint
086         * registered, then the singleton is returned.  Otherwise, a new {@link Endpoint} is created
087         * and if the endpoint is a singleton it is registered as a singleton endpoint.
088         */
089        Endpoint getEndpoint(String uri);
090    
091        /**
092         * Resolves the given URI to an {@link Endpoint} of the specified type.
093         * If the URI has a singleton endpoint registered, then the singleton is returned.
094         * Otherwise, a new {@link Endpoint} is created and if the endpoint is a
095         * singleton it is registered as a singleton endpoint.
096         */
097        <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType);
098    
099        /**
100         * Returns the collection of all registered singleton endpoints.
101         */
102        Collection<Endpoint> getSingletonEndpoints();
103    
104        /**
105         * Adds the endpoint to the context using the given URI.  The endpoint will be registered as a singleton.
106         *
107         * @param uri the URI to be used to resolve this endpoint
108         * @param endpoint the endpoint to be added to the context
109         * @return the old endpoint that was previously registered to the context if there was
110         * already an endpoint for that URI
111         * @throws Exception if the new endpoint could not be started or the old endpoint could not be stopped
112         */
113        Endpoint addSingletonEndpoint(String uri, Endpoint endpoint) throws Exception;
114    
115        /**
116         * Removes the singleton endpoint with the given URI
117         *
118         * @param uri the URI to be used to remove
119         * @return the endpoint that was removed or null if there is no endpoint for this URI
120         * @throws Exception if endpoint could not be stopped
121         */
122        Endpoint removeSingletonEndpoint(String uri) throws Exception;
123    
124    
125        // Route Management Methods
126        //-----------------------------------------------------------------------
127    
128        /**
129         * Returns a list of the current route definitions
130         */
131        List<RouteType> getRouteDefinitions();
132    
133        /**
134         * Returns the current routes in this context
135         */
136        List<Route> getRoutes();
137    
138        /**
139         * Sets the routes for this context, replacing any current routes
140         *
141         * @param routes the new routes to use
142         */
143        void setRoutes(List<Route> routes);
144    
145        /**
146         * Adds a collection of routes to this context
147         *
148         * @param routes the routes to add
149         */
150        void addRoutes(Collection<Route> routes) throws Exception;
151    
152        /**
153         * Adds a collection of routes to this context using the given builder
154         * to build them
155         *
156         * @param builder the builder which will create the routes and add them to this context
157         * @throws Exception if the routes could not be created for whatever reason
158         */
159        void addRoutes(Routes builder) throws Exception;
160    
161        /**
162         * Adds a collection of route definitions to the context
163         */
164        void addRouteDefinitions(Collection<RouteType> routeDefinitions) throws Exception;
165    
166    
167        // Properties
168        //-----------------------------------------------------------------------
169    
170        /**
171         * Returns the converter of exchanges from one type to another
172         */
173        ExchangeConverter getExchangeConverter();
174    
175        /**
176         * Returns the type converter used to coerce types from one type to another
177         */
178        TypeConverter getTypeConverter();
179    
180        /**
181         * Returns the registry used to lookup components by name and type such as the Spring ApplicationContext,
182         * JNDI or the OSGi Service Registry
183         */
184        Registry getRegistry();
185    
186        /**
187         * Returns the injector used to instantiate objects by type
188         */
189        Injector getInjector();
190    
191        /**
192         * Returns the lifecycle strategy used to handle lifecycle notification
193         */
194        LifecycleStrategy getLifecycleStrategy();
195    
196        /**
197         * Resolves a language for creating expressions
198         */
199        Language resolveLanguage(String language);
200    
201        /**
202         * Creates a new ProducerTemplate
203         */
204        <E extends Exchange> ProducerTemplate<E> createProducerTemplate();
205    
206        void addInterceptStrategy(InterceptStrategy interceptStrategy);
207    
208        /**
209         * Gets the default error handler builder which is inherited by the routes
210         */
211        ErrorHandlerBuilder getErrorHandlerBuilder();
212    
213        /**
214         * Sets the default error handler builder which is inherited by the routes
215         */
216        void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder);
217    }