// 
// Decompiled by Procyon v0.6.0
// 

package com.nimbusds.jose.jwk.source;

import java.util.Objects;
import com.nimbusds.jose.KeySourceException;
import com.nimbusds.jose.util.events.Event;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.util.events.EventListener;
import com.nimbusds.jose.shaded.jcip.ThreadSafe;
import com.nimbusds.jose.proc.SecurityContext;

@ThreadSafe
public class RetryingJWKSetSource<C extends SecurityContext> extends JWKSetSourceWrapper<C>
{
    private final EventListener<RetryingJWKSetSource<C>, C> eventListener;
    
    public RetryingJWKSetSource(final JWKSetSource<C> source, final EventListener<RetryingJWKSetSource<C>, C> eventListener) {
        super(source);
        this.eventListener = eventListener;
    }
    
    @Override
    public JWKSet getJWKSet(final JWKSetCacheRefreshEvaluator refreshEvaluator, final long currentTime, final C context) throws KeySourceException {
        try {
            return this.getSource().getJWKSet(refreshEvaluator, currentTime, context);
        }
        catch (final JWKSetUnavailableException e) {
            if (this.eventListener != null) {
                this.eventListener.notify((Event<RetryingJWKSetSource<C>, C>)new RetrialEvent(this, (Exception)e, (SecurityContext)context));
            }
            return this.getSource().getJWKSet(refreshEvaluator, currentTime, context);
        }
    }
    
    public static class RetrialEvent<C extends SecurityContext> extends AbstractJWKSetSourceEvent<RetryingJWKSetSource<C>, C>
    {
        private final Exception exception;
        
        private RetrialEvent(final RetryingJWKSetSource<C> source, final Exception exception, final C securityContext) {
            super(source, securityContext);
            Objects.requireNonNull(exception);
            this.exception = exception;
        }
        
        public Exception getException() {
            return this.exception;
        }
    }
}
