| 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.List; |
| 22 |
import java.util.Objects; |
| 23 |
import java.util.Set; |
| 24 |
import java.util.stream.Collectors; |
| 25 |
import java.util.stream.Stream; |
| 26 |
|
| 27 |
import org.apache.commons.lang3.StringUtils; |
| 28 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
| 29 |
import org.apache.commons.lang3.tuple.Pair; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class DefaultExceptionContext implements ExceptionContext, Serializable { |
| 43 |
|
| 44 |
|
| 45 |
private static final long serialVersionUID = 20110706L; |
| 46 |
|
| 47 |
|
| 48 |
private final List<Pair<String, Object>> contextValues = new ArrayList<>(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public DefaultExceptionContext() { |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@Override |
| 61 |
public DefaultExceptionContext addContextValue(final String label, final Object value) { |
| 62 |
contextValues.add(new ImmutablePair<>(label, value)); |
| 63 |
return this; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
@Override |
| 70 |
public List<Pair<String, Object>> getContextEntries() { |
| 71 |
return contextValues; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@Override |
| 78 |
public Set<String> getContextLabels() { |
| 79 |
return stream().map(Pair::getKey).collect(Collectors.toSet()); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
@Override |
| 86 |
public List<Object> getContextValues(final String label) { |
| 87 |
return stream().filter(pair -> StringUtils.equals(label, pair.getKey())).map(Pair::getValue).collect(Collectors.toList()); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@Override |
| 94 |
public Object getFirstContextValue(final String label) { |
| 95 |
return stream().filter(pair -> StringUtils.equals(label, pair.getKey())).findFirst().map(Pair::getValue).orElse(null); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public String getFormattedExceptionMessage(final String baseMessage) { |
| 106 |
final StringBuilder buffer = new StringBuilder(256); |
| 107 |
if (baseMessage != null) { |
| 108 |
buffer.append(baseMessage); |
| 109 |
} |
| 110 |
if (!contextValues.isEmpty()) { |
| 111 |
if (buffer.length() > 0) { |
| 112 |
buffer.append('\n'); |
| 113 |
} |
| 114 |
buffer.append("Exception Context:\n"); |
| 115 |
int i = 0; |
| 116 |
for (final Pair<String, Object> pair : contextValues) { |
| 117 |
buffer.append("\t["); |
| 118 |
buffer.append(++i); |
| 119 |
buffer.append(':'); |
| 120 |
buffer.append(pair.getKey()); |
| 121 |
buffer.append("="); |
| 122 |
final Object value = pair.getValue(); |
| 123 |
try { |
| 124 |
buffer.append(Objects.toString(value)); |
| 125 |
} catch (final Exception e) { |
| 126 |
buffer.append("Exception thrown on toString(): "); |
| 127 |
buffer.append(ExceptionUtils.getStackTrace(e)); |
| 128 |
} |
| 129 |
buffer.append("]\n"); |
| 130 |
} |
| 131 |
buffer.append("---------------------------------"); |
| 132 |
} |
| 133 |
return buffer.toString(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@Override |
| 140 |
public DefaultExceptionContext setContextValue(final String label, final Object value) { |
| 141 |
contextValues.removeIf(p -> StringUtils.equals(label, p.getKey())); |
| 142 |
addContextValue(label, value); |
| 143 |
return this; |
| 144 |
} |
| 145 |
|
| 146 |
private Stream<Pair<String, Object>> stream() { |
| 147 |
return contextValues.stream(); |
| 148 |
} |
| 149 |
|
| 150 |
} |