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