From f6e5c1768ee1b28d86e81f8a8476fb7d2afae35f Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 11 May 2009 22:22:11 +0000 Subject: [PATCH] eagerly load NestedExceptionUtils to avoid potential getMessage deadlock on OSGi (SPR-5607) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1149 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../core/NestedCheckedException.java | 8 ++++++- .../core/NestedIOException.java | 17 +++++++++++---- .../core/NestedRuntimeException.java | 8 ++++++- .../web/util/NestedServletException.java | 21 +++++++++++++------ 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/NestedCheckedException.java b/org.springframework.core/src/main/java/org/springframework/core/NestedCheckedException.java index 99094d7b7a3..781edfc6088 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/NestedCheckedException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/NestedCheckedException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,12 @@ public abstract class NestedCheckedException extends Exception { /** Use serialVersionUID from Spring 1.2 for interoperability */ private static final long serialVersionUID = 7100714597678207546L; + static { + // Eagerly load the NestedExceptionUtils class to avoid classloader deadlock + // issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607. + NestedExceptionUtils.class.getName(); + } + /** * Construct a NestedCheckedException with the specified detail message. diff --git a/org.springframework.core/src/main/java/org/springframework/core/NestedIOException.java b/org.springframework.core/src/main/java/org/springframework/core/NestedIOException.java index bcdd152f4aa..50412d81f80 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/NestedIOException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/NestedIOException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,12 +19,14 @@ package org.springframework.core; import java.io.IOException; /** - * Subclass of IOException that properly handles a root cause, + * Subclass of {@link IOException} that properly handles a root cause, * exposing the root cause just like NestedChecked/RuntimeException does. * + *

Proper root cause handling has not been added to standard IOException before + * Java 6, which is why we need to do it ourselves for Java 5 compatibility purposes. + * *

The similarity between this class and the NestedChecked/RuntimeException - * class is unavoidable, as this class needs to derive from IOException - * and cannot derive from NestedCheckedException. + * class is unavoidable, as this class needs to derive from IOException. * * @author Juergen Hoeller * @since 2.0 @@ -35,6 +37,13 @@ import java.io.IOException; */ public class NestedIOException extends IOException { + static { + // Eagerly load the NestedExceptionUtils class to avoid classloader deadlock + // issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607. + NestedExceptionUtils.class.getName(); + } + + /** * Construct a NestedIOException with the specified detail message. * @param msg the detail message diff --git a/org.springframework.core/src/main/java/org/springframework/core/NestedRuntimeException.java b/org.springframework.core/src/main/java/org/springframework/core/NestedRuntimeException.java index dde2b1dcca6..8c8eaeb0671 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/NestedRuntimeException.java +++ b/org.springframework.core/src/main/java/org/springframework/core/NestedRuntimeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,12 @@ public abstract class NestedRuntimeException extends RuntimeException { /** Use serialVersionUID from Spring 1.2 for interoperability */ private static final long serialVersionUID = 5439915454935047936L; + static { + // Eagerly load the NestedExceptionUtils class to avoid classloader deadlock + // issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607. + NestedExceptionUtils.class.getName(); + } + /** * Construct a NestedRuntimeException with the specified detail message. diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java b/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java index cecb399c40b..31b1eb02c03 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java +++ b/org.springframework.web/src/main/java/org/springframework/web/util/NestedServletException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,14 +21,17 @@ import javax.servlet.ServletException; import org.springframework.core.NestedExceptionUtils; /** - * Subclass of ServletException that properly handles a root cause in terms + * Subclass of {@link ServletException} that properly handles a root cause in terms * of message and stacktrace, just like NestedChecked/RuntimeException does. - * Note that the plain ServletException doesn't expose its root cause at all, - * neither in the exception message nor in printed stack traces! + * + *

Note that the plain ServletException doesn't expose its root cause at all, + * neither in the exception message nor in printed stack traces! While this might + * be fixed in later Servlet API variants (which even differ per vendor for the + * same API version), it is not reliably available on Servlet 2.4 (the minimum + * version required by Spring 3.x), which is why we need to do it ourselves. * *

The similarity between this class and the NestedChecked/RuntimeException - * class is unavoidable, as this class needs to derive from ServletException - * and cannot derive from NestedCheckedException. + * class is unavoidable, as this class needs to derive from ServletException. * * @author Juergen Hoeller * @since 1.2.5 @@ -42,6 +45,12 @@ public class NestedServletException extends ServletException { /** Use serialVersionUID from Spring 1.2 for interoperability */ private static final long serialVersionUID = -5292377985529381145L; + static { + // Eagerly load the NestedExceptionUtils class to avoid classloader deadlock + // issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607. + NestedExceptionUtils.class.getName(); + } + /** * Construct a NestedServletException with the specified detail message.