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 */
017package org.apache.camel.spi;
018
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.Endpoint;
025import org.apache.camel.EndpointAware;
026import org.apache.camel.ErrorHandlerFactory;
027import org.apache.camel.NamedNode;
028import org.apache.camel.Processor;
029import org.apache.camel.Route;
030import org.apache.camel.RuntimeConfiguration;
031import org.apache.camel.meta.Experimental;
032
033/**
034 * The context used to activate new routing rules
035 */
036public interface RouteContext extends RuntimeConfiguration, EndpointAware {
037
038    /**
039     * Gets the route id
040     */
041    String getRouteId();
042
043    /**
044     * Get the route type
045     *
046     * @return the route type
047     */
048    NamedNode getRoute();
049
050    /**
051     * Gets the camel context
052     *
053     * @return the camel context
054     */
055    CamelContext getCamelContext();
056
057    /**
058     * Resolves an endpoint from the URI
059     *
060     * @param uri the URI
061     * @return the resolved endpoint
062     */
063    Endpoint resolveEndpoint(String uri);
064
065    /**
066     * Resolves an endpoint from either a URI or a named reference
067     *
068     * @param uri  the URI or
069     * @param ref  the named reference
070     * @return the resolved endpoint
071     */
072    Endpoint resolveEndpoint(String uri, String ref);
073
074    /**
075     * lookup an object by name and type
076     *
077     * @param name  the name to lookup
078     * @param type  the expected type
079     * @return the found object
080     */
081    <T> T lookup(String name, Class<T> type);
082
083    /**
084     * lookup an object by name and type or throws {@link org.apache.camel.NoSuchBeanException} if not found.
085     *
086     * @param name  the name to lookup
087     * @param type  the expected type
088     * @return the found object
089     */
090    <T> T mandatoryLookup(String name, Class<T> type);
091
092    /**
093     * lookup objects by type
094     *
095     * @param type the expected type
096     * @return the found objects with the name as the key in the map. Returns an empty map if none found.
097     */
098    <T> Map<String, T> lookupByType(Class<T> type);
099
100    /**
101     * For completing the route creation, creating a single event driven route
102     * for the current from endpoint with any processors required
103     */
104    Route commit();
105
106    /**
107     * Adds an event driven processor
108     *
109     * @param processor the processor
110     */
111    void addEventDrivenProcessor(Processor processor);
112
113    /**
114     * This method retrieves the InterceptStrategy instances this route context.
115     *
116     * @return the strategy
117     */
118    List<InterceptStrategy> getInterceptStrategies();
119
120    /**
121     * This method sets the InterceptStrategy instances on this route context.
122     *
123     * @param interceptStrategies the strategies
124     */
125    void setInterceptStrategies(List<InterceptStrategy> interceptStrategies);
126
127    /**
128     * Adds a InterceptStrategy to this route context
129     *
130     * @param interceptStrategy the strategy
131     */
132    void addInterceptStrategy(InterceptStrategy interceptStrategy);
133
134    /**
135     * Sets a special intercept strategy for management.
136     * <p/>
137     * Is by default used to correlate managed performance counters with processors
138     * when the runtime route is being constructed
139     *
140     * @param interceptStrategy the managed intercept strategy
141     */
142    void setManagementInterceptStrategy(ManagementInterceptStrategy interceptStrategy);
143
144    /**
145     * Gets the special managed intercept strategy if any
146     *
147     * @return the managed intercept strategy, or <tt>null</tt> if not managed
148     */
149    ManagementInterceptStrategy getManagementInterceptStrategy();
150
151    /**
152     * If this flag is true, {@link org.apache.camel.reifier.ProcessorReifier#addRoutes(RouteContext)}
153     * will not add processor to addEventDrivenProcessor to the RouteContext and it
154     * will prevent from adding an EventDrivenRoute.
155     *
156     * @param value the flag
157     */
158    void setIsRouteAdded(boolean value);
159
160    void setEndpoint(Endpoint endpoint);
161
162    /**
163     * Returns the isRouteAdded flag
164     * 
165     * @return the flag
166     */
167    boolean isRouteAdded();
168    
169    /**
170     * Gets the route policy List
171     *
172     * @return the route policy list if any
173     */
174    List<RoutePolicy> getRoutePolicyList();
175
176    /**
177     * Sets a custom route policy List
178     *
179     * @param routePolicyList the custom route policy list
180     */
181    void setRoutePolicyList(List<RoutePolicy> routePolicyList);
182
183    /**
184     * Sets whether the route should automatically start when Camel starts.
185     * <p/>
186     * Default is <tt>true</tt> to always start up.
187     *
188     * @param autoStartup whether to start up automatically.
189     */
190    void setAutoStartup(Boolean autoStartup);
191
192    /**
193     * Gets whether the route should automatically start when Camel starts.
194     * <p/>
195     * Default is <tt>true</tt> to always start up.
196     *
197     * @return <tt>true</tt> if route should automatically start
198     */
199    Boolean isAutoStartup();
200
201    void setStartupOrder(Integer startupOrder);
202
203    Integer getStartupOrder();
204
205    void setErrorHandlerFactory(ErrorHandlerFactory errorHandlerFactory);
206
207    ErrorHandlerFactory getErrorHandlerFactory();
208
209    void addAdvice(CamelInternalProcessorAdvice<?> advice);
210
211    void addProperty(String key, Object value);
212
213    /**
214     * Gets the last error.
215     *
216     * @return the error
217     */
218    default RouteError getLastError() {
219        return null;
220    }
221
222    /**
223     * Sets the last error.
224     *
225     * @param error the error
226     */
227    default void setLastError(RouteError error) {
228    }
229
230    /**
231     * Gets the  {@link RouteController} for this route.
232     *
233     * @return the route controller,
234     */
235    @Experimental
236    default RouteController getRouteController() {
237        return null;
238    }
239
240    /**
241     * Sets the {@link RouteController} for this route.
242     *
243     * @param controller the RouteController
244     */
245    @Experimental
246    default void setRouteController(RouteController controller) {
247    }
248
249    Processor getOnCompletion(String onCompletionId);
250
251    void setOnCompletion(String onCompletionId, Processor processor);
252
253    Processor getOnException(String onExceptionId);
254
255    void setOnException(String onExceptionId, Processor processor);
256
257    /**
258     * Adds error handler for the given exception type
259     *
260     * @param factory       the error handler factory
261     * @param exception     the exception to handle
262     */
263    void addErrorHandler(ErrorHandlerFactory factory, NamedNode exception);
264
265    /**
266     * Gets the error handlers
267     *
268     * @param factory       the error handler factory
269     */
270    Set<NamedNode> getErrorHandlers(ErrorHandlerFactory factory);
271
272    /**
273     * Link the error handlers from a factory to another
274     *
275     * @param source        the source factory
276     * @param target        the target factory
277     */
278    void addErrorHandlerFactoryReference(ErrorHandlerFactory source, ErrorHandlerFactory target);
279
280}