1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.exchangesimple.monitor;
35
36 import info.magnolia.cms.i18n.MessagesManager;
37
38 import java.text.SimpleDateFormat;
39 import java.util.Calendar;
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.Date;
43 import java.util.Map;
44 import java.util.concurrent.atomic.AtomicInteger;
45 import java.util.concurrent.atomic.AtomicLong;
46
47 import org.apache.commons.collections.Buffer;
48 import org.apache.commons.collections.BufferUtils;
49 import org.apache.commons.collections.buffer.CircularFifoBuffer;
50 import org.apache.commons.collections.map.LRUMap;
51
52
53
54
55 public class MemoryActivationStorage implements ActivationStorage {
56
57 private static final String DATE_FORMAT = MessagesManager.getMessages("info.magnolia.module.exchangesimple.messages").get("activationMonitor.dateformat");
58
59 private static SimpleDateFormat date = new SimpleDateFormat(DATE_FORMAT);
60
61 private AtomicInteger activations;
62 private AtomicInteger deactivations;
63 private AtomicInteger activationErrors;
64 private AtomicInteger commitedTransactions;
65 private AtomicInteger rollbackedTransactions;
66 private AtomicLong sizeOfActivatedContent;
67 private AtomicLong activationTime;
68 private Date lastRestart;
69 private Buffer activationLog;
70 private Buffer activationErrorLog;
71 private Map<String, Integer> activationsPerWorkspace;
72 private Map<String, Buffer> activationsMadeByUser;
73 private Map<String, ResponseTimeEntry> subscriberResponseTimes;
74
75 @SuppressWarnings("unchecked")
76 public MemoryActivationStorage() {
77 activations = new AtomicInteger();
78 deactivations = new AtomicInteger();
79 activationErrors = new AtomicInteger();
80 commitedTransactions = new AtomicInteger();
81 rollbackedTransactions = new AtomicInteger();
82 sizeOfActivatedContent = new AtomicLong();
83 activationTime = new AtomicLong();
84 lastRestart = Calendar.getInstance().getTime();
85 activationLog = BufferUtils.synchronizedBuffer(new CircularFifoBuffer(500));
86 activationErrorLog = BufferUtils.synchronizedBuffer(new CircularFifoBuffer(500));
87 activationsPerWorkspace = Collections.synchronizedMap(new LRUMap(500));
88 activationsMadeByUser = Collections.synchronizedMap(new LRUMap(500));
89 subscriberResponseTimes = Collections.synchronizedMap(new LRUMap(500));
90 }
91
92 @SuppressWarnings("unchecked")
93 @Override
94 public void logActivation(String path, String user, String workspaceName, String subscriber, boolean deactivation, boolean success) {
95 if (deactivation) {
96 deactivations.incrementAndGet();
97 } else {
98 activations.incrementAndGet();
99 }
100
101 ActivationLogEntry entry = new ActivationLogEntry(deactivation, success, path, subscriber, user, workspaceName, getCurrentDate());
102
103 this.activationLog.add(entry);
104
105 if (!this.activationsPerWorkspace.containsKey(workspaceName)) {
106 this.activationsPerWorkspace.put(workspaceName, 1);
107 } else {
108 AtomicInteger count = new AtomicInteger(this.activationsPerWorkspace.get(workspaceName));
109 this.activationsPerWorkspace.put(workspaceName, count.incrementAndGet());
110 }
111
112 if (!this.activationsMadeByUser.containsKey(user)) {
113 Buffer buffer = BufferUtils.synchronizedBuffer(new CircularFifoBuffer(100));
114 buffer.add(entry);
115 this.activationsMadeByUser.put(user, buffer);
116 } else {
117 Buffer buffer = this.activationsMadeByUser.get(user);
118 buffer.add(entry);
119 }
120 }
121
122 private static String getCurrentDate() {
123 final Date d = Calendar.getInstance().getTime();
124 return date.format(d);
125 }
126
127 @Override
128 public int getActivations() {
129 return this.activations.get();
130 }
131
132 @Override
133 public int getDeactivations() {
134 return this.deactivations.get();
135 }
136
137 @Override
138 public int getActivationErrors() {
139 return this.activationErrors.get();
140 }
141
142 @Override
143 public int getCommitedTransactions() {
144 return this.commitedTransactions.get();
145 }
146
147 @Override
148 public int getRollbackedTransactions() {
149 return this.rollbackedTransactions.get();
150 }
151
152 @Override
153 public long getSizeOfActivatedContent() {
154 return this.sizeOfActivatedContent.get();
155 }
156
157 @SuppressWarnings("unchecked")
158 @Override
159 public Collection<ActivationLogEntry> getActivationLog() {
160 return this.activationLog;
161 }
162
163 @SuppressWarnings("unchecked")
164 @Override
165 public Collection<ActivationLogEntry> getActivationsMadeByUser(String user) {
166 return this.activationsMadeByUser.get(user);
167 }
168
169 @Override
170 public Map<String, Integer> getActivationsPerWorkspace() {
171 return this.activationsPerWorkspace;
172 }
173
174 @Override
175 public void setSubscriberResponseTime(String subscriber, long time) {
176 if (!this.subscriberResponseTimes.containsKey(subscriber)) {
177 final ResponseTimeEntry newSubscriber = new ResponseTimeEntry();
178 newSubscriber.setMax(time);
179 newSubscriber.setMin(time);
180 newSubscriber.setAvg(time);
181 subscriberResponseTimes.put(subscriber, newSubscriber);
182 } else {
183 final ResponseTimeEntry current = this.subscriberResponseTimes.get(subscriber);
184 current.setAvg((current.getAvg() + time) / 2);
185 if (current.getMax() < time) {
186 current.setMax(time);
187 }
188 if (current.getMin() > time) {
189 current.setMin(time);
190 }
191 }
192 }
193
194 @SuppressWarnings("unchecked")
195 @Override
196 public void logError(String path, String user, String workspaceName, String subscriber, Throwable t, boolean deactivation) {
197 this.activationErrors.incrementAndGet();
198
199 ActivationLogEntry entry = new ActivationLogEntry(deactivation, false, path, subscriber, user, workspaceName, getCurrentDate(), t);
200 this.activationErrorLog.add(entry);
201 }
202
203 @Override
204 public Map<String, ResponseTimeEntry> getSubscriberResponseTimes() {
205 return this.subscriberResponseTimes;
206 }
207
208 @SuppressWarnings("unchecked")
209 @Override
210 public Collection<ActivationLogEntry> getActivationErrorLog() {
211 return this.activationErrorLog;
212 }
213
214 @Override
215 public void addSizeOfActivatedContent(long size) {
216 this.sizeOfActivatedContent.addAndGet(size);
217 }
218
219 @Override
220 public void addActivationTime(long time) {
221 this.activationTime.addAndGet(time);
222 }
223
224 @Override
225 public long getActivationTime() {
226 return this.activationTime.get();
227 }
228
229 @Override
230 public Date getLastRestartDate() {
231 return this.lastRestart;
232 }
233
234 @Override
235 public void addCommitedTransaction() {
236 this.commitedTransactions.incrementAndGet();
237 }
238
239 @Override
240 public void addRollbackedTransaction() {
241 this.rollbackedTransactions.incrementAndGet();
242 }
243
244 }