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.Predicate;
023import org.apache.camel.Processor;
024import org.apache.camel.Route;
025import org.apache.camel.model.CatchDefinition;
026import org.apache.camel.model.ProcessorDefinition;
027import org.apache.camel.model.TryDefinition;
028import org.apache.camel.processor.CatchProcessor;
029
030public class CatchReifier extends ProcessorReifier<CatchDefinition> {
031
032    public CatchReifier(Route route, ProcessorDefinition<?> definition) {
033        super(route, CatchDefinition.class.cast(definition));
034    }
035
036    @Override
037    public CatchProcessor createProcessor() throws Exception {
038        // create and load exceptions if not done
039        if (definition.getExceptionClasses() == null) {
040            definition.setExceptionClasses(createExceptionClasses());
041        }
042
043        // must have at least one exception
044        if (definition.getExceptionClasses().isEmpty()) {
045            throw new IllegalArgumentException("At least one Exception must be configured to catch");
046        }
047
048        // parent must be a try
049        if (!(definition.getParent() instanceof TryDefinition)) {
050            throw new IllegalArgumentException("This doCatch should have a doTry as its parent on " + definition);
051        }
052
053        // do catch does not mandate a child processor
054        Processor childProcessor = this.createChildProcessor(false);
055
056        Predicate when = null;
057        if (definition.getOnWhen() != null) {
058            when = createPredicate(definition.getOnWhen().getExpression());
059        }
060
061        return new CatchProcessor(definition.getExceptionClasses(), childProcessor, when, null);
062    }
063
064    protected List<Class<? extends Throwable>> createExceptionClasses() throws ClassNotFoundException {
065        // must use the class resolver from CamelContext to load classes to
066        // ensure it can
067        // be loaded in all kind of environments such as JEE servers and OSGi
068        // etc.
069        List<String> list = definition.getExceptions();
070        List<Class<? extends Throwable>> answer = new ArrayList<>(list.size());
071        for (String name : list) {
072            Class<Throwable> type = camelContext.getClassResolver().resolveMandatoryClass(name, Throwable.class);
073            answer.add(type);
074        }
075        return answer;
076    }
077}