Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache.mbean

File CacheMonitor.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
66% of files have more coverage

Code metrics

14
41
12
1
218
143
20
0.49
3.42
12
1.67
23% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
CacheMonitor 61 41 23% 20 46
0.3134328431.3%
 

Contributing tests

This file is covered by 18 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.cache.mbean;
35   
36    import info.magnolia.cms.util.MBeanUtil;
37    import info.magnolia.commands.CommandsManager;
38    import info.magnolia.commands.chain.Command;
39    import info.magnolia.context.Context;
40    import info.magnolia.context.SimpleContext;
41    import info.magnolia.context.SystemContext;
42    import info.magnolia.module.cache.Cache;
43    import info.magnolia.module.cache.CacheFactory;
44    import info.magnolia.module.cache.CacheModule;
45    import info.magnolia.module.cache.cachekey.DefaultCacheKey;
46    import info.magnolia.module.cache.cachepolicy.Default;
47    import info.magnolia.module.cache.inject.CacheFactoryProvider;
48    import info.magnolia.objectfactory.Components;
49   
50    import java.util.HashMap;
51    import java.util.Map;
52   
53    import javax.inject.Inject;
54    import javax.inject.Provider;
55    import javax.inject.Singleton;
56   
57    /**
58    * <code>MBean</code> implementation for the cache status.
59    */
60    @Singleton
 
61    public class CacheMonitor implements CacheMonitorMBean {
62   
63    private final CommandsManager commandsManager;
64    private final CacheFactoryProvider cacheFactoryProvider;
65    private final SystemContext systemContext;
66   
67    private int start;
68    private int stop;
69    private Map<String, Integer> calls = new HashMap<String, Integer>();
70    private Map<String, Integer> caches = new HashMap<String, Integer>();
71    private Map<String, Integer> domains = new HashMap<String, Integer>();
72   
73   
 
74  39 toggle @Inject
75    public CacheMonitor(CommandsManager commandsManager, CacheFactoryProvider cacheFactoryProvider, SystemContext systemContext) {
76  39 MBeanUtil.registerMBean("CacheMonitor", this);
77  39 this.commandsManager = commandsManager;
78  39 this.cacheFactoryProvider = cacheFactoryProvider;
79  39 this.systemContext = systemContext;
80    }
81   
82    /**
83    * @deprecated since 5.4. Use {@link #CacheMonitor(info.magnolia.commands.CommandsManager, info.magnolia.module.cache.inject.CacheFactoryProvider, info.magnolia.context.SystemContext)} instead.
84    */
 
85  0 toggle @Deprecated
86    public CacheMonitor(CommandsManager commandsManager, Provider<CacheModule> cacheModuleProvider, SystemContext systemContext) {
87  0 MBeanUtil.registerMBean("CacheMonitor", this);
88  0 this.commandsManager = commandsManager;
89  0 this.systemContext = systemContext;
90  0 this.cacheFactoryProvider = Components.getComponent(CacheFactoryProvider.class);
91    }
92   
 
93  0 toggle private Command getCommand(String commandName) {
94  0 Command flush = commandsManager.getCommand("cache", commandName);
95  0 if (flush == null) {
96    // TODO: why does it happen that commands are not ready?
97  0 commandsManager.reload();
98  0 flush = commandsManager.getCommand("cache", commandName);
99    }
100  0 if (flush == null) {
101  0 throw new RuntimeException("Failed to invoke cache " + commandName + " command");
102    }
103  0 return flush;
104    }
105   
 
106  0 toggle public void countStart() {
107  0 start++;
108    }
109   
 
110  0 toggle public void stop() {
111  0 stop++;
112    }
113   
114    /**
115    * @param name
116    */
 
117  17 toggle public void logBehavior(String name) {
118  17 Integer count = calls.get(name);
119  17 calls.put(name, count == null ? 1 : ++count);
120    }
121   
 
122  1 toggle public void countFlush(String name) {
123  1 caches.put(name, caches.get(name) == null ? 1 : caches.get(name) + 1);
124    }
125   
 
126  0 toggle public void addCache(String name) {
127  0 caches.put(name, 0);
128    }
129   
 
130  17 toggle public void logAccess(Object cacheKey) {
131  17 if (cacheKey == null || !(cacheKey instanceof DefaultCacheKey)) {
132  16 return;
133    }
134  1 DefaultCacheKey key = (DefaultCacheKey) cacheKey;
135  1 Integer count = this.domains.get(key.getDomain());
136  1 this.domains.put(key.getDomain(), count == null ? 1 : ++count);
137    }
138   
139    // mbean exposed operations
 
140  0 toggle @Override
141    public void flushAll() throws Exception {
142  0 Command flush = getCommand("flushAll");
143  0 flush.execute(systemContext);
144    }
145   
 
146  0 toggle @Override
147    public void flushByUUID(String repository, String uuid) throws Exception {
148  0 Command flush = getCommand("flushByUUID");
149  0 Context ctx = new SimpleContext(systemContext);
150  0 ctx.put("repository", repository);
151  0 ctx.put("uuid", uuid);
152  0 flush.execute(ctx);
153    }
154   
155    // mbean exposed attributes
 
156    toggle @Override
157    public Map<String, Integer> getAll() {
158    return calls;
159    }
160   
 
161    toggle @Override
162    public int getHits() {
163    return calls.get("useCache");
164    }
165   
 
166    toggle @Override
167    public int getBypasses() {
168    return calls.get("bypass");
169    }
170   
 
171    toggle @Override
172    public int getPuts() {
173    return calls.get("store");
174    }
175   
 
176    toggle @Override
177    public int getStopCalls() {
178    return stop;
179    }
180   
 
181    toggle @Override
182    public int getStartCalls() {
183    return start;
184    }
185   
 
186    toggle @Override
187    public Map<String, Integer> getFlushes() {
188    return caches;
189    }
190   
 
191    toggle @Override
192    public Map<String, Integer> getDomainAccesses() {
193    return domains;
194    }
195   
 
196  0 toggle @Override
197    public int getCachedKeysCount() {
198  0 int count = 0;
199    // there's most likely gonna be ever just one map in the default cache, but let's not assume that and search all configured caches
200  0 CacheFactory factory = cacheFactoryProvider.get();
201  0 for (String name : caches.keySet()) {
202    // skip the uuid-key cache itself
203  0 if (Default.UUID_KEY_MAP_KEY.equals(name)) {
204  0 continue;
205    }
206  0 Cache cache = factory.getCache(name);
207  0 count += cache.getSize();
208    }
209  0 return count;
210    }
211   
 
212    toggle @Override
213    public int getCachedUUIDsCount() {
214    CacheFactory factory = cacheFactoryProvider.get();
215    Cache cache = factory.getCache(Default.UUID_KEY_MAP_KEY);
216    return cache.getSize();
217    }
218    }