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.model; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.apache.camel.Processor; 026import org.apache.camel.processor.ThrowExceptionProcessor; 027import org.apache.camel.spi.Metadata; 028import org.apache.camel.spi.RouteContext; 029import org.apache.camel.util.ObjectHelper; 030 031/** 032 * Throws an exception 033 */ 034@Metadata(label = "error") 035@XmlRootElement(name = "throwException") 036@XmlAccessorType(XmlAccessType.FIELD) 037public class ThrowExceptionDefinition extends NoOutputDefinition<ThrowExceptionDefinition> { 038 @XmlAttribute 039 private String ref; 040 @XmlAttribute 041 private String message; 042 @XmlTransient 043 private Exception exception; 044 @XmlAttribute 045 private String exceptionType; 046 @XmlTransient 047 private Class<? extends Exception> exceptionClass; 048 049 public ThrowExceptionDefinition() { 050 } 051 052 @Override 053 public String toString() { 054 return "ThrowException[" + description() + "]"; 055 } 056 057 protected String description() { 058 return exception != null ? exception.getClass().getCanonicalName() : "ref:" + ref; 059 } 060 061 @Override 062 public String getShortName() { 063 return "throwException"; 064 } 065 066 @Override 067 public String getLabel() { 068 return "throwException[" + description() + "]"; 069 } 070 071 @Override 072 public Processor createProcessor(RouteContext routeContext) { 073 if (ref != null && exception == null) { 074 this.exception = routeContext.getCamelContext().getRegistry().lookupByNameAndType(ref, Exception.class); 075 } 076 077 if (exceptionType != null && exceptionClass == null) { 078 try { 079 this.exceptionClass = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(exceptionType, Exception.class); 080 } catch (ClassNotFoundException e) { 081 throw ObjectHelper.wrapRuntimeCamelException(e); 082 } 083 } 084 085 if (exception == null && exceptionClass == null) { 086 throw new IllegalArgumentException("exception or exceptionClass/exceptionType must be configured on: " + this); 087 } 088 return new ThrowExceptionProcessor(exception, exceptionClass, message); 089 } 090 091 public String getRef() { 092 return ref; 093 } 094 095 /** 096 * Reference to the exception instance to lookup from the registry to throw 097 */ 098 public void setRef(String ref) { 099 this.ref = ref; 100 } 101 102 public Exception getException() { 103 return exception; 104 } 105 106 public void setException(Exception exception) { 107 this.exception = exception; 108 } 109 110 public String getMessage() { 111 return message; 112 } 113 114 /** 115 * To create a new exception instance and use the given message as caused message (supports simple language) 116 */ 117 public void setMessage(String message) { 118 this.message = message; 119 } 120 121 public String getExceptionType() { 122 return exceptionType; 123 } 124 125 /** 126 * The class of the exception to create using the message. 127 * 128 * @see #setMessage(String) 129 */ 130 public void setExceptionType(String exceptionType) { 131 this.exceptionType = exceptionType; 132 } 133 134 public Class<? extends Exception> getExceptionClass() { 135 return exceptionClass; 136 } 137 138 /** 139 * The class of the exception to create using the message. 140 * 141 * @see #setMessage(String) 142 */ 143 public void setExceptionClass(Class<? extends Exception> exceptionClass) { 144 this.exceptionClass = exceptionClass; 145 } 146}