Search in sources :

Example 1 with CouldntConnectException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException in project binnavi by google.

the class PostgreSQLTypeInstanceFunctionsTests method setUp.

@Before
public void setUp() throws IOException, CouldntLoadDriverException, CouldntConnectException, IllegalStateException, CouldntLoadDataException, InvalidDatabaseException, CouldntInitializeDatabaseException, CouldntSaveDataException, InvalidExporterDatabaseFormatException, InvalidDatabaseVersionException, LoadCancelledException, FileReadException {
    final String[] parts = CConfigLoader.loadPostgreSQL();
    database = new CDatabase("None", CJdbcDriverNames.jdbcPostgreSQLDriverName, parts[0], "test_disassembly", parts[1], parts[2], parts[3], false, false);
    database.connect();
    database.load();
    try {
        final Field privateProviderField = CDatabase.class.getDeclaredField("provider");
        privateProviderField.setAccessible(true);
        provider = (SQLProvider) privateProviderField.get(database);
    } catch (final Exception exception) {
        throw new RuntimeException(exception);
    }
    provider.createDebuggerTemplate("Test Debugger", "localhost", 2222);
    final CProject project = provider.createProject("Test Project");
    provider.createAddressSpace(project, "Test Address Space");
    ConfigManager.instance().read();
    module = database.getContent().getModules().get(0);
}
Also used : Field(java.lang.reflect.Field) CProject(com.google.security.zynamics.binnavi.disassembly.CProject) CDatabase(com.google.security.zynamics.binnavi.Database.CDatabase) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException) CouldntInitializeDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntInitializeDatabaseException) CouldntLoadDriverException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDriverException) InvalidExporterDatabaseFormatException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidExporterDatabaseFormatException) FileReadException(com.google.security.zynamics.binnavi.config.FileReadException) InvalidDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseException) CouldntConnectException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) IOException(java.io.IOException) InvalidDatabaseVersionException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseVersionException) CPartialLoadException(com.google.security.zynamics.binnavi.Database.Exceptions.CPartialLoadException) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) Before(org.junit.Before)

Example 2 with CouldntConnectException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException in project binnavi by google.

the class CDatabaseConnection method connect.

public static Pair<CConnection, SQLProvider> connect(final CDatabaseConfiguration m_databaseConfiguration, final IDatabaseLoadProgressReporter<LoadEvents> reporter) throws CouldntLoadDriverException, CouldntConnectException, CouldntInitializeDatabaseException, InvalidDatabaseException, InvalidExporterDatabaseFormatException, LoadCancelledException {
    try {
        reportProgress(reporter, LoadEvents.CONNECTING_TO_DATABASE);
        final CConnection connection = new CConnection(m_databaseConfiguration);
        final AbstractSQLProvider sql = new PostgreSQLProvider(connection);
        reportProgress(reporter, LoadEvents.CHECKING_EXPORTER_TABLE_FORMAT);
        if (!sql.isExporterDatabaseFormatValid()) {
            throw new InvalidExporterDatabaseFormatException("E00202: Database exporter format is not compatible with " + Constants.PROJECT_NAME_VERSION);
        }
        reportProgress(reporter, LoadEvents.CHECKING_INITIALIZATION_STATUS);
        final DatabaseVersion databaseVersion = sql.getDatabaseVersion();
        final DatabaseVersion currentVersion = new DatabaseVersion(Constants.PROJECT_VERSION);
        if ((databaseVersion.compareTo(currentVersion) == 0) && !sql.isInitialized()) {
            reportProgress(reporter, LoadEvents.INITIALIZING_DATABASE_TABLES);
            sql.initializeDatabase();
        }
        if ((databaseVersion.compareTo(currentVersion) == 0) && !sql.isInitialized()) {
            throw new CouldntInitializeDatabaseException("E00052: Database could not be initialized.");
        }
        return new Pair<CConnection, SQLProvider>(connection, sql);
    } catch (final CouldntLoadDataException e) {
        throw new CouldntConnectException(e, 0, "");
    } catch (final SQLException e) {
        throw new CouldntConnectException(e, e.getErrorCode(), e.getSQLState());
    }
}
Also used : SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) CouldntInitializeDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntInitializeDatabaseException) CouldntConnectException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException) InvalidExporterDatabaseFormatException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidExporterDatabaseFormatException) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 3 with CouldntConnectException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException in project binnavi by google.

