protected void doInitialize()

in kerby-backend/mysql-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/MySQLIdentityBackend.java [101:190]


    protected void doInitialize() throws KrbException {
        LOG.info("Initializing the MySQL identity backend.");

        // Initialize data base connection pool
        if (MySQLIdentityBackend.dataSource == null) {
            String driver = getConfig().getString(MySQLConfKey.MYSQL_DRIVER, true);
            String user = getConfig().getString(MySQLConfKey.MYSQL_USER, true);
            String password = getConfig().getString(MySQLConfKey.MYSQL_PASSWORD, true);

            String urlString = getConfig().getString(MySQLConfKey.MYSQL_URL, true);
            if (urlString == null || urlString.isEmpty()) {
                urlString = getBackendConfig().getString(MySQLConfKey.MYSQL_URL, true);
            }

            try {
                initializeDataSource(driver, urlString, user, password);
            } catch (SQLException e) {
                LOG.error("Failed to initialize data source. " + e.toString());
                throw new KrbException("Failed to initialize data source.", e);
            }
        }

        Connection connection = null;
        ResultSet resCheckTable = null;
        PreparedStatement preInitialize = null;
        PreparedStatement preKdcRealm = null;
        ResultSet resKdcRealm = null;
        PreparedStatement preIdentity = null;
        PreparedStatement preKey = null;
        try {
            connection = dataSource.getConnection();

            resCheckTable = connection.getMetaData().getTables(null, null, "kdc_config", null);
            if (resCheckTable.next()) {
                // Set initialized for kdc config table if HAS enabled
                String stmInitialize = "UPDATE `kdc_config` SET initialized = true WHERE id = 1";
                preInitialize = connection.prepareStatement(stmInitialize);
                preInitialize.executeUpdate();

                // Get identity table name according to realm of kdc
                String stmKdcRealm = "SELECT realm FROM `kdc_config`";
                preKdcRealm = connection.prepareStatement(stmKdcRealm);
                resKdcRealm = preKdcRealm.executeQuery();
                if (resKdcRealm.next()) {
                    String realm = resKdcRealm.getString("realm").toLowerCase();
                    identityTable = "`" + realm + "_identity" + "`";
                    keyInfoTable = "`" + realm + "_key" + "`";
                } else {
                    throw new KrbException("Failed to get kdc config.");
                }
            } else {
                identityTable = "`" + "kerby_identity" + "`";
                keyInfoTable = "`" + "kerby_key" + "`";
            }

            // Create identity table
            String stmIdentity = "CREATE TABLE IF NOT EXISTS " + identityTable
                + " (principal varchar(255) NOT NULL, key_version INTEGER "
                + "DEFAULT 1, kdc_flags INTEGER DEFAULT 0, disabled bool "
                + "DEFAULT NULL, locked bool DEFAULT NULL, created_time "
                + "BIGINT DEFAULT 0, expire_time BIGINT DEFAULT 0, "
                + "PRIMARY KEY (principal) ) ENGINE=INNODB "
                + "DEFAULT CHARSET=utf8;";
            preIdentity = connection.prepareStatement(stmIdentity);
            preIdentity.executeUpdate();

            // Create key table
            String stmKey = "CREATE TABLE IF NOT EXISTS " + keyInfoTable
                + " (key_id INTEGER NOT NULL AUTO_INCREMENT, key_type "
                + "VARCHAR(255) DEFAULT NULL, kvno INTEGER DEFAULT -1, "
                + "key_value BLOB DEFAULT NULL, principal VARCHAR(255) NOT NULL,"
                + "PRIMARY KEY (key_id), INDEX (principal), FOREIGN KEY "
                + "(principal) REFERENCES " + identityTable + "(principal) "
                + ") ENGINE=INNODB DEFAULT CHARSET=utf8;";
            preKey = connection.prepareStatement(stmKey);
            preKey.executeUpdate();

        } catch (SQLException e) {
            LOG.error("Error occurred while initialize MySQL backend.", e);
            throw new KrbException("Failed to create table in database. ", e);
        } finally {
            DbUtils.closeQuietly(resCheckTable);
            DbUtils.closeQuietly(preInitialize);
            DbUtils.closeQuietly(preKdcRealm);
            DbUtils.closeQuietly(resKdcRealm);
            DbUtils.closeQuietly(preIdentity);
            DbUtils.closeQuietly(preKey);
            DbUtils.closeQuietly(connection);
        }
    }