TokenManager.java
1.97 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TokenManager {
private final Logger log;
private final Object sync;
private final Set<String> tokens;
public TokenManager() {
this.log = LoggerFactory.getLogger(this.getClass());
this.sync = new Object();
this.tokens = new HashSet<String>();
}
public boolean aquireToken(String tokenKey, long timeoutMillis) throws InterruptedException {
long cutoffTime = System.currentTimeMillis() + timeoutMillis;
this.log.debug("Trying to aquire token {}", (Object)tokenKey);
boolean result = this.tryLock0(tokenKey, cutoffTime);
this.log.debug("Aquire result for token {} is {}", (Object)tokenKey, (Object)result);
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private boolean tryLock0(String campaignPath, long cutoffTime) throws InterruptedException {
Object object = this.sync;
synchronized (object) {
while (this.tokens.contains(campaignPath) && System.currentTimeMillis() < cutoffTime) {
this.sync.wait(cutoffTime - System.currentTimeMillis());
}
if (this.tokens.contains(campaignPath)) {
return false;
}
this.tokens.add(campaignPath);
return true;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void releaseToken(String campaignPath) {
this.log.debug("Releasing token {}", (Object)campaignPath);
Object object = this.sync;
synchronized (object) {
this.tokens.remove(campaignPath);
this.sync.notifyAll();
}
}
}