All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.credentials.provider.SessionCredentialsProvider Maven / Gradle / Ivy

Go to download

Alibaba Cloud Credentials for Java Copyright (C) Alibaba Cloud Computing All rights reserved. 版权所有 (C)阿里云计算有限公司 https://www.aliyun.com

There is a newer version: 0.3.12
Show newest version
package com.aliyun.credentials.provider;

import com.aliyun.credentials.models.CredentialModel;

import java.util.Date;
import java.util.concurrent.Callable;

public abstract class SessionCredentialsProvider implements AlibabaCloudCredentialsProvider, AutoCloseable {
    private final boolean asyncCredentialUpdateEnabled;
    private RefreshCachedSupplier credentialsCache;
    private final Callable> refreshCallable = new Callable>() {
        @Override
        public RefreshResult call() throws Exception {
            return refreshCredentials();
        }
    };

    public abstract RefreshResult refreshCredentials();

    protected SessionCredentialsProvider(BuilderImpl builder) {
        this.asyncCredentialUpdateEnabled = builder.asyncCredentialUpdateEnabled;
        this.credentialsCache = RefreshCachedSupplier.builder(refreshCallable)
                .asyncUpdateEnabled(this.asyncCredentialUpdateEnabled)
                .build();
    }

    public long getStaleTime(long expiration) {
        return expiration <= 0 ?
                new Date().getTime() + 60 * 60 * 1000
                : expiration - 3 * 60 * 1000;
    }

    @Override
    public CredentialModel getCredentials() {
        return credentialsCache.get();
    }

    public boolean isAsyncCredentialUpdateEnabled() {
        return asyncCredentialUpdateEnabled;
    }

    @Override
    public void close() {
        if (null != credentialsCache) {
            credentialsCache.close();
        }
    }

    public interface Builder {
        BuilderT asyncCredentialUpdateEnabled(Boolean asyncCredentialUpdateEnabled);

        ProviderT build();
    }

    protected abstract static class BuilderImpl
            implements Builder {
        private boolean asyncCredentialUpdateEnabled = false;

        protected BuilderImpl() {
        }

        @Override
        public BuilderT asyncCredentialUpdateEnabled(Boolean asyncCredentialUpdateEnabled) {
            this.asyncCredentialUpdateEnabled = asyncCredentialUpdateEnabled;
            return (BuilderT) this;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy