TokenManager.java 1.97 KB
/*
 * 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();
        }
    }
}