View Javadoc

1   /**
2    * This file Copyright (c) 2010-2010 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.context.Context;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.context.SimpleContext;
41  import info.magnolia.module.ModuleRegistry;
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.DefaultCacheKey;
46  import info.magnolia.module.cache.cachepolicy.Default;
47  
48  import java.util.HashMap;
49  import java.util.Map;
50  
51  import org.apache.commons.chain.Command;
52  
53  /**
54   * <code>MBean</code> implementation for the cache status.
55   * @author had
56   * @version $Id:$
57   */
58  public class CacheMonitor implements CacheMonitorMBean {
59  
60      private static CacheMonitor instance = new CacheMonitor();
61      private int start;
62      private int stop;
63      private Map<String, Integer> calls = new HashMap<String, Integer>();
64      private Map<String, Integer> caches = new HashMap<String, Integer>();
65      private Map<String, Integer> domains = new HashMap<String, Integer>();
66      private CommandsManager commandsManager;
67  
68      public CacheMonitor() {
69          MBeanUtil.registerMBean("CacheMonitor", this);
70          commandsManager = CommandsManager.getInstance();
71      }
72  
73      private CacheFactory getCacheFactory() {
74          CacheFactory factory = ModuleRegistry.Factory.getInstance().getModuleInstance(CacheModule.class).getCacheFactory();
75          return factory;
76      }
77  
78      private Command getCommand(String commandName) {
79          Command flush = commandsManager.getCommand("cache", commandName);
80          if (flush == null) {
81              // TODO: why does it happen that commands are not ready?
82              commandsManager.reload();
83              flush = commandsManager.getCommand("cache", commandName);
84          }
85          if (flush == null) {
86              throw new RuntimeException("Failed to invoke cache " + commandName + " command");
87          }
88          return flush;
89      }
90  
91      public static CacheMonitor getInstance() {
92          return instance;
93      }
94  
95      public void countStart() {
96          start++;
97      }
98  
99      public void stop() {
100         stop++;
101     }
102 
103     /**
104      * @param name
105      */
106     public void logBehavior(String name) {
107         Integer count = calls.get(name);
108         calls.put(name, count == null ? 1 : ++count);
109     }
110 
111     public void countFlush(String name) {
112         caches.put(name, caches.get(name) + 1);
113     }
114 
115     public void addCache(String name) {
116         caches.put(name, 0);
117     }
118 
119     public void logAccess(Object cacheKey) {
120         if (cacheKey == null || !(cacheKey instanceof DefaultCacheKey)) {
121             return;
122         }
123         DefaultCacheKey key = (DefaultCacheKey) cacheKey;
124         Integer count = this.domains.get(key.getDomain());
125         this.domains.put(key.getDomain(), count == null ? 1 : ++count);
126     }
127 
128     // mbean exposed operations
129     public void flushAll() throws Exception {
130         Command flush = getCommand("flushAll");
131         flush.execute(MgnlContext.getSystemContext());
132     }
133 
134     public void flushByUUID(String repository, String uuid) throws Exception {
135         Command flush = getCommand("flushByUUID");
136         Context ctx = new SimpleContext(MgnlContext.getSystemContext());
137         ctx.put("repository", repository);
138         ctx.put("uuid", uuid);
139         flush.execute(ctx);
140     }
141 
142     // mbean exposed attributes
143     public Map<String, Integer> getAll() {
144         return calls;
145     }
146 
147     public int getHits() {
148         return calls.get("useCache");
149     }
150 
151     public int getBypasses() {
152         return calls.get("bypass");
153     }
154 
155     public int getPuts() {
156         return calls.get("store");
157     }
158 
159     public int getStopCalls() {
160         return stop;
161     }
162 
163     public int getStartCalls() {
164         return start;
165     }
166 
167     public Map<String, Integer> getFlushes() {
168         return caches;
169     }
170 
171     public Map<String, Integer> getDomainAccesses() {
172         return domains;
173     }
174 
175     public int getCachedKeysCount() {
176         int count = 0;
177         // 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
178         CacheFactory factory = getCacheFactory();
179         for (String name : caches.keySet()) {
180             // skip the uuid-key cache itself
181             if (Default.UUID_KEY_MAP_KEY.equals(name)) {
182                 continue;
183             }
184             Cache cache = factory.getCache(name);
185             count += cache.getSize();
186         }
187         return count;
188     }
189 
190     public int getCachedUUIDsCount() {
191         CacheFactory factory = getCacheFactory();
192         Cache cache = factory.getCache(Default.UUID_KEY_MAP_KEY);
193         return cache.getSize();
194     }
195 }