// Copyright (C) 2008, 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.connector.manager;

import com.google.common.collect.ImmutableMap;
import com.google.enterprise.connector.instantiator.InstantiatorException;
import com.google.enterprise.connector.persist.ConnectorNotFoundException;
import com.google.enterprise.connector.persist.PersistentStoreException;
import com.google.enterprise.connector.spi.AuthenticationIdentity;
import com.google.enterprise.connector.spi.AuthenticationManager;
import com.google.enterprise.connector.spi.AuthenticationResponse;
import com.google.enterprise.saml.server.BackEnd;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This class is temporary -- while the connector manager and the security
 * manager are different builds. This gives the security manager a connector
 * manager with an implementation for the overloading of authenticate() that
 * includes a security context.
 *
 * This functionality will eventually be merged with the connector manager's
 * {@link ProductionManager}.
 */
public class ConnectorManager extends ProductionManager {

  private static final Logger LOGGER = Logger.getLogger(ConnectorManager.class.getName());

  private BackEnd backEnd;
  private boolean securityManagerConnectorsInitialized;

  public ConnectorManager() {
    super();
    securityManagerConnectorsInitialized = false;
  }

  public BackEnd getBackEnd() {
    return backEnd;
  }

  public void setBackEnd(BackEnd backEnd) {
    backEnd.setConnectorManager(this);
    this.backEnd = backEnd;
  }

  @Override
  public boolean authenticate(String connectorName, 
      AuthenticationIdentity identity) {
    throw new UnsupportedOperationException();
  }

  /**
   * This method will become part of the {@link Manager} interface
   */
  public AuthenticationResponse authenticate(String connectorName, AuthenticationIdentity id,
      SecAuthnContext securityContext) {
    AuthenticationManager authnManager = null;
    try {
      authnManager = instantiator.getAuthenticationManager(connectorName);
    } catch (ConnectorNotFoundException e) {
      LOGGER.log(Level.WARNING, "Connector " + connectorName + " Not Found: ", e);
    } catch (InstantiatorException e) {
      LOGGER.log(Level.WARNING, "Instantiator: ", e);
    }

    // Some connectors don't implement the AuthenticationManager interface, or
    // there may been an instantiation problem.
    if (authnManager == null) {
      return null;
    }

    AuthnCaller authnCaller = new AuthnCaller(authnManager, id, securityContext);

    return authnCaller.authenticate();
  }

  @Override
  public List<ConnectorStatus> getConnectorStatuses() {
    if (!securityManagerConnectorsInitialized) {
      try {
        this.setConnectorConfig("BasicAuth", "BasicAuthConnector",
                                ImmutableMap.of("ServerUrl", "foo"),  // dummy parameter
                                "en", false);
        this.setConnectorConfig("FormAuth", "FormAuthConnector",
                                ImmutableMap.of("CookieName", "bar"),  // dummy parameter
                                "en", false);
        this.setConnectorConfig("ConnAuth", "ConnAuthConnector",
                                ImmutableMap.of("SpiVersion", "0"),
                                "en", false);
        securityManagerConnectorsInitialized = true;
      } catch (ConnectorNotFoundException e) {
        LOGGER.info("ConnectorNotFound: " + e.toString());
      } catch (InstantiatorException e) {
        LOGGER.info("Instantiator: " + e.toString());
      } catch (PersistentStoreException e) {
        LOGGER.info("PersistentStore: " + e.toString());
      }
    }
    List<ConnectorStatus> result = super.getConnectorStatuses();
    return result;
  }
}

@@ -1182,16 +1182,38 @@
 l.List;%0A
+import java.util.Map;%0A
 import j
@@ -1830,162 +1830,8 @@
 End;
-%0A  private boolean securityManagerConnectorsInitialized;%0A%0A  public ConnectorManager() %7B%0A    super();%0A    securityManagerConnectorsInitialized = false;%0A  %7D
 %0A%0A  
@@ -2064,17 +2064,16 @@
 torName,
- 
 %0A      A
@@ -3117,59 +3117,8 @@
 ) %7B%0A
-    if (!securityManagerConnectorsInitialized) %7B%0A  
     
@@ -3125,32 +3125,33 @@
 try %7B%0A      
-  this.s
+checkAndS
 etConnectorC
@@ -3183,32 +3183,33 @@
 AuthConnector%22,%0A
+ 
                 
@@ -3305,32 +3305,33 @@
                 
+ 
 %22en%22, false);%0A  
@@ -3330,32 +3330,33 @@
 lse);%0A      
-  
-this.s
+checkAndS
 etConnectorC
@@ -3386,32 +3386,33 @@
 AuthConnector%22,%0A
+ 
                 
@@ -3509,32 +3509,33 @@
                 
+ 
 %22en%22, false);%0A  
@@ -3542,16 +3542,17 @@
     
-  this.s
+checkAndS
 etCo
@@ -3590,32 +3590,33 @@
 AuthConnector%22,%0A
+ 
                 
@@ -3691,32 +3691,33 @@
                 
+ 
 %22en%22, false);%0A  
@@ -3718,63 +3718,8 @@
 e);%0A
-        securityManagerConnectorsInitialized = true;%0A  
     
@@ -3757,34 +3757,32 @@
 tion e) %7B%0A      
- 
- 
 LOGGER.info(%22Con
@@ -3812,34 +3812,32 @@
 oString());%0A    
-  
 %7D catch (Instant
@@ -3848,34 +3848,32 @@
 rException e) %7B%0A
-  
       LOGGER.inf
@@ -3912,18 +3912,16 @@
 ));%0A    
-  
 %7D catch 
@@ -3947,26 +3947,24 @@
 eption e) %7B%0A
-  
       LOGGER
@@ -4010,16 +4010,8 @@
 ));%0A
-      %7D%0A
     
@@ -4100,10 +4100,576 @@
 lt;%0A  %7D%0A
+%0A  private void checkAndSetConnectorConfig(String connectorName,%0A                                          String connectorTypeName,%0A                                          Map configData, String language,%0A                                          boolean update)%0A      throws PersistentStoreException, InstantiatorException %7B%0A    try %7B%0A      this.getConnectorConfig(connectorName);%0A    %7D catch (ConnectorNotFoundException e) %7B%0A      this.setConnectorConfig(connectorName, connectorTypeName, configData,%0A                              language, update);%0A    %7D%0A  %7D%0A%0A
 %7D%0A

Fix ConnectorManager initialization issue.