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