All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.io7m.idstore.server.api.IdServerConfigurations Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2023 Mark Raynsford  https://www.io7m.com
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */


package com.io7m.idstore.server.api;

import com.io7m.idstore.database.api.IdDatabaseConfiguration;
import com.io7m.idstore.database.api.IdDatabaseFactoryType;
import com.io7m.idstore.strings.IdStrings;

import java.time.Clock;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

import static com.io7m.idstore.database.api.IdDatabaseCreate.CREATE_DATABASE;
import static com.io7m.idstore.database.api.IdDatabaseCreate.DO_NOT_CREATE_DATABASE;
import static com.io7m.idstore.database.api.IdDatabaseUpgrade.DO_NOT_UPGRADE_DATABASE;
import static com.io7m.idstore.database.api.IdDatabaseUpgrade.UPGRADE_DATABASE;

/**
 * Functions to produce server configurations.
 */

public final class IdServerConfigurations
{
  private IdServerConfigurations()
  {

  }

  /**
   * Read a server configuration from the given file.
   *
   * @param locale The locale
   * @param clock  The clock
   * @param file   The file
   *
   * @return A server configuration
   */

  public static IdServerConfiguration ofFile(
    final Locale locale,
    final Clock clock,
    final IdServerConfigurationFile file)
  {
    Objects.requireNonNull(locale, "locale");
    Objects.requireNonNull(clock, "clock");
    Objects.requireNonNull(file, "file");

    final var fileDbConfig =
      file.databaseConfiguration();

    final var strings =
      IdStrings.create(locale);

    final var databaseConfiguration =
      new IdDatabaseConfiguration(
        fileDbConfig.ownerRoleName(),
        fileDbConfig.ownerRolePassword(),
        fileDbConfig.workerRolePassword(),
        fileDbConfig.readerRolePassword(),
        fileDbConfig.address(),
        fileDbConfig.port(),
        fileDbConfig.databaseName(),
        fileDbConfig.create() ? CREATE_DATABASE : DO_NOT_CREATE_DATABASE,
        fileDbConfig.upgrade() ? UPGRADE_DATABASE : DO_NOT_UPGRADE_DATABASE,
        strings,
        clock
      );

    final var databaseFactories =
      ServiceLoader.load(IdDatabaseFactoryType.class)
        .iterator();

    final var database =
      findDatabase(databaseFactories, fileDbConfig.kind());

    final var fileHttp =
      file.httpConfiguration();

    return new IdServerConfiguration(
      locale,
      clock,
      database,
      databaseConfiguration,
      file.mailConfiguration(),
      fileHttp.userAPIService(),
      fileHttp.userViewService(),
      fileHttp.adminAPIService(),
      file.sessionConfiguration(),
      file.brandingConfiguration(),
      file.historyConfiguration(),
      file.rateLimit(),
      file.maintenanceConfiguration(),
      file.passwordExpiration(),
      file.openTelemetry()
    );
  }

  private static IdDatabaseFactoryType findDatabase(
    final Iterator databaseFactories,
    final IdServerDatabaseKind kind)
  {
    if (!databaseFactories.hasNext()) {
      throw new ServiceConfigurationError(
        "No available implementations of type %s"
          .formatted(IdDatabaseFactoryType.class)
      );
    }

    final var kinds = new ArrayList();
    while (databaseFactories.hasNext()) {
      final var database = databaseFactories.next();
      kinds.add(database.kind());
      if (Objects.equals(database.kind(), kind.name())) {
        return database;
      }
    }

    throw new ServiceConfigurationError(
      "No available databases of kind %s (Available databases include: %s)"
        .formatted(IdDatabaseFactoryType.class, kinds)
    );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy