Search in sources :

Example 1 with InvalidConfigurationException

use of horse.wtf.nzyme.configuration.InvalidConfigurationException in project nzyme by lennartkoopmann.

the class Main method main.

public static void main(String[] argv) {
    final CLIArguments cliArguments = new CLIArguments();
    // Parse CLI arguments.
    JCommander.newBuilder().addObject(cliArguments).build().parse(argv);
    // Override log level if requested.
    if (cliArguments.isDebugMode()) {
        Logging.setRootLoggerLevel(Level.DEBUG);
    }
    if (cliArguments.isTraceMode()) {
        Logging.setRootLoggerLevel(Level.TRACE);
    }
    // Parse configuration.
    BaseConfiguration baseConfiguration = null;
    try {
        baseConfiguration = new BaseConfigurationLoader(new File(cliArguments.getConfigFilePath())).get();
    } catch (InvalidConfigurationException | ConfigException e) {
        LOG.error("Invalid baseconfiguration. Please refer to the example configuration file or documentation.", e);
        System.exit(FAILURE);
    } catch (IncompleteConfigurationException e) {
        LOG.error("Incomplete base configuration. Please refer to the example configuration file or documentation.", e);
        System.exit(FAILURE);
    } catch (FileNotFoundException e) {
        LOG.error("Could not read configuration file.", e);
        System.exit(FAILURE);
    }
    switch(baseConfiguration.mode()) {
        case LEADER:
            LeaderConfiguration leaderConfiguration = null;
            try {
                leaderConfiguration = new LeaderConfigurationLoader(new File(cliArguments.getConfigFilePath()), false).get();
            } catch (InvalidConfigurationException | ConfigException e) {
                LOG.error("Invalid configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (IncompleteConfigurationException e) {
                LOG.error("Incomplete configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (FileNotFoundException e) {
                LOG.error("Could not read configuration file.", e);
                System.exit(FAILURE);
            }
            // Database.
            Database database = new Database(leaderConfiguration);
            try {
                database.initializeAndMigrate();
            } catch (LiquibaseException e) {
                LOG.fatal("Error during database initialization and migration.", e);
                System.exit(FAILURE);
            }
            NzymeLeader nzyme = new NzymeLeaderImpl(baseConfiguration, leaderConfiguration, database);
            try {
                nzyme.initialize();
            } catch (Exception e) {
                LOG.fatal("Could not initialize nzyme.", e);
                System.exit(FAILURE);
            }
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                Thread.currentThread().setName("shutdown-hook");
                nzyme.shutdown();
            }));
            break;
        case TRACKER:
            TrackerConfiguration trackerConfiguration = null;
            try {
                trackerConfiguration = new TrackerConfigurationLoader(new File(cliArguments.getConfigFilePath())).get();
            } catch (InvalidConfigurationException | ConfigException e) {
                LOG.error("Invalid configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (IncompleteConfigurationException e) {
                LOG.error("Incomplete configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (FileNotFoundException e) {
                LOG.error("Could not read configuration file.", e);
                System.exit(FAILURE);
            }
            NzymeTracker tracker = new NzymeTrackerImpl(baseConfiguration, trackerConfiguration);
            try {
                tracker.initialize();
            } catch (Exception e) {
                LOG.fatal("Could not initialize nzyme.", e);
                System.exit(FAILURE);
            }
    }
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            break;
        /* nein */
        }
    }
}
Also used : BaseConfigurationLoader(horse.wtf.nzyme.configuration.base.BaseConfigurationLoader) CLIArguments(horse.wtf.nzyme.configuration.CLIArguments) FileNotFoundException(java.io.FileNotFoundException) ConfigException(com.typesafe.config.ConfigException) TrackerConfigurationLoader(horse.wtf.nzyme.configuration.tracker.TrackerConfigurationLoader) IncompleteConfigurationException(horse.wtf.nzyme.configuration.IncompleteConfigurationException) IncompleteConfigurationException(horse.wtf.nzyme.configuration.IncompleteConfigurationException) FileNotFoundException(java.io.FileNotFoundException) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException) ConfigException(com.typesafe.config.ConfigException) LiquibaseException(liquibase.exception.LiquibaseException) TrackerConfiguration(horse.wtf.nzyme.configuration.tracker.TrackerConfiguration) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException) LeaderConfiguration(horse.wtf.nzyme.configuration.leader.LeaderConfiguration) BaseConfiguration(horse.wtf.nzyme.configuration.base.BaseConfiguration) Database(horse.wtf.nzyme.database.Database) LiquibaseException(liquibase.exception.LiquibaseException) File(java.io.File) LeaderConfigurationLoader(horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader)

Example 2 with InvalidConfigurationException

use of horse.wtf.nzyme.configuration.InvalidConfigurationException in project nzyme by lennartkoopmann.

the class EmailCallback method parseConfiguration.

