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

net.plsar.DatabaseEnvironmentManager Maven / Gradle / Ivy

Go to download

PLSA.R is an Open Source Server + Framework Environment for small to large scale requirements. There are no static references, no file reads, access to static fields per request. Everything is either cached and or instantiated on the fly. PLSA.R runs via one command so there are no .war files to deploy, no additional plugins to install it is a simple yet powerful alternative to container deployment environments.

There is a newer version: 3.0.1
Show newest version
package net.plsar;

import org.h2.tools.RunScript;

import javax.sql.DataSource;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.logging.Logger;

public class DatabaseEnvironmentManager {

    Logger Log = Logger.getLogger(DatabaseEnvironmentManager.class.getName());

    public void configure(SchemaConfig schemaConfig, PersistenceConfig persistenceConfig){

        try {
            Path schemaFilePath = Paths.get("src", "main", "resources", schemaConfig.getSchema());
            File schemaConfigFile = new File(schemaFilePath.toAbsolutePath().toString());
            if (!schemaConfigFile.exists()) {
                Log.info("non-persistence mode");
                Log.info("schema.sql missing in src/main/resources/. project will be treated as a non persistent application.");
                Log.info("database environment setup complete");
                return;
            }

            InputStream in = new FileInputStream(schemaConfigFile);

            if (in == null || in.available() == 0) {
                Log.info("src/main/resources/{schema}.sql contains no tables. project will be treated as a non persistent application.");
                Log.info("database environment setup complete");
                return;
            }

            StringBuilder schemaSql = new StringBuilder();
            if (in.available() > 0){
                Scanner scanner = new Scanner(in);
                do {
                    schemaSql.append(scanner.nextLine() + "\n");
                } while(scanner.hasNext());
                in.close();
            }

            DataSource datasource = new ExecutableDatasource.Builder()
                    .url(persistenceConfig.getUrl())
                    .driver(persistenceConfig.getDriver())
                    .user(persistenceConfig.getUser())
                    .password(persistenceConfig.getPassword())
                    .create();

            Connection conn = datasource.getConnection();

            RunScript.execute(conn, new StringReader("drop all objects;"));

            if (!schemaSql.toString().equals("")) {
                RunScript.execute(conn, new StringReader(schemaSql.toString()));
            }

            conn.commit();
            conn.close();

            Log.info("database environment setup complete");

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy