/*
  * (non-Javadoc)
  * @see sif3.infra.common.interfaces.EnvironmentManager#getSessionBySecurityToken(java.lang.String)
  */
 @Override
 public SIF3Session getSessionBySecurityToken(String securityToken) {
   if ((getSIF3Session() != null) && (StringUtils.notEmpty(securityToken))) {
     if (securityToken.equals(getSIF3Session().getSecurityToken())) {
       return getSIF3Session();
     } else {
       logger.error(
           "The securityToken for the session in this Provider Environment Manager ("
               + getSIF3Session().getSecurityToken()
               + ") is different to the securityToken from the request ("
               + securityToken
               + "). User is not allowed to access this session.");
       return null;
     }
   }
   return null; // no session or invalid sessionToken
 }
  /*
   * This method load information in relation to existing environments. If bits are missing or don't make sense then
   * an error is logged and false is returned.
   */
  private boolean loadExistingEnvInfo(AdvancedProperties adapterProperties) {
    environment.setUseExistingEnv(adapterProperties.getPropertyAsBool("env.use.existing", false));
    environment.setExistingSessionToken(
        adapterProperties.getPropertyAsString("env.existing.sessionToken", null));
    String envURI = adapterProperties.getPropertyAsString("env.existing.environmentURI", null);

    boolean allOK = true;
    if (environment.getUseExistingEnv()) {
      if (StringUtils.isEmpty(environment.getExistingSessionToken())) {
        logger.error(
            "env.use.existing is set to true but no sessionToken has been provided. This is an invalid configuration in "
                + adapterProperties.getPropFileNameFull());
        allOK = false;
      }
      if (StringUtils.isEmpty(envURI)) {
        logger.error(
            "env.use.existing is set to true but no existing environment URI has been provided. This is an invalid configuration in "
                + adapterProperties.getPropFileNameFull());
        allOK = false;
      }
      if (allOK) {
        environment.setExistingEnvURI(envURI);
        if (environment.getExistingEnvURI() == null) {
          logger.error(
              "The URI given in the property env.existing.environmentURI appears to be invalid in "
                  + adapterProperties.getPropFileNameFull());
          allOK = false;
        }
      }
    } else {
      if (StringUtils.notEmpty(envURI)) {
        environment.setExistingEnvURI(envURI);
      }
    }
    return allOK;
  }
  private boolean loadProviderInfo(AdvancedProperties props, ProviderEnvironment envInfo) {
    boolean errorsFound = false;

    envInfo.setEventsSupported(props.getPropertyAsBool("env.events.supported", false));

    // The following properties are required if it is a BROKERED environment. For DIRECT
    // environments these values
    // are read from the SIF_APP_TEMPLATE table!
    if (envInfo.getEnvironmentType() == EnvironmentType.BROKERED) {
      // Application Key
      envInfo
          .getEnvironmentKey()
          .setApplicationKey(adapterProperties.getPropertyAsString("env.application.key", null));
      if (StringUtils.isEmpty(envInfo.getEnvironmentKey().getApplicationKey())) {
        logger.error(
            "Property 'env.application.key' is null or empty in "
                + getAdapterFileNameWithoutExt()
                + ".properties. Application Key must be set!");
        errorsFound = true;
      }

      // Password
      envInfo.setPassword(adapterProperties.getPropertyAsString("env.pwd", null));
      if (StringUtils.isEmpty(envInfo.getPassword())) {
        logger.error(
            "Property 'env.pwd' is null or empty in "
                + getAdapterFileNameWithoutExt()
                + ".properties. Password for this application must be set!");
        errorsFound = true;
      }

      // Authentication Method
      envInfo.setAuthMethod(
          adapterProperties.getPropertyAsString(
              "env.authentication.method", AuthenticationMethod.Basic.name()));

      envInfo.setTemplateXMLFileName(props.getPropertyAsString("env.xml.file.name", null));
      if (StringUtils.isEmpty(envInfo.getTemplateXMLFileName())) {
        logger.error(
            "Property 'env.xml.file.name' is null or empty in "
                + getAdapterFileNameWithoutExt()
                + ".properties. File name for environment XML template must be set!");
        errorsFound = true;
      }
    }

    // Base URL for connectors
    String connectorBaseURLStr = props.getPropertyAsString("env.connector.url", null);

    // Secure URL Base URL for connectors
    String secureConnectorBaseURLStr = props.getPropertyAsString("env.connector.url.secure", null);

    if (StringUtils.isEmpty(connectorBaseURLStr)
        && StringUtils.isEmpty(secureConnectorBaseURLStr)) {
      logger.error(
          "env.connector.url AND env.connector.url.secure for "
              + getAdapterFileNameWithoutExt()
              + ".properties is missing. You must provide at least one of these URLs for them.");
      errorsFound = true;
    } else {
      if (StringUtils.notEmpty(connectorBaseURLStr)) {
        envInfo.setConnectorBaseURI(cleanURI(connectorBaseURLStr, "env.connector.url"));
        if (envInfo.getConnectorBaseURI() == null) {
          errorsFound = true;
        }
      }
      if (StringUtils.notEmpty(secureConnectorBaseURLStr)) {
        envInfo.setSecureConnectorBaseURI(
            cleanURI(secureConnectorBaseURLStr, "env.connector.url.secure"));
        if (envInfo.getSecureConnectorBaseURI() == null) {
          errorsFound = true;
        }
      }
    }

    envInfo.setBaseURI(cleanURI(props.getPropertyAsString("env.baseURI", null), "env.baseURI"));
    if (envInfo.getEnvironmentType() == EnvironmentType.BROKERED) // The baseURI is required
    {
      if (envInfo.getBaseURI() == null) {
        logger.error(
            "Property 'env.baseURI' is null, empty or invalid in "
                + getAdapterFileNameWithoutExt()
                + ".properties. Base URI must be set for brokered environments!");
        errorsFound = true;
      }
    }

    envInfo.setDefaultUpdateType(getUpdateType(props));

    // The properties below might only be applicable for DIRECT environments but this remains to be
    // seen. Since they all have a default
    // value it should not matter if they are set or not and what environment we are in.
    envInfo.setAutoCreateEnvironment(props.getPropertyAsBool("env.allow.autoCreate", false));

    // Authentication Method
    envInfo.setAccessTokenAuthMethod(
        adapterProperties.getPropertyAsString(
            "adapter.default.accessToken.authentication.method",
            AuthenticationMethod.Bearer.name()));

    if (errorsFound) {
      logger.error(
          "Errors found in reading environment information from "
              + getAdapterFileNameWithoutExt()
              + ".properties. See previous log entries for details and please correct them.");
    }
    return errorsFound;
  }