-
Notifications
You must be signed in to change notification settings - Fork 51
/
CacheAction.java
241 lines (211 loc) · 7.15 KB
/
CacheAction.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright 2016, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.action;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.infinispan.Cache;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.stats.Stats;
import org.zanata.i18n.Messages;
import org.zanata.security.annotations.CheckRole;
import org.zanata.util.Zanata;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* @author Armagan Ersoz
* <a href="mailto:aersoz@redhat.com">aersoz@redhat.com</a>
*/
@CheckRole("admin")
@Named("cacheAction")
@ViewScoped
public class CacheAction implements Serializable {
private static final org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger(CacheAction.class);
private static final long serialVersionUID = -4179920199273998215L;
@SuppressFBWarnings("SE_BAD_FIELD")
@Inject
@Zanata
private EmbeddedCacheManager cacheManager;
@Inject
private Messages msgs;
public CacheAction() {
}
public List<String> getCacheList() {
ArrayList<String> cacheNames =
new ArrayList<>(cacheManager.getCacheNames());
Collections.sort(cacheNames);
return cacheNames;
}
public Stats getStats(String cacheName) {
log.debug("getting stats for cache \'{}\'", cacheName);
try {
Cache<Object, Object> cache = cacheManager.getCache(cacheName);
if (cache.getCacheConfiguration().jmxStatistics().enabled()) {
return cache.getAdvancedCache().getStats();
} else {
log.debug("Statistics are not enabled for cache \'{}\'",
cacheName);
// -1 just means no stats (same as Infinispan)
return new UnavailableStats(-1);
}
// eg getStats() throws IndexOutOfBoundsException if stats not
// enabled
} catch (Exception e) {
log.warn(
"Error getting Stats object - are statistics enabled for cache \'{}\'?",
cacheName, e);
// -2 also means no stats, but serves as a hint that something might
// be wrong
return new UnavailableStats(-2);
}
}
/**
* Return some sanitised HTML for the display name of the cache
*
* @param cacheName
* @return
*/
public String getDisplayName(String cacheName) {
String emptyString = msgs.get("jsf.cacheStats.emptyString");
if (cacheName.isEmpty()) {
return "<em>(" + emptyString + ")</em>";
} else {
// Escape cache name in case it includes user input in future
return StringEscapeUtils.escapeHtml4(cacheName);
}
}
public void clearCache(String cacheName) {
cacheManager.getCache(cacheName).clear();
}
public void resetCacheStats(String cacheName) {
getStats(cacheName).reset();
}
public void clearAllCaches() {
cacheManager.getCacheNames().forEach(this::clearCache);
}
/**
* Return the entire set of properties for which the specified bean provides
* a read method. In this case, the bean is a stats object. The returning
* value is the set of StatsImpl (org.infinispan.stats.impl) class's
* properties. The returning map's keys are plain properties' names (e.g.
* currentNumberOfEntries) and the map's values are values from the
* Infinispan Stats object (converted to Strings if necessary). This
* returning value is used for composing the cache statistics table at the
* admin site.
*/
public Map<String, String> getCacheStatsProperties(String cacheName) {
try {
Map<String, String> properties =
BeanUtils.describe(getStats(cacheName));
properties.remove("class");
return properties;
} catch (IllegalAccessException | InvocationTargetException
| NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
public String getNameOfProperty(String key) {
return msgs.get("jsf.cacheStats." + key + ".name");
}
public String getDescOfProperty(String key) {
return msgs.get("jsf.cacheStats." + key + ".description");
}
/**
* Dummy Stats implementation returned when Stats are disabled or otherwise
* unavailable.
*/
private static class UnavailableStats implements Stats {
private final int val;
private UnavailableStats(int val) {
this.val = val;
}
@Override
public long getTimeSinceStart() {
return val;
}
@Override
public long getTimeSinceReset() {
return val;
}
@Override
public int getCurrentNumberOfEntries() {
return val;
}
@Override
public long getTotalNumberOfEntries() {
return val;
}
@Override
public long getStores() {
return val;
}
@Override
public long getRetrievals() {
return val;
}
@Override
public long getHits() {
return val;
}
@Override
public long getMisses() {
return val;
}
@Override
public long getRemoveHits() {
return val;
}
@Override
public long getRemoveMisses() {
return val;
}
@Override
public long getEvictions() {
return val;
}
@Override
public long getAverageReadTime() {
return val;
}
@Override
public long getAverageWriteTime() {
return val;
}
@Override
public long getAverageRemoveTime() {
return val;
}
@Override
public void reset() {
}
@Override
public void setStatisticsEnabled(boolean enabled) {
}
}
}