Search in sources :

Example 1 with COSCredentialsProvider

use of com.qcloud.cos.auth.COSCredentialsProvider in project hadoop-cos by tencentyun.

the class COSCredentialProviderList method close.

@Override
public void close() throws Exception {
    if (this.closed()) {
        return;
    }
    int remainder = this.refCount.decrementAndGet();
    if (remainder != 0) {
        return;
    }
    this.isClosed.set(true);
    for (COSCredentialsProvider provider : this.providers) {
        if (provider instanceof Closeable) {
            ((Closeable) provider).close();
        }
    }
}
Also used : COSCredentialsProvider(com.qcloud.cos.auth.COSCredentialsProvider) Closeable(java.io.Closeable)

Example 2 with COSCredentialsProvider

use of com.qcloud.cos.auth.COSCredentialsProvider in project hadoop-cos by tencentyun.

the class CosNUtils method createCOSCredentialProvider.

/**
 * create cos cred
 * @param uri cos uri
 * @param conf config
 * @param credClass cred class
 * @return provider
 * @throws IOException
 */
public static COSCredentialsProvider createCOSCredentialProvider(URI uri, Configuration conf, Class<?> credClass) throws IOException {
    COSCredentialsProvider credentialsProvider;
    if (!COSCredentialsProvider.class.isAssignableFrom(credClass)) {
        throw new IllegalArgumentException("class " + credClass + " " + NOT_COS_CREDENTIAL_PROVIDER);
    }
    if (Modifier.isAbstract(credClass.getModifiers())) {
        throw new IllegalArgumentException("class " + credClass + " " + ABSTRACT_CREDENTIAL_PROVIDER);
    }
    LOG.debug("Credential Provider class: " + credClass.getName());
    try {
        // new credClass()
        Constructor constructor = getConstructor(credClass);
        if (constructor != null) {
            credentialsProvider = (COSCredentialsProvider) constructor.newInstance();
            return credentialsProvider;
        }
        // new credClass(conf)
        constructor = getConstructor(credClass, Configuration.class);
        if (null != constructor) {
            credentialsProvider = (COSCredentialsProvider) constructor.newInstance(conf);
            return credentialsProvider;
        }
        // new credClass(uri, conf)
        constructor = getConstructor(credClass, URI.class, Configuration.class);
        if (null != constructor) {
            credentialsProvider = (COSCredentialsProvider) constructor.newInstance(uri, conf);
            return credentialsProvider;
        }
        Method factory = getFactoryMethod(credClass, COSCredentialsProvider.class, "getInstance");
        if (null != factory) {
            credentialsProvider = (COSCredentialsProvider) factory.invoke(null);
            return credentialsProvider;
        }
        throw new IllegalArgumentException("Not supported constructor or factory method found");
    } catch (IllegalAccessException e) {
        throw new IOException(credClass.getName() + " " + INSTANTIATION_EXCEPTION + ": " + e, e);
    } catch (InstantiationException e) {
        throw new IOException(credClass.getName() + " " + INSTANTIATION_EXCEPTION + ": " + e, e);
    } catch (InvocationTargetException e) {
        Throwable targetException = e.getTargetException();
        if (targetException == null) {
            targetException = e;
        }
        throw new IOException(credClass.getName() + " " + INSTANTIATION_EXCEPTION + ": " + targetException, targetException);
    }
}
Also used : COSCredentialsProvider(com.qcloud.cos.auth.COSCredentialsProvider) Configuration(org.apache.hadoop.conf.Configuration) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method) IOException(java.io.IOException) URI(java.net.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with COSCredentialsProvider

use of com.qcloud.cos.auth.COSCredentialsProvider in project cos-java-sdk-v5 by tencentyun.

the class AbstractCOSClientTest method buildCVMInstanceCredentialsCOSClient.

protected static COSClient buildCVMInstanceCredentialsCOSClient() {
    InstanceMetadataCredentialsEndpointProvider endpointProvider = new InstanceMetadataCredentialsEndpointProvider(InstanceMetadataCredentialsEndpointProvider.Instance.CVM);
    InstanceCredentialsFetcher instanceCredentialsFetcher = new InstanceCredentialsFetcher(endpointProvider);
    COSCredentialsProvider cosCredentialsProvider = new InstanceCredentialsProvider(instanceCredentialsFetcher);
    clientConfig = new ClientConfig(new Region(region));
    return new COSClient(cosCredentialsProvider, clientConfig);
}
Also used : InstanceMetadataCredentialsEndpointProvider(com.qcloud.cos.auth.InstanceMetadataCredentialsEndpointProvider) InstanceCredentialsFetcher(com.qcloud.cos.auth.InstanceCredentialsFetcher) COSCredentialsProvider(com.qcloud.cos.auth.COSCredentialsProvider) InstanceCredentialsProvider(com.qcloud.cos.auth.InstanceCredentialsProvider) Region(com.qcloud.cos.region.Region)

Example 4 with COSCredentialsProvider

use of com.qcloud.cos.auth.COSCredentialsProvider in project cos-java-sdk-v5 by tencentyun.

the class AbstractCOSClientTest method buildCPMInstanceCredentialsCOSClient.

protected static COSClient buildCPMInstanceCredentialsCOSClient() {
    InstanceMetadataCredentialsEndpointProvider endpointProvider = new InstanceMetadataCredentialsEndpointProvider(InstanceMetadataCredentialsEndpointProvider.Instance.CPM);
    InstanceCredentialsFetcher instanceCredentialsFetcher = new InstanceCredentialsFetcher(endpointProvider);
    COSCredentialsProvider cosCredentialsProvider = new InstanceCredentialsProvider(instanceCredentialsFetcher);
    clientConfig = new ClientConfig(new Region(region));
    return new COSClient(cosCredentialsProvider, clientConfig);
}
Also used : InstanceMetadataCredentialsEndpointProvider(com.qcloud.cos.auth.InstanceMetadataCredentialsEndpointProvider) InstanceCredentialsFetcher(com.qcloud.cos.auth.InstanceCredentialsFetcher) COSCredentialsProvider(com.qcloud.cos.auth.COSCredentialsProvider) InstanceCredentialsProvider(com.qcloud.cos.auth.InstanceCredentialsProvider) Region(com.qcloud.cos.region.Region)

Aggregations

COSCredentialsProvider (com.qcloud.cos.auth.COSCredentialsProvider)4 InstanceCredentialsFetcher (com.qcloud.cos.auth.InstanceCredentialsFetcher)2 InstanceCredentialsProvider (com.qcloud.cos.auth.InstanceCredentialsProvider)2 InstanceMetadataCredentialsEndpointProvider (com.qcloud.cos.auth.InstanceMetadataCredentialsEndpointProvider)2 Region (com.qcloud.cos.region.Region)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 Configuration (org.apache.hadoop.conf.Configuration)1