VersionPurgeTask.java
5.6 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.VersionManager
* com.day.cq.wcm.api.VersionManager$PurgeInfo
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.event.jobs.Job
* org.apache.sling.event.jobs.consumer.JobExecutionContext
* org.apache.sling.event.jobs.consumer.JobExecutionContext$ResultBuilder
* org.apache.sling.event.jobs.consumer.JobExecutionResult
* org.apache.sling.event.jobs.consumer.JobExecutor
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.component.ComponentContext
*/
package com.day.cq.wcm.core.impl;
import com.day.cq.wcm.api.VersionManager;
import java.util.Dictionary;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobExecutionContext;
import org.apache.sling.event.jobs.consumer.JobExecutionResult;
import org.apache.sling.event.jobs.consumer.JobExecutor;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
@Component(label="%versionpurge.name", description="%versionpurge.description", metatype=1)
@Service(value={JobExecutor.class})
@Properties(value={@Property(name="job.topics", value={"com/adobe/granite/maintenance/job/VersionPurgeTask"}, propertyPrivate=1), @Property(name="granite.maintenance.name", value={"com.day.cq.wcm.core.impl.VersionPurgeTask"}, propertyPrivate=1), @Property(name="granite.maintenance.title", value={"Version Purge"}, propertyPrivate=1), @Property(name="granite.maintenance.schedule", value={"weekly"}), @Property(name="granite.maintenance.isConservative", boolValue={0})})
public class VersionPurgeTask
implements JobExecutor {
@Reference
private VersionManager versionManager;
@Reference
private SlingRepository repository;
private static final String[] DEFAULT_PATHS = new String[]{"/content"};
private static final boolean DEFAULT_RECURSIVE = true;
private static final int DEFAULT_MAX_VERSIONS = 5;
private static final int DEFAULT_MAX_AGE_DAYS = 30;
private static final String VERSION_PURGE = "version-purge";
@Property(value={"/content"}, cardinality=Integer.MAX_VALUE)
private static final String PROPERTY_PATHS = "versionpurge.paths";
@Property(boolValue={1})
private static final String PROPERTY_RECURSIVE = "versionpurge.recursive";
@Property(intValue={5})
private static final String PROPERTY_MAX_VERSIONS = "versionpurge.maxVersions";
@Property(intValue={30})
private static final String PROPERTY_MAX_AGE_DAYS = "versionpurge.maxAgeDays";
private String[] paths;
private boolean recursive;
private int maxVersions;
private int maxAgeDays;
@Activate
protected void activate(ComponentContext context) {
Dictionary props = context.getProperties();
this.paths = PropertiesUtil.toStringArray(props.get("versionpurge.paths"), (String[])DEFAULT_PATHS);
this.recursive = PropertiesUtil.toBoolean(props.get("versionpurge.recursive"), (boolean)true);
this.maxVersions = PropertiesUtil.toInteger(props.get("versionpurge.maxVersions"), (int)5);
this.maxAgeDays = PropertiesUtil.toInteger(props.get("versionpurge.maxAgeDays"), (int)30);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public JobExecutionResult process(Job job, JobExecutionContext context) {
Session session = null;
try {
session = this.repository.loginService("version-purge", null);
for (String p : this.paths) {
for (VersionManager.PurgeInfo info : this.versionManager.purgeVersions(session, p, false, this.recursive, this.maxVersions, this.maxAgeDays)) {
if (info.isRetained()) continue;
context.log("Purged version {0} of {1}", new Object[]{info.getVersionName(), info.getVersionedNodePath()});
}
}
}
catch (RepositoryException e) {
context.log("Exception while purging versions: {0}", new Object[]{e});
JobExecutionResult len$ = context.result().message(e.getMessage()).failed();
return len$;
}
finally {
if (session != null) {
session.logout();
}
}
return context.result().succeeded();
}
protected void bindVersionManager(VersionManager versionManager) {
this.versionManager = versionManager;
}
protected void unbindVersionManager(VersionManager versionManager) {
if (this.versionManager == versionManager) {
this.versionManager = null;
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
}