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.cms.util.DeprecationUtil; |
37 |
|
import info.magnolia.cms.util.ObservationUtil; |
38 |
|
import info.magnolia.objectfactory.Components; |
39 |
|
import info.magnolia.repository.RepositoryManager; |
40 |
|
|
41 |
|
import java.util.ArrayList; |
42 |
|
import java.util.Arrays; |
43 |
|
import java.util.Collection; |
44 |
|
import java.util.HashMap; |
45 |
|
import java.util.Iterator; |
46 |
|
import java.util.List; |
47 |
|
import java.util.Map; |
48 |
|
|
49 |
|
import javax.inject.Inject; |
50 |
|
import javax.jcr.RepositoryException; |
51 |
|
import javax.jcr.observation.Event; |
52 |
|
import javax.jcr.observation.EventIterator; |
53 |
|
import javax.jcr.observation.EventListener; |
54 |
|
|
55 |
|
import org.apache.commons.lang3.StringUtils; |
56 |
|
import org.slf4j.Logger; |
57 |
|
import org.slf4j.LoggerFactory; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
|
|
| 51.3% |
Uncovered Elements: 37 (76) |
Complexity: 22 |
Complexity Density: 0.47 |
|
62 |
|
public abstract class AbstractListeningFlushPolicy implements FlushPolicy { |
63 |
|
|
64 |
|
private static final Logger log = LoggerFactory.getLogger(AbstractListeningFlushPolicy.class); |
65 |
|
|
66 |
|
private List<String> workspaces; |
67 |
|
private List<String> excludedWorkspaces = new ArrayList<String>(); |
68 |
|
private Map<String, EventListener> registeredListeners = new HashMap<>(); |
69 |
|
|
70 |
|
private final CacheModule cacheModule; |
71 |
|
private final RepositoryManager repositoryManager; |
72 |
|
|
73 |
|
|
74 |
|
@deprecated@link |
75 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
76 |
0 |
public AbstractListeningFlushPolicy() {... |
77 |
0 |
this.cacheModule = Components.getComponent(CacheModule.class); |
78 |
0 |
this.repositoryManager = Components.getComponent(RepositoryManager.class); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
81 |
77 |
@Inject... |
82 |
|
public AbstractListeningFlushPolicy(CacheModule cacheModule, RepositoryManager repositoryManager) { |
83 |
77 |
this.cacheModule = cacheModule; |
84 |
77 |
this.repositoryManager = repositoryManager; |
85 |
|
} |
86 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
87 |
7 |
@Override... |
88 |
|
public void start(Cache cache) { |
89 |
20 |
for (Iterator<String> iter = this.getWorkspacesToProcess().iterator(); iter.hasNext(); ) { |
90 |
13 |
final String workspace = iter.next(); |
91 |
13 |
try { |
92 |
13 |
if (repositoryManager.getWorkspaceMapping(workspace) != null) { |
93 |
13 |
for (String path : getPaths(cache, workspace)) { |
94 |
13 |
final CacheCleaner cacheCleaner = new CacheCleaner(cache, workspace); |
95 |
13 |
final EventListener listener = ObservationUtil.instanciateDeferredEventListener(cacheCleaner, 5000, 30000); |
96 |
13 |
ObservationUtil.registerChangeListener(workspace, path, listener); |
97 |
13 |
registeredListeners.put(cache.getName() + ":" + workspace + ":" + path, listener); |
98 |
|
} |
99 |
|
} |
100 |
|
} catch (Exception e) { |
101 |
0 |
log.warn("Failed to register cache flushing observation for workspace '{}' (cache named {}): ", workspace, cache.getName(), e); |
102 |
|
} |
103 |
|
} |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
13 |
protected Collection<String> getPaths(Cache cache, String workspace) {... |
107 |
13 |
return Arrays.asList(getPath(cache)); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
@deprecated@link |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
113 |
13 |
@Deprecated... |
114 |
|
protected String getPath(Cache cache) { |
115 |
13 |
return "/"; |
116 |
|
} |
117 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
118 |
5 |
@Override... |
119 |
|
public void stop(Cache cache) { |
120 |
5 |
for (Map.Entry<String, EventListener> entry : new HashMap<String, EventListener>(registeredListeners).entrySet()) { |
121 |
6 |
if (entry.getValue() == null) { |
122 |
|
|
123 |
0 |
continue; |
124 |
|
} |
125 |
6 |
if (StringUtils.startsWith(entry.getKey(), cache.getName() + ":")) { |
126 |
4 |
ObservationUtil.unregisterChangeListener(StringUtils.substringBetween(entry.getKey(), ":"), entry.getValue()); |
127 |
4 |
registeredListeners.remove(entry.getKey()); |
128 |
|
} |
129 |
|
} |
130 |
|
} |
131 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
132 |
7 |
private List<String> getWorkspacesToProcess() {... |
133 |
7 |
if (this.getWorkspaces() != null) { |
134 |
0 |
return this.getWorkspaces(); |
135 |
|
} else { |
136 |
7 |
ArrayList<String> workspaces = new ArrayList<String>(repositoryManager.getWorkspaceNames()); |
137 |
7 |
workspaces.removeAll(this.getExcludedWorkspaces()); |
138 |
7 |
return workspaces; |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@return |
146 |
|
|
147 |
|
protected abstract boolean preHandleEvents(Cache cache, String repository); |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@link |
152 |
|
|
153 |
|
protected abstract void postHandleEvents(Cache cache, String repository); |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
@link |
159 |
|
|
160 |
|
protected abstract void handleSingleEvent(Cache cache, String repository, Event event); |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
166 |
0 |
protected void flushByUUID(String uuid, String repository, Cache cache) {... |
167 |
|
|
168 |
0 |
final ContentCachingConfiguration config = cacheModule.getContentCaching(cache.getName()); |
169 |
0 |
final CachePolicy policy = config.getCachePolicy(); |
170 |
0 |
if (policy == null) { |
171 |
|
|
172 |
0 |
return; |
173 |
|
} |
174 |
|
|
175 |
0 |
Object[] cacheEntryKeys = config.getCachePolicy().retrieveCacheKeys(uuid, repository); |
176 |
0 |
log.debug("Flushing {} due to detected content {}:{} update.", cacheEntryKeys, repository, uuid); |
177 |
|
|
178 |
0 |
if (cacheEntryKeys == null || cacheEntryKeys.length == 0) { |
179 |
|
|
180 |
0 |
return; |
181 |
|
} |
182 |
0 |
for (Object key : cacheEntryKeys) { |
183 |
0 |
cache.remove(key); |
184 |
|
} |
185 |
|
|
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
|
|
| 11.5% |
Uncovered Elements: 23 (26) |
Complexity: 7 |
Complexity Density: 0.44 |
|
191 |
|
protected class CacheCleaner implements EventListener { |
192 |
|
private final Cache cache; |
193 |
|
private final String repository; |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
195 |
13 |
public CacheCleaner(Cache cache, String repository) {... |
196 |
13 |
this.cache = cache; |
197 |
13 |
this.repository = repository; |
198 |
|
} |
199 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 6 |
Complexity Density: 0.43 |
|
200 |
0 |
@Override... |
201 |
|
public void onEvent(EventIterator events) { |
202 |
0 |
List<Event> eventList = new ArrayList<Event>(); |
203 |
|
|
204 |
0 |
while (events.hasNext()) { |
205 |
0 |
final Event event = events.nextEvent(); |
206 |
0 |
try { |
207 |
0 |
if (!event.getPath().startsWith("/jcr:")) { |
208 |
0 |
eventList.add(event); |
209 |
|
} |
210 |
|
} catch (RepositoryException e) { |
211 |
0 |
log.warn("Failed to process an event {}, the observation based cache flushing might not have been fully completed.", event.toString()); |
212 |
|
} |
213 |
|
} |
214 |
0 |
if (eventList.isEmpty()) { |
215 |
0 |
return; |
216 |
|
} |
217 |
|
|
218 |
0 |
boolean shouldContinue = preHandleEvents(cache, repository); |
219 |
0 |
if (shouldContinue) { |
220 |
0 |
for (Event event : eventList) { |
221 |
0 |
handleSingleEvent(cache, repository, event); |
222 |
|
} |
223 |
0 |
postHandleEvents(cache, repository); |
224 |
|
} |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
234 |
|
public List<String> getWorkspaces() {... |
235 |
|
return workspaces; |
236 |
|
} |
237 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
238 |
0 |
public void setWorkspaces(List<String> workspaces) {... |
239 |
0 |
if (!this.getExcludedWorkspaces().isEmpty()) { |
240 |
0 |
log.error("You should configure only 'workspaces' or 'excludedWorkspaces' on {}. Not both of them.", this.getClass()); |
241 |
0 |
return; |
242 |
|
} |
243 |
0 |
this.workspaces = workspaces; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
249 |
|
public List<String> getExcludedWorkspaces() {... |
250 |
|
return excludedWorkspaces; |
251 |
|
} |
252 |
|
|
|
|
| 50% |
Uncovered Elements: 3 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
253 |
3 |
public void setExcludedWorkspaces(List<String> excludedWorkspaces) {... |
254 |
3 |
if (this.getWorkspaces() != null) { |
255 |
0 |
log.error("You should configure only 'workspaces' or 'excludedWorkspaces' on {}. Not both of them.", this.getClass()); |
256 |
0 |
return; |
257 |
|
} |
258 |
3 |
this.excludedWorkspaces = excludedWorkspaces; |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
|
@deprecated@link |
263 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
264 |
|
public List<String> getRepositories() {... |
265 |
|
DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.AbstractListeningFlushPolicy#getWorkspaces instead."); |
266 |
|
return this.getWorkspaces(); |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
|
@deprecated@link |
271 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
272 |
|
public void setRepositories(List<String> repositories) {... |
273 |
|
DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.AbstractListeningFlushPolicy#setWorkspaces instead."); |
274 |
|
this.setWorkspaces(repositories); |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
|
@deprecated@link |
279 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
280 |
0 |
public void addRepository(String repository) {... |
281 |
0 |
DeprecationUtil.isDeprecated("Use info.magnolia.module.cache.AbstractListeningFlushPolicy#setWorkspaces instead."); |
282 |
0 |
workspaces.add(repository); |
283 |
|
} |
284 |
|
} |