| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
package net.admin4j.entity; |
| 15 | |
|
| 16 | |
import java.util.Date; |
| 17 | |
|
| 18 | |
import net.admin4j.deps.commons.lang3.Validate; |
| 19 | |
import net.admin4j.deps.commons.lang3.builder.EqualsBuilder; |
| 20 | |
import net.admin4j.deps.commons.lang3.builder.HashCodeBuilder; |
| 21 | |
import net.admin4j.exception.ExceptionTracker; |
| 22 | |
import net.admin4j.util.ExpiringCache; |
| 23 | |
|
| 24 | |
import org.slf4j.Logger; |
| 25 | |
import org.slf4j.LoggerFactory; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
public class ExceptionInfo extends ExceptionInfoBase { |
| 33 | |
|
| 34 | 15 | private static Logger logger = LoggerFactory.getLogger(ExceptionTracker.class); |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
@SuppressWarnings("rawtypes") |
| 43 | 39 | public ExceptionInfo(Class exceptionClass, StackTraceElement[] trace, String id) { |
| 44 | |
Validate.notNull(exceptionClass, "Null exceptionClass not allowed."); |
| 45 | 39 | if (trace == null) { |
| 46 | 0 | this.stackTrace = new StackTraceElement[0]; |
| 47 | |
} |
| 48 | 39 | else this.stackTrace = trace; |
| 49 | |
|
| 50 | 39 | this.setExceptionClassName(exceptionClass.getName()); |
| 51 | |
|
| 52 | 39 | if (id == null) { |
| 53 | 0 | this.alternateId = deriveId(); |
| 54 | |
} |
| 55 | 39 | } |
| 56 | |
|
| 57 | |
public ExceptionInfo(String exceptionClassName, StackTraceElement[] trace) { |
| 58 | 75 | this(exceptionClassName, trace, null); |
| 59 | 75 | } |
| 60 | |
|
| 61 | 126 | public ExceptionInfo(String exceptionClassName, StackTraceElement[] trace, String id) { |
| 62 | |
Validate.notEmpty(exceptionClassName, "Null or blank exceptionClassName not allowed."); |
| 63 | 126 | if (trace == null) { |
| 64 | 0 | this.stackTrace = new StackTraceElement[0]; |
| 65 | |
} |
| 66 | 126 | else this.stackTrace = trace; |
| 67 | |
|
| 68 | 126 | this.setExceptionClassName(exceptionClassName); |
| 69 | |
|
| 70 | 126 | if (id == null) { |
| 71 | 75 | this.alternateId = deriveId(); |
| 72 | |
} |
| 73 | 126 | } |
| 74 | |
|
| 75 | 15 | private static ExpiringCache occuranceTimeStampMap = |
| 76 | |
new ExpiringCache(60000,60000); |
| 77 | |
private String alternateId; |
| 78 | |
private String deriveId() { |
| 79 | 75 | StringBuffer buffer = new StringBuffer(1024); |
| 80 | 75 | buffer.append(this.getExceptionClassName()); |
| 81 | 75 | buffer.append("|"); |
| 82 | |
|
| 83 | 1914 | for (StackTraceElement element: this.getStackTrace()) { |
| 84 | 1839 | buffer.append(element); |
| 85 | |
} |
| 86 | |
|
| 87 | 75 | return buffer.toString(); |
| 88 | |
} |
| 89 | |
|
| 90 | |
public void postOccurance(long systemIdHash) { |
| 91 | 36 | TimeStamp time = (TimeStamp)occuranceTimeStampMap.get(systemIdHash); |
| 92 | 36 | if (time == null) { |
| 93 | 36 | this.setLastOccuranceDt(new Date()); |
| 94 | 36 | this.totalNbrExceptions.addAndGet(1); |
| 95 | 36 | occuranceTimeStampMap.put(systemIdHash, new TimeStamp(System.currentTimeMillis())); |
| 96 | |
} |
| 97 | 0 | else logger.debug("Duplicate posting detected -- sys id hash={}", systemIdHash); |
| 98 | 36 | } |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public static class TimeStamp { |
| 106 | 36 | public TimeStamp(long time) { |
| 107 | 36 | timestampInMillis = time; |
| 108 | 36 | } |
| 109 | |
public Long timestampInMillis; |
| 110 | |
public Long getTimestampInMillis() { |
| 111 | 0 | return timestampInMillis; |
| 112 | |
} |
| 113 | |
public void setTimestampInMillis(Long timestampInMillis) { |
| 114 | 0 | this.timestampInMillis = timestampInMillis; |
| 115 | 0 | } |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
@Override |
| 122 | |
public boolean equals(Object obj) { |
| 123 | 117 | if (obj == null) { |
| 124 | 3 | return false; |
| 125 | |
} |
| 126 | |
return new EqualsBuilder() |
| 127 | |
.appendSuper(super.equals(obj)) |
| 128 | |
.isEquals(); |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
@Override |
| 135 | |
public int hashCode() { |
| 136 | |
return new HashCodeBuilder(17,37) |
| 137 | |
.appendSuper(super.hashCode()) |
| 138 | |
.toHashCode(); |
| 139 | |
} |
| 140 | |
|
| 141 | |
public String getAlternateId() { |
| 142 | 72 | return alternateId; |
| 143 | |
} |
| 144 | |
|
| 145 | |
public void setAlternateId(String id) { |
| 146 | 84 | this.alternateId = id; |
| 147 | 84 | } |
| 148 | |
|
| 149 | |
} |