the class CDatabaseLoader method loadDatabase.

/**
   * Loads the content of a database.
   * 
   * @param parent Parent window used for dialogs.
   * @param database The database to load.
   */
public static void loadDatabase(final Window parent, final IDatabase database) {
    final CDatabaseLoaderOperation operation = new CDatabaseLoaderOperation(database);
    try {
        database.connect();
        database.load();
    } catch (final CouldntLoadDriverException exception) {
        final String message = "E00012: " + "Database driver could not be loaded";
        final String description = CUtilityFunctions.createDescription(String.format("BinNavi could not create a database connection because the database " + "driver '%s' could not be loaded", database.getConfiguration().getDriver()), new String[] { "The database driver string is wrong.", "The database driver file could not be found." }, new String[] { "BinNavi can not load data from the given database until the " + "problem is resolved." });
        NaviErrorDialog.show(parent, message, description, exception);
    } catch (final CouldntLoadDataException exception) {
        final String message = "E00014: " + "Could not load data from the database";
        final String description = CUtilityFunctions.createDescription("An error occurred when loading data from the database.", new String[] { "The connection to the database was dropped while the data was loaded.", "The database contains inconsistent information." }, new String[] { "Close the database and open it again. Maybe close and re-start " + "BinNavi too. If the program persists, please contact the BinNavi support." });
        NaviErrorDialog.show(parent, message, description, exception);
    } catch (final InvalidDatabaseException exception) {
        final String message = "E00015: " + "Database is in an inconsistent state";
        final String description = CUtilityFunctions.createDescription("The selected database contains an invalid combination of BinNavi tables.", new String[] { "An earlier connection attempt failed and left the database in an " + "inconsistent state.", "Some BinNavi tables were deleted accidentally by an outside program." }, new String[] { "BinNavi can not use this database anymore. If the database is " + "empty, please delete the database and create a new database to work with " + "BinNavi. If the database already contains data please contact the BinNavi " + "support." });
        NaviErrorDialog.show(parent, message, description, exception);
    } catch (final CouldntInitializeDatabaseException exception) {
        final String message = "E00016: Database could not be initialized";
        final String description = CUtilityFunctions.createDescription("BinNavi could not initialize the tables required for storing disassembly data " + "in the database.", new String[] { "There might have been a communication problem with the database." }, new String[] { "The database is probably corrupted at this point. It is " + "recommended to delete the database. Afterwards you can try again with a " + "fresh database. If you do not want to do this please contact the BinNavi " + "support to find out what other options exist for you." });
        NaviErrorDialog.show(parent, message, description, exception);
    } catch (final InvalidExporterDatabaseFormatException exception) {
        final String message = "E00017: " + "Database has invalid exporter tables";
        final String description = CUtilityFunctions.createDescription("BinNavi could not load data from the selected database because the database " + "contains invalid exporter tables", new String[] { "The database is too old to use with BinNavi." }, new String[] { "It is recommended to create a database for this version of " + "BinNavi. If you do not want to do this please contact the BinNavi support " + "to find out what other options exist for you." });
        NaviErrorDialog.show(parent, message, description, exception);
    } catch (final InvalidDatabaseVersionException exception) {
        final String exceptionVersion = exception.getVersion().getString();
        if (!exceptionVersion.equals("4.0.0") || !exceptionVersion.equals("5.0.0")) {
            CMessageBox.showInformation(parent, String.format("You are trying to connect to an outdated BinNavi %s database.\n\n" + "Unfortunately you can not upgrade this database. Please create a " + "new database and export your modules again.", exceptionVersion));
        } else {
            CMessageBox.showInformation(parent, String.format("You are trying to connect to an outdated BinNavi %s database.\n\n" + "You have the option to update the database.", exceptionVersion));
            if (JOptionPane.YES_OPTION == CMessageBox.showYesNoQuestion(parent, "Do you want to upgrade the database now?\n\n(The upgrade process can take " + "very long depending on the size of your current database)\n\n(Make " + "sure that the identity field contains a user name)")) {
                final CDefaultProgressOperation updateOperation = new CDefaultProgressOperation("Upgrading database", true, false);
                updateOperation.getProgressPanel().setText("Upgrading database");
                try {
                    database.update();
                    database.close();
                } catch (final CouldntUpdateDatabaseException upgradeException) {
                    CUtilityFunctions.logException(upgradeException);
                    final String message = "E00018: " + "Database could not be upgraded";
                    final String description = CUtilityFunctions.createDescription(String.format("BinNavi could not upgrade the database (database error %d). " + "This is a serious problem because the database could be " + "left in an inconsistent state. Please try to fix the " + "problem that led to the error and try to update the " + "database again.", upgradeException.getErrorCode()), new String[] { getErrorCode(upgradeException) }, new String[] { "Please note that nobody must work with this database " + "until the database conversion process is complete. If someone " + "works with the database in its current state, partial or total " + "data loss could happen." });
                    NaviErrorDialog.show(parent, message, description, exception);
                } finally {
                    updateOperation.stop();
                }
                loadDatabase(parent, database);
            } else {
                database.close();
            }
        }
    } catch (final CouldntConnectException exception) {
        final CDatabaseConfiguration config = database.getConfiguration();
        if (exception.getSqlState().equalsIgnoreCase(PostgreSQLErrorCodes.INVALID_PASSWORD)) {
            CMessageBox.showInformation(parent, String.format("The password for user '%s' on database '%s' is invalid", config.getUser(), config.getUrl()));
            return;
        }
        if (exception.getSqlState().equalsIgnoreCase(PostgreSQLErrorCodes.POSTGRES_INVALID_CATALOG_NAME)) {
            if (JOptionPane.YES_OPTION == CMessageBox.showYesNoCancelQuestion(parent, String.format("The database '%s' does not exist. Do you want to create it now?", config.getUrl()))) {
                CDatabaseCreator.createDatabase(parent, config);
            }
        } else {
            final String message = "E00013: Database connection could not be established";
            final String description = CUtilityFunctions.createDescription(String.format("BinNavi could not connect to the database '%s'", database.getConfiguration().getName()), new String[] { exception.getMessage() }, new String[] { "BinNavi can not load data from the given database until the " + "problem is resolved." });
            NaviErrorDialog.show(parent, message, description, exception);
        }
    } catch (final LoadCancelledException exception) {
    // We do not signal to the user that he cancelled loading.
    } finally {
        operation.stop();
    }
}
Also used : InvalidDatabaseVersionException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseVersionException) CouldntUpdateDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntUpdateDatabaseException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) CouldntInitializeDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntInitializeDatabaseException) InvalidExporterDatabaseFormatException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidExporterDatabaseFormatException) CouldntLoadDriverException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDriverException) CDefaultProgressOperation(com.google.security.zynamics.binnavi.Gui.Progress.CDefaultProgressOperation) InvalidDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseException) CouldntConnectException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException)

