View Javadoc
1   /**
2    * This file Copyright (c) 2010-2015 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      @Inject
75      public CacheMonitor(CommandsManager commandsManager, CacheFactoryProvider cacheFactoryProvider, SystemContext systemContext) {
76          MBeanUtil.registerMBean("CacheMonitor", this);
77          this.commandsManager = commandsManager;
78          this.cacheFactoryProvider = cacheFactoryProvider;
79          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      @Deprecated
86      public CacheMonitor(CommandsManager commandsManager, Provider<CacheModule> cacheModuleProvider, SystemContext systemContext) {
87          MBeanUtil.registerMBean("CacheMonitor", this);
88          this.commandsManager = commandsManager;
89          this.systemContext = systemContext;
90          this.cacheFactoryProvider = Components.getComponent(CacheFactoryProvider.class);
91      }
92  
93      private Command getCommand(String commandName) {
94          Command flush = commandsManager.getCommand("cache", commandName);
95          if (flush == null) {
96              // TODO: why does it happen that commands are not ready?
97              commandsManager.reload();
98              flush = commandsManager.getCommand("cache", commandName);
99          }
100         if (flush == null) {
101             throw new RuntimeException("Failed to invoke cache " + commandName + " command");
102         }
103         return flush;
104     }
105 
106     public void countStart() {
107         start++;
108     }
109 
110     public void stop() {
111         stop++;
112     }
113 
114     /**
115      * @param name
116      */
117     public void logBehavior(String name) {
118         Integer count = calls.get(name);
119         calls.put(name, count == null ? 1 : ++count);
120     }
121 
122     public void countFlush(String name) {
123         caches.put(name, caches.get(name) + 1);
124     }
125 
126     public void addCache(String name) {
127         caches.put(name, 0);
128     }
129 
130     public void logAccess(Object cacheKey) {
131         if (cacheKey == null || !(cacheKey instanceof DefaultCacheKey)) {
132             return;
133         }
134         DefaultCacheKey key = (DefaultCacheKey) cacheKey;
135         Integer count = this.domains.get(key.getDomain());
136         this.domains.put(key.getDomain(), count == null ? 1 : ++count);
137     }
138 
139     // mbean exposed operations
140     @Override
141     public void flushAll() throws Exception {
142         Command flush = getCommand("flushAll");
143         flush.execute(systemContext);
144     }
145 
146     @Override
147     public void flushByUUID(String repository, String uuid) throws Exception {
148         Command flush = getCommand("flushByUUID");
149         Context ctx = new SimpleContext(systemContext);
150         ctx.put("repository", repository);
151         ctx.put("uuid", uuid);
152         flush.execute(ctx);
153     }
154 
155     // mbean exposed attributes
156     @Override
157     public Map<String, Integer> getAll() {
158         return calls;
159     }
160 
161     @Override
162     public int getHits() {
163         return calls.get("useCache");
164     }
165 
166     @Override
167     public int getBypasses() {
168         return calls.get("bypass");
169     }
170 
171     @Override
172     public int getPuts() {
173         return calls.get("store");
174     }
175 
176     @Override
177     public int getStopCalls() {
178         return stop;
179     }
180 
181     @Override
182     public int getStartCalls() {
183         return start;
184     }
185 
186     @Override
187     public Map<String, Integer> getFlushes() {
188         return caches;
189     }
190 
191     @Override
192     public Map<String, Integer> getDomainAccesses() {
193         return domains;
194     }
195 
196     @Override
197     public int getCachedKeysCount() {
198         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         CacheFactory factory = cacheFactoryProvider.get();
201         for (String name : caches.keySet()) {
202             // skip the uuid-key cache itself
203             if (Default.UUID_KEY_MAP_KEY.equals(name)) {
204                 continue;
205             }
206             Cache cache = factory.getCache(name);
207             count += cache.getSize();
208         }
209         return count;
210     }
211 
212     @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 }