1
18 package net.bull.javamelody.internal.model;
19
20 import java.io.Serializable;
21 import java.sql.Connection;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
27
28 import net.bull.javamelody.Parameter;
29
30
36 public class ConnectionInformations implements Serializable {
37 private static final long serialVersionUID = -6063966419161604125L;
38 private static final String OWN_PACKAGE = ConnectionInformations.class.getName().substring(0,
39 ConnectionInformations.class.getName().lastIndexOf('.'));
40 private static final boolean CONNECTIONS_STACK_TRACES_DISABLED = Parameter.CONNECTIONS_STACK_TRACES_DISABLED
41 .getValueAsBoolean();
42 private final long openingTime;
43 private final StackTraceElement[] openingStackTrace;
44 private final long threadId;
45
46 public ConnectionInformations() {
47 super();
48 this.openingTime = System.currentTimeMillis();
49 final Thread currentThread = Thread.currentThread();
50 if (CONNECTIONS_STACK_TRACES_DISABLED) {
51 this.openingStackTrace = null;
52 } else {
53 this.openingStackTrace = currentThread.getStackTrace();
54 }
55 this.threadId = currentThread.getId();
56 }
57
58 public static int getUniqueIdOfConnection(Connection connection) {
59
60
61 return System.identityHashCode(connection);
62 }
63
64 public Date getOpeningDate() {
65 return new Date(openingTime);
66 }
67
68 public List<StackTraceElement> getOpeningStackTrace() {
69 if (openingStackTrace == null) {
70 return Collections.emptyList();
71 }
72 final List<StackTraceElement> stackTrace = new ArrayList<>(
73 Arrays.asList(openingStackTrace));
74
75
76
77 stackTrace.remove(0);
78 while (stackTrace.get(0).getClassName().startsWith(OWN_PACKAGE)) {
79 stackTrace.remove(0);
80 }
81 return stackTrace;
82 }
83
84 public long getThreadId() {
85 return threadId;
86 }
87
88
89 @Override
90 public String toString() {
91 return getClass().getSimpleName() + "[openingDate=" + getOpeningDate() + ", threadId="
92 + getThreadId() + ']';
93 }
94 }
95