<commit_before>package org.broad.igv.dev.db;

import org.apache.log4j.Logger;
import org.broad.igv.Globals;
import org.broad.igv.PreferenceManager;
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.util.MessageUtils;
import org.broad.igv.util.LoginDialog;

import java.awt.*;
import java.sql.*;
import java.util.*;
import java.util.concurrent.Executor;

/**
 * Class for prototyping database connections.  Prototype only -- hardcoded for mysql,  connects to single database,
 * keeps single connection, etc.
 *
 * @author <NAME>
 * @date 10/31/11
 */
public class DBManager {

    private static Logger log = Logger.getLogger(DBManager.class);

    static Collection<ConnectionWrapper> connectionPool =
            Collections.synchronizedCollection(new ArrayList<ConnectionWrapper>());
    static String username;
    static String password;

    public static Connection getConnection() {

        Iterator<ConnectionWrapper> poolIter = connectionPool.iterator();
        while (poolIter.hasNext()) {
            ConnectionWrapper conn = poolIter.next();
            try {
                if (conn == null || conn.isReallyClosed()) {
                    poolIter.remove();
                } else if (!conn.isClosed()) {
                    return conn;
                }
            } catch (SQLException e) {
                log.error("Bad connection", e);
                poolIter.remove();
            }
        }

        // No valid connections
        ConnectionWrapper conn = createConnection();
        if (conn != null) {
            connectionPool.add(conn);
            log.info("Connection pool size: " + connectionPool.size());
        }
        return conn;

    }


