| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
package net.admin4j.entity; |
| 15 | |
|
| 16 | |
import java.util.ArrayList; |
| 17 | |
import java.util.Collections; |
| 18 | |
import java.util.HashMap; |
| 19 | |
import java.util.List; |
| 20 | |
import java.util.Map; |
| 21 | |
import java.util.Map.Entry; |
| 22 | |
|
| 23 | |
import net.admin4j.deps.commons.beanutils.BeanComparator; |
| 24 | |
import net.admin4j.deps.commons.collections.comparators.ReverseComparator; |
| 25 | |
import net.admin4j.deps.commons.lang3.Validate; |
| 26 | |
import net.admin4j.deps.commons.lang3.builder.ToStringBuilder; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public class ExecutionPoint implements Cloneable { |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
private StackTraceElement stackTraceElement; |
| 40 | 396 | private Map<StackTraceElement, Long> calledStackTraceElementMap = new HashMap<StackTraceElement, Long>(); |
| 41 | 396 | private Map<StackTraceElement, Long> callingStackTraceElementMap = new HashMap<StackTraceElement, Long>(); |
| 42 | 396 | private Map<String, Long> blockingSynchronizedClassMap = new HashMap<String, Long>(); |
| 43 | 396 | private Object mapLock = new Object(); |
| 44 | |
|
| 45 | 396 | private long nbrExecutions = 0; |
| 46 | 396 | private long nbrBlockedExecutions = 0; |
| 47 | 396 | private long createTimestampInMillis = System.currentTimeMillis(); |
| 48 | |
|
| 49 | 396 | public ExecutionPoint(StackTraceElement stackTraceElement) { |
| 50 | |
Validate.notNull(stackTraceElement, "Null stackTraceElement not allowed."); |
| 51 | 396 | this.stackTraceElement = stackTraceElement; |
| 52 | 396 | } |
| 53 | |
|
| 54 | |
public long getNbrExecutions() { |
| 55 | 183 | return nbrExecutions; |
| 56 | |
} |
| 57 | |
|
| 58 | |
public void setNbrExecutions(long nbrExecutions) { |
| 59 | 165 | this.nbrExecutions = nbrExecutions; |
| 60 | 165 | } |
| 61 | |
|
| 62 | |
public StackTraceElement getStackTraceElement() { |
| 63 | 12 | return stackTraceElement; |
| 64 | |
} |
| 65 | |
|
| 66 | |
@SuppressWarnings("unchecked") |
| 67 | |
public List<Entry<StackTraceElement, Long>> getCalledStackTraceElementList() { |
| 68 | 6 | List<Entry<StackTraceElement, Long>> list = new ArrayList<Entry<StackTraceElement, Long>>(); |
| 69 | 6 | synchronized(mapLock) { |
| 70 | 6 | list.addAll(calledStackTraceElementMap.entrySet()); |
| 71 | 6 | } |
| 72 | |
Collections.sort(list, new ReverseComparator(new BeanComparator("value"))); |
| 73 | 6 | return list; |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
@Override |
| 80 | |
public int hashCode() { |
| 81 | 234 | return this.stackTraceElement.hashCode(); |
| 82 | |
} |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
@Override |
| 88 | |
public boolean equals(Object obj) { |
| 89 | 0 | if (obj == null) return false; |
| 90 | 0 | if ( !(obj instanceof ExecutionPoint) ) { |
| 91 | 0 | return false; |
| 92 | |
} |
| 93 | 0 | ExecutionPoint ep = (ExecutionPoint)obj; |
| 94 | 0 | return ep.stackTraceElement.equals(this.stackTraceElement); |
| 95 | |
} |
| 96 | |
|
| 97 | |
public long getCreateTimestampInMillis() { |
| 98 | 0 | return createTimestampInMillis; |
| 99 | |
} |
| 100 | |
|
| 101 | |
public void setCreateTimestampInMillis(long createTimestampInMillis) { |
| 102 | 0 | this.createTimestampInMillis = createTimestampInMillis; |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
@Override |
| 109 | |
public Object clone() throws CloneNotSupportedException { |
| 110 | 234 | ExecutionPoint newPoint = new ExecutionPoint(this.stackTraceElement); |
| 111 | 234 | newPoint.nbrExecutions = this.nbrExecutions; |
| 112 | 234 | newPoint.nbrBlockedExecutions = this.nbrBlockedExecutions; |
| 113 | 234 | newPoint.createTimestampInMillis = this.createTimestampInMillis; |
| 114 | 234 | synchronized (mapLock) { |
| 115 | 234 | newPoint.calledStackTraceElementMap.putAll(this.calledStackTraceElementMap); |
| 116 | 234 | newPoint.callingStackTraceElementMap.putAll(this.callingStackTraceElementMap); |
| 117 | 234 | newPoint.blockingSynchronizedClassMap.putAll(this.blockingSynchronizedClassMap); |
| 118 | 234 | } |
| 119 | 234 | return newPoint; |
| 120 | |
} |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
@Override |
| 126 | |
public String toString() { |
| 127 | 78 | synchronized (mapLock) { |
| 128 | |
return new ToStringBuilder(this). |
| 129 | |
append("stackTraceElement", stackTraceElement). |
| 130 | |
append("nbrExecutions", nbrExecutions). |
| 131 | |
append("createTimestampInMillis", createTimestampInMillis). |
| 132 | |
append("calledStackTraceElementMap", calledStackTraceElementMap). |
| 133 | |
append("callingStackTraceElementMap", callingStackTraceElementMap). |
| 134 | |
append("blockingSynchronizedClassMap", blockingSynchronizedClassMap). |
| 135 | |
toString(); |
| 136 | 0 | } |
| 137 | |
} |
| 138 | |
|
| 139 | |
@SuppressWarnings("unchecked") |
| 140 | |
public List<Entry<StackTraceElement, Long>> getCallingStackTraceElementList() { |
| 141 | 84 | List<Entry<StackTraceElement, Long>> list = new ArrayList<Entry<StackTraceElement, Long>>(); |
| 142 | 84 | synchronized(mapLock) { |
| 143 | 84 | list.addAll(callingStackTraceElementMap.entrySet()); |
| 144 | 84 | } |
| 145 | |
Collections.sort(list, new ReverseComparator(new BeanComparator("value"))); |
| 146 | 84 | return list; |
| 147 | |
} |
| 148 | |
|
| 149 | |
@SuppressWarnings("unchecked") |
| 150 | |
public List<Entry<String, Long>> getBlockingSynchronizedClassList() { |
| 151 | 87 | List<Entry<String, Long>> list = new ArrayList<Entry<String, Long>>(); |
| 152 | 87 | synchronized(mapLock) { |
| 153 | 87 | list.addAll(blockingSynchronizedClassMap.entrySet()); |
| 154 | 87 | } |
| 155 | |
Collections.sort(list, new ReverseComparator(new BeanComparator("value"))); |
| 156 | 87 | return list; |
| 157 | |
} |
| 158 | |
|
| 159 | |
public long getNbrBlockedExecutions() { |
| 160 | 6 | return nbrBlockedExecutions; |
| 161 | |
} |
| 162 | |
|
| 163 | |
public void setNbrBlockedExecutions(long nbrBlockedExecutions) { |
| 164 | 0 | this.nbrBlockedExecutions = nbrBlockedExecutions; |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
public void addCallingStackTraceElement(StackTraceElement element) { |
| 168 | |
Validate.notNull(element, "Null element not allowed."); |
| 169 | |
|
| 170 | 162 | synchronized(mapLock) { |
| 171 | 162 | Long nbrExecutions = callingStackTraceElementMap.get(element); |
| 172 | 162 | if (nbrExecutions == null) { |
| 173 | 162 | callingStackTraceElementMap.put(element, Long.valueOf(1)); |
| 174 | |
} |
| 175 | 0 | else callingStackTraceElementMap.put(element, Long.valueOf(nbrExecutions + 1)); |
| 176 | 162 | } |
| 177 | 162 | } |
| 178 | |
|
| 179 | |
public void addCalledStackTraceElement(StackTraceElement element) { |
| 180 | |
Validate.notNull(element, "Null element not allowed."); |
| 181 | |
|
| 182 | 168 | synchronized(mapLock) { |
| 183 | 168 | Long nbrExecutions = calledStackTraceElementMap.get(element); |
| 184 | 168 | if (nbrExecutions == null) { |
| 185 | 168 | calledStackTraceElementMap.put(element, Long.valueOf(1)); |
| 186 | |
} |
| 187 | 0 | else calledStackTraceElementMap.put(element, Long.valueOf(nbrExecutions + 1)); |
| 188 | 168 | } |
| 189 | 168 | } |
| 190 | |
|
| 191 | |
public void addBlockingSynchronizedClassName(String className) { |
| 192 | |
Validate.notEmpty(className, "Null or blank className not allowed."); |
| 193 | |
|
| 194 | 6 | String localClassName = className; |
| 195 | 6 | int atOffset = localClassName.indexOf("@"); |
| 196 | 6 | if (atOffset > 0) { |
| 197 | 0 | localClassName = localClassName.substring(0, atOffset); |
| 198 | |
} |
| 199 | |
|
| 200 | 6 | synchronized(mapLock) { |
| 201 | 6 | Long nbrBlocks = blockingSynchronizedClassMap.get(localClassName); |
| 202 | 6 | if (nbrBlocks == null) { |
| 203 | 6 | blockingSynchronizedClassMap.put(localClassName, Long.valueOf(1)); |
| 204 | |
} |
| 205 | 0 | else blockingSynchronizedClassMap.put(localClassName, Long.valueOf(nbrBlocks + 1)); |
| 206 | 6 | } |
| 207 | 6 | } |
| 208 | |
|
| 209 | |
} |