Search in sources :

Example 1 with DatabaseSettings

use of me.retrodaredevil.solarthing.config.databases.DatabaseSettings in project solarthing by wildmountainfarms.

the class SolarMain method doMain.

private static int doMain(String[] args) {
    String logMessage = "Beginning main. Jar: " + getJarInfo();
    LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "[LOG] " + logMessage);
    System.out.println("[stdout] " + logMessage);
    System.err.println("[stderr] " + logMessage);
    Cli<CommandOptions> cli = CliFactory.createCli(CommandOptions.class);
    final CommandOptions commandOptions;
    try {
        commandOptions = cli.parseArguments(args);
    } catch (ArgumentValidationException ex) {
        System.out.println(cli.getHelpMessage());
        if (ex instanceof HelpRequestedException) {
            return 0;
        }
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, ex.getMessage());
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "(Fatal)Incorrect args");
        return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
    }
    if (commandOptions.getBaseConfigFile() != null) {
        return doMainCommand(commandOptions, commandOptions.getBaseConfigFile());
    }
    if (commandOptions.getCouchDbSetupFile() != null) {
        final DatabaseConfig config;
        try {
            config = ConfigUtil.MAPPER.readValue(commandOptions.getCouchDbSetupFile(), DatabaseConfig.class);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Problem reading CouchDB database settings file.");
            return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
        }
        DatabaseSettings settings = config.getSettings();
        if (!(settings instanceof CouchDbDatabaseSettings)) {
            System.err.println("Must be CouchDB database settings!");
            return SolarThingConstants.EXIT_CODE_INVALID_CONFIG;
        }
        try {
            return CouchDbSetupMain.createFrom((CouchDbDatabaseSettings) settings).doCouchDbSetupMain();
        } catch (CouchDbException e) {
            if (e instanceof CouchDbCodeException) {
                ErrorResponse error = ((CouchDbCodeException) e).getErrorResponse();
                if (error != null) {
                    System.err.println(error.getError());
                    System.err.println(error.getReason());
                }
            }
            throw new RuntimeException(e);
        }
    }
    List<String> legacyArguments = commandOptions.getLegacyOptionsRaw();
    if (legacyArguments == null || legacyArguments.isEmpty()) {
        System.err.println(cli.getHelpMessage());
        return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
    }
    System.err.println("Invalid sub command: " + legacyArguments.get(0));
    return SolarThingConstants.EXIT_CODE_INVALID_OPTIONS;
}
Also used : CouchDbException(me.retrodaredevil.couchdbjava.exception.CouchDbException) ErrorResponse(me.retrodaredevil.couchdbjava.response.ErrorResponse) DatabaseSettings(me.retrodaredevil.solarthing.config.databases.DatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) HelpRequestedException(com.lexicalscope.jewel.cli.HelpRequestedException) CouchDbCodeException(me.retrodaredevil.couchdbjava.exception.CouchDbCodeException) ArgumentValidationException(com.lexicalscope.jewel.cli.ArgumentValidationException)

Example 2 with DatabaseSettings

use of me.retrodaredevil.solarthing.config.databases.DatabaseSettings in project solarthing by wildmountainfarms.

the class CommonProvider method init.

@PostConstruct
public void init() {
    defaultInstanceOptions = DefaultInstanceOptions.create(getDefaultSourceId(), getDefaultFragmentId());
    LOGGER.debug("Using defaultInstanceOptions=" + defaultInstanceOptions);
    LOGGER.debug("Database file: " + databaseFile.getAbsolutePath());
    LOGGER.debug("Working directory: " + new File(".").getAbsolutePath());
    ObjectMapper objectMapper = JacksonUtil.defaultMapper();
    objectMapper.getSubtypeResolver().registerSubtypes(DatabaseSettings.class, CouchDbDatabaseSettings.class);
    final FileInputStream reader;
    try {
        reader = new FileInputStream(databaseFile);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    final DatabaseConfig databaseConfig;
    try {
        databaseConfig = objectMapper.readValue(reader, DatabaseConfig.class);
    } catch (IOException e) {
        throw new RuntimeException("Couldn't parse data!", e);
    }
    DatabaseSettings databaseSettings = databaseConfig.getSettings();
    if (!(databaseSettings instanceof CouchDbDatabaseSettings)) {
        throw new UnsupportedOperationException("Only CouchDB is supported right now!");
    }
    couchDbDatabaseSettings = (CouchDbDatabaseSettings) databaseSettings;
}
Also used : CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) DatabaseSettings(me.retrodaredevil.solarthing.config.databases.DatabaseSettings) CouchDbDatabaseSettings(me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) DatabaseConfig(me.retrodaredevil.solarthing.program.DatabaseConfig) PostConstruct(javax.annotation.PostConstruct)

Aggregations

DatabaseSettings (me.retrodaredevil.solarthing.config.databases.DatabaseSettings)2 CouchDbDatabaseSettings (me.retrodaredevil.solarthing.config.databases.implementations.CouchDbDatabaseSettings)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArgumentValidationException (com.lexicalscope.jewel.cli.ArgumentValidationException)1 HelpRequestedException (com.lexicalscope.jewel.cli.HelpRequestedException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PostConstruct (javax.annotation.PostConstruct)1 CouchDbCodeException (me.retrodaredevil.couchdbjava.exception.CouchDbCodeException)1 CouchDbException (me.retrodaredevil.couchdbjava.exception.CouchDbException)1 ErrorResponse (me.retrodaredevil.couchdbjava.response.ErrorResponse)1 DatabaseConfig (me.retrodaredevil.solarthing.program.DatabaseConfig)1