Search in sources :

Example 1 with ScriptRunner

use of io.clownfish.clownfish.jdbc.ScriptRunner in project Clownfish by rawdog71.

the class Main method bootstrap_update.

/**
 * Checks the applications.properties file and runs the bootstrap routine when the bootstrap parameter is set to 1
 * Fetches the database (MySQL) parameters from applications.properties and runs the sql-bootstrap.sql script
 * The script creates the database user for reading/writing (user=clownfish), creates all tables and initializes some tables with data
 */
public static void bootstrap_update() {
    InputStream fis = null;
    try {
        Properties props = new Properties();
        String propsfile = "application.properties";
        fis = new FileInputStream(propsfile);
        if (null != fis) {
            props.load(fis);
        }
        String dbclass = props.getProperty("app.datasource.driverClassName");
        String dburl = props.getProperty("app.datasource.url");
        String dbuser = props.getProperty("app.datasource.username");
        String dbpassword = props.getProperty("app.datasource.password");
        String path = new File(".").getCanonicalPath();
        File bootstrapDirectory = new File(path);
        File[] files = bootstrapDirectory.listFiles();
        Arrays.sort(files);
        for (int i = 0; i < files.length; i++) {
            if (files[i].getName().startsWith("bootstrap_")) {
                InputStream is = null;
                try {
                    Properties boot_props = new Properties();
                    is = new FileInputStream(files[i]);
                    if (null != is) {
                        boot_props.load(is);
                    }
                    int bootstrap = Integer.parseInt(boot_props.getProperty("bootstrap"));
                    String version = boot_props.getProperty("version");
                    String bootstrapfile = boot_props.getProperty("bootstrapfile");
                    if (1 == bootstrap) {
                        bootstrap = 0;
                        AnsiConsole.systemInstall();
                        System.out.println(ansi().fg(GREEN));
                        System.out.println("BOOTSTRAPPING UPDATE VERSION " + version);
                        System.out.println(ansi().reset());
                        JDBCUtil jdbcutil = new JDBCUtil(dbclass, dburl, dbuser, dbpassword);
                        ScriptRunner runner = new ScriptRunner(jdbcutil.getConnection(), false, false);
                        runner.runScript(new BufferedReader(new FileReader(bootstrapfile)));
                        File f = new File(files[i].getName());
                        OutputStream out = new FileOutputStream(f);
                        boot_props.setProperty("bootstrap", String.valueOf(bootstrap));
                        DefaultPropertiesPersister p = new DefaultPropertiesPersister();
                        p.store(boot_props, out, "Bootstrap properties");
                    }
                } catch (FileNotFoundException ex) {
                    LOGGER.error(ex.getMessage());
                } catch (IOException | SQLException ex) {
                    LOGGER.error(ex.getMessage());
                } finally {
                    try {
                        if (null != is) {
                            is.close();
                        }
                    } catch (IOException ex) {
                        LOGGER.error(ex.getMessage());
                    }
                }
            }
        }
    } catch (FileNotFoundException ex) {
        LOGGER.error(ex.getMessage());
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage());
    } finally {
        try {
            if (null != fis) {
                fis.close();
            }
        } catch (IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    }
}
Also used : SQLException(java.sql.SQLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) JDBCUtil(io.clownfish.clownfish.jdbc.JDBCUtil) IOException(java.io.IOException) Properties(java.util.Properties) ScriptRunner(io.clownfish.clownfish.jdbc.ScriptRunner) FileInputStream(java.io.FileInputStream) DefaultPropertiesPersister(org.springframework.util.DefaultPropertiesPersister) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 2 with ScriptRunner

use of io.clownfish.clownfish.jdbc.ScriptRunner in project Clownfish by rawdog71.

the class Main method bootstrap.

/**
 * Checks the applications.properties file and runs the bootstrap routine when the bootstrap parameter is set to 1
 * Fetches the database (MySQL) parameters from applications.properties and runs the sql-bootstrap.sql script
 * The script creates the database user for reading/writing (user=clownfish), creates all tables and initializes some tables with data
 */
public static void bootstrap() {
    InputStream is = null;
    try {
        Properties props = new Properties();
        String propsfile = "application.properties";
        is = new FileInputStream(propsfile);
        if (null != is) {
            props.load(is);
        }
        int bootstrap = Integer.parseInt(props.getProperty("bootstrap"));
        String dbclass = props.getProperty("app.datasource.driverClassName");
        String dburl = props.getProperty("app.datasource.urlroot");
        String dbuser = props.getProperty("app.datasource.root");
        String dbpassword = props.getProperty("app.datasource.rootpw");
        if (1 == bootstrap) {
            AnsiConsole.systemInstall();
            System.out.println(ansi().fg(GREEN));
            System.out.println("BOOTSTRAPPING I");
            System.out.println(ansi().reset());
            JDBCUtil jdbcutil = new JDBCUtil(dbclass, dburl, dbuser, dbpassword);
            ScriptRunner runner = new ScriptRunner(jdbcutil.getConnection(), true, false);
            String file = "sql-bootstrap.sql";
            runner.runScript(new BufferedReader(new FileReader(file)));
        }
    } catch (FileNotFoundException ex) {
        LOGGER.error(ex.getMessage());
    } catch (IOException | SQLException ex) {
        LOGGER.error(ex.getMessage());
    } finally {
        try {
            if (null != is) {
                is.close();
            }
        } catch (IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    }
}
Also used : SQLException(java.sql.SQLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) JDBCUtil(io.clownfish.clownfish.jdbc.JDBCUtil) IOException(java.io.IOException) Properties(java.util.Properties) ScriptRunner(io.clownfish.clownfish.jdbc.ScriptRunner) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Aggregations

JDBCUtil (io.clownfish.clownfish.jdbc.JDBCUtil)2 ScriptRunner (io.clownfish.clownfish.jdbc.ScriptRunner)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 SQLException (java.sql.SQLException)2 Properties (java.util.Properties)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 DefaultPropertiesPersister (org.springframework.util.DefaultPropertiesPersister)1