001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel; 019 020 import org.apache.camel.builder.RouteBuilder; 021 import org.apache.camel.spi.ExchangeConverter; 022 import org.apache.camel.spi.Injector; 023 024 import java.util.Collection; 025 import java.util.List; 026 import java.util.concurrent.Callable; 027 028 /** 029 * Interface used to represent the context used to configure routes and the 030 * policies to use during message exchanges between endpoints. 031 * 032 * @version $Revision: 545303 $ 033 */ 034 public interface CamelContext extends Service { 035 036 // Component Management Methods 037 //----------------------------------------------------------------------- 038 039 /** 040 * Adds a component to the context. 041 */ 042 void addComponent(String componentName, Component component); 043 044 /** 045 * Gets a component from the context by name. 046 */ 047 Component getComponent(String componentName); 048 049 /** 050 * Gets a component from the context by name and specifying the expected type of component. 051 */ 052 <T extends Component> T getComponent(String name, Class<T> componentType); 053 054 /** 055 * Removes a previously added component. 056 * 057 * @param componentName 058 * @return the previously added component or null if it had not been previously added. 059 */ 060 Component removeComponent(String componentName); 061 062 /** 063 * Gets the a previously added component by name or lazily creates the component 064 * using the factory Callback. 065 * 066 * @param componentName the name of the component 067 * @param factory used to create a new component instance if the component was not previously added. 068 * @return 069 */ 070 Component getOrCreateComponent(String componentName, Callable<Component> factory); 071 072 // Endpoint Management Methods 073 //----------------------------------------------------------------------- 074 075 /** 076 * Resolves the given URI to an {@see Endpoint}. If the URI has a singleton endpoint 077 * registered, then the singleton is returned. Otherwise, a new {@see Endpoint} is created 078 * and if the endpoint is a singleton it is registered as a singleton endpoint. 079 */ 080 Endpoint getEndpoint(String uri); 081 082 /** 083 * Resolves the given URI to an {@see Endpoint} of the specified type. 084 * If the URI has a singleton endpoint registered, then the singleton is returned. 085 * Otherwise, a new {@see Endpoint} is created and if the endpoint is a 086 * singleton it is registered as a singleton endpoint. 087 */ 088 <T extends Endpoint> T getEndpoint(String name, Class<T> endpointType); 089 090 /** 091 * Returns the collection of all registered singleton endpoints. 092 */ 093 Collection<Endpoint> getSingletonEndpoints(); 094 095 /** 096 * Adds the endpoint to the context using the given URI. The endpoint will be registered as a singleton. 097 * 098 * @param uri the URI to be used to resolve this endpoint 099 * @param endpoint the endpoint to be added to the context 100 * @return the old endpoint that was previously registered to the context if there was 101 * already an endpoint for that URI 102 * @throws Exception if the new endpoint could not be started or the old endpoint could not be stopped 103 */ 104 Endpoint addSingletonEndpoint(String uri, Endpoint endpoint) throws Exception; 105 106 /** 107 * Removes the singleton endpoint with the given URI 108 * 109 * @param uri the URI to be used to remove 110 * @return the endpoint that was removed or null if there is no endpoint for this URI 111 * @throws Exception if endpoint could not be stopped 112 */ 113 Endpoint removeSingletonEndpoint(String uri) throws Exception; 114 115 116 // Route Management Methods 117 //----------------------------------------------------------------------- 118 119 /** 120 * Returns the current routes in this context 121 * 122 * @return the current routes in this context 123 */ 124 List<Route> getRoutes(); 125 126 /** 127 * Sets the routes for this context, replacing any current routes 128 * 129 * @param routes the new routes to use 130 */ 131 void setRoutes(List<Route> routes); 132 133 /** 134 * Adds a collection of routes to this context 135 * 136 * @param routes the routes to add 137 */ 138 void addRoutes(Collection<Route> routes) throws Exception; 139 140 /** 141 * Adds a collection of routes to this context using the given builder 142 * to build them 143 * 144 * @param builder the builder which will create the routes and add them to this context 145 * @throws Exception if the routes could not be created for whatever reason 146 */ 147 void addRoutes(RouteBuilder builder) throws Exception; 148 149 // Properties 150 //----------------------------------------------------------------------- 151 152 /** 153 * Returns the converter of exchanges from one type to another 154 * @return 155 */ 156 ExchangeConverter getExchangeConverter(); 157 158 /** 159 * Returns the type converter used to coerce types from one type to another 160 */ 161 TypeConverter getTypeConverter(); 162 163 /** 164 * Returns the injector used to instantiate objects by type 165 */ 166 Injector getInjector(); 167 168 }