| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.lang3.exception; |
| 18 | |
|
| 19 | |
import java.io.Serializable; |
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.HashSet; |
| 22 | |
import java.util.Iterator; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.Set; |
| 25 | |
|
| 26 | |
import org.apache.commons.lang3.StringUtils; |
| 27 | |
import org.apache.commons.lang3.tuple.ImmutablePair; |
| 28 | |
import org.apache.commons.lang3.tuple.Pair; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | 279 | public class DefaultExceptionContext implements ExceptionContext, Serializable { |
| 42 | |
|
| 43 | |
|
| 44 | |
private static final long serialVersionUID = 20110706L; |
| 45 | |
|
| 46 | |
|
| 47 | 53 | private final List<Pair<String, Object>> contextValues = new ArrayList<Pair<String,Object>>(); |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
@Override |
| 53 | |
public DefaultExceptionContext addContextValue(final String label, final Object value) { |
| 54 | 226 | contextValues.add(new ImmutablePair<String, Object>(label, value)); |
| 55 | 226 | return this; |
| 56 | |
} |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
@Override |
| 62 | |
public DefaultExceptionContext setContextValue(final String label, final Object value) { |
| 63 | 18 | for (final Iterator<Pair<String, Object>> iter = contextValues.iterator(); iter.hasNext();) { |
| 64 | 111 | final Pair<String, Object> p = iter.next(); |
| 65 | 111 | if (StringUtils.equals(label, p.getKey())) { |
| 66 | 24 | iter.remove(); |
| 67 | |
} |
| 68 | 111 | } |
| 69 | 18 | addContextValue(label, value); |
| 70 | 18 | return this; |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
@Override |
| 77 | |
public List<Object> getContextValues(final String label) { |
| 78 | 6 | final List<Object> values = new ArrayList<Object>(); |
| 79 | 6 | for (final Pair<String, Object> pair : contextValues) { |
| 80 | 36 | if (StringUtils.equals(label, pair.getKey())) { |
| 81 | 9 | values.add(pair.getValue()); |
| 82 | |
} |
| 83 | 36 | } |
| 84 | 6 | return values; |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public Object getFirstContextValue(final String label) { |
| 92 | 27 | for (final Pair<String, Object> pair : contextValues) { |
| 93 | 102 | if (StringUtils.equals(label, pair.getKey())) { |
| 94 | 21 | return pair.getValue(); |
| 95 | |
} |
| 96 | 81 | } |
| 97 | 6 | return null; |
| 98 | |
} |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
@Override |
| 104 | |
public Set<String> getContextLabels() { |
| 105 | 39 | final Set<String> labels = new HashSet<String>(); |
| 106 | 39 | for (final Pair<String, Object> pair : contextValues) { |
| 107 | 231 | labels.add(pair.getKey()); |
| 108 | 231 | } |
| 109 | 39 | return labels; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
@Override |
| 116 | |
public List<Pair<String, Object>> getContextEntries() { |
| 117 | 21 | return contextValues; |
| 118 | |
} |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
@Override |
| 127 | |
public String getFormattedExceptionMessage(final String baseMessage){ |
| 128 | 41 | final StringBuilder buffer = new StringBuilder(256); |
| 129 | 41 | if (baseMessage != null) { |
| 130 | 24 | buffer.append(baseMessage); |
| 131 | |
} |
| 132 | |
|
| 133 | 41 | if (contextValues.size() > 0) { |
| 134 | 20 | if (buffer.length() > 0) { |
| 135 | 8 | buffer.append('\n'); |
| 136 | |
} |
| 137 | 20 | buffer.append("Exception Context:\n"); |
| 138 | |
|
| 139 | 20 | int i = 0; |
| 140 | 20 | for (final Pair<String, Object> pair : contextValues) { |
| 141 | 112 | buffer.append("\t["); |
| 142 | 112 | buffer.append(++i); |
| 143 | 112 | buffer.append(':'); |
| 144 | 112 | buffer.append(pair.getKey()); |
| 145 | 112 | buffer.append("="); |
| 146 | 112 | final Object value = pair.getValue(); |
| 147 | 112 | if (value == null) { |
| 148 | 20 | buffer.append("null"); |
| 149 | |
} else { |
| 150 | |
String valueStr; |
| 151 | |
try { |
| 152 | 92 | valueStr = value.toString(); |
| 153 | 11 | } catch (final Exception e) { |
| 154 | 11 | valueStr = "Exception thrown on toString(): " + ExceptionUtils.getStackTrace(e); |
| 155 | 81 | } |
| 156 | 92 | buffer.append(valueStr); |
| 157 | |
} |
| 158 | 112 | buffer.append("]\n"); |
| 159 | 112 | } |
| 160 | 20 | buffer.append("---------------------------------"); |
| 161 | |
} |
| 162 | 41 | return buffer.toString(); |
| 163 | |
} |
| 164 | |
|
| 165 | |
} |