001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.util; 018 019import javax.servlet.ServletException; 020 021import org.springframework.core.NestedExceptionUtils; 022 023/** 024 * Subclass of {@link ServletException} that properly handles a root cause in terms 025 * of message and stacktrace, just like NestedChecked/RuntimeException does. 026 * 027 * <p>Note that the plain ServletException doesn't expose its root cause at all, 028 * neither in the exception message nor in printed stack traces! While this might 029 * be fixed in later Servlet API variants (which even differ per vendor for the 030 * same API version), it is not reliably available on Servlet 2.4 (the minimum 031 * version required by Spring 3.x), which is why we need to do it ourselves. 032 * 033 * <p>The similarity between this class and the NestedChecked/RuntimeException 034 * class is unavoidable, as this class needs to derive from ServletException. 035 * 036 * @author Juergen Hoeller 037 * @since 1.2.5 038 * @see #getMessage 039 * @see #printStackTrace 040 * @see org.springframework.core.NestedCheckedException 041 * @see org.springframework.core.NestedRuntimeException 042 */ 043public class NestedServletException extends ServletException { 044 045 /** Use serialVersionUID from Spring 1.2 for interoperability */ 046 private static final long serialVersionUID = -5292377985529381145L; 047 048 static { 049 // Eagerly load the NestedExceptionUtils class to avoid classloader deadlock 050 // issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607. 051 NestedExceptionUtils.class.getName(); 052 } 053 054 055 /** 056 * Construct a {@code NestedServletException} with the specified detail message. 057 * @param msg the detail message 058 */ 059 public NestedServletException(String msg) { 060 super(msg); 061 } 062 063 /** 064 * Construct a {@code NestedServletException} with the specified detail message 065 * and nested exception. 066 * @param msg the detail message 067 * @param cause the nested exception 068 */ 069 public NestedServletException(String msg, Throwable cause) { 070 super(msg, cause); 071 // Set JDK 1.4 exception chain cause if not done by ServletException class already 072 // (this differs between Servlet API versions). 073 if (getCause() == null && cause!=null) { 074 initCause(cause); 075 } 076 } 077 078 079 /** 080 * Return the detail message, including the message from the nested exception 081 * if there is one. 082 */ 083 @Override 084 public String getMessage() { 085 return NestedExceptionUtils.buildMessage(super.getMessage(), getCause()); 086 } 087 088}