Coverage Report - org.apache.camel.processor.Splitter
 
Classes in this File Line Coverage Branch Coverage Complexity
Splitter
100% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.processor;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Expression;
 22  
 import org.apache.camel.Processor;
 23  
 import org.apache.camel.converter.ObjectConverter;
 24  
 import org.apache.camel.impl.ServiceSupport;
 25  
 import static org.apache.camel.util.ObjectHelper.notNull;
 26  
 import org.apache.camel.util.ServiceHelper;
 27  
 
 28  
 import java.util.Iterator;
 29  
 
 30  
 /**
 31  
  * Implements a dynamic <a href="http://activemq.apache.org/camel/splitter.html">Splitter</a> pattern
 32  
  * where an expression is evaluated to iterate through each of the parts of a message and then each part is then send to some endpoint.
 33  
  *
 34  
  * @version $Revision: 534145 $
 35  
  */
 36  
 public class Splitter extends ServiceSupport implements Processor {
 37  
     private final Processor processor;
 38  
     private final Expression expression;
 39  
 
 40  2
     public Splitter(Processor destination, Expression expression) {
 41  2
         this.processor = destination;
 42  2
         this.expression = expression;
 43  2
         notNull(destination, "destination");
 44  2
         notNull(expression, "expression");
 45  2
     }
 46  
 
 47  
     @Override
 48  
     public String toString() {
 49  5
         return "Splitter[on: " + expression + " to: " + processor + "]";
 50  
     }
 51  
 
 52  
     public void process(Exchange exchange) throws Exception {
 53  1
         Object value = expression.evaluate(exchange);
 54  1
         Iterator iter = ObjectConverter.iterator(value);
 55  5
         while (iter.hasNext()) {
 56  4
             Object part = iter.next();
 57  4
             Exchange newExchange = exchange.copy();
 58  4
             newExchange.getIn().setBody(part);
 59  4
             processor.process(newExchange);
 60  4
         }
 61  1
     }
 62  
 
 63  
     protected void doStart() throws Exception {
 64  1
         ServiceHelper.startServices(processor);
 65  1
     }
 66  
 
 67  
     protected void doStop() throws Exception {
 68  1
         ServiceHelper.stopServices(processor);
 69  1
     }
 70  
 }