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