Ejemplo n.º 1
0
  private void prepareDatabase(
      String driver, String url, String username, String password, String isolation) {
    // Setup the data source
    DataSourceConfig ds = new DataSourceConfig();
    ds.setDriver(driver);
    ds.setUrl(replaceDatabaseString(url));
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setIsolationLevel(TransactionIsolation.getLevel(isolation));

    // Setup the server configuration
    ServerConfig sc = new ServerConfig();
    sc.setDefaultServer(false);
    sc.setRegister(false);
    sc.setName(ds.getUrl().replaceAll("[^a-zA-Z0-9]", ""));

    // Get all persistent classes
    List<Class<?>> classes = getDatabaseClasses();

    // Do a sanity check first
    if (classes.size() == 0) {
      // Exception: There is no use in continuing to load this database
      throw new RuntimeException("Database has been enabled, but no classes are registered to it");
    }

    // Register them with the EbeanServer
    sc.setClasses(classes);

    // Check if the SQLite JDBC supplied with Bukkit is being used
    if (ds.getDriver().equalsIgnoreCase("org.sqlite.JDBC")) {
      // Remember the database is a SQLite-database
      usingSQLite = true;

      // Modify the platform, as SQLite has no AUTO_INCREMENT field
      sc.setDatabasePlatform(new SQLitePlatform());
      sc.getDatabasePlatform().getDbDdlSyntax().setIdentity("");
    }

    prepareDatabaseAdditionalConfig(ds, sc);

    // Finally the data source
    sc.setDataSourceConfig(ds);

    // Store the ServerConfig
    serverConfig = sc;
  }
Ejemplo n.º 2
0
  /**
   * Initializes this plugin with the given variables.
   *
   * <p>This method should never be called manually.
   *
   * @param loader PluginLoader that is responsible for this plugin
   * @param server Server instance that is running this plugin
   * @param description PluginDescriptionFile containing metadata on this plugin
   * @param dataFolder Folder containing the plugin's data
   * @param file File containing this plugin
   * @param classLoader ClassLoader which holds this plugin
   */
  protected final void initialize(
      PluginLoader loader,
      Server server,
      PluginDescriptionFile description,
      File dataFolder,
      File file,
      ClassLoader classLoader) {
    if (!initialized) {
      this.initialized = true;
      this.loader = loader;
      this.server = server;
      this.file = file;
      this.description = description;
      this.dataFolder = dataFolder;
      this.classLoader = classLoader;
      this.configFile = new File(dataFolder, "config.yml");

      if (description.isDatabaseEnabled()) {
        ServerConfig db = new ServerConfig();

        db.setDefaultServer(false);
        db.setRegister(false);
        db.setClasses(getDatabaseClasses());
        db.setName(description.getName());
        server.configureDbConfig(db);

        DataSourceConfig ds = db.getDataSourceConfig();

        ds.setUrl(replaceDatabaseString(ds.getUrl()));
        getDataFolder().mkdirs();

        ClassLoader previous = Thread.currentThread().getContextClassLoader();

        Thread.currentThread().setContextClassLoader(classLoader);
        ebean = EbeanServerFactory.create(db);
        Thread.currentThread().setContextClassLoader(previous);
      }
    }
  }