1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59
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 }