public static Configuration parseConfiguration(Config c, String httpExternalUri) throws InvalidConfigurationException, IncompleteConfigurationException {
    // Completeness.
    ConfigurationValidator.expect(c, ConfigurationKeys.TRANSPORT_STRATEGY, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.HOST, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.PORT, WHERE, Integer.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.USERNAME, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.PASSWORD, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.RECIPIENTS, WHERE, List.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.FROM, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.SUBJECT_PREFIX, WHERE, String.class);
    // Validity.
    // Transport strategy exists.
    TransportStrategy transportStrategy;
    try {
        transportStrategy = TransportStrategy.valueOf(c.getString(ConfigurationKeys.TRANSPORT_STRATEGY));
    } catch (IllegalArgumentException e) {
        throw new InvalidConfigurationException("Invalid SMTP transport strategy.", e);
    }
    // Recipients are valid.
    List<Recipient> recipients = Lists.newArrayList();
    for (String rec : c.getStringList(ConfigurationKeys.RECIPIENTS)) {
        recipients.add(Tools.parseEmailAddress(rec));
    }
    return Configuration.create(transportStrategy, c.getString(ConfigurationKeys.HOST), c.getInt(ConfigurationKeys.PORT), c.getString(ConfigurationKeys.USERNAME), c.getString(ConfigurationKeys.PASSWORD), recipients, // recipient type is ignored
    Tools.parseEmailAddress(c.getString(ConfigurationKeys.FROM)), c.getString(ConfigurationKeys.SUBJECT_PREFIX), httpExternalUri);
}
Also used : Recipient(org.simplejavamail.api.email.Recipient) TransportStrategy(org.simplejavamail.api.mailer.config.TransportStrategy) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException)

Example 3 with InvalidConfigurationException

use of horse.wtf.nzyme.configuration.InvalidConfigurationException in project nzyme by lennartkoopmann.

the class FileCallback method parseConfiguration.

public static FileCallback.Configuration parseConfiguration(Config c) throws InvalidConfigurationException, IncompleteConfigurationException {
    ConfigurationValidator.expect(c, ConfigurationKeys.PATH, WHERE, String.class);
    Path filePath;
    try {
        filePath = new File(c.getString(ConfigurationKeys.PATH)).toPath();
    } catch (Exception e) {
        throw new InvalidConfigurationException("Could not build path to file.", e);
    }
    if (Files.exists(filePath) && !Files.isWritable(filePath)) {
        // Only check if file is writable if it exists. It could be that it simply hasn't been written yet and that's fine.
        throw new InvalidConfigurationException("File [" + filePath + "] exists but is not writable.");
    }
    return Configuration.create(filePath);
}
Also used : Path(java.nio.file.Path) File(java.io.File) IncompleteConfigurationException(horse.wtf.nzyme.configuration.IncompleteConfigurationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException)

Example 4 with InvalidConfigurationException

use of horse.wtf.nzyme.configuration.InvalidConfigurationException in project nzyme by lennartkoopmann.

the class BaseConfigurationLoader method validate.

public void validate() throws InvalidConfigurationException, IncompleteConfigurationException {
    ConfigurationValidator.expectEnum(general, ConfigurationKeys.ROLE, ConfigurationKeys.GENERAL, Role.class);
    // Node ID exists and is valid.
    ConfigurationValidator.expect(general, ConfigurationKeys.ID, ConfigurationKeys.GENERAL, String.class);
    if (!Tools.isSafeID(parseNodeID())) {
        throw new InvalidConfigurationException("Node ID must only contain alphanumeric characters, underscores or dashes.");
    }
    // Data directory exists and is readable?
    File dataDirectory = new File(parseDataDirectory());
    if (!dataDirectory.exists()) {
        throw new InvalidConfigurationException("Data directory [" + parseDataDirectory() + "] does not exist.");
    }
    if (!dataDirectory.isDirectory()) {
        throw new InvalidConfigurationException("Data directory [" + parseDataDirectory() + "] is not a directory.");
    }
    if (!dataDirectory.canWrite()) {
        throw new InvalidConfigurationException("Data directory [" + parseDataDirectory() + "] is not writable.");
    }
}
Also used : File(java.io.File) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException)

Aggregations

InvalidConfigurationException (horse.wtf.nzyme.configuration.InvalidConfigurationException)4 File (java.io.File)3 IncompleteConfigurationException (horse.wtf.nzyme.configuration.IncompleteConfigurationException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ConfigException (com.typesafe.config.ConfigException)1 CLIArguments (horse.wtf.nzyme.configuration.CLIArguments)1 BaseConfiguration (horse.wtf.nzyme.configuration.base.BaseConfiguration)1 BaseConfigurationLoader (horse.wtf.nzyme.configuration.base.BaseConfigurationLoader)1 LeaderConfiguration (horse.wtf.nzyme.configuration.leader.LeaderConfiguration)1 LeaderConfigurationLoader (horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader)1 TrackerConfiguration (horse.wtf.nzyme.configuration.tracker.TrackerConfiguration)1 TrackerConfigurationLoader (horse.wtf.nzyme.configuration.tracker.TrackerConfigurationLoader)1 Database (horse.wtf.nzyme.database.Database)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 LiquibaseException (liquibase.exception.LiquibaseException)1 Recipient (org.simplejavamail.api.email.Recipient)1 TransportStrategy (org.simplejavamail.api.mailer.config.TransportStrategy)1