EbeanServer setConfig() {
    // programmatically build a EbeanServer instance
    // specify the configuration...

    ServerConfig config = new ServerConfig();
    config.setName("mysql");

    // Define DataSource parameters
    DataSourceConfig postgresDb = new DataSourceConfig();
    postgresDb.setDriver("com.mysql.jdbc.Driver");
    postgresDb.setUsername("root");
    postgresDb.setPassword("");
    postgresDb.setUrl("jdbc:mysql://127.0.0.1:3306/perbekalan_farmasi");

    // specify a JNDI DataSource
    // config.setDataSourceJndiName("someJndiDataSourceName");

    // set DDL options...
    //    	config.setDdlGenerate(true);
    //    	config.setDdlRun(true);
    config.setDdlGenerate(false);
    config.setDdlRun(false);
    config.setDefaultServer(false);
    config.setRegister(false);

    // automatically determine the DatabasePlatform
    // using the jdbc driver
    config.setDatabasePlatform(new MySqlPlatform());
    config.setDataSourceConfig(postgresDb);
    // specify the entity classes (and listeners etc)
    // ... if these are not specified Ebean will search
    // ... the classpath looking for entity classes.
    //    	config.addClass(Order.class);
    config.addClass(DeletedGoods.class);
    config.addClass(Goods.class);
    config.addClass(GoodsConsumption.class);
    config.addClass(GoodsReception.class);
    config.addClass(Insurance.class);
    config.addClass(Invoice.class);
    config.addClass(InvoiceItem.class);
    config.addClass(PurchaseOrder.class);
    config.addClass(PurchaseOrderItem.class);
    config.addClass(ReqPlanning.class);
    config.addClass(Role.class);
    config.addClass(Setting.class);
    config.addClass(Supplier.class);
    config.addClass(SupplierGoods.class);
    config.addClass(User.class);
    config.addClass(Manufacturer.class);

    //    	...

    // specify jars to search for entity beans
    //    	config.addJar("someJarThatContainsEntityBeans.jar");

    // create the EbeanServer instance
    EbeanServer server = EbeanServerFactory.create(config);

    return server;
  }
  private EbeanServer initializeDatabase(boolean dropAndCreateDatabase, String dbName) {
    ServerConfig config = new ServerConfig();
    config.setName(dbName);
    DataSourceConfig hdDB = new DataSourceConfig();
    hdDB.setDriver("org.h2.Driver");
    hdDB.setUsername("test");
    hdDB.setPassword("test");
    hdDB.setUrl("jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1");
    hdDB.setHeartbeatSql("select 1 ");
    config.setDataSourceConfig(hdDB);

    config.setDefaultServer(false);
    config.setRegister(false);

    if (dropAndCreateDatabase) {
      config.setDdlGenerate(true);
      config.setDdlRun(true);
      // config.setDebugSql(true);
    }

    config.addClass(Viite.class);
    config.addClass(Attribuutti.class);

    return EbeanServerFactory.create(config);
  }
Example #3
0
  private void tryLoadOldSensors() {

    java.io.File f = null;

    for (File fi : this.getDataFolder().listFiles()) {
      if (fi.getName().toLowerCase().contains("minecartmaniarebornsigncommands.db")) {
        f = fi;
        break;
      }
    }

    if (f == null) {
      return;
    }

    Logger.debug("Found old sensor DB. Attempting load...");

    com.avaje.ebean.config.DataSourceConfig dsc = new com.avaje.ebean.config.DataSourceConfig();
    dsc.setUsername("temp");
    dsc.setPassword("temp");
    dsc.setDriver("org.sqlite.JDBC");
    dsc.setIsolationLevel(8);

    dsc.setUrl("jdbc:sqlite:plugins/minecartmania/minecartmaniarebornsigncommands.db");

    ServerConfig config = new ServerConfig();
    config.setDataSourceConfig(dsc);
    config.setName("Old DB");
    config.addClass(com.afforess.minecartmaniasigncommands.sensor.SensorDataTable.class);
    config.addJar("MinecartMania.jar");
    SensorManager.database = com.avaje.ebean.EbeanServerFactory.create(config);

    SensorManager.loadsensors();

    if (SensorManager.getCount() > 0) {
      Logger.severe("Found sensors in old db, moving...");
      // loaded old sensors
      for (Sensor s : SensorManager.getSensorList().values()) {
        SensorManager.saveSensor(s);
      }
      Logger.severe("Complete. Removing old db.");
    }

    SensorManager.database = this.getDatabase();

    f.delete();
  }
Example #4
0
  private void loadDatabase() {
    // Declare a few local variables for later use
    ClassLoader currentClassLoader = null;
    Field cacheField = null;
    boolean cacheValue = true;

    try {
      // Store the current ClassLoader, so it can be reverted later
      currentClassLoader = Thread.currentThread().getContextClassLoader();

      // Set the ClassLoader to Plugin ClassLoader
      Thread.currentThread().setContextClassLoader(classLoader);

      // Get a reference to the private static "defaultUseCaches"-field in URLConnection
      cacheField = URLConnection.class.getDeclaredField("defaultUseCaches");

      // Make it accessible, store the default value and set it to false
      cacheField.setAccessible(true);
      cacheValue = cacheField.getBoolean(null);
      cacheField.setBoolean(null, false);

      // Setup Ebean based on the configuration
      ebeanServer = EbeanServerFactory.create(serverConfig);
    } catch (Exception ex) {
      throw new RuntimeException("Failed to create a new instance of the EbeanServer", ex);
    } finally {
      // Revert the ClassLoader back to its original value
      if (currentClassLoader != null) {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
      }

      // Revert the "defaultUseCaches"-field in URLConnection back to its original value
      try {
        if (cacheField != null) {
          cacheField.setBoolean(null, cacheValue);
        }
      } catch (Exception e) {
        System.out.println(
            "Failed to revert the \"defaultUseCaches\"-field back to its original value, URLConnection-caching remains disabled.");
      }
    }
  }
Example #5
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);
      }
    }
  }
 /** Initialise the Ebean servers. */
 public void start() {
   config
       .serverConfigs()
       .forEach((key, serverConfig) -> servers.put(key, EbeanServerFactory.create(serverConfig)));
 }