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.event.EventBus;
37 import info.magnolia.event.SystemEventBus;
38 import info.magnolia.module.ModuleLifecycleContext;
39 import info.magnolia.module.ModulesStartedEvent;
40 import info.magnolia.module.cache.factory.DelegatingCacheFactory;
41 import info.magnolia.module.cache.mbean.CacheMonitor;
42
43 import java.util.Collections;
44 import java.util.HashMap;
45 import java.util.HashSet;
46 import java.util.Map;
47 import java.util.Set;
48
49 import javax.inject.Inject;
50 import javax.inject.Named;
51
52
53
54
55
56
57
58 public class CacheModule extends AbstractCacheModule {
59
60 private final Set<CacheModuleLifecycleListener> listeners = Collections.synchronizedSet(new HashSet<CacheModuleLifecycleListener>());
61 private CacheFactory cacheFactory = new DelegatingCacheFactory();
62 private Map<String, ContentCachingConfiguration> contentCaching = new HashMap<String, ContentCachingConfiguration>();
63 private ContentCompression compression;
64
65 @Inject
66 public CacheModule(CacheMonitor cacheMonitor, @Named(SystemEventBus.NAME) EventBus systemEventBus) {
67 super(cacheMonitor);
68 systemEventBus.addHandler(ModulesStartedEvent.class, new ModulesStartedEvent.Handler() {
69 @Override
70 public void onModuleStartupCompleted(ModulesStartedEvent event) {
71 start();
72 }
73 });
74 }
75
76
77
78
79 @Override
80 public CacheFactory getCacheFactory() {
81 return cacheFactory;
82 }
83
84 public void setCacheFactory(CacheFactory cacheFactory) {
85 this.cacheFactory = cacheFactory;
86 }
87
88 public Map<String, ContentCachingConfiguration> getContentCaching() {
89 return contentCaching;
90 }
91
92 public void setContentCaching(Map<String, ContentCachingConfiguration> contentCaching) {
93 this.contentCaching = contentCaching;
94 }
95
96 public void setCompression(ContentCompression compression) {
97 this.compression = compression;
98 }
99
100 public ContentCompression getCompression() {
101 return compression;
102 }
103
104 public void register(CacheModuleLifecycleListener listener) {
105 listeners.add(listener);
106 }
107
108 @Override
109 public ContentCachingConfiguration getContentCaching(String name) {
110 return this.getContentCaching().get(name) != null ? this.getContentCaching().get(name) : this.getContentCaching().get(DEFAULT_CACHE_CONFIG);
111 }
112
113 @Override
114 public void start(ModuleLifecycleContext moduleLifecycleContext) {
115 cacheFactory.start(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
116 this.getCacheMonitor().countStart();
117 if (ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase()) {
118 start();
119 for (CacheModuleLifecycleListener listener : listeners) {
120 listener.onCacheModuleStart();
121 }
122 }
123 }
124
125 @Override
126 public void stop(ModuleLifecycleContext moduleLifecycleContext) {
127 this.getCacheMonitor().stop();
128 for (ContentCachingConfiguration cfg : contentCaching.values()) {
129 final String name = cfg.getCacheName();
130 this.stopCache(name);
131 }
132 cacheFactory.stop(ModuleLifecycleContext.PHASE_MODULE_RESTART == moduleLifecycleContext.getPhase());
133 }
134
135 private void start() {
136
137 for (ContentCachingConfiguration cfg : contentCaching.values()) {
138 final String name = cfg.getCacheName();
139 this.startCache(name);
140 }
141 }
142 }