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