Package ru.tehkode.permissions.exceptions

Examples of ru.tehkode.permissions.exceptions.PermissionBackendException


    super(manager, backendConfig);
    Map<String, PermissionBackend> backendMap = new HashMap<>();
    List<String> backendNames = backendConfig.getStringList("backends");
    if (backendNames.isEmpty()) {
      backendConfig.set("backends", new ArrayList<String>());
      throw new PermissionBackendException("No backends configured for multi backend! Please configure this!");
    }
    for (String name : backendConfig.getStringList("backends")) {
      PermissionBackend backend = manager.createBackend(name);
      backends.add(backend);
      backendMap.put(name, backend);
    }

    // Fallbacks
    ConfigurationSection fallbackSection = backendConfig.getConfigurationSection("fallback");
    if (fallbackSection != null) {
      for (Map.Entry<String, Object> ent : fallbackSection.getValues(false).entrySet()) {
        @SuppressWarnings("SuspiciousMethodCalls")
        PermissionBackend backend = backendMap.get(ent.getValue());
        if (backend == null) {
          throw new PermissionBackendException("Fallback backend type " + ent.getValue() + " is not listed in the backends section of MultiBackend (and must be for this contraption to work)");
        }
        fallbackBackends.put(ent.getKey(), backend);
      }
    }
  }
View Full Code Here


        if (!asyncExecutor.awaitTermination(2, TimeUnit.MINUTES)) {
          getLogger().warning("All backend tasks not completed after another 2 minutes, giving up on the wait.");
        }
      }
    } catch (InterruptedException e) {
      throw new PermissionBackendException(e);
    }
  }
View Full Code Here

        worldInheritanceCache.clear();
        this.permissions = newPermissions;
        initNewConfiguration();
      }
    } catch (Throwable e) {
      throw new PermissionBackendException("Error loading permissions file!", e);
    }
  }
View Full Code Here

        permissions.set("groups/default/permissions", defaultPermissions);
        permissions.set("schema-version", getLatestSchemaVersion());

        this.save();
      } catch (IOException e) {
        throw new PermissionBackendException(e);
      }
    }
  }
