View Javadoc
1   package com.meltmedia.jgroups.aws;
2   
3   import com.amazonaws.auth.AWSCredentialsProvider;
4   import org.jgroups.logging.Log;
5   import org.jgroups.logging.LogFactory;
6   import org.jgroups.util.Util;
7   
8   public class CredentialsProviderFactory {
9     private final Log log;
10  
11    public CredentialsProviderFactory() {
12      this(LogFactory.getLog(AWS_PING.class));
13    }
14  
15    public CredentialsProviderFactory(final Log log) {
16      this.log = log;
17    }
18  
19    /**
20     * Loads a new instance of the credential provider, using the same class loading rules from org.jgroups.Util.loadClass(String, Class).
21     * 
22     * @param credentialProviderClass the class name of the AWSCredentialsProvider to load.
23     * @return an instance of the credential provider
24     * @throws ClassNotFoundException if the implementation could not be found.
25     * @throws InstantiationException if the implementation does not have a no argument constructor.
26     */
27    public AWSCredentialsProvider createCredentialsProvider(final String credentialProviderClass) throws Exception {
28      try {
29        final Class<?> credsProviderClazz = Util.loadClass(credentialProviderClass, getClass());
30        return (AWSCredentialsProvider) credsProviderClazz.newInstance();
31      } catch (ClassNotFoundException e) {
32        throw new Exception("unable to load credentials provider class " + credentialProviderClass);
33      } catch (InstantiationException e) {
34        log.error("an instance of " + credentialProviderClass + " could not be created. Please check that it implements" +
35            " interface AWSCredentialsProvider and that is has a public empty constructor !");
36        throw e;
37      }
38    }
39  }