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