package ca.firstvoices.cognito;

import ca.firstvoices.cognito.exceptions.MiscellaneousFailureException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.runtime.api.Framework;
import org.nuxeo.runtime.model.ComponentInstance;
import org.nuxeo.runtime.model.DefaultComponent;

public class AWSAuthenticationServiceFactory extends DefaultComponent {
  private static Log LOG = LogFactory.getLog(AWSAuthenticationServiceFactory.class);


  private AWSAuthenticationServiceConfigurationDescriptor config;
  private AWSAuthenticationService authenticationService;

  @Override public <T> T getAdapter(Class<T> adapter) {

    if (AWSAuthenticationService.class.isAssignableFrom(adapter)) {
      if (this.authenticationService == null) {
        LOG.warn("AWSAuthenticationService instance requested but we are not configured");
      }
      return (T) this.authenticationService;
    }
    return null;
  }

  @Override
  public void registerContribution(Object contribution, String xp, ComponentInstance component) {

    boolean awsAuthenticationEnabled =
        Framework.getService(AWSAwareUserManagerConfigurationService.class)
            .getConfig().authenticateWithCognito;

    if (awsAuthenticationEnabled) {
      if ("configuration".equals(xp)) {
        LOG.info("Configuration loaded: " + contribution.toString());

        this.config = (AWSAuthenticationServiceConfigurationDescriptor) contribution;

        this.authenticationService = new AWSAuthenticationServiceImpl(this.config.accessKey,
            this.config.secretKey,
            this.config.userPool,
            this.config.region,
            this.config.clientID);
        try {
          this.authenticationService.testConnection();
        } catch (MiscellaneousFailureException e) {
          LOG.warn("An exception occurred while testing the connection. AWS Cognito authentication"
              + " will not work", e);
        }
      }

      super.registerContribution(contribution, xp, component);
    }
  }

  @Override
  public void unregisterContribution(Object contribution, String xp, ComponentInstance component) {
    if ("configuration".equals(xp)) {
      this.config = null;
      this.authenticationService = null;
    }
    super.unregisterContribution(contribution, xp, component);
  }


}

@@ -187,48 +187,8 @@
 ry;%0A
-import org.nuxeo.runtime.api.Framework;%0A
 impo
@@ -1027,212 +1027,8 @@
  %7B%0A%0A
-    boolean awsAuthenticationEnabled =%0A        Framework.getService(AWSAwareUserManagerConfigurationService.class)%0A            .getConfig().authenticateWithCognito;%0A%0A    if (awsAuthenticationEnabled) %7B%0A  
     
@@ -1057,26 +1057,24 @@
 uals(xp)) %7B%0A
-  
       LOG.in
@@ -1132,26 +1132,24 @@
 ());%0A%0A      
- 
- 
 this.config 
@@ -1215,18 +1215,16 @@
 ution;%0A%0A
-  
       th
@@ -1308,34 +1308,32 @@
 sKey,%0A          
-  
 this.config.secr
@@ -1335,26 +1335,24 @@
 .secretKey,%0A
-  
           th
@@ -1373,34 +1373,32 @@
 Pool,%0A          
-  
 this.config.regi
@@ -1401,18 +1401,16 @@
 region,%0A
-  
         
@@ -1444,18 +1444,14 @@
     
-  
 try %7B%0A
-  
     
@@ -1505,18 +1505,16 @@
 ;%0A      
-  
 %7D catch 
@@ -1553,26 +1553,24 @@
 ) %7B%0A        
- 
- 
 LOG.warn(%22An
@@ -1659,18 +1659,16 @@
         
-  
 + %22 will
@@ -1685,30 +1685,26 @@
 , e);%0A      
-  %7D%0A  
+%7D%0A
     %7D%0A%0A     
@@ -1702,18 +1702,16 @@
  %7D%0A%0A    
- 
- 
 super.re
@@ -1759,22 +1759,16 @@
 onent);%0A
-    %7D%0A
   %7D%0A%0A  @

Revert commit that broke Cognito (#670)