    private static ConnectionWrapper createConnection() {
        String driver = "com.mysql.jdbc.Driver";
        try {
            Class.forName(driver).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        final PreferenceManager preferenceManager = PreferenceManager.getInstance();
        String host = preferenceManager.get(PreferenceManager.DB_HOST);
        String db = preferenceManager.get(PreferenceManager.DB_NAME);
        String port = preferenceManager.get(PreferenceManager.DB_PORT);

        String url = "jdbc:mysql://" + host;
        if (!port.equals("-1")) {
            url += ":" + port;
        }
        url += "/" + db;

        return connect(url);
    }

    private static ConnectionWrapper connect(String url) {
        try {
            return new ConnectionWrapper(DriverManager.getConnection(url, username, password));
        } catch (SQLException e) {
            int errorCode = e.getErrorCode();
            if (errorCode == 1044 || errorCode == 1045) {
                String host = PreferenceManager.getInstance().get(PreferenceManager.DB_HOST);

                Frame parent = Globals.isHeadless() ? null : IGV.getMainFrame();
                LoginDialog dlg = new LoginDialog(parent, false, host, false);
                dlg.setVisible(true);
                if (dlg.isCanceled()) {
                    throw new RuntimeException("Must login to access" + host);
                }
                username = dlg.getUsername();
                password = new String(dlg.<PASSWORD>());
                return connect(url);

            } else {
                MessageUtils.showMessage("<html>Error connecting to database: <br>" + e.getMessage());
                return null;
            }

        }
    }

    public static void shutdown() {
        for (ConnectionWrapper conn : connectionPool) {
            if (conn != null) {
                try {
                    conn.reallyClose();
                } catch (SQLException e) {

                }
            }
        }
        connectionPool.clear();
    }


    static class ConnectionWrapper implements Connection {

        Connection conn;
        boolean closed;

        ConnectionWrapper(Connection conn) {
            this.conn = conn;
            closed = false;
        }

        public void close() throws SQLException {
            closed = true;
        }

        public boolean isClosed() throws SQLException {
            return closed;
        }

        public void reallyClose() throws SQLException {
            closed = true;
            conn.close();
        }

        public boolean isReallyClosed() throws SQLException {
            return conn.isClosed();
        }


        public void clearWarnings() throws SQLException {
            conn.clearWarnings();
        }

        public void commit() throws SQLException {
            conn.commit();
        }

        public Array createArrayOf(String s, Object[] objects) throws SQLException {
            return conn.createArrayOf(s, objects);
        }

        public Blob createBlob() throws SQLException {
            return conn.createBlob();
        }

        public Clob createClob() throws SQLException {
            return conn.createClob();
        }

        public NClob createNClob() throws SQLException {
            return conn.createNClob();
        }

        public SQLXML createSQLXML() throws SQLException {
            return conn.createSQLXML();
        }

        public Statement createStatement() throws SQLException {
            return conn.createStatement();
        }

        public Statement createStatement(int i, int i1) throws SQLException {
            return conn.createStatement(i, i1);
        }

        public Statement createStatement(int i, int i1, int i2) throws SQLException {
            return conn.createStatement(i, i1, i2);
        }

        public Struct createStruct(String s, Object[] objects) throws SQLException {
            return conn.createStruct(s, objects);
        }

        public void setSchema(String schema) throws SQLException {
            throw new SQLException("Operation not supported for backwards compatibility to Java 6");
        }

        public String getSchema() throws SQLException {
            return null; //TODO
        }

        public void abort(Executor executor) throws SQLException {
            throw new SQLException("Operation not supported for backwards compatibility to Java 6");
        }

        public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
            throw new SQLException("Operation not supported for backwards compatibility to Java 6");
        }

        public int getNetworkTimeout() {
            return -1;
        }

        public boolean getAutoCommit() throws SQLException {
            return conn.getAutoCommit();
        }<commit_msg>Change to more appropriate Exception for methods we can't implement.<commit_after>package org.broad.igv.dev.db;

import org.apache.log4j.Logger;
import org.broad.igv.Globals;
import org.broad.igv.PreferenceManager;
import org.broad.igv.ui.IGV;
import org.broad.igv.ui.util.MessageUtils;
import org.broad.igv.util.LoginDialog;

import java.awt.*;
import java.sql.*;
import java.util.*;
import java.util.concurrent.Executor;

/**
 * Class for prototyping database connections.  Prototype only -- hardcoded for mysql,  connects to single database,
 * keeps single connection, etc.
 *
 * @author <NAME>
 * @date 10/31/11
 */
public class DBManager {

    private static Logger log = Logger.getLogger(DBManager.class);

    static Collection<ConnectionWrapper> connectionPool =
            Collections.synchronizedCollection(new ArrayList<ConnectionWrapper>());
    static String username;
    static String password;

    public static Connection getConnection() {

        Iterator<ConnectionWrapper> poolIter = connectionPool.iterator();
        while (poolIter.hasNext()) {
            ConnectionWrapper conn = poolIter.next();
            try {
                if (conn == null || conn.isReallyClosed()) {
                    poolIter.remove();
                } else if (!conn.isClosed()) {
                    return conn;
                }
            } catch (SQLException e) {
                log.error("Bad connection", e);
                poolIter.remove();
            }
        }

        // No valid connections
        ConnectionWrapper conn = createConnection();
        if (conn != null) {
            connectionPool.add(conn);
            log.info("Connection pool size: " + connectionPool.size());
        }
        return conn;

    }


    private static ConnectionWrapper createConnection() {
        String driver = "com.mysql.jdbc.Driver";
        try {
            Class.forName(driver).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        final PreferenceManager preferenceManager = PreferenceManager.getInstance();
        String host = preferenceManager.get(PreferenceManager.DB_HOST);
        String db = preferenceManager.get(PreferenceManager.DB_NAME);
        String port = preferenceManager.get(PreferenceManager.DB_PORT);

        String url = "jdbc:mysql://" + host;
        if (!port.equals("-1")) {
            url += ":" + port;
        }
        url += "/" + db;

        return connect(url);
    }

    private static ConnectionWrapper connect(String url) {
        try {
            return new ConnectionWrapper(DriverManager.getConnection(url, username, password));
        } catch (SQLException e) {
            int errorCode = e.getErrorCode();
            if (errorCode == 1044 || errorCode == 1045) {
                String host = PreferenceManager.getInstance().get(PreferenceManager.DB_HOST);

                Frame parent = Globals.isHeadless() ? null : IGV.getMainFrame();
                LoginDialog dlg = new LoginDialog(parent, false, host, false);
                dlg.setVisible(true);
                if (dlg.isCanceled()) {
                    throw new RuntimeException("Must login to access" + host);
                }
                username = dlg.getUsername();
                password = new String(<PASSWORD>());
                return connect(url);

            } else {
                MessageUtils.showMessage("<html>Error connecting to database: <br>" + e.getMessage());
                return null;
            }

        }
    }

    public static void shutdown() {
        for (ConnectionWrapper conn : connectionPool) {
            if (conn != null) {
                try {
                    conn.reallyClose();
                } catch (SQLException e) {

                }
            }
        }
        connectionPool.clear();
    }


    static class ConnectionWrapper implements Connection {

        Connection conn;
        boolean closed;

        ConnectionWrapper(Connection conn) {
            this.conn = conn;
            closed = false;
        }

        public void close() throws SQLException {
            closed = true;
        }

        public boolean isClosed() throws SQLException {
            return closed;
        }

        public void reallyClose() throws SQLException {
            closed = true;
            conn.close();
        }

        public boolean isReallyClosed() throws SQLException {
            return conn.isClosed();
        }


        public void clearWarnings() throws SQLException {
            conn.clearWarnings();
        }

        public void commit() throws SQLException {
            conn.commit();
        }

        public Array createArrayOf(String s, Object[] objects) throws SQLException {
            return conn.createArrayOf(s, objects);
        }

        public Blob createBlob() throws SQLException {
            return conn.createBlob();
        }

        public Clob createClob() throws SQLException {
            return conn.createClob();
        }

        public NClob createNClob() throws SQLException {
            return conn.createNClob();
        }

        public SQLXML createSQLXML() throws SQLException {
            return conn.createSQLXML();
        }

        public Statement createStatement() throws SQLException {
            return conn.createStatement();
        }

        public Statement createStatement(int i, int i1) throws SQLException {
            return conn.createStatement(i, i1);
        }

        public Statement createStatement(int i, int i1, int i2) throws SQLException {
            return conn.createStatement(i, i1, i2);
        }

        public Struct createStruct(String s, Object[] objects) throws SQLException {
            return conn.createStruct(s, objects);
        }

        public void setSchema(String schema) throws SQLException {
            throw new UnsupportedOperationException("Operation not supported for backwards compatibility to Java 6");
        }

        public String getSchema() throws SQLException {
            return null; //TODO
        }

        public void abort(Executor executor) throws SQLException {
            throw new UnsupportedOperationException("Operation not supported for backwards compatibility to Java 6");
        }

        public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
            throw new UnsupportedOperationException("Operation not supported for backwards compatibility to Java 6");
        }

        public int getNetworkTimeout() {
            return -1;
        }

        public boolean getAutoCommit() throws SQLException {
            return conn.getAutoCommit();
        }
{'fraction_non_alphanumeric': 0.09127103365384616, 'fraction_numerical': 0.00390625, 'mean_word_length': 1.9466578131916776, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2479317', 'n_tokens_mistral': 3304, 'n_tokens_neox': 3124, 'n_words': 885}