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.impl;
018
019 import java.util.ArrayList;
020 import java.util.Collection;
021 import java.util.List;
022 import java.util.Map;
023
024 import org.apache.camel.CamelContext;
025 import org.apache.camel.Endpoint;
026 import org.apache.camel.NoSuchEndpointException;
027 import org.apache.camel.Processor;
028 import org.apache.camel.Route;
029 import org.apache.camel.model.DataFormatDefinition;
030 import org.apache.camel.model.FromDefinition;
031 import org.apache.camel.model.ProcessorDefinition;
032 import org.apache.camel.model.RouteDefinition;
033 import org.apache.camel.processor.Pipeline;
034 import org.apache.camel.processor.UnitOfWorkProcessor;
035 import org.apache.camel.spi.InterceptStrategy;
036 import org.apache.camel.spi.RouteContext;
037
038 /**
039 * The context used to activate new routing rules
040 *
041 * @version $Revision: 782535 $
042 */
043 public class DefaultRouteContext implements RouteContext {
044 private final RouteDefinition route;
045 private FromDefinition from;
046 private final Collection<Route> routes;
047 private Endpoint endpoint;
048 private final List<Processor> eventDrivenProcessors = new ArrayList<Processor>();
049 private CamelContext camelContext;
050 private List<InterceptStrategy> interceptStrategies = new ArrayList<InterceptStrategy>();
051 private boolean routeAdded;
052
053 public DefaultRouteContext(RouteDefinition route, FromDefinition from, Collection<Route> routes) {
054 this.route = route;
055 this.from = from;
056 this.routes = routes;
057 }
058
059 /**
060 * Only used for lazy construction from inside ExpressionType
061 */
062 public DefaultRouteContext(CamelContext camelContext) {
063 this.camelContext = camelContext;
064 routes = new ArrayList<Route>();
065 route = new RouteDefinition("temporary");
066 }
067
068 public Endpoint getEndpoint() {
069 if (endpoint == null) {
070 endpoint = from.resolveEndpoint(this);
071 }
072 return endpoint;
073 }
074
075 public FromDefinition getFrom() {
076 return from;
077 }
078
079 public RouteDefinition getRoute() {
080 return route;
081 }
082
083 public CamelContext getCamelContext() {
084 if (camelContext == null) {
085 camelContext = getRoute().getCamelContext();
086 }
087 return camelContext;
088 }
089
090 public Processor createProcessor(ProcessorDefinition node) throws Exception {
091 return node.createOutputsProcessor(this);
092 }
093
094 public Endpoint resolveEndpoint(String uri) {
095 return route.resolveEndpoint(uri);
096 }
097
098 public Endpoint resolveEndpoint(String uri, String ref) {
099 Endpoint endpoint = null;
100 if (uri != null) {
101 endpoint = resolveEndpoint(uri);
102 if (endpoint == null) {
103 throw new NoSuchEndpointException(uri);
104 }
105 }
106 if (ref != null) {
107 endpoint = lookup(ref, Endpoint.class);
108 if (endpoint == null) {
109 throw new NoSuchEndpointException("ref:" + ref);
110 }
111 }
112 if (endpoint == null) {
113 throw new IllegalArgumentException("Either 'uri' or 'ref' must be specified on: " + this);
114 } else {
115 return endpoint;
116 }
117 }
118
119 public <T> T lookup(String name, Class<T> type) {
120 return getCamelContext().getRegistry().lookup(name, type);
121 }
122
123 public <T> Map<String, T> lookupByType(Class<T> type) {
124 return getCamelContext().getRegistry().lookupByType(type);
125 }
126
127 @SuppressWarnings("unchecked")
128 public void commit() {
129 // now lets turn all of the event driven consumer processors into a single route
130 if (!eventDrivenProcessors.isEmpty()) {
131 Processor processor = Pipeline.newInstance(eventDrivenProcessors);
132
133 // and wrap it in a unit of work so the UoW is on the top, so the entire route will be in the same UoW
134 Processor unitOfWorkProcessor = new UnitOfWorkProcessor(processor);
135
136 // and create the route that wraps the UoW
137 Route edcr = new EventDrivenConsumerRoute(getEndpoint(), unitOfWorkProcessor);
138 edcr.getProperties().put(Route.ID_PROPERTY, route.idOrCreate());
139 edcr.getProperties().put(Route.PARENT_PROPERTY, Integer.toHexString(route.hashCode()));
140 if (route.getGroup() != null) {
141 edcr.getProperties().put(Route.GROUP_PROPERTY, route.getGroup());
142 }
143
144 routes.add(edcr);
145 }
146 }
147
148 public void addEventDrivenProcessor(Processor processor) {
149 eventDrivenProcessors.add(processor);
150 }
151
152 public List<InterceptStrategy> getInterceptStrategies() {
153 return interceptStrategies;
154 }
155
156 public void setInterceptStrategies(List<InterceptStrategy> interceptStrategies) {
157 this.interceptStrategies = interceptStrategies;
158 }
159
160 public void addInterceptStrategy(InterceptStrategy interceptStrategy) {
161 getInterceptStrategies().add(interceptStrategy);
162 }
163
164 public boolean isRouteAdded() {
165 return routeAdded;
166 }
167
168 public void setIsRouteAdded(boolean routeAdded) {
169 this.routeAdded = routeAdded;
170 }
171
172 public DataFormatDefinition getDataFormat(String ref) {
173 Map<String, DataFormatDefinition> dataFormats = getCamelContext().getDataFormats();
174 if (dataFormats != null) {
175 return dataFormats.get(ref);
176 } else {
177 return null;
178 }
179 }
180
181 }