TokenCleanupTask.java
5.18 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.security.user.UserManagementService
* javax.jcr.InvalidItemStateException
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.crx.security.token.impl;
import com.adobe.granite.security.user.UserManagementService;
import com.day.crx.security.token.impl.helper.AllTokensIterator;
import com.day.crx.security.token.impl.helper.Token;
import java.util.Calendar;
import javax.jcr.InvalidItemStateException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe CRX Token Authentication Handler: Cleanup Task", description="Task to regularly purge expired tokens from the repository")
@Service(value={Runnable.class})
@Properties(value={@Property(name="scheduler.expression", value={"23 17 * * * ?"}, label="Schedule", description="Cron expression scheudling this job. Default is hourly 17m23s after the hour. See http://www.docjar.com/docs/api/org/quartz/CronTrigger.html for a description of the format for this value."), @Property(name="scheduler.runOn", value={"LEADER"}, propertyPrivate=1), @Property(name="service.description", value={"Periodic Cleanup Job"}, propertyPrivate=1), @Property(name="service.vendor", value={"Adobe Systems Incorporated"}, propertyPrivate=1)})
public class TokenCleanupTask
implements Runnable {
private final Logger log;
@Reference
private SlingRepository repository;
@Reference
private UserManagementService userManagementService;
public TokenCleanupTask() {
this.log = LoggerFactory.getLogger(this.getClass());
}
public void run() {
this.log.debug("TokenCleanupTask: Starting cleanup");
this.cleanup();
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private void cleanup() {
long start;
int numLive;
int numRemoved;
block11 : {
start = System.currentTimeMillis();
numRemoved = 0;
numLive = 0;
Session admin = null;
try {
admin = this.repository.loginAdministrative(null);
long currentTime = System.currentTimeMillis();
AllTokensIterator tokens = new AllTokensIterator(admin, this.userManagementService);
while (tokens.hasNext()) {
Token token = tokens.next();
if (token.getExpiry().getTimeInMillis() < currentTime) {
this.log.debug("TokenCleanupTask: Removing token {} of user {}", (Object)token.getIdentifier(), (Object)tokens.getCurrentUserName());
token.remove();
++numRemoved;
continue;
}
++numLive;
}
if (!admin.hasPendingChanges()) break block11;
try {
admin.refresh(true);
admin.save();
}
catch (InvalidItemStateException iise) {
this.log.info("TokenCleanupTask: Concurrent modification to one or more of the tokens to be removed. Retrying later");
}
catch (RepositoryException re) {
this.log.info("TokenCleanupTask: Failed persisting token removal. Retrying later");
}
}
catch (Throwable t) {
this.log.error("TokenCleanupTask: General failure while trying to cleanup tokens", t);
}
finally {
if (admin != null) {
admin.logout();
}
}
}
long end = System.currentTimeMillis();
this.log.info("TokenCleanupTask: Removed {} token(s) in {}ms ({} token(s) still active)", new Object[]{numRemoved, end - start, numLive});
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
protected void bindUserManagementService(UserManagementService userManagementService) {
this.userManagementService = userManagementService;
}
protected void unbindUserManagementService(UserManagementService userManagementService) {
if (this.userManagementService == userManagementService) {
this.userManagementService = null;
}
}
}