<commit_before>package org.dbflute.erflute.db;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.StringTokenizer;

import org.dbflute.erflute.editor.model.settings.JDBCDriverSetting;
import org.dbflute.erflute.preference.PreferenceInitializer;
import org.dbflute.erflute.preference.jdbc.JDBCPathDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.ui.PlatformUI;

/**
 * @author modified by jflute (originated in ermaster)
 */
public abstract class DBManagerBase implements DBManager {

    private final Set<String> reservedWords;

    public DBManagerBase() {
        DBManagerFactory.addDB(this);
        this.reservedWords = this.getReservedWords();
    }

    @Override
    public String getURL(String serverName, String dbName, int port) {
        String temp = serverName.replaceAll("\\\\", "\\\\\\\\");
        String url = this.getURL().replaceAll("<SERVER NAME>", temp);
        url = url.replaceAll("<PORT>", String.valueOf(port));
        temp = dbName.replaceAll("\\\\", "\\\\\\\\");
        url = url.replaceAll("<DB NAME>", temp);
        return url;
    }

    @Override
    public Class<Driver> getDriverClass(String driverClassName) {
        String path = null;
        try {
            try {
                @SuppressWarnings("unchecked")
                final Class<Driver> clazz = (Class<Driver>) Class.forName(driverClassName);
                return clazz;
            } catch (final ClassNotFoundException e) {
                path = PreferenceInitializer.getJDBCDriverPath(this.getId(), driverClassName);
                final ClassLoader loader = this.getClassLoader(path);
                @SuppressWarnings("unchecked")
                final Class<Driver> clazz = (Class<Driver>) loader.loadClass(driverClassName);
                return clazz;
            }
        } catch (final MalformedURLException | ClassNotFoundException e) {
            final JDBCPathDialog dialog = new JDBCPathDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), this.getId(),
                    driverClassName, path, new ArrayList<JDBCDriverSetting>(), false);
            if (dialog.open() == IDialogConstants.OK_ID) {
                final JDBCDriverSetting newDriverSetting =
                        new JDBCDriverSetting(this.getId(), dialog.getDriverClassName(), dialog.getPath());
                final List<JDBCDriverSetting> driverSettingList = PreferenceInitializer.getJDBCDriverSettingList();
                if (driverSettingList.contains(newDriverSetting)) {
                    driverSettingList.remove(newDriverSetting);
                }
                driverSettingList.add(newDriverSetting);
                PreferenceInitializer.saveJDBCDriverSettingList(driverSettingList);
                return this.getDriverClass(dialog.getDriverClassName());
            }
        }
        return null;
    }

    private ClassLoader getClassLoader(String uri) throws MalformedURLException {<commit_msg>Added error message when "JDBC Driver Class" can not be found.<commit_after>package org.dbflute.erflute.db;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.StringTokenizer;

import org.dbflute.erflute.core.util.Check;
import org.dbflute.erflute.editor.model.settings.JDBCDriverSetting;
import org.dbflute.erflute.preference.PreferenceInitializer;
import org.dbflute.erflute.preference.jdbc.JDBCPathDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.ui.PlatformUI;

/**
 * @author modified by jflute (originated in ermaster)
 */
public abstract class DBManagerBase implements DBManager {

    private final Set<String> reservedWords;

    public DBManagerBase() {
        DBManagerFactory.addDB(this);
        this.reservedWords = this.getReservedWords();
    }

    @Override
    public String getURL(String serverName, String dbName, int port) {
        String temp = serverName.replaceAll("\\\\", "\\\\\\\\");
        String url = this.getURL().replaceAll("<SERVER NAME>", temp);
        url = url.replaceAll("<PORT>", String.valueOf(port));
        temp = dbName.replaceAll("\\\\", "\\\\\\\\");
        url = url.replaceAll("<DB NAME>", temp);
        return url;
    }

    @Override
    public Class<Driver> getDriverClass(String driverClassName) {
        String path = null;
        try {
            try {
                @SuppressWarnings("unchecked")
                final Class<Driver> clazz = (Class<Driver>) Class.forName(driverClassName);
                return clazz;
            } catch (final ClassNotFoundException e) {
                path = PreferenceInitializer.getJDBCDriverPath(this.getId(), driverClassName);
                if (Check.isEmpty(path)) {
                    throw new IllegalStateException(String.format("JDBC Driver Class \"%s\" is not found", driverClassName));
                }
                final ClassLoader loader = this.getClassLoader(path);
                @SuppressWarnings("unchecked")
                final Class<Driver> clazz = (Class<Driver>) loader.loadClass(driverClassName);
                return clazz;
            }
        } catch (final MalformedURLException | ClassNotFoundException e) {
            final JDBCPathDialog dialog = new JDBCPathDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), this.getId(),
                    driverClassName, path, new ArrayList<JDBCDriverSetting>(), false);
            if (dialog.open() == IDialogConstants.OK_ID) {
                final JDBCDriverSetting newDriverSetting =
                        new JDBCDriverSetting(this.getId(), dialog.getDriverClassName(), dialog.getPath());
                final List<JDBCDriverSetting> driverSettingList = PreferenceInitializer.getJDBCDriverSettingList();
                if (driverSettingList.contains(newDriverSetting)) {
                    driverSettingList.remove(newDriverSetting);
                }
                driverSettingList.add(newDriverSetting);
                PreferenceInitializer.saveJDBCDriverSettingList(driverSettingList);
                return this.getDriverClass(dialog.getDriverClassName());
            }
        }
        return null;
    }

    private ClassLoader getClassLoader(String uri) throws MalformedURLException {
{'fraction_non_alphanumeric': 0.10544511668107173, 'fraction_numerical': 0.0, 'mean_word_length': 2.8550805108273183, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '2358592', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1822, 'n_words': 372}