|
| 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.aws; |
| 7 | + |
| 8 | +import com.amazonaws.auth.AWSCredentialsProvider; |
| 9 | +import com.amazonaws.auth.AWSCredentialsProviderChain; |
| 10 | +import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper; |
| 11 | +import com.amazonaws.auth.EnvironmentVariableCredentialsProvider; |
| 12 | +import com.amazonaws.auth.SystemPropertiesCredentialsProvider; |
| 13 | +import com.amazonaws.auth.WebIdentityTokenCredentialsProvider; |
| 14 | +import com.amazonaws.auth.profile.ProfileCredentialsProvider; |
| 15 | +import com.amazonaws.services.s3.transfer.TransferManager; |
| 16 | +import com.typesafe.config.Config; |
| 17 | +import io.jooby.Extension; |
| 18 | +import io.jooby.Jooby; |
| 19 | +import io.jooby.ServiceRegistry; |
| 20 | +import io.jooby.internal.aws.ConfigCredentialsProvider; |
| 21 | +import io.jooby.internal.aws.ServiceShutdown; |
| 22 | + |
| 23 | +import javax.annotation.Nonnull; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.function.Consumer; |
| 29 | +import java.util.function.Function; |
| 30 | +import java.util.stream.Stream; |
| 31 | + |
| 32 | +/** |
| 33 | + * Aws module for aws-java-sdk 1.x. This module: |
| 34 | + * |
| 35 | + * - Integrates AWS credentials within application properties. |
| 36 | + * - Register AWS services as application services (so they can be used by require calls or DI). |
| 37 | + * - Add graceful shutdown to AWS services. |
| 38 | + * |
| 39 | + * Usage: |
| 40 | + * |
| 41 | + * <pre>{@code |
| 42 | + * { |
| 43 | + * install( |
| 44 | + * new AwsModule() |
| 45 | + * .setup(credentials -> { |
| 46 | + * return TransferManagerBuilder.standard() |
| 47 | + * .withS3Client( |
| 48 | + * AmazonS3ClientBuilder.standard() |
| 49 | + * .withRegion(Regions.US_EAST_1) |
| 50 | + * .withCredentials(credentials) |
| 51 | + * .build() |
| 52 | + * ).build(); |
| 53 | + * }) |
| 54 | + * ); |
| 55 | + * } |
| 56 | + * }</pre> |
| 57 | + * |
| 58 | + * <p>Previous example register AmazonS3Client and TransferManager services</p> |
| 59 | + * |
| 60 | + * <p>NOTE: You need to add the required service dependency to your project.</p> |
| 61 | + * |
| 62 | + * @author edgar |
| 63 | + */ |
| 64 | +public class AwsModule implements Extension { |
| 65 | + |
| 66 | + private List<Function<AWSCredentialsProvider, Object>> factoryList = new ArrayList<>(); |
| 67 | + |
| 68 | + /** |
| 69 | + * Setup a new AWS service. Supported outputs are: |
| 70 | + * |
| 71 | + * - Single amazon service |
| 72 | + * - Stream of amazon services |
| 73 | + * - Collection of amazon services |
| 74 | + * |
| 75 | + * Each of the services returned by this function are added to the application service registry |
| 76 | + * and shutdown at application shutdown time. |
| 77 | + * |
| 78 | + * @param provider Service provider/factory. |
| 79 | + * @return AWS service. |
| 80 | + */ |
| 81 | + public @Nonnull AwsModule setup(@Nonnull Function<AWSCredentialsProvider, Object> provider) { |
| 82 | + factoryList.add(provider); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + @Override public void install(@Nonnull Jooby application) throws Exception { |
| 87 | + AWSCredentialsProvider credentialsProvider = newCredentialsProvider(application.getConfig()); |
| 88 | + List<Object> serviceList = new ArrayList<>(factoryList.size()); |
| 89 | + for (Function<AWSCredentialsProvider, Object> factory : factoryList) { |
| 90 | + Object value = factory.apply(credentialsProvider); |
| 91 | + if (value instanceof Stream) { |
| 92 | + ((Stream) value).forEach(serviceList::add); |
| 93 | + } else if (value instanceof Collection) { |
| 94 | + ((Collection) value).forEach(serviceList::add); |
| 95 | + } else { |
| 96 | + extractServices(value, serviceList::add); |
| 97 | + } |
| 98 | + } |
| 99 | + ServiceRegistry services = application.getServices(); |
| 100 | + // for each service |
| 101 | + for (Object service : serviceList) { |
| 102 | + Stream.of(service.getClass(), |
| 103 | + Stream.of(service.getClass().getInterfaces()).findFirst().orElse(null)) |
| 104 | + .filter(Objects::nonNull) |
| 105 | + .forEach(serviceType -> { |
| 106 | + services.putIfAbsent((Class) serviceType, service); |
| 107 | + }); |
| 108 | + } |
| 109 | + serviceList.stream() |
| 110 | + .distinct() |
| 111 | + .forEach(service -> application.onStop(new ServiceShutdown(application.getLog(), service))); |
| 112 | + serviceList.clear(); |
| 113 | + factoryList.clear(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Creates a credentials provider, exactly like |
| 118 | + * {@link com.amazonaws.auth.DefaultAWSCredentialsProviderChain} appending the application |
| 119 | + * properties provider. |
| 120 | + * |
| 121 | + * @param config Application properties. |
| 122 | + * @return Credentials provider. |
| 123 | + */ |
| 124 | + public static @Nonnull AWSCredentialsProvider newCredentialsProvider(@Nonnull Config config) { |
| 125 | + return new AWSCredentialsProviderChain( |
| 126 | + new EnvironmentVariableCredentialsProvider(), |
| 127 | + new SystemPropertiesCredentialsProvider(), |
| 128 | + WebIdentityTokenCredentialsProvider.create(), |
| 129 | + new ProfileCredentialsProvider(), |
| 130 | + new EC2ContainerCredentialsProviderWrapper(), |
| 131 | + // Application configuration |
| 132 | + new ConfigCredentialsProvider(config) |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + private void extractServices(Object value, Consumer<Object> consumer) { |
| 137 | + transferManager(value, consumer); |
| 138 | + consumer.accept(value); |
| 139 | + } |
| 140 | + |
| 141 | + private void transferManager(Object value, Consumer<Object> consumer) { |
| 142 | + try { |
| 143 | + if (value instanceof TransferManager) { |
| 144 | + consumer.accept(((TransferManager) value).getAmazonS3Client()); |
| 145 | + } |
| 146 | + } catch (NoClassDefFoundError x) { |
| 147 | + // s3 dependency is optional |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments