Search in sources :

Example 1 with ConfigFile

use of com.helger.settings.exchange.configfile.ConfigFile in project phoss-smp by phax.

the class SMPManagerProviderSQL method beforeInitManagers.

@Override
public void beforeInitManagers() {
    final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
    // Set the special PhotonSecurityManager factory
    // Must be before Flyway, so that auditing of Flyway actions (may) work
    PhotonSecurityManagerFactoryJDBC.install(SMPDBExecutor::new, SMPDBExecutor.TABLE_NAME_CUSTOMIZER);
    // Flyway migration is enabled by default
    if (aCF.getAsBoolean(SMPJDBCConfiguration.CONFIG_SMP_FLYWAY_ENABLED, true))
        FlywayMigrator.Singleton.INSTANCE.runFlyway(m_eDBType);
    else
        LOGGER.warn("Flyway Migration is disabled according to the configuration item " + SMPJDBCConfiguration.CONFIG_SMP_FLYWAY_ENABLED);
    // Register this here, so that the SMPMetaManager is available
    DBExecutor.setConnectionStatusChangeCallback((eOld, eNew) -> {
        // false: don't trigger callback, because the source is DBExecutor
        SMPMetaManager.getInstance().setBackendConnectionState(eNew, false);
    });
    // Allow communicating in the other direction as well
    SMPMetaManager.getInstance().setBackendConnectionStateChangeCallback(eNew -> DBExecutor.resetConnectionEstablished());
}
Also used : ConfigFile(com.helger.settings.exchange.configfile.ConfigFile) SMPDBExecutor(com.helger.phoss.smp.backend.sql.SMPDBExecutor)

Example 2 with ConfigFile

use of com.helger.settings.exchange.configfile.ConfigFile in project phoss-smp by phax.

the class SMPInternalErrorHandler method doSetup.

public static void doSetup() {
    final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
    final String sSenderAddress = aCF.getAsString("smp.errorhandler.sender.email");
    final String sSenderName = aCF.getAsString("smp.errorhandler.sender.name", "SMP Internal Error Sender");
    final String sReceiverAddress = aCF.getAsString("smp.errorhandler.receiver.email");
    final String sReceiverName = aCF.getAsString("smp.errorhandler.receiver.name");
    final String sSMTPHostName = aCF.getAsString("smp.smtp.hostname");
    final int nSMTPPort = aCF.getAsInt("smp.smtp.port", -1);
    final String sSMTPUserName = aCF.getAsString("smp.smtp.username");
    final String sSMTPPassword = aCF.getAsString("smp.smtp.password");
    final boolean bSMTPSSLEnabled = aCF.getAsBoolean("smp.smtp.ssl", false);
    final boolean bSMTPSTARTTLSEnabled = aCF.getAsBoolean("smp.smtp.starttls", false);
    final long nSMTPConnectionTimeoutMS = aCF.getAsLong("smp.smtp.connectiontimeoutms", 10_000);
    final long nSMTPSocketTimeoutMS = aCF.getAsLong("smp.smtp.sockettimeoutms", 10_000);
    final boolean bSMTPDebug = aCF.getAsBoolean("smp.smtp.debug", false);
    final SMTPSettings aSMTPSettings = StringHelper.hasText(sSMTPHostName) ? new SMTPSettings(sSMTPHostName, nSMTPPort, sSMTPUserName, sSMTPPassword, StandardCharsets.UTF_8, bSMTPSSLEnabled, bSMTPSTARTTLSEnabled, nSMTPConnectionTimeoutMS, nSMTPSocketTimeoutMS, bSMTPDebug) : null;
    if (StringHelper.hasText(sSenderAddress) && StringHelper.hasText(sReceiverAddress) && aSMTPSettings != null && aSMTPSettings.areRequiredFieldsSet()) {
        // Set global internal error handlers
        new SMPInternalErrorHandler().install();
        InternalErrorSettings.setSMTPSenderAddress(new EmailAddress(sSenderAddress, sSenderName));
        InternalErrorSettings.setSMTPReceiverAddresses(new EmailAddress(sReceiverAddress, sReceiverName));
        InternalErrorSettings.setSMTPSettings(aSMTPSettings);
        InternalErrorSettings.setFallbackLocale(CSMPServer.DEFAULT_LOCALE);
        if (LOGGER.isInfoEnabled())
            LOGGER.info("Setup internal error handler to send emails on internal errors to " + sReceiverAddress);
    } else {
        LOGGER.info("No internal error handler configuration was found. So not sending emails in case of error.");
    }
}
Also used : SMTPSettings(com.helger.smtp.settings.SMTPSettings) ConfigFile(com.helger.settings.exchange.configfile.ConfigFile) EmailAddress(com.helger.commons.email.EmailAddress)

