Allow contexted exception to store non-serializable objects (more flexible, and doesn't prevent serialization)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@829404 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2009-10-24 16:38:08 +00:00
parent 926ed9ea0e
commit 0a72dbad5a
5 changed files with 19 additions and 58 deletions

View File

@ -16,7 +16,6 @@
*/ */
package org.apache.commons.lang.exception; package org.apache.commons.lang.exception;
import java.io.Serializable;
import java.util.Set; import java.util.Set;
/** /**
@ -141,15 +140,13 @@ public class ContextedException extends Exception implements ExceptionContext {
* the problem. For the information to be meaningful, the value passed * the problem. For the information to be meaningful, the value passed
* should have a reasonable toString() implementation. * should have a reasonable toString() implementation.
* <p> * <p>
* Note: If the value provided isn't Serializable, one solution would be * Note: This exception is only serializable if the object added is serializable.
* to provide its toString() if it has a meaningful implementation or
* individual properties of the value object instead.
* *
* @param label a textual label associated with information, null not recommended * @param label a textual label associated with information, null not recommended
* @param value information needed to understand exception, may be null * @param value information needed to understand exception, may be null
* @return this, for method chaining * @return this, for method chaining
*/ */
public ContextedException addLabeledValue(String label, Serializable value) { public ContextedException addLabeledValue(String label, Object value) {
exceptionContext.addLabeledValue(label, value); exceptionContext.addLabeledValue(label, value);
return this; return this;
} }
@ -160,7 +157,7 @@ public class ContextedException extends Exception implements ExceptionContext {
* @param label the label to get the contextual value for, may be null * @param label the label to get the contextual value for, may be null
* @return the contextual value associated with the label, may be null * @return the contextual value associated with the label, may be null
*/ */
public Serializable getLabeledValue(String label) { public Object getLabeledValue(String label) {
return exceptionContext.getLabeledValue(label); return exceptionContext.getLabeledValue(label);
} }

View File

@ -16,7 +16,6 @@
*/ */
package org.apache.commons.lang.exception; package org.apache.commons.lang.exception;
import java.io.Serializable;
import java.util.Set; import java.util.Set;
/** /**
@ -142,15 +141,13 @@ public class ContextedRuntimeException extends Exception implements ExceptionCon
* the problem. For the information to be meaningful, the value passed * the problem. For the information to be meaningful, the value passed
* should have a reasonable toString() implementation. * should have a reasonable toString() implementation.
* <p> * <p>
* Note: If the value provided isn't Serializable, one solution would be * Note: This exception is only serializable if the object added is serializable.
* to provide its toString() if it has a meaningful implementation or
* individual properties of the value object instead.
* *
* @param label a textual label associated with information, null not recommended * @param label a textual label associated with information, null not recommended
* @param value information needed to understand exception, may be null * @param value information needed to understand exception, may be null
* @return this, for method chaining * @return this, for method chaining
*/ */
public ContextedRuntimeException addLabeledValue(String label, Serializable value) { public ContextedRuntimeException addLabeledValue(String label, Object value) {
exceptionContext.addLabeledValue(label, value); exceptionContext.addLabeledValue(label, value);
return this; return this;
} }
@ -161,7 +158,7 @@ public class ContextedRuntimeException extends Exception implements ExceptionCon
* @param label the label to get the contextual value for, may be null * @param label the label to get the contextual value for, may be null
* @return the contextual value associated with the label, may be null * @return the contextual value associated with the label, may be null
*/ */
public Serializable getLabeledValue(String label) { public Object getLabeledValue(String label) {
return exceptionContext.getLabeledValue(label); return exceptionContext.getLabeledValue(label);
} }

View File

@ -25,16 +25,19 @@ import org.apache.commons.lang.SystemUtils;
/** /**
* Default implementation of the context storing the label-value pairs for contexted exceptions. * Default implementation of the context storing the label-value pairs for contexted exceptions.
* <p>
* This implementation is serializable, however this is dependent on the values that
* are added also being serializable.
* *
* @author D. Ashmore * @author D. Ashmore
* @since 3.0 * @since 3.0
*/ */
class DefaultExceptionContext implements ExceptionContext { class DefaultExceptionContext implements ExceptionContext, Serializable {
/** The serialization version. */ /** The serialization version. */
private static final long serialVersionUID = 293747957535772807L; private static final long serialVersionUID = 293747957535772807L;
/** The ordered map storing the label-data pairs. */ /** The ordered map storing the label-data pairs. */
private Map<String, Serializable> contextValueMap = new LinkedHashMap<String, Serializable>(); private Map<String, Object> contextValueMap = new LinkedHashMap<String, Object>();
/** /**
* Adds a contextual label-value pair into this context. * Adds a contextual label-value pair into this context.
@ -45,7 +48,7 @@ class DefaultExceptionContext implements ExceptionContext {
* @param value the value of item to add, may be null * @param value the value of item to add, may be null
* @return this, for method chaining * @return this, for method chaining
*/ */
public ExceptionContext addLabeledValue(String label, Serializable value) { public ExceptionContext addLabeledValue(String label, Object value) {
contextValueMap.put(label, value); contextValueMap.put(label, value);
return this; return this;
} }
@ -56,7 +59,7 @@ class DefaultExceptionContext implements ExceptionContext {
* @param label the label to get the contextual value for, may be null * @param label the label to get the contextual value for, may be null
* @return the contextual value associated with the label, may be null * @return the contextual value associated with the label, may be null
*/ */
public Serializable getLabeledValue(String label) { public Object getLabeledValue(String label) {
return contextValueMap.get(label); return contextValueMap.get(label);
} }

View File

@ -16,21 +16,18 @@
*/ */
package org.apache.commons.lang.exception; package org.apache.commons.lang.exception;
import java.io.Serializable;
import java.util.Set; import java.util.Set;
/** /**
* Provides context information for exceptions. It is available as separate interface to allow * Allows the storage and retrieval of contextual information based on label-value
* it usage independently from the {@link ContextedException} and * pairs for exceptions.
* {@link ContextedRuntimeException}.
* *
* @see ContextedException * @see ContextedException
* @see ContextedRuntimeException * @see ContextedRuntimeException
* @author D. Ashmore * @author D. Ashmore
* @since 3.0 * @since 3.0
*/ */
public interface ExceptionContext extends Serializable { public interface ExceptionContext {
/** /**
* Adds a contextual label-value pair into this context. * Adds a contextual label-value pair into this context.
@ -41,7 +38,7 @@ public interface ExceptionContext extends Serializable {
* @param value the value of item to add, may be null * @param value the value of item to add, may be null
* @return context itself to allow method chaining * @return context itself to allow method chaining
*/ */
public ExceptionContext addLabeledValue(String label, Serializable value); public ExceptionContext addLabeledValue(String label, Object value);
/** /**
* Retrieves a contextual data value associated with the label. * Retrieves a contextual data value associated with the label.
@ -49,7 +46,7 @@ public interface ExceptionContext extends Serializable {
* @param label the label to get the contextual value for, may be null * @param label the label to get the contextual value for, may be null
* @return the contextual value associated with the label, may be null * @return the contextual value associated with the label, may be null
*/ */
public Serializable getLabeledValue(String label); public Object getLabeledValue(String label);
/** /**
* Retrieves the labels defined in the contextual data. * Retrieves the labels defined in the contextual data.

View File

@ -18,7 +18,6 @@ package org.apache.commons.lang.exception;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter; import java.io.StringWriter;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -26,14 +25,9 @@ import java.lang.reflect.Method;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.StringTokenizer; import java.util.StringTokenizer;
//import net.jcip.annotations.GuardedBy;
//import net.jcip.annotations.ThreadSafe;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ClassUtils; import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -88,37 +82,10 @@ public class ExceptionUtils {
* <p>The Method object for Java 1.4 getCause.</p> * <p>The Method object for Java 1.4 getCause.</p>
*/ */
private static final Method THROWABLE_CAUSE_METHOD; private static final Method THROWABLE_CAUSE_METHOD;
/** /**
* <p>The Method object for Java 1.4 initCause.</p> * <p>The Method object for Java 1.4 initCause.</p>
*/ */
private static final Method THROWABLE_INITCAUSE_METHOD; private static final Method THROWABLE_INITCAUSE_METHOD;
/**
* An empty {@link ExceptionContext}.
* @since 3.0
*/
public static final ExceptionContext EMPTY_CONTEXT = new ExceptionContext() {
private static final long serialVersionUID = 1L;
public ExceptionContext addLabeledValue(String label, Serializable value) {
throw new UnsupportedOperationException();
}
public Serializable getLabeledValue(String label) {
return null;
}
public Set<String> getLabelSet() {
return Collections.<String>emptySet();
}
public String getFormattedExceptionMessage(String baseMessage) {
return baseMessage;
}
};
static { static {
Method causeMethod; Method causeMethod;
try { try {