1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.util.socketstatistics;
18
19 import java.io.BufferedWriter;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.Set;
25 import java.util.SortedSet;
26 import java.util.TreeSet;
27
28
29
30
31
32
33
34
35 public class SocketStatistics implements SocketStatisticsMXBean {
36
37
38
39
40 private static int s_keepStats = 600;
41
42
43
44
45 private static long s_socketIdCounter = 0;
46
47
48
49
50 private static long s_nrofclosedsockets = 0;
51
52
53
54
55
56 private static SortedSet<ConnectionStatistics> s_statsbydate = new TreeSet<ConnectionStatistics>();
57
58
59
60
61
62
63 public static synchronized ConnectionStatistics addNewConStats() {
64
65 synchronized (s_statsbydate) {
66 long i = getNextID();
67 ConnectionStatistics cs = new ConnectionStatistics(i);
68 s_statsbydate.add(cs);
69 cleanupStats();
70 return cs;
71 }
72 }
73
74
75
76
77
78
79 private static synchronized long getNextID() {
80 return ++s_socketIdCounter;
81 }
82
83
84 public long getClosedSocketsCount() {
85 return s_nrofclosedsockets;
86 }
87
88
89 public long getOpenSocketsCount() {
90 return s_socketIdCounter - s_nrofclosedsockets;
91 }
92
93
94 public Set<ConnectionStatistics> getConnectionStatistics() {
95 return new TreeSet<ConnectionStatistics>(s_statsbydate);
96 }
97
98
99 public int getKeepStats() {
100 return s_keepStats;
101 }
102
103
104 public void setKeepStats(int ks) {
105 setKeepStatsS(ks);
106 }
107
108
109
110
111
112
113
114 private static void setKeepStatsS(int ks) {
115 s_keepStats = ks;
116 }
117
118
119 public void exportStatisticsCSV(String filepath) {
120 BufferedWriter out = null;
121 try {
122 out = new BufferedWriter(new FileWriter(filepath));
123 out.write("Socket ID;Creation Date;Destruction Date;Remote Adress;Remote Port;Local Port;Bytes received;Bytes sent\n");
124 synchronized (s_statsbydate) {
125 for (ConnectionStatistics cs : s_statsbydate) {
126 out.write(cs.getStatisticsCSV());
127 }
128 }
129 } catch (IOException ex) {
130 ex.printStackTrace();
131 } finally {
132 try {
133 if (out != null) {
134 out.flush();
135 out.close();
136 }
137 } catch (IOException ex) {
138 ex.printStackTrace();
139 }
140 }
141 }
142
143
144 public void deleteStatistics() {
145 synchronized (s_statsbydate) {
146 s_statsbydate.clear();
147 }
148 }
149
150
151
152
153
154
155
156 public static synchronized void setConnectionDestroyed(ConnectionStatistics cs) {
157
158 synchronized (s_statsbydate) {
159
160 s_statsbydate.remove(cs);
161
162 cs.setDestroyedDateInt();
163 s_nrofclosedsockets++;
164
165 s_statsbydate.add(cs);
166 }
167 }
168
169
170
171
172 public void showSocketsStats() {
173 cleanupStats();
174
175 synchronized (s_statsbydate) {
176 if (s_statsbydate.isEmpty()) {
177 System.out.println("No statistics available");
178 } else {
179 for (ConnectionStatistics cs : s_statsbydate) {
180 System.out.println(cs.getStatistics());
181 }
182 }
183 }
184 }
185
186
187
188
189 private static synchronized void cleanupStats() {
190
191
192 synchronized (s_statsbydate) {
193 ConnectionStatistics cs;
194 Date dd;
195 long timecheck = new Date().getTime() - (s_keepStats * 1000);
196 Iterator<ConnectionStatistics> i = s_statsbydate.iterator();
197
198
199 while (i.hasNext()) {
200 cs = i.next();
201 dd = cs.getDestroyedDateInt();
202 if (dd != null) {
203 if (dd.getTime() < timecheck) {
204 i.remove();
205 } else {
206 break;
207 }
208 } else {
209 break;
210 }
211 }
212 }
213
214 }
215
216 }