Coverage Report - org.apache.camel.model.TryType
 
Classes in this File Line Coverage Branch Coverage Complexity
TryType
57% 
73% 
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.model;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.List;
 22  
 
 23  
 import javax.xml.bind.annotation.XmlAccessType;
 24  
 import javax.xml.bind.annotation.XmlAccessorType;
 25  
 import javax.xml.bind.annotation.XmlRootElement;
 26  
 import javax.xml.bind.annotation.XmlTransient;
 27  
 
 28  
 import org.apache.camel.Endpoint;
 29  
 import org.apache.camel.Processor;
 30  
 import org.apache.camel.impl.RouteContext;
 31  
 import org.apache.camel.processor.CatchProcessor;
 32  
 import org.apache.camel.processor.TryProcessor;
 33  
 
 34  
 /**
 35  
  * @version $Revision: 1.1 $
 36  
  */
 37  
 @XmlRootElement(name = "try")
 38  
 @XmlAccessorType(XmlAccessType.FIELD)
 39  6
 public class TryType extends OutputType {
 40  
     @XmlTransient
 41  
     private List<CatchType> catchClauses;
 42  
     @XmlTransient
 43  
     private FinallyType finallyClause;
 44  
     @XmlTransient
 45  
     private boolean initialized;
 46  
     @XmlTransient
 47  
     private List<ProcessorType> outputsWithoutCatches;
 48  
 
 49  
     @Override
 50  
     public String toString() {
 51  0
         return "Try[ " + getOutputs() + "]";
 52  
     }
 53  
 
 54  
     @Override
 55  
     public Processor createProcessor(RouteContext routeContext) throws Exception {
 56  6
         Processor tryProcessor = createOutputsProcessor(routeContext, getOutputsWithoutCatches());
 57  
 
 58  6
         Processor finallyProcessor = null;
 59  6
         if (finallyClause != null) {
 60  0
             finallyProcessor = finallyClause.createProcessor(routeContext);
 61  
         }
 62  6
         List<CatchProcessor> catchProcessors = new ArrayList<CatchProcessor>();
 63  6
         if (catchClauses != null) {
 64  6
             for (CatchType catchClause : catchClauses) {
 65  6
                 catchProcessors.add(catchClause.createProcessor(routeContext));
 66  6
             }
 67  
         }
 68  6
         return new TryProcessor(tryProcessor, catchProcessors, finallyProcessor);
 69  
     }
 70  
 
 71  
     // Fluent API
 72  
     // -------------------------------------------------------------------------
 73  
     public CatchType handle(Class<?> exceptionType) {
 74  6
         CatchType answer = new CatchType(exceptionType);
 75  6
         addOutput(answer);
 76  6
         return answer;
 77  
     }
 78  
 
 79  
     public FinallyType handleAll() {
 80  0
         FinallyType answer = new FinallyType();
 81  0
         addOutput(answer);
 82  0
         return answer;
 83  
     }
 84  
 
 85  
     public TryType process(Processor processor) {
 86  6
         super.process(processor);
 87  6
         return this;
 88  
     }
 89  
 
 90  
     public TryType to(Endpoint endpoint) {
 91  0
         super.to(endpoint);
 92  0
         return this;
 93  
     }
 94  
 
 95  
     public TryType to(Collection<Endpoint> endpoints) {
 96  0
         super.to(endpoints);
 97  0
         return this;
 98  
     }
 99  
 
 100  
     public TryType to(Endpoint... endpoints) {
 101  0
         super.to(endpoints);
 102  0
         return this;
 103  
     }
 104  
 
 105  
     public TryType to(String uri) {
 106  6
         super.to(uri);
 107  6
         return this;
 108  
     }
 109  
 
 110  
     public TryType to(String... uris) {
 111  0
         super.to(uris);
 112  0
         return this;
 113  
     }
 114  
 
 115  
     // Properties
 116  
     // -------------------------------------------------------------------------
 117  
 
 118  
     public List<CatchType> getCatchClauses() {
 119  0
         if (catchClauses == null) {
 120  0
             checkInitialized();
 121  
         }
 122  0
         return catchClauses;
 123  
     }
 124  
 
 125  
     public FinallyType getFinallyClause() {
 126  0
         if (finallyClause == null) {
 127  0
             checkInitialized();
 128  
         }
 129  0
         return finallyClause;
 130  
     }
 131  
 
 132  
     public List<ProcessorType> getOutputsWithoutCatches() {
 133  6
         if (outputsWithoutCatches == null) {
 134  6
             checkInitialized();
 135  
         }
 136  6
         return outputsWithoutCatches;
 137  
     }
 138  
 
 139  
     public void setOutputs(List<ProcessorType> outputs) {
 140  0
         initialized = false;
 141  0
         super.setOutputs(outputs);
 142  0
     }
 143  
 
 144  
     public void addOutput(ProcessorType output) {
 145  18
         initialized = false;
 146  18
         getOutputs().add(output);
 147  18
     }
 148  
 
 149  
     /**
 150  
      * Checks whether or not this object has been initialized
 151  
      */
 152  
     protected void checkInitialized() {
 153  6
         if (!initialized) {
 154  6
             initialized = true;
 155  6
             outputsWithoutCatches = new ArrayList<ProcessorType>();
 156  6
             catchClauses = new ArrayList<CatchType>();
 157  6
             finallyClause = null;
 158  
 
 159  6
             for (ProcessorType output : outputs) {
 160  18
                 if (output instanceof CatchType) {
 161  6
                     catchClauses.add((CatchType)output);
 162  6
                 } else if (output instanceof FinallyType) {
 163  0
                     if (finallyClause != null) {
 164  0
                         throw new IllegalArgumentException("Multiple finally clauses added: " + finallyClause
 165  
                                                            + " and " + output);
 166  
                     } else {
 167  0
                         finallyClause = (FinallyType)output;
 168  
                     }
 169  0
                 } else {
 170  12
                     outputsWithoutCatches.add(output);
 171  
                 }
 172  18
             }
 173  
         }
 174  6
     }
 175  
 }