View Full Code Here

    if (dbUri == null || dbUri.isEmpty()) {
      getConfig().set("uri", "mysql://localhost/exampledb");
      getConfig().set("user", "databaseuser");
      getConfig().set("password", "databasepassword");
      manager.getConfiguration().save();
      throw new PermissionBackendException("SQL connection is not configured, see config.yml");
    }
    dbDriver = dbUri.split(":", 2)[0];

    this.ds = new BasicDataSource();
    String driverClass = getDriverClass(dbDriver);
    if (driverClass != null) {
      this.ds.setDriverClassName(driverClass);
    }
    this.ds.setUrl("jdbc:" + dbUri);
    this.ds.setUsername(dbUser);
    this.ds.setPassword(dbPassword);
    this.ds.setMaxActive(20);
    this.ds.setMaxWait(200); // 4 ticks
    this.ds.setValidationQuery("SELECT 1 AS dbcp_validate");
    this.ds.setTestOnBorrow(true);

    InputStream queryLocation = getClass().getResourceAsStream("/sql/" + dbDriver + "/queries.properties");
    if (queryLocation != null) {
      try {
        this.queryCache = new SQLQueryCache(queryLocation, DEFAULT_QUERY_CACHE);
      } catch (IOException e) {
        throw new PermissionBackendException("Unable to access database-specific queries", e);
      }
    } else {
      this.queryCache = DEFAULT_QUERY_CACHE;
    }
    try (SQLConnection conn = getSQL()) {
      conn.checkConnection();
    } catch (Exception e) {
      if (e.getCause() != null && e.getCause() instanceof Exception) {
        e = (Exception) e.getCause();
      }
      throw new PermissionBackendException("Unable to connect to SQL database", e);
    }

    getManager().getLogger().info("Successfully connected to SQL database");

    addSchemaUpdate(new SchemaUpdate(2) {
      @Override
      public void performUpdate() throws PermissionBackendException {
        // Change encoding for all columns to utf8mb4
        // Change collation for all columns to utf8mb4_general_ci
        try (SQLConnection conn = getSQL()) {
          conn.prep("ALTER TABLE `{permissions}` DROP KEY `unique`, MODIFY COLUMN `permission` TEXT NOT NULL").execute();
          conn.prep("ALTER TABLE `{permissions}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci").execute();
          conn.prep("ALTER TABLE `{permissions_entity}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci").execute();
          conn.prep("ALTER TABLE `{permissions_inheritance}` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci").execute();
        } catch (SQLException | IOException e) {
          throw new PermissionBackendException(e);
        }
      }
    });
    addSchemaUpdate(new SchemaUpdate(1) {
      @Override
      public void performUpdate() throws PermissionBackendException {
        try (SQLConnection conn = getSQL()) {
          PreparedStatement updateStmt = conn.prep("entity.options.add");
          ResultSet res = conn.prepAndBind("SELECT `name`, `type` FROM `{permissions_entity}` WHERE `default`='1'").executeQuery();
          while (res.next()) {
              conn.bind(updateStmt, res.getString("name"), res.getInt("type"), "default", "", "true");
              updateStmt.addBatch();
          }
          updateStmt.executeBatch();

          // Update tables
          conn.prep("ALTER TABLE `{permissions_entity}` DROP COLUMN `default`").execute();
        } catch (SQLException | IOException e) {
          throw new PermissionBackendException(e);
        }
      }
    });
    addSchemaUpdate(new SchemaUpdate(0) {
      @Override
      public void performUpdate() throws PermissionBackendException {
        try (SQLConnection conn = getSQL()) {
          // TODO: Table modifications not supported in SQLite
          // Prefix/sufix -> options
          PreparedStatement updateStmt = conn.prep("entity.options.add");
          ResultSet res = conn.prepAndBind("SELECT `name`, `type`, `prefix`, `suffix` FROM `{permissions_entity}` WHERE LENGTH(`prefix`)>0 OR LENGTH(`suffix`)>0").executeQuery();
          while (res.next()) {
            String prefix = res.getString("prefix");
            if (!prefix.isEmpty() && !prefix.equals("null")) {
              conn.bind(updateStmt, res.getString("name"), res.getInt("type"), "prefix", "", prefix);
              updateStmt.addBatch();
            }
            String suffix = res.getString("suffix");
            if (!suffix.isEmpty() && !suffix.equals("null")) {
              conn.bind(updateStmt, res.getString("name"), res.getInt("type"), "suffix", "", suffix);
              updateStmt.addBatch();
            }
          }
          updateStmt.executeBatch();

          // Data type corrections

          // Update tables
          conn.prep("ALTER TABLE `{permissions_entity}` DROP KEY `name`").execute();
          conn.prep("ALTER TABLE `{permissions_entity}` DROP COLUMN `prefix`, DROP COLUMN `suffix`").execute();
          conn.prep("ALTER TABLE `{permissions_entity}` ADD CONSTRAINT UNIQUE KEY `name` (`name`, `type`)").execute();

          conn.prep("ALTER TABLE `{permissions}` DROP KEY `unique`").execute();
          conn.prep("ALTER TABLE `{permissions}` ADD CONSTRAINT UNIQUE `unique` (`name`,`permission`,`world`,`type`)").execute();
        } catch (SQLException | IOException e) {
          throw new PermissionBackendException(e);
        }
      }
    });
    this.setupAliases();
    this.deployTables();
View Full Code Here

      getLogger().info("Deploying default database scheme");
      executeStream(conn, databaseDumpStream);
      setSchemaVersion(getLatestSchemaVersion());
    } catch (Exception e) {
      throw new PermissionBackendException("Deploying of default data failed. Please initialize database manually using " + dbDriver + ".sql", e);
    }

    PermissionsGroupData defGroup = getGroupData("default");
    defGroup.setPermissions(Collections.singletonList("modifyworld.*"), null);
    defGroup.setOption("default", "true", null);
View Full Code Here

  public void close() throws PermissionBackendException {
    if (ds != null) {
      try {
        ds.close();
      } catch (SQLException e) {
        throw new PermissionBackendException("Error while closing", e);
      }
    }
    super.close();
  }
View Full Code Here

TOP

Related Classes of ru.tehkode.permissions.exceptions.PermissionBackendException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.