Example 4 with CouldntConnectException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException in project binnavi by google.

the class CDatabase method connect.

@Override
public void connect() throws CouldntLoadDriverException, CouldntConnectException, InvalidDatabaseException, CouldntInitializeDatabaseException, InvalidExporterDatabaseFormatException, LoadCancelledException {
    loadReporter.start();
    isConnecting = true;
    try {
        final Pair<CConnection, SQLProvider> connectionData = CDatabaseConnection.connect(description, loadReporter);
        provider = connectionData.second();
    } catch (final CouldntLoadDriverException exception) {
        loadReporter.report(LoadEvents.LOADING_FINISHED);
        throw exception;
    } catch (final CouldntConnectException exception) {
        loadReporter.report(LoadEvents.LOADING_FINISHED);
        throw exception;
    } catch (final CouldntInitializeDatabaseException exception) {
        loadReporter.report(LoadEvents.LOADING_FINISHED);
        throw exception;
    } catch (final InvalidDatabaseException exception) {
        loadReporter.report(LoadEvents.LOADING_FINISHED);
        throw exception;
    } catch (final InvalidExporterDatabaseFormatException exception) {
        loadReporter.report(LoadEvents.LOADING_FINISHED);
        throw exception;
    } catch (final LoadCancelledException exception) {
        loadReporter.report(LoadEvents.LOADING_FINISHED);
        throw exception;
    } finally {
        isConnecting = false;
    }
    for (final IDatabaseListener listener : listeners) {
        try {
            listener.openedDatabase(this);
        } catch (final Exception exception) {
            CUtilityFunctions.logException(exception);
        }
    }
    try {
        final PostgreSQLNotificationProvider notificationProvider = PostgreSQLNotificationProvider.initialize(provider, description);
        notificationProvider.listen(NotificationChannel.all());
        notificationProvider.startPolling();
    } catch (final SQLException exception) {
        NaviLogger.severe("Error: Could not establish a channel for receiving notifications from the database %s", exception);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntInitializeDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntInitializeDatabaseException) InvalidExporterDatabaseFormatException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidExporterDatabaseFormatException) SQLProvider(com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider) IDatabaseListener(com.google.security.zynamics.binnavi.Database.Interfaces.IDatabaseListener) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException) CouldntInitializeDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntInitializeDatabaseException) CouldntLoadDriverException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDriverException) InvalidExporterDatabaseFormatException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidExporterDatabaseFormatException) SQLException(java.sql.SQLException) InvalidDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseException) CouldntConnectException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) CouldntUpdateDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntUpdateDatabaseException) InvalidDatabaseVersionException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseVersionException) CouldntLoadDriverException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDriverException) PostgreSQLNotificationProvider(com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.PostgreSQLNotificationProvider) InvalidDatabaseException(com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseException) CouldntConnectException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException)

