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.module.cache;
35
36 import info.magnolia.module.InstallContext;
37 import info.magnolia.module.ModuleLifecycle;
38 import info.magnolia.module.ModuleLifecycleContext;
39 import info.magnolia.module.ModuleManager;
40 import info.magnolia.module.ModuleRegistry;
41 import info.magnolia.module.cache.mbean.CacheMonitor;
42
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Map;
46 import java.util.Set;
47 import javax.inject.Inject;
48
49
50
51
52
53
54
55
56
57 public class CacheModule implements ModuleLifecycle {
58 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CacheModule.class);
59
60 private final ModuleManager moduleManager;
61 private final CacheMonitor cacheMonitor;
62
63 private final Set<CacheModuleLifecycleListener> listeners = new HashSet<CacheModuleLifecycleListener>();
64 private CacheFactory cacheFactory;
65 private Map<String, CacheConfiguration> configurations = new HashMap<String, CacheConfiguration>();
66 private ContentCompression compression;
67
68
69
70
71 @Inject
72 public CacheModule(ModuleManager moduleManager, CacheMonitor cacheMonitor) {
73 this.moduleManager = moduleManager;
74 this.cacheMonitor = cacheMonitor;
75 }
76
77 public CacheFactory getCacheFactory() {
78 return cacheFactory;
79 }
80
81 public void setCacheFactory(CacheFactory cacheFactory) {
82 this.cacheFactory = cacheFactory;
83 }
84
85 public Map<String, CacheConfiguration> getConfigurations() {
86 return configurations;
87 }
88
89 public void setConfigurations(Map<String, CacheConfiguration> configurations) {
90 this.configurations = configurations;
91 }
92
93 public void addConfiguration(String name, CacheConfiguration config) {
94 configurations.put(name, config);
95 }
96
97 public CacheConfiguration getConfiguration(String name) {
98 return configurations.get(name);
99 }
100
101 public void setCompression(ContentCompression compression) {
102 this.compression = compression;
103 }
104
105 public ContentCompression getCompression() {
106 return compression;
107 }
108
109 public void register(CacheModuleLifecycleListener listener) {
110 listeners.add(listener);
111 }
112
113
114 @Override
115 public void start(ModuleLifecycleContext moduleLifecycleContext) {
116
117
118 cacheFactory.start();
119
120
121 cacheMonitor.countStart();
122
123 for (CacheConfiguration cfg : configurations.values()) {
124 final String name = cfg.getName();
125 final Cache cache = cacheFactory.getCache(name);
126 cacheMonitor.addCache(name);
127 if (cfg.getFlushPolicy() != null) {
128 cfg.getFlushPolicy().start(cache);
129 } else {
130 log.warn("Flush Policy is not configured properly for {} cache configuration.", name);
131 }
132 }
133
134 for (CacheModuleLifecycleListener listener : listeners) {
135 listener.onCacheModuleStart();
136 }
137
138
139 if (moduleLifecycleContext.getPhase() == ModuleLifecycleContext.PHASE_SYSTEM_STARTUP) {
140 final InstallContext installContext = moduleManager.getInstallContext();
141
142 if (installContext.getStatus() != null) {
143 log.info("Flushing all caches ...");
144 for (CacheConfiguration config : configurations.values()) {
145 cacheFactory.getCache(config.getName()).clear();
146 log.info(" flushed cache: {}", config.getName());
147 }
148 }
149 }
150 }
151
152 @Override
153 public void stop(ModuleLifecycleContext moduleLifecycleContext) {
154 cacheMonitor.stop();
155 for (CacheConfiguration cfg : configurations.values()) {
156 final String name = cfg.getName();
157 Cache cache = null;
158 try {
159 cache = cacheFactory.getCache(name);
160 } catch (IllegalStateException e) {
161 log.warn("Cache {} is not running anymore. Check your configuration and log files to find out if there were any errors forcing cache shutdown.", name);
162 }
163 if (cfg.getFlushPolicy() != null) {
164 cfg.getFlushPolicy().stop(cache);
165 } else {
166 log.warn("Flush Policy is not configured properly for {} cache configuration.", name);
167 }
168 }
169
170
171
172
173 cacheFactory.stop();
174
175 }
176
177
178
179
180 public static CacheModule getInstance() {
181 return ModuleRegistry.Factory.getInstance().getModuleInstance(CacheModule.class);
182 }
183 }