Search in sources :

Example 11 with DBOpException

use of com.djrapitops.plan.exceptions.database.DBOpException in project Plan by plan-player-analytics.

the class DatabaseCommands method onHotswap.

public void onHotswap(CMDSender sender, Arguments arguments) {
    DBType toDB = arguments.get(0).flatMap(DBType::getForName).orElseThrow(() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_INCORRECT_DB, arguments.get(0).orElse("<MySQL/SQLite>"))));
    try {
        Database database = dbSystem.getActiveDatabaseByType(toDB);
        database.init();
        if (database.getState() == Database.State.CLOSED) {
            return;
        }
        config.set(DatabaseSettings.TYPE, toDB.getName());
        config.save();
    } catch (DBOpException | IOException e) {
        errorLogger.warn(e, ErrorContext.builder().related(toDB).build());
        sender.send(locale.getString(CommandLang.PROGRESS_FAIL, e.getMessage()));
        return;
    }
    statusCommands.onReload(sender);
}
Also used : DBOpException(com.djrapitops.plan.exceptions.database.DBOpException) Database(com.djrapitops.plan.storage.database.Database) DBType(com.djrapitops.plan.storage.database.DBType) IOException(java.io.IOException)

Example 12 with DBOpException

use of com.djrapitops.plan.exceptions.database.DBOpException in project Plan by plan-player-analytics.

the class RegistrationCommands method registerUser.

