Coverage Report - org.apache.camel.impl.RouteContext
 
Classes in this File Line Coverage Branch Coverage Complexity
RouteContext
77% 
88% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.impl;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.camel.CamelContext;
 24  
 import org.apache.camel.Endpoint;
 25  
 import org.apache.camel.NoSuchEndpointException;
 26  
 import org.apache.camel.Processor;
 27  
 import org.apache.camel.Route;
 28  
 import org.apache.camel.model.FromType;
 29  
 import org.apache.camel.model.ProcessorType;
 30  
 import org.apache.camel.model.RouteType;
 31  
 import org.apache.camel.processor.Interceptor;
 32  
 import org.apache.camel.processor.Pipeline;
 33  
 import org.apache.camel.processor.ProceedProcessor;
 34  
 
 35  
 /**
 36  
  * The context used to activate new routing rules
 37  
  * 
 38  
  * @version $Revision: $
 39  
  */
 40  
 public class RouteContext {
 41  
     private final RouteType route;
 42  
     private final FromType from;
 43  
     private final Collection<Route> routes;
 44  
     private Endpoint endpoint;
 45  267
     private List<Processor> eventDrivenProcessors = new ArrayList<Processor>();
 46  
     private Interceptor lastInterceptor;
 47  
 
 48  267
     public RouteContext(RouteType route, FromType from, Collection<Route> routes) {
 49  267
         this.route = route;
 50  267
         this.from = from;
 51  267
         this.routes = routes;
 52  267
     }
 53  
 
 54  
     public Endpoint getEndpoint() {
 55  531
         if (endpoint == null) {
 56  267
             endpoint = from.resolveEndpoint(this);
 57  
         }
 58  531
         return endpoint;
 59  
     }
 60  
 
 61  
     public FromType getFrom() {
 62  0
         return from;
 63  
     }
 64  
 
 65  
     public RouteType getRoute() {
 66  336
         return route;
 67  
     }
 68  
 
 69  
     public CamelContext getCamelContext() {
 70  0
         return getRoute().getCamelContext();
 71  
     }
 72  
 
 73  
     public Processor createProcessor(ProcessorType node) throws Exception {
 74  198
         return node.createOutputsProcessor(this);
 75  
     }
 76  
 
 77  
     public Endpoint resolveEndpoint(String uri) {
 78  579
         return route.resolveEndpoint(uri);
 79  
     }
 80  
 
 81  
     /**
 82  
      * Resolves an endpoint from either a URI or a named reference
 83  
      */
 84  
     public Endpoint resolveEndpoint(String uri, String ref) {
 85  579
         Endpoint endpoint = null;
 86  579
         if (uri != null) {
 87  579
             endpoint = resolveEndpoint(uri);
 88  576
             if (endpoint == null) {
 89  0
                 throw new NoSuchEndpointException(uri);
 90  
             }
 91  
         }
 92  576
         if (ref != null) {
 93  0
             endpoint = lookup(ref, Endpoint.class);
 94  0
             if (endpoint == null) {
 95  0
                 throw new NoSuchEndpointException("ref:" + ref);
 96  
             }
 97  
         }
 98  576
         if (endpoint == null) {
 99  0
             throw new IllegalArgumentException("Either 'uri' or 'ref' must be specified on: " + this);
 100  
         } else {
 101  576
             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  0
         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  264
         if (!eventDrivenProcessors.isEmpty()) {
 120  258
             Processor processor = Pipeline.newInstance(eventDrivenProcessors);
 121  258
             routes.add(new EventDrivenConsumerRoute(getEndpoint(), processor));
 122  
         }
 123  264
     }
 124  
 
 125  
     public void addEventDrivenProcessor(Processor processor) {
 126  294
         eventDrivenProcessors.add(processor);
 127  294
     }
 128  
 
 129  
     public void intercept(Interceptor interceptor) {
 130  24
         getRoute().intercept(interceptor);
 131  24
         lastInterceptor = interceptor;
 132  24
     }
 133  
 
 134  
     public Processor createProceedProcessor() {
 135  24
         if (lastInterceptor == null) {
 136  0
             throw new IllegalArgumentException("Cannot proceed() from outside of an interceptor!");
 137  
         }
 138  
         else {
 139  24
             return new ProceedProcessor(lastInterceptor);
 140  
         }
 141  
     }
 142  
 }