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