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.CamelContext; 024 import org.apache.camel.Endpoint; 025 import org.apache.camel.NoSuchEndpointException; 026 import org.apache.camel.Processor; 027 import org.apache.camel.Route; 028 import org.apache.camel.model.FromType; 029 import org.apache.camel.model.ProcessorType; 030 import org.apache.camel.model.RouteType; 031 import org.apache.camel.processor.Interceptor; 032 import org.apache.camel.processor.Pipeline; 033 import org.apache.camel.processor.ProceedProcessor; 034 035 /** 036 * The context used to activate new routing rules 037 * 038 * @version $Revision: $ 039 */ 040 public class RouteContext { 041 private final RouteType route; 042 private final FromType from; 043 private final Collection<Route> routes; 044 private Endpoint endpoint; 045 private List<Processor> eventDrivenProcessors = new ArrayList<Processor>(); 046 private Interceptor lastInterceptor; 047 048 public RouteContext(RouteType route, FromType from, Collection<Route> routes) { 049 this.route = route; 050 this.from = from; 051 this.routes = routes; 052 } 053 054 public Endpoint getEndpoint() { 055 if (endpoint == null) { 056 endpoint = from.resolveEndpoint(this); 057 } 058 return endpoint; 059 } 060 061 public FromType getFrom() { 062 return from; 063 } 064 065 public RouteType getRoute() { 066 return route; 067 } 068 069 public CamelContext getCamelContext() { 070 return getRoute().getCamelContext(); 071 } 072 073 public Processor createProcessor(ProcessorType node) throws Exception { 074 return node.createOutputsProcessor(this); 075 } 076 077 public Endpoint resolveEndpoint(String uri) { 078 return route.resolveEndpoint(uri); 079 } 080 081 /** 082 * Resolves an endpoint from either a URI or a named reference 083 */ 084 public Endpoint resolveEndpoint(String uri, String ref) { 085 Endpoint endpoint = null; 086 if (uri != null) { 087 endpoint = resolveEndpoint(uri); 088 if (endpoint == null) { 089 throw new NoSuchEndpointException(uri); 090 } 091 } 092 if (ref != null) { 093 endpoint = lookup(ref, Endpoint.class); 094 if (endpoint == null) { 095 throw new NoSuchEndpointException("ref:" + ref); 096 } 097 } 098 if (endpoint == null) { 099 throw new IllegalArgumentException("Either 'uri' or 'ref' must be specified on: " + this); 100 } else { 101 return endpoint; 102 } 103 } 104 105 /** 106 * lookup an object by name and type 107 */ 108 public <T> T lookup(String name, Class<T> type) { 109 return getCamelContext().getRegistry().lookup(name, type); 110 } 111 112 /** 113 * Lets complete the route creation, creating a single event driven route 114 * for the current from endpoint with any processors required 115 */ 116 public void commit() { 117 // now lets turn all of the event driven consumer processors into a 118 // single route 119 if (!eventDrivenProcessors.isEmpty()) { 120 Processor processor = Pipeline.newInstance(eventDrivenProcessors); 121 routes.add(new EventDrivenConsumerRoute(getEndpoint(), processor)); 122 } 123 } 124 125 public void addEventDrivenProcessor(Processor processor) { 126 eventDrivenProcessors.add(processor); 127 } 128 129 public void intercept(Interceptor interceptor) { 130 getRoute().intercept(interceptor); 131 lastInterceptor = interceptor; 132 } 133 134 public Processor createProceedProcessor() { 135 if (lastInterceptor == null) { 136 throw new IllegalArgumentException("Cannot proceed() from outside of an interceptor!"); 137 } 138 else { 139 return new ProceedProcessor(lastInterceptor); 140 } 141 } 142 }