View Javadoc

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