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 */
017package org.apache.camel.reifier;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.camel.Processor;
023import org.apache.camel.model.CatchDefinition;
024import org.apache.camel.model.FinallyDefinition;
025import org.apache.camel.model.ProcessorDefinition;
026import org.apache.camel.model.TryDefinition;
027import org.apache.camel.processor.TryProcessor;
028import org.apache.camel.spi.RouteContext;
029
030public class TryReifier extends ProcessorReifier<TryDefinition> {
031
032    public TryReifier(ProcessorDefinition<?> definition) {
033        super((TryDefinition)definition);
034    }
035
036    @Override
037    public Processor createProcessor(RouteContext routeContext) throws Exception {
038        Processor tryProcessor = createOutputsProcessor(routeContext, definition.getOutputsWithoutCatches());
039        if (tryProcessor == null) {
040            throw new IllegalArgumentException("Definition has no children on " + this);
041        }
042
043        List<Processor> catchProcessors = new ArrayList<>();
044        if (definition.getCatchClauses() != null) {
045            for (CatchDefinition catchClause : definition.getCatchClauses()) {
046                catchProcessors.add(createProcessor(routeContext, catchClause));
047            }
048        }
049
050        FinallyDefinition finallyDefinition = definition.getFinallyClause();
051        if (finallyDefinition == null) {
052            finallyDefinition = new FinallyDefinition();
053            finallyDefinition.setParent(definition);
054        }
055        Processor finallyProcessor = createProcessor(routeContext, finallyDefinition);
056
057        // must have either a catch or finally
058        if (definition.getFinallyClause() == null && definition.getCatchClauses() == null) {
059            throw new IllegalArgumentException("doTry must have one or more catch or finally blocks on " + this);
060        }
061
062        return new TryProcessor(tryProcessor, catchProcessors, finallyProcessor);
063    }
064
065}