<commit_before>/**
 * Copyright (C) 2000 - 2012 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.silverpeas.authentication;

import com.silverpeas.util.StringUtil;
import com.stratelia.silverpeas.silvertrace.SilverTrace;
import com.stratelia.webactiv.util.ResourceLocator;
import com.stratelia.webactiv.util.exception.SilverpeasException;

import java.util.ArrayList;
import java.util.List;

/**
 * The authentication server is a proxy in Silverpeas side of the external authentication service
 * related to a given user domain. This service is identified by an unique name. The authentication
 * is delegated to an implementation of the Authentication abstract class that knows how to perform
 * the authentication with the remote service.
 *
 * An external authentication service can be backed by one or more remote authentication servers.
 * So, the authentication with each server is then performed by a different Authentication instance
 * of the same type; each Authentication instance is mapped with a given server behind the external
 * authentication service.
 *
 * The correct implementation of the Authentication abstract class is loaded from the properties
 * mapped to an authentication service name. Each service name identifies uniquely an external
 * security service (SQL database, LDAP, NTLM, ...)
 *
 * @author tleroi
 * @author mmoquillon
 */
public class AuthenticationServer {

  private static final String module = "authentication";

  protected String fallbackMode;
  protected List<Authentication> authServers;
  protected boolean passwordChangeAllowed;

  /**
   * Gets the authentication server identified by the specified name.
   *
   * @param serverName the authentication server name.
   * @return the authentication server with the specified name.
   */
  public static AuthenticationServer getAuthenticationServer(String serverName) {
    return new AuthenticationServer(serverName);
  }

  /**
   * Creates an authentication server proxying the external one defined by the specified name.
   * All the settings to communicate with the remote service are then loaded from the properties
   * mapped with the specified server name.
   * @param authServerName an authentication server name.
   */
  private AuthenticationServer(String authServerName) {
    try {
      ResourceLocator serverSettings = new ResourceLocator("org.silverpeas.authentication."
          + authServerName, "");
      fallbackMode = serverSettings.getString("fallbackType");
      passwordChangeAllowed = serverSettings.getBoolean("allowPasswordChange", false);
      int nbServers = Integer.parseInt(serverSettings.getString("autServersCount"));
      authServers = new ArrayList<Authentication>(nbServers);
      for (int i = 0; i < nbServers; i++) {
        String serverName = "autServer" + i;
        if (serverSettings.getBoolean(serverName + ".enabled", true)) {
          try {
            Authentication authenticationWithAServer = (Authentication) Class.forName(
                serverSettings.getString(serverName + ".type")).newInstance();
            authenticationWithAServer.init(serverName, serverSettings);
            authServers.add(authenticationWithAServer);
          } catch (Exception ex) {
            SilverTrace.error(module, "AuthenticationServer.AuthenticationServer",
                "authentication.EX_CANT_INSTANCIATE_SERVER_CLASS",
                authServerName + " / " + serverName, ex);
          }
        }
      }
    } catch (Exception e) {
      SilverTrace.error(module, "AuthenticationServer.AuthenticationServer",
          "authentication.EX_DOMAIN_INFO_ERROR", "Server=" + authServerName, e);
    }
  }<commit_msg>Fix a bug with the load of the resource locator for authentication servers when the properties for an domain authentication is located in the old place (com/stratelia/silverpeas/authentication instead of org/silverpeas/authentication)<commit_after>/**
 * Copyright (C) 2000 - 2012 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.silverpeas.authentication;

import com.silverpeas.util.StringUtil;
import com.stratelia.silverpeas.silvertrace.SilverTrace;
import com.stratelia.webactiv.util.ResourceLocator;
import com.stratelia.webactiv.util.exception.SilverpeasException;

import java.util.ArrayList;
import java.util.List;

/**
 * The authentication server is a proxy in Silverpeas side of the external authentication service
 * related to a given user domain. This service is identified by an unique name. The authentication
 * is delegated to an implementation of the Authentication abstract class that knows how to perform
 * the authentication with the remote service.
 *
 * An external authentication service can be backed by one or more remote authentication servers.
 * So, the authentication with each server is then performed by a different Authentication instance
 * of the same type; each Authentication instance is mapped with a given server behind the external
 * authentication service.
 *
 * The correct implementation of the Authentication abstract class is loaded from the properties
 * mapped to an authentication service name. Each service name identifies uniquely an external
 * security service (SQL database, LDAP, NTLM, ...)
 *
 * @author tleroi
 * @author mmoquillon
 */
public class AuthenticationServer {

  private static final String module = "authentication";

  protected String fallbackMode;
  protected List<Authentication> authServers;
  protected boolean passwordChangeAllowed;

  /**
   * Gets the authentication server identified by the specified name.
   *
   * @param serverName the authentication server name.
   * @return the authentication server with the specified name.
   */
  public static AuthenticationServer getAuthenticationServer(String serverName) {
    return new AuthenticationServer(serverName);
  }

  /**
   * Creates an authentication server proxying the external one defined by the specified name.
   * All the settings to communicate with the remote service are then loaded from the properties
   * mapped with the specified server name.
   * @param authServerName an authentication server name.
   */
  private AuthenticationServer(String authServerName) {
    try {
      ResourceLocator serverSettings = new ResourceLocator("com.stratelia.silverpeas.authentication."
          + authServerName, "");
      fallbackMode = serverSettings.getString("fallbackType");
      passwordChangeAllowed = serverSettings.getBoolean("allowPasswordChange", false);
      int nbServers = Integer.parseInt(serverSettings.getString("autServersCount"));
      authServers = new ArrayList<Authentication>(nbServers);
      for (int i = 0; i < nbServers; i++) {
        String serverName = "autServer" + i;
        if (serverSettings.getBoolean(serverName + ".enabled", true)) {
          try {
            Authentication authenticationWithAServer = (Authentication) Class.forName(
                serverSettings.getString(serverName + ".type")).newInstance();
            authenticationWithAServer.init(serverName, serverSettings);
            authServers.add(authenticationWithAServer);
          } catch (Exception ex) {
            SilverTrace.error(module, "AuthenticationServer.AuthenticationServer",
                "authentication.EX_CANT_INSTANCIATE_SERVER_CLASS",
                authServerName + " / " + serverName, ex);
          }
        }
      }
    } catch (Exception e) {
      SilverTrace.error(module, "AuthenticationServer.AuthenticationServer",
          "authentication.EX_DOMAIN_INFO_ERROR", "Server=" + authServerName, e);
    }
  }
{'fraction_non_alphanumeric': 0.07080374601214366, 'fraction_numerical': 0.002469898116702686, 'mean_word_length': 3.8736208625877633, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '142499', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2344, 'n_words': 953}