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

fr.ird.observe.entities.ObserveTopiaConfigurationFactory Maven / Gradle / Ivy

There is a newer version: 4.34
Show newest version
package fr.ird.observe.entities;

/*-
 * #%L
 * ObServe Toolkit :: Common Persistence
 * %%
 * Copyright (C) 2017 - 2020 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */


import com.google.common.collect.ImmutableMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nuiton.topia.persistence.jdbc.JdbcConfiguration;
import org.nuiton.topia.persistence.jdbc.JdbcConfigurationBuilder;
import org.nuiton.topia.service.migration.TopiaMigrationService;
import org.nuiton.topia.service.script.TopiaSqlScriptGeneratorServiceImpl;

import java.io.File;
import java.nio.file.Path;

/**
 * Created on 23/08/15.
 *
 * @author Tony Chemit - [email protected]
 */
public class ObserveTopiaConfigurationFactory {

    /** l'url d'acces a la base locale */
    private static final String H2_LOCAL_URL =
            "jdbc:h2:file:%s;" +
                    // on peut aussi utiliser file, socket
                    "FILE_LOCK=file;" +
                    //1 or 2 is needed to restore avec crash
                    // 0: logging is disabled (faster),
                    // 1: logging of the data is enabled, but logging of the index
                    // changes is disabled (default), 2: logging of both data and index
                    // changes are enabled
                    "LOG=0;" +
                    // on peut aussi utiliser hsqldb, mysql ou postgresql
                    "MODE=postgresql;" +
                    //"MODE=hsqldb;" +
                    // Sets the default lock timeout (in milliseconds) in this
                    // database that is used for the new sessions.
                    "DEFAULT_LOCK_TIMEOUT=100;" +
                    // -1: the database is never closed until the close delay is set to
                    // some other rev or SHUTDOWN is called., 0: no delay (default; the
                    // database is closed if the last connection to it is closed)., n:
                    // the database is left open for n second after the last connection
                    // is closed.
                    "DB_CLOSE_DELAY=0;" +
                    // 0: no locking (should only be used for testing),
                    // 1: table level locking (default),
                    // 2: table level locking with garbage collection (if the
                    // application does not close all connections).
                    // LOCK_MODE 3 (READ_COMMITTED). Table level locking, but only when
                    // writing (no read locks).
                    "LOCK_MODE=3;" +
                    // Levels: 0=off, 1=error, 2=info, 3=debug.
                    "TRACE_LEVEL_FILE=0;" +
                    // on system.out: 0=off, 1=error, 2=info, 3=debug.
                    "TRACE_LEVEL_SYSTEM_OUT=0;" +
                    // maximumn cache to improve performance...
                    "CACHE_SIZE=65536;" +
                    // avoid timeout on reading tables (see http://stackoverflow.com/questions/4162557/timeout-error-trying-to-lock-table-in-h2)
                    "MVCC=true";

    private static final JdbcConfigurationBuilder JDBC_CONFIGURATION_BUILDER = new JdbcConfigurationBuilder();
    private static final Logger log = LogManager.getLogger(ObserveTopiaConfigurationFactory.class);

    public static ObserveTopiaConfiguration forPostgresqlDatabase(String jdbcUrl,
                                                                  String username,
                                                                  String password,
                                                                  Path temporaryDirectory,
                                                                  boolean initSchema,
                                                                  boolean traceSql) {

        JdbcConfiguration jdbcConfiguration = JDBC_CONFIGURATION_BUILDER.forPostgresqlDatabase(jdbcUrl, username, password);

        ObserveTopiaConfiguration topiaConfiguration = createTopiaConfiguration(jdbcConfiguration,
                                                                                temporaryDirectory,
                                                                                initSchema,
                                                                                false,
                                                                                traceSql);

        log.debug("PG topia configuration: " + topiaConfiguration);
        return topiaConfiguration;

    }

    public static ObserveTopiaConfiguration forH2Database(File dbDirectory,
                                                          String dbName,
                                                          String username,
                                                          String password,
                                                          Path temporaryDirectory,
                                                          boolean initSchema,
                                                          boolean traceSql) {

        String dbPath = new File(dbDirectory, dbName).getPath();
        String jdbcUrl = String.format(H2_LOCAL_URL, dbPath);

        JdbcConfiguration jdbcConfiguration = JDBC_CONFIGURATION_BUILDER.forH2Database(jdbcUrl, username, password);

        ObserveTopiaConfiguration topiaConfiguration = createTopiaConfiguration(jdbcConfiguration,
                                                                                temporaryDirectory,
                                                                                initSchema,
                                                                                true,
                                                                                traceSql);

        log.debug("H2 topia configuration: " + topiaConfiguration);
        return topiaConfiguration;

    }

    private static ObserveTopiaConfiguration createTopiaConfiguration(JdbcConfiguration jdbcConfiguration,
                                                                      Path temporaryDirectory,
                                                                      boolean initSchema,
                                                                      boolean h2Configuration,
                                                                      boolean traceSql) {

        ObserveTopiaConfiguration topiaConfiguration = new ObserveTopiaConfiguration(jdbcConfiguration, temporaryDirectory, h2Configuration, traceSql);
        topiaConfiguration.setTopiaIdFactoryClass(ObserveTopiaIdFactory.class);
        topiaConfiguration.setInitSchema(initSchema);
        topiaConfiguration.setValidateSchema(false);
        topiaConfiguration.setUseHikariForJdbcConnectionPooling(true);

        log.debug("jdbcUrl: " + topiaConfiguration.getJdbcConnectionUrl());

        ImmutableMap serviceConfigurationParameters = ImmutableMap.of("java.io.tmpdir", temporaryDirectory.toFile().getAbsolutePath());
        topiaConfiguration.addDeclaredService("migration", TopiaMigrationService.class, serviceConfigurationParameters);
        topiaConfiguration.addDeclaredService("sqlScriptGenerator", TopiaSqlScriptGeneratorServiceImpl.class, serviceConfigurationParameters);

        return topiaConfiguration;

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy