203203 */
204204package org .jooby .aws ;
205205
206+ import com .amazonaws .AmazonWebServiceClient ;
207+ import com .amazonaws .auth .AWSCredentialsProvider ;
208+ import com .google .common .collect .ImmutableList ;
209+ import com .google .common .collect .ImmutableList .Builder ;
210+ import com .google .inject .Binder ;
211+ import com .typesafe .config .Config ;
206212import static java .util .Objects .requireNonNull ;
207-
208- import java .util .ArrayList ;
209- import java .util .List ;
210- import java .util .function .BiFunction ;
211- import java .util .function .Function ;
212-
213213import org .jooby .Env ;
214214import org .jooby .Jooby ;
215215import org .jooby .internal .aws .AwsShutdownSupport ;
216- import org .jooby .internal .aws .ConfigCredentialsProvider ;
216+ import org .jooby .internal .aws .CredentialsFactory ;
217+ import org .jooby .internal .aws .ForwardingCredentialsProvider ;
217218import org .slf4j .Logger ;
218219import org .slf4j .LoggerFactory ;
219220
220- import com .amazonaws .AmazonWebServiceClient ;
221- import com .amazonaws .auth .AWSCredentialsProvider ;
222- import com .google .common .collect .ImmutableList ;
223- import com .google .common .collect .ImmutableList .Builder ;
224- import com .google .inject .Binder ;
225- import com .typesafe .config .Config ;
221+ import java .util .ArrayList ;
222+ import java .util .List ;
223+ import java .util .function .BiFunction ;
224+ import java .util .function .Function ;
226225
227226/**
228227 * <h1>aws module</h1>
264263 * </p>
265264 *
266265 * <p>
267- * This module is small and simple. All it does is bind {@link AmazonWebServiceClient} instances in
266+ * This module is small and simple. All it does is bind <code>amazon service</code> instances in
268267 * Guice. It also helps to bind utility classes like <code>TransferManager</code>.
269268 * </p>
270269 *
306305 * );
307306 * </pre>
308307 *
309- * It uses the {@link AmazonWebServiceClient#getServiceName()} method in order to find per service
310- * keys.
308+ * <p>
309+ * The module also install the defaults aws credentials provider.
310+ * Provider precedence is as follows:
311+ * </p>
312+ *
313+ * <ul>
314+ * <li>application *.conf file (application.conf, application.prod.conf, etc...)</li>
315+ * <li>environment (EnvironmentVariableCredentialsProvider)</li>
316+ * <li>jvm system properties (SystemPropertiesCredentialsProvider)</li>
317+ * <li>aws configuration profile (ProfileCredentialsProvider)</li>
318+ * <li>ec2 (EC2ContainerCredentialsProviderWrapper)</li>
319+ * </ul>
311320 *
312321 * @author edgar
313322 * @since 0.7.0
314323 */
315- @ SuppressWarnings ({"rawtypes" , "unchecked" })
324+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
316325public class Aws implements Jooby .Module {
317326
318327 /** The logging system. */
319328 private final Logger log = LoggerFactory .getLogger (getClass ());
320329
321- private Builder <BiFunction <AWSCredentialsProvider , Config , AmazonWebServiceClient >> callbacks =
330+ private Builder <BiFunction <AWSCredentialsProvider , Config , Object >> callbacks =
322331 ImmutableList .builder ();
323332
324333 private List <BiFunction > after = new ArrayList <>();
@@ -336,8 +345,7 @@ public class Aws implements Jooby.Module {
336345 * @param callback A creation callback.
337346 * @return This module.
338347 */
339- public Aws with (
340- final BiFunction <AWSCredentialsProvider , Config , AmazonWebServiceClient > callback ) {
348+ public Aws with (final BiFunction <AWSCredentialsProvider , Config , Object > callback ) {
341349 requireNonNull (callback , "Callback is required." );
342350 callbacks .add (callback );
343351 return this ;
@@ -360,7 +368,7 @@ public Aws with(
360368 * @param callback A creation callback.
361369 * @return This module.
362370 */
363- public Aws with (final Function <AWSCredentialsProvider , AmazonWebServiceClient > callback ) {
371+ public Aws with (final Function <AWSCredentialsProvider , Object > callback ) {
364372 return with ((creds , conf ) -> callback .apply (creds ));
365373 }
366374
@@ -416,9 +424,21 @@ public <T extends AmazonWebServiceClient> Aws doWith(final Function<T, Object> c
416424 public void configure (final Env env , final Config config , final Binder binder ) {
417425
418426 callbacks .build ().forEach (it -> {
419- ConfigCredentialsProvider creds = new ConfigCredentialsProvider (config );
420- AmazonWebServiceClient service = it .apply (creds , config );
421- creds .service (service .getServiceName ());
427+ ForwardingCredentialsProvider fcp = new ForwardingCredentialsProvider ();
428+ Object service = it .apply (fcp , config );
429+ String serviceName ;
430+ if (service instanceof AmazonWebServiceClient ) {
431+ serviceName = ((AmazonWebServiceClient ) service ).getServiceName ();
432+ } else {
433+ serviceName = service .getClass ().getSimpleName ()
434+ .replace ("AWS" , "" )
435+ .replace ("Amazon" , "" )
436+ .replace ("JavaClient" , "" )
437+ .replace ("ServiceClient" , "" )
438+ .replace ("Client" , "" )
439+ .toLowerCase ();
440+ }
441+ fcp .setProvider (CredentialsFactory .create (config , serviceName ));
422442 Class serviceType = service .getClass ();
423443 Class [] interfaces = serviceType .getInterfaces ();
424444 if (interfaces .length > 0 ) {
@@ -432,7 +452,7 @@ public void configure(final Env env, final Config config, final Binder binder) {
432452 }
433453
434454 private void after (final Env env , final Binder binder , final Config config ,
435- final AmazonWebServiceClient service ) {
455+ final Object service ) {
436456 after .forEach (it -> {
437457 try {
438458 Object dep = it .apply (service , config );
0 commit comments