Coverage Report - org.apache.camel.processor.TryProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
TryProcessor
73% 
100% 
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.processor;
 18  
 
 19  
 import java.util.List;
 20  
 
 21  
 import org.apache.camel.Exchange;
 22  
 import org.apache.camel.Processor;
 23  
 import org.apache.camel.impl.ServiceSupport;
 24  
 import org.apache.camel.util.ServiceHelper;
 25  
 import org.apache.commons.logging.Log;
 26  
 import org.apache.commons.logging.LogFactory;
 27  
 
 28  
 /**
 29  
  * Implements try/catch/finally type processing
 30  
  * 
 31  
  * @version $Revision: $
 32  
  */
 33  
 public class TryProcessor extends ServiceSupport implements Processor {
 34  3
     private static final Log LOG = LogFactory.getLog(TryProcessor.class);
 35  
 
 36  
     private final Processor tryProcessor;
 37  
     private final List<CatchProcessor> catchClauses;
 38  
     private final Processor finallyProcessor;
 39  
 
 40  6
     public TryProcessor(Processor tryProcessor, List<CatchProcessor> catchClauses, Processor finallyProcessor) {
 41  6
         this.tryProcessor = tryProcessor;
 42  6
         this.catchClauses = catchClauses;
 43  6
         this.finallyProcessor = finallyProcessor;
 44  6
     }
 45  
 
 46  
     public String toString() {
 47  6
         String finallyText = (finallyProcessor == null) ? "" : " Finally {" + finallyProcessor + "}";
 48  6
         return "Try {" + tryProcessor + "} " + catchClauses + finallyText;
 49  
     }
 50  
 
 51  
     public void process(Exchange exchange) throws Exception {
 52  6
         boolean doneTry = false;
 53  
         try {
 54  6
             tryProcessor.process(exchange);
 55  3
             doneTry = true;
 56  
 
 57  3
             if (finallyProcessor != null) {
 58  0
                 finallyProcessor.process(exchange);
 59  
             }
 60  3
         } catch (Exception e) {
 61  3
             handleException(exchange, e);
 62  
 
 63  3
             if (!doneTry && finallyProcessor != null) {
 64  
                 try {
 65  0
                     finallyProcessor.process(exchange);
 66  0
                 } catch (Exception e2) {
 67  0
                     LOG.warn("Caught exception in finally block while handling other exception: " + e2, e2);
 68  0
                 }
 69  
             }
 70  3
         }
 71  6
     }
 72  
 
 73  
     protected void doStart() throws Exception {
 74  6
         ServiceHelper.startServices(tryProcessor, catchClauses, finallyProcessor);
 75  6
     }
 76  
 
 77  
     protected void doStop() throws Exception {
 78  6
         ServiceHelper.stopServices(tryProcessor, catchClauses, finallyProcessor);
 79  6
     }
 80  
 
 81  
     protected void handleException(Exchange exchange, Exception e) throws Exception {
 82  3
         for (CatchProcessor catchClause : catchClauses) {
 83  3
             if (catchClause.catches(e)) {
 84  
                 // lets attach the exception to the exchange
 85  3
                 exchange.setException(e);
 86  
                 try {
 87  3
                     catchClause.process(exchange);
 88  0
                 } catch (Exception e1) {
 89  0
                     LOG.warn("Caught exception inside catch clause: " + e1, e1);
 90  0
                     throw e1;
 91  3
                 }
 92  3
                 return;
 93  
             }
 94  0
         }
 95  
 
 96  
         // unhandled exception
 97  0
         throw e;
 98  
     }
 99  
 }