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