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.cms.cache.pages;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.util.AlertUtil;
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.module.ModuleRegistry;
40  import info.magnolia.module.admininterface.TemplatedMVCHandler;
41  import info.magnolia.module.cache.CacheConfiguration;
42  import info.magnolia.module.cache.CacheFactory;
43  import info.magnolia.module.cache.CacheModule;
44  import info.magnolia.module.cache.commands.FlushCachesCommand;
45  import info.magnolia.module.cache.commands.FlushFromCachesByUUIDCommand;
46  import info.magnolia.module.cache.commands.FlushNamedCacheCommand;
47  
48  import java.util.Collection;
49  import java.util.HashMap;
50  import java.util.Iterator;
51  import java.util.Map;
52  
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletResponse;
55  
56  /**
57   * Tools page to show cache info and flush caches.
58   * 
59   * @author Bert Leunis
60   */
61  public class CacheToolsPage extends TemplatedMVCHandler {
62  
63      private final CacheModule cacheModule;
64  
65      public CacheToolsPage(String name, HttpServletRequest request, HttpServletResponse response) {
66          super(name, request, response);
67          cacheModule = ModuleRegistry.Factory.getInstance().getModuleInstance(CacheModule.class);
68      }
69  
70      public String refresh() {
71          return this.show();
72      }
73  
74      public String flushNamedCache() {
75          try {
76              AlertUtil.setMessage(flushNamedCacheCommand());
77          }
78          catch (Exception e) {
79              AlertUtil.setMessage("Error when flushing cache: " + e.getMessage(), e);
80          }
81          return this.show();
82      }
83  
84      private String flushNamedCacheCommand() {
85          FlushNamedCacheCommand command = new FlushNamedCacheCommand();
86          try {
87              command.execute(MgnlContext.getInstance());
88          } catch (Exception e) {
89              return "Error occurred during command execution. (" + e.getMessage() +")";
90          }
91  
92          return "Cache \"" + MgnlContext.getParameter(FlushNamedCacheCommand.CACHE_NAME) + "\" is flushed.";
93      }
94  
95      public String flushByUUID() {
96          try {
97              AlertUtil.setMessage(flushByUUIDCommand());
98          }
99          catch (Exception e) {
100             AlertUtil.setMessage("Error when flushing by uuid: " + e.getMessage(), e);
101         }
102         return this.show();
103     }
104 
105     private String flushByUUIDCommand() {
106         FlushFromCachesByUUIDCommand command = new FlushFromCachesByUUIDCommand();
107         try {
108             command.execute(MgnlContext.getInstance());
109         } catch (Exception e) {
110             return "Error occurred during command execution. (" + e.getMessage() +")";
111         }
112         return "UUID \"" + MgnlContext.getParameter(FlushFromCachesByUUIDCommand.UUID) + "\" is flushed from cache.";
113     }
114 
115     public String flushAllCaches() {
116         try {
117             AlertUtil.setMessage(flushAllCacheCommand());
118         }
119         catch (Exception e) {
120             AlertUtil.setMessage("Error when flushing all caches: " + e.getMessage(), e);
121         }
122         return this.show();
123     }
124 
125     private String flushAllCacheCommand() {
126         FlushCachesCommand command = new FlushCachesCommand();
127         try {
128             command.execute(MgnlContext.getInstance());
129         } catch (Exception e) {
130             return "Error occurred during command execution. (" + e.getMessage() +")";
131         }
132         return "All caches are flushed.";
133     }
134 
135     public Map<String, Integer> getCacheSizes() {
136         Map<String, Integer> cacheNames = new HashMap<String, Integer>();
137         CacheFactory factory = cacheModule.getCacheFactory();
138         Collection<CacheConfiguration> cacheConfigs = cacheModule.getConfigurations().values();
139         for (CacheConfiguration config : cacheConfigs) {
140             String cacheName = config.getName();
141             cacheNames.put(cacheName, factory.getCache(cacheName).getSize());
142         }
143         return cacheNames;
144     }
145 
146     public Iterator getRepositories() {
147         return ContentRepository.getAllRepositoryNames();
148     }
149 
150 }