private void registerUser(User user, CMDSender sender, int permissionLevel) {
    String username = user.getUsername();
    user.setPermissionLevel(permissionLevel);
    try {
        Database database = dbSystem.getDatabase();
        boolean userExists = database.query(WebUserQueries.fetchUser(username)).isPresent();
        if (userExists)
            throw new IllegalArgumentException(locale.getString(CommandLang.FAIL_WEB_USER_EXISTS));
        database.executeTransaction(new RegisterWebUserTransaction(user)).get();
        sender.send(locale.getString(CommandLang.WEB_USER_REGISTER_SUCCESS, username));
        logger.info(locale.getString(CommandLang.WEB_USER_REGISTER_NOTIFY, username, permissionLevel));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (DBOpException | ExecutionException e) {
        errorLogger.warn(e, ErrorContext.builder().related(sender, user, permissionLevel).build());
    }
}
Also used : DBOpException(com.djrapitops.plan.exceptions.database.DBOpException) RegisterWebUserTransaction(com.djrapitops.plan.storage.database.transactions.commands.RegisterWebUserTransaction) Database(com.djrapitops.plan.storage.database.Database) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with DBOpException

use of com.djrapitops.plan.exceptions.database.DBOpException in project Plan by plan-player-analytics.

the class BasicAuthentication method getUser.

@Override
public User getUser() {
    String decoded = Base64Util.decode(authenticationString);
    String[] userInfo = StringUtils.split(decoded, ':');
    if (userInfo.length != 2) {
        throw new WebUserAuthException(FailReason.USER_AND_PASS_NOT_SPECIFIED, Arrays.toString(userInfo));
    }
    String username = userInfo[0];
    String passwordRaw = userInfo[1];
    Database.State dbState = database.getState();
    if (dbState != Database.State.OPEN) {
        throw new WebUserAuthException(FailReason.DATABASE_NOT_OPEN, "State was: " + dbState.name());
    }
    try {
        User user = database.query(WebUserQueries.fetchUser(username)).orElseThrow(() -> new WebUserAuthException(FailReason.USER_DOES_NOT_EXIST, username));
        boolean correctPass = user.doesPasswordMatch(passwordRaw);
        if (!correctPass) {
            throw new WebUserAuthException(FailReason.USER_PASS_MISMATCH, username);
        }
        return user;
    } catch (DBOpException | PassEncryptException e) {
        throw new WebUserAuthException(e);
    }
}
Also used : DBOpException(com.djrapitops.plan.exceptions.database.DBOpException) User(com.djrapitops.plan.delivery.domain.auth.User) PassEncryptException(com.djrapitops.plan.exceptions.PassEncryptException) Database(com.djrapitops.plan.storage.database.Database) WebUserAuthException(com.djrapitops.plan.exceptions.WebUserAuthException)

Example 14 with DBOpException

use of com.djrapitops.plan.exceptions.database.DBOpException in project Plan by plan-player-analytics.

the class DBCleanTask method run.

@Override
public void run() {
    Database database = dbSystem.getDatabase();
    try {
        if (database.getState() != Database.State.CLOSED) {
            database.executeTransaction(new RemoveOldSampledDataTransaction(serverInfo.getServerUUID(), config.get(TimeSettings.DELETE_TPS_DATA_AFTER), config.get(TimeSettings.DELETE_PING_DATA_AFTER)));
            database.executeTransaction(new RemoveDuplicateUserInfoTransaction());
            database.executeTransaction(new RemoveUnsatisfiedConditionalPlayerResultsTransaction());
            database.executeTransaction(new RemoveUnsatisfiedConditionalServerResultsTransaction());
            int removed = cleanOldPlayers(database);
            if (removed > 0) {
                logger.info(locale.getString(PluginLang.DB_NOTIFY_CLEAN, removed));
            }
            Long deleteExtensionDataAfter = config.get(TimeSettings.DELETE_EXTENSION_DATA_AFTER);
            Long databaseCleanPeriod = config.get(TimeSettings.CLEAN_DATABASE_PERIOD);
            if (databaseCleanPeriod > deleteExtensionDataAfter) {
                logger.warn("Data of Disabled Extensions can not be cleaned due to " + TimeSettings.CLEAN_DATABASE_PERIOD.getPath() + " being larger than " + TimeSettings.DELETE_EXTENSION_DATA_AFTER.getPath());
            }
            // for plugins being deleted all the time.
            if (System.currentTimeMillis() - lastReload <= deleteExtensionDataAfter) {
                database.executeTransaction(new RemoveOldExtensionsTransaction(config.getExtensionSettings(), deleteExtensionDataAfter, serverInfo.getServerUUID()));
            }
        }
    } catch (DBOpException e) {
        errorLogger.error(e);
        cancel();
    }
}
Also used : DBOpException(com.djrapitops.plan.exceptions.database.DBOpException) RemoveUnsatisfiedConditionalPlayerResultsTransaction(com.djrapitops.plan.extension.implementation.storage.transactions.results.RemoveUnsatisfiedConditionalPlayerResultsTransaction) RemoveOldExtensionsTransaction(com.djrapitops.plan.storage.database.transactions.init.RemoveOldExtensionsTransaction) RemoveOldSampledDataTransaction(com.djrapitops.plan.storage.database.transactions.init.RemoveOldSampledDataTransaction) RemoveUnsatisfiedConditionalServerResultsTransaction(com.djrapitops.plan.extension.implementation.storage.transactions.results.RemoveUnsatisfiedConditionalServerResultsTransaction) Database(com.djrapitops.plan.storage.database.Database) RemoveDuplicateUserInfoTransaction(com.djrapitops.plan.storage.database.transactions.init.RemoveDuplicateUserInfoTransaction)

Example 15 with DBOpException

use of com.djrapitops.plan.exceptions.database.DBOpException in project Plan by plan-player-analytics.

the class PingStoreTransaction method performOperations.

@Override
protected void performOperations() {
    Ping ping = calculateAggregatePing();
    DBOpException userInsertError = null;
    if (Boolean.FALSE.equals(query(PlayerFetchQueries.isPlayerRegistered(playerUUID)))) {
        userInsertError = tryToRegisterUser(ping.getDate());
    }
    try {
        execute(DataStoreQueries.storePing(playerUUID, serverUUID, ping));
    } catch (DBOpException failed) {
        if (userInsertError != null)
            failed.addSuppressed(userInsertError);
        if (failed.isUserIdConstraintViolation()) {
            retry(ping, failed);
        } else {
            throw failed;
        }
    }
}
Also used : DBOpException(com.djrapitops.plan.exceptions.database.DBOpException) Ping(com.djrapitops.plan.gathering.domain.Ping)

Aggregations

DBOpException (com.djrapitops.plan.exceptions.database.DBOpException)18 Database (com.djrapitops.plan.storage.database.Database)6 ExecutionException (java.util.concurrent.ExecutionException)4 BackupCopyTransaction (com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction)2 User (com.djrapitops.plan.delivery.domain.auth.User)1 PassEncryptException (com.djrapitops.plan.exceptions.PassEncryptException)1 WebUserAuthException (com.djrapitops.plan.exceptions.WebUserAuthException)1 RemoveUnsatisfiedConditionalPlayerResultsTransaction (com.djrapitops.plan.extension.implementation.storage.transactions.results.RemoveUnsatisfiedConditionalPlayerResultsTransaction)1 RemoveUnsatisfiedConditionalServerResultsTransaction (com.djrapitops.plan.extension.implementation.storage.transactions.results.RemoveUnsatisfiedConditionalServerResultsTransaction)1 Ping (com.djrapitops.plan.gathering.domain.Ping)1 DBType (com.djrapitops.plan.storage.database.DBType)1 SQLiteDB (com.djrapitops.plan.storage.database.SQLiteDB)1 RegisterWebUserTransaction (com.djrapitops.plan.storage.database.transactions.commands.RegisterWebUserTransaction)1 RemoveEverythingTransaction (com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction)1 RemoveDuplicateUserInfoTransaction (com.djrapitops.plan.storage.database.transactions.init.RemoveDuplicateUserInfoTransaction)1 RemoveOldExtensionsTransaction (com.djrapitops.plan.storage.database.transactions.init.RemoveOldExtensionsTransaction)1 RemoveOldSampledDataTransaction (com.djrapitops.plan.storage.database.transactions.init.RemoveOldSampledDataTransaction)1 IOException (java.io.IOException)1 UUID (java.util.UUID)1