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 */
}
}
}
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);
}
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);
}
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.");
}
}
Aggregations