Example 3 with ConfigFile

use of com.helger.settings.exchange.configfile.ConfigFile in project phoss-smp by phax.

the class FlywayMigrator method runFlyway.

void runFlyway(@Nonnull final EDatabaseType eDBType) {
    ValueEnforcer.notNull(eDBType, "DBType");
    LOGGER.info("Starting to run Flyway for DB type " + eDBType);
    final ConfigFile aCF = SMPServerConfiguration.getConfigFile();
    final Callback aCallbackLogging = new BaseCallback() {

        public void handle(@Nonnull final Event aEvent, @Nonnull final Context aContext) {
            if (LOGGER.isInfoEnabled())
                LOGGER.info("Flyway: Event " + aEvent.getId());
            if (aEvent == Event.AFTER_EACH_MIGRATE && aContext != null) {
                final MigrationInfo aMI = aContext.getMigrationInfo();
                if (aMI instanceof MigrationInfoImpl) {
                    final ResolvedMigration aRM = ((MigrationInfoImpl) aMI).getResolvedMigration();
                    if (aRM != null)
                        if (LOGGER.isInfoEnabled())
                            LOGGER.info("  Performed migration: " + aRM);
                }
            }
        }
    };
    final Callback aCallbackAudit = new BaseCallback() {

        public void handle(@Nonnull final Event aEvent, @Nonnull final Context aContext) {
            if (aEvent == Event.AFTER_EACH_MIGRATE && aContext != null) {
                final MigrationInfo aMI = aContext.getMigrationInfo();
                if (aMI instanceof MigrationInfoImpl) {
                    final ResolvedMigration aRM = ((MigrationInfoImpl) aMI).getResolvedMigration();
                    // before that version
                    if (aRM != null && aRM.getVersion().isAtLeast("7"))
                        AuditHelper.onAuditExecuteSuccess("sql-migration-success", aRM.getVersion().toString(), aRM.getDescription(), aRM.getScript(), aRM.getType().name(), aRM.getPhysicalLocation());
                }
            }
        }
    };
    final FluentConfiguration aConfig = Flyway.configure().dataSource(new DriverDataSource(FlywayMigrator.class.getClassLoader(), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_DRIVER), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_URL), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_USER), aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_PASSWORD))).baselineOnMigrate(true).validateOnMigrate(false).baselineVersion("1").baselineDescription("SMP 5.2.x database layout, MySQL only").locations("db/migrate-" + eDBType.getID()).javaMigrations(new V2__MigrateDBUsersToPhotonUsers(), new V5__MigrateTransportProfilesToDB(), new V10__MigrateRolesToDB(), new V11__MigrateUsersToDB(), new V12__MigrateUserGroupsToDB(), new V14__MigrateSettingsToDB(), new V15__MigrateDBUsersToPhotonUsers()).callbacks(aCallbackLogging, aCallbackAudit);
    // Flyway to handle the DB schema?
    final String sSchema = aCF.getAsString(SMPJDBCConfiguration.CONFIG_JDBC_SCHEMA);
    if (StringHelper.hasText(sSchema)) {
        // Use the schema only, if it is explicitly configured
        // The default schema name is ["$user", public] and as such unusable
        aConfig.schemas(sSchema);
    }
    // If no schema is specified, schema create should also be disabled
    final boolean bCreateSchema = aCF.getAsBoolean(SMPJDBCConfiguration.CONFIG_JDBC_SCHEMA_CREATE, false);
    aConfig.createSchemas(bCreateSchema);
    final Flyway aFlyway = aConfig.load();
    if (false)
        aFlyway.validate();
    aFlyway.migrate();
    LOGGER.info("Finished running Flyway");
}
Also used : Context(org.flywaydb.core.api.callback.Context) MigrationInfo(org.flywaydb.core.api.MigrationInfo) Flyway(org.flywaydb.core.Flyway) DriverDataSource(org.flywaydb.core.internal.jdbc.DriverDataSource) V15__MigrateDBUsersToPhotonUsers(com.helger.phoss.smp.backend.sql.migration.V15__MigrateDBUsersToPhotonUsers) ConfigFile(com.helger.settings.exchange.configfile.ConfigFile) Nonnull(javax.annotation.Nonnull) MigrationInfoImpl(org.flywaydb.core.internal.info.MigrationInfoImpl) V12__MigrateUserGroupsToDB(com.helger.phoss.smp.backend.sql.migration.V12__MigrateUserGroupsToDB) FluentConfiguration(org.flywaydb.core.api.configuration.FluentConfiguration) V2__MigrateDBUsersToPhotonUsers(com.helger.phoss.smp.backend.sql.migration.V2__MigrateDBUsersToPhotonUsers) V11__MigrateUsersToDB(com.helger.phoss.smp.backend.sql.migration.V11__MigrateUsersToDB) Callback(org.flywaydb.core.api.callback.Callback) BaseCallback(org.flywaydb.core.api.callback.BaseCallback) V5__MigrateTransportProfilesToDB(com.helger.phoss.smp.backend.sql.migration.V5__MigrateTransportProfilesToDB) V14__MigrateSettingsToDB(com.helger.phoss.smp.backend.sql.migration.V14__MigrateSettingsToDB) Event(org.flywaydb.core.api.callback.Event) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) V10__MigrateRolesToDB(com.helger.phoss.smp.backend.sql.migration.V10__MigrateRolesToDB) BaseCallback(org.flywaydb.core.api.callback.BaseCallback)

Example 4 with ConfigFile

use of com.helger.settings.exchange.configfile.ConfigFile in project phoss-smp by phax.

the class MongoClientSingleton method onAfterInstantiation.

@Override
protected void onAfterInstantiation(@Nonnull final IScope aScope) {
    // Standard configuration file
    final ConfigFile aConfigFile = SMPServerConfiguration.getConfigFile();
    final String sConnectionString = aConfigFile.getAsString(CONFIG_MONGODB_CONNECTION_STRING);
    if (StringHelper.hasNoText(sConnectionString))
        throw new IllegalStateException("The MongoDB connection string is missing in the configuration. See property '" + CONFIG_MONGODB_CONNECTION_STRING + "'");
    final String sDBName = aConfigFile.getAsString(CONFIG_MONGODB_DB_NAME);
    if (StringHelper.hasNoText(sDBName))
        throw new IllegalStateException("The MongoDB database name is missing in the configuration. See property '" + CONFIG_MONGODB_DB_NAME + "'");
    LOGGER.info("Using Mongo DB database name '" + sDBName + "'");
    m_aProvider = new MongoClientProvider(sConnectionString, sDBName);
}
Also used : ConfigFile(com.helger.settings.exchange.configfile.ConfigFile)

Aggregations

ConfigFile (com.helger.settings.exchange.configfile.ConfigFile)4 EmailAddress (com.helger.commons.email.EmailAddress)1 SMPDBExecutor (com.helger.phoss.smp.backend.sql.SMPDBExecutor)1 V10__MigrateRolesToDB (com.helger.phoss.smp.backend.sql.migration.V10__MigrateRolesToDB)1 V11__MigrateUsersToDB (com.helger.phoss.smp.backend.sql.migration.V11__MigrateUsersToDB)1 V12__MigrateUserGroupsToDB (com.helger.phoss.smp.backend.sql.migration.V12__MigrateUserGroupsToDB)1 V14__MigrateSettingsToDB (com.helger.phoss.smp.backend.sql.migration.V14__MigrateSettingsToDB)1 V15__MigrateDBUsersToPhotonUsers (com.helger.phoss.smp.backend.sql.migration.V15__MigrateDBUsersToPhotonUsers)1 V2__MigrateDBUsersToPhotonUsers (com.helger.phoss.smp.backend.sql.migration.V2__MigrateDBUsersToPhotonUsers)1 V5__MigrateTransportProfilesToDB (com.helger.phoss.smp.backend.sql.migration.V5__MigrateTransportProfilesToDB)1 SMTPSettings (com.helger.smtp.settings.SMTPSettings)1 Nonnull (javax.annotation.Nonnull)1 Flyway (org.flywaydb.core.Flyway)1 MigrationInfo (org.flywaydb.core.api.MigrationInfo)1 BaseCallback (org.flywaydb.core.api.callback.BaseCallback)1 Callback (org.flywaydb.core.api.callback.Callback)1 Context (org.flywaydb.core.api.callback.Context)1 Event (org.flywaydb.core.api.callback.Event)1 FluentConfiguration (org.flywaydb.core.api.configuration.FluentConfiguration)1 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)1