Example 5 with CouldntConnectException

use of com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException in project binnavi by google.

the class CDatabaseConnector method initialize.

/**
   * Initializes a new PostgreSQL database
   *
   * @param configuration The configuration where the database name for database creation is taken
   *        from.
   *
   * @throws CouldntLoadDriverException if the JDBC driver could not be loaded.
   * @throws CouldntConnectException if the JDBC driver could not establish a connection to the
   *         database.
   *
   * @throws SQLException if the statement executed on the database failed.
   */
public static void initialize(final CDatabaseConfiguration configuration) throws CouldntLoadDriverException, CouldntConnectException, SQLException {
    checkDriver(configuration);
    final String url = POSTGRES_DRIVER + "//" + configuration.getHost() + "/" + POSTGRES_DEFAULT_DB;
    Connection connection = null;
    try {
        connection = DriverManager.getConnection(url, configuration.getUser(), configuration.getPassword());
    } catch (final SQLException exception) {
        CUtilityFunctions.logException(exception);
        throw new CouldntConnectException(exception, exception.getErrorCode(), exception.getSQLState());
    }
    final String statement = "CREATE DATABASE \"" + configuration.getName() + "\"";
    final PreparedStatement preparedStatement = connection.prepareStatement(statement);
    try {
        preparedStatement.execute();
    } finally {
        preparedStatement.close();
        connection.close();
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CouldntConnectException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

CouldntConnectException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntConnectException)5 CouldntInitializeDatabaseException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntInitializeDatabaseException)4 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)4 InvalidExporterDatabaseFormatException (com.google.security.zynamics.binnavi.Database.Exceptions.InvalidExporterDatabaseFormatException)4 CouldntLoadDriverException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDriverException)3 InvalidDatabaseException (com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseException)3 InvalidDatabaseVersionException (com.google.security.zynamics.binnavi.Database.Exceptions.InvalidDatabaseVersionException)3 LoadCancelledException (com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException)3 SQLException (java.sql.SQLException)3 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)2 CouldntUpdateDatabaseException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntUpdateDatabaseException)2 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)1 CDatabase (com.google.security.zynamics.binnavi.Database.CDatabase)1 CPartialLoadException (com.google.security.zynamics.binnavi.Database.Exceptions.CPartialLoadException)1 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)1 IDatabaseListener (com.google.security.zynamics.binnavi.Database.Interfaces.IDatabaseListener)1 SQLProvider (com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider)1 PostgreSQLNotificationProvider (com.google.security.zynamics.binnavi.Database.PostgreSQL.Notifications.PostgreSQLNotificationProvider)1 CDefaultProgressOperation (com.google.security.zynamics.binnavi.Gui.Progress.CDefaultProgressOperation)1 FileReadException (com.google.security.zynamics.binnavi.config.FileReadException)1