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