|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.awssdkv2; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | +import java.util.function.Function; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import com.typesafe.config.Config; |
| 13 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 14 | +import io.jooby.Extension; |
| 15 | +import io.jooby.Jooby; |
| 16 | +import io.jooby.ServiceRegistry; |
| 17 | +import software.amazon.awssdk.auth.credentials.*; |
| 18 | +import software.amazon.awssdk.utils.SdkAutoCloseable; |
| 19 | + |
| 20 | +/** |
| 21 | + * Aws module for aws-java-sdk 2.x. This module: |
| 22 | + * |
| 23 | + * <p>- Integrates AWS credentials within application properties. |
| 24 | + * |
| 25 | + * <p>- Register AWS services as application services (so they can be used by require calls or DI). |
| 26 | + * |
| 27 | + * <p>- Add graceful shutdown to any {@link SdkAutoCloseable} instance. |
| 28 | + * |
| 29 | + * <p>Usage: |
| 30 | + * |
| 31 | + * <pre>{@code |
| 32 | + * { |
| 33 | + * install( |
| 34 | + * new AwsModule() |
| 35 | + * .setup(credentials -> { |
| 36 | + * var s3 = S3Client.builder().region(Region.US_EAST_1).build(); |
| 37 | + * var s3transfer = S3TransferManager.builder().s3Client(s3).build() |
| 38 | + * return Stream.of(s3, s3transfer); |
| 39 | + * }) |
| 40 | + * ); |
| 41 | + * } |
| 42 | + * }</pre> |
| 43 | + * |
| 44 | + * <p>Previous example register AmazonS3Client and TransferManager services |
| 45 | + * |
| 46 | + * <p>NOTE: You need to add the service dependencies to your project. |
| 47 | + * |
| 48 | + * @author edgar |
| 49 | + * @since 3.3.1 |
| 50 | + */ |
| 51 | +public class AwsModule implements Extension { |
| 52 | + private final AwsCredentialsProvider credentialsProvider; |
| 53 | + private final List<Function<AwsCredentialsProvider, Object>> factoryList = new ArrayList<>(); |
| 54 | + |
| 55 | + public AwsModule(@NonNull AwsCredentialsProvider credentialsProvider) { |
| 56 | + this.credentialsProvider = credentialsProvider; |
| 57 | + } |
| 58 | + |
| 59 | + public AwsModule() { |
| 60 | + this.credentialsProvider = null; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Setup a new AWS service. Supported outputs are: |
| 65 | + * |
| 66 | + * <p>- Single amazon service - Stream of amazon services - Collection of amazon services |
| 67 | + * |
| 68 | + * <p>Each of the services returned by this function are added to the application service registry |
| 69 | + * and shutdown at application shutdown time. |
| 70 | + * |
| 71 | + * @param provider Service provider/factory. |
| 72 | + * @return AWS service. |
| 73 | + */ |
| 74 | + public @NonNull AwsModule setup(@NonNull Function<AwsCredentialsProvider, Object> provider) { |
| 75 | + factoryList.add(provider); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void install(@NonNull Jooby application) throws Exception { |
| 81 | + var config = application.getConfig(); |
| 82 | + var credentialsProvider = |
| 83 | + Optional.ofNullable(this.credentialsProvider) |
| 84 | + .orElseGet(() -> newCredentialsProvider(config)); |
| 85 | + |
| 86 | + var serviceList = new ArrayList<>(factoryList.size()); |
| 87 | + for (var factory : factoryList) { |
| 88 | + Object value = factory.apply(credentialsProvider); |
| 89 | + if (value instanceof Stream values) { |
| 90 | + values.forEach(serviceList::add); |
| 91 | + } else if (value instanceof Iterable values) { |
| 92 | + values.forEach(serviceList::add); |
| 93 | + } else { |
| 94 | + serviceList.add(value); |
| 95 | + } |
| 96 | + } |
| 97 | + ServiceRegistry services = application.getServices(); |
| 98 | + // for each service |
| 99 | + for (Object service : serviceList) { |
| 100 | + Stream.of( |
| 101 | + service.getClass(), |
| 102 | + Stream.of(service.getClass().getInterfaces()).findFirst().orElse(null)) |
| 103 | + .filter(Objects::nonNull) |
| 104 | + .forEach(serviceType -> services.putIfAbsent((Class) serviceType, service)); |
| 105 | + } |
| 106 | + serviceList.stream() |
| 107 | + .filter(SdkAutoCloseable.class::isInstance) |
| 108 | + .map(SdkAutoCloseable.class::cast) |
| 109 | + .forEach(application::onStop); |
| 110 | + serviceList.clear(); |
| 111 | + factoryList.clear(); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Creates a credentials provider, exactly like {@link DefaultCredentialsProvider#create()} |
| 116 | + * appending the application properties provider. |
| 117 | + * |
| 118 | + * @param config Application properties. |
| 119 | + * @return Credentials provider. |
| 120 | + */ |
| 121 | + public static @NonNull AwsCredentialsProvider newCredentialsProvider(@NonNull Config config) { |
| 122 | + return AwsCredentialsProviderChain.of( |
| 123 | + DefaultCredentialsProvider.create(), new ConfigCredentialsProvider(config)); |
| 124 | + } |
| 125 | +} |
0 commit comments