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 023 import org.apache.camel.AsyncProcessor; 024 import org.apache.camel.CamelContext; 025 import org.apache.camel.Endpoint; 026 import org.apache.camel.Exchange; 027 import org.apache.camel.Intercept; 028 import org.apache.camel.NoSuchEndpointException; 029 import org.apache.camel.Processor; 030 import org.apache.camel.Route; 031 import org.apache.camel.impl.converter.AsyncProcessorTypeConverter; 032 import org.apache.camel.model.FromType; 033 import org.apache.camel.model.ProcessorType; 034 import org.apache.camel.model.RouteType; 035 import org.apache.camel.processor.Interceptor; 036 import org.apache.camel.processor.Pipeline; 037 import org.apache.camel.processor.ProceedProcessor; 038 import org.apache.camel.processor.UnitOfWorkProcessor; 039 import org.apache.camel.spi.InterceptStrategy; 040 import org.apache.camel.spi.RouteContext; 041 042 /** 043 * The context used to activate new routing rules 044 * 045 * @version $Revision: 42218 $ 046 */ 047 public class DefaultRouteContext implements RouteContext { 048 private RouteType route; 049 private FromType from; 050 private Collection<Route> routes; 051 private Endpoint<? extends Exchange> endpoint; 052 private List<Processor> eventDrivenProcessors = new ArrayList<Processor>(); 053 private Interceptor lastInterceptor; 054 private CamelContext camelContext; 055 private List<InterceptStrategy> interceptStrategies = new ArrayList<InterceptStrategy>(); 056 057 public DefaultRouteContext(RouteType route, FromType from, Collection<Route> routes) { 058 this.route = route; 059 this.from = from; 060 this.routes = routes; 061 } 062 063 /** 064 * Only used for lazy construction from inside ExpressionType 065 */ 066 public DefaultRouteContext(CamelContext camelContext) { 067 this.camelContext = camelContext; 068 routes = new ArrayList<Route>(); 069 route = new RouteType("temporary"); 070 } 071 072 public Endpoint<? extends Exchange> getEndpoint() { 073 if (endpoint == null) { 074 endpoint = from.resolveEndpoint(this); 075 } 076 return endpoint; 077 } 078 079 public FromType getFrom() { 080 return from; 081 } 082 083 public RouteType getRoute() { 084 return route; 085 } 086 087 public CamelContext getCamelContext() { 088 if (camelContext == null) { 089 camelContext = getRoute().getCamelContext(); 090 } 091 return camelContext; 092 } 093 094 public Processor createProcessor(ProcessorType node) throws Exception { 095 return node.createOutputsProcessor(this); 096 } 097 098 public Endpoint<? extends Exchange> resolveEndpoint(String uri) { 099 return route.resolveEndpoint(uri); 100 } 101 102 public Endpoint<? extends Exchange> resolveEndpoint(String uri, String ref) { 103 Endpoint<? extends Exchange> endpoint = null; 104 if (uri != null) { 105 endpoint = resolveEndpoint(uri); 106 if (endpoint == null) { 107 throw new NoSuchEndpointException(uri); 108 } 109 } 110 if (ref != null) { 111 endpoint = lookup(ref, Endpoint.class); 112 if (endpoint == null) { 113 throw new NoSuchEndpointException("ref:" + ref); 114 } 115 } 116 if (endpoint == null) { 117 throw new IllegalArgumentException("Either 'uri' or 'ref' must be specified on: " + this); 118 } else { 119 return endpoint; 120 } 121 } 122 123 public <T> T lookup(String name, Class<T> type) { 124 return getCamelContext().getRegistry().lookup(name, type); 125 } 126 127 public void commit() { 128 // now lets turn all of the event driven consumer processors into a 129 // single route 130 if (!eventDrivenProcessors.isEmpty()) { 131 Processor processor = Pipeline.newInstance(eventDrivenProcessors); 132 133 // lets create the async processor 134 final AsyncProcessor asyncProcessor = AsyncProcessorTypeConverter.convert(processor); 135 Processor unitOfWorkProcessor = new UnitOfWorkProcessor(asyncProcessor); 136 137 // TODO: hz: move all this into the lifecycle strategy! (used by jmx naming strategy) 138 Route edcr = new EventDrivenConsumerRoute(getEndpoint(), unitOfWorkProcessor); 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 routes.add(edcr); 144 } 145 } 146 147 public void addEventDrivenProcessor(Processor processor) { 148 eventDrivenProcessors.add(processor); 149 } 150 151 public void intercept(Intercept interceptor) { 152 /* 153 InterceptorRef block = new InterceptorRef(interceptor); 154 RouteType route = getRoute(); 155 List<ProcessorType<?>> list = route.getOutputs(); 156 for (ProcessorType<?> processorType : list) { 157 block.addOutput(processorType); 158 } 159 route.clearOutput(); 160 route.intercept(block); 161 */ 162 163 //getRoute().getInterceptors().add(new InterceptorRef(interceptor)); 164 lastInterceptor = (Interceptor)interceptor; 165 } 166 167 public Processor createProceedProcessor() { 168 if (lastInterceptor == null) { 169 throw new IllegalArgumentException("Cannot proceed() from outside of an interceptor!"); 170 } else { 171 return new ProceedProcessor(lastInterceptor); 172 } 173 } 174 175 public List<InterceptStrategy> getInterceptStrategies() { 176 return interceptStrategies; 177 } 178 179 public void setInterceptStrategies(List<InterceptStrategy> interceptStrategies) { 180 this.interceptStrategies = interceptStrategies; 181 } 182 183 public void addInterceptStrategy(InterceptStrategy interceptStrategy) { 184 getInterceptStrategies().add(interceptStrategy); 185 } 186 }