Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
splendidbits committed Mar 25, 2018
1 parent 3e179b6 commit 079e758
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 94 deletions.
12 changes: 0 additions & 12 deletions app/annotations/CommuteEbeanServer.java

This file was deleted.

5 changes: 2 additions & 3 deletions app/controllers/SignupController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package controllers;

import annotations.CommuteEbeanServer;
import com.google.inject.Inject;
import enums.pushservices.PlatformType;
import helpers.ValidationHelper;
Expand All @@ -9,13 +8,13 @@
import models.accounts.PlatformAccount;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import play.Logger;
import play.data.DynamicForm;
import play.data.FormFactory;
import play.data.validation.ValidationError;
import play.mvc.Controller;
import play.mvc.Result;
import play.twirl.api.Html;
import play.Logger;
import views.html.signup;

import javax.annotation.Nonnull;
Expand All @@ -32,7 +31,7 @@ public class SignupController extends Controller {
private FormFactory mFormFactory;

@Inject
public SignupController(@CommuteEbeanServer EbeanServer ebeanServer, FormFactory formFactory) {
public SignupController(EbeanServer ebeanServer, FormFactory formFactory) {
mEbeanServer = ebeanServer;
mFormFactory = formFactory;
}
Expand Down
3 changes: 1 addition & 2 deletions app/dao/AccountDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dao;

import annotations.CommuteEbeanServer;
import enums.pushservices.PlatformType;
import io.ebean.EbeanServer;
import io.ebean.FetchConfig;
Expand All @@ -22,7 +21,7 @@
public class AccountDao extends BaseDao {

@Inject
public AccountDao(@CommuteEbeanServer EbeanServer ebeanServer) {
public AccountDao(EbeanServer ebeanServer) {
super(ebeanServer);
}

Expand Down
3 changes: 1 addition & 2 deletions app/dao/AgencyDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dao;

import annotations.CommuteEbeanServer;
import io.ebean.EbeanServer;
import io.ebean.FetchConfig;
import io.ebean.OrderBy;
Expand All @@ -23,7 +22,7 @@
public class AgencyDao extends BaseDao {

@Inject
public AgencyDao(@CommuteEbeanServer EbeanServer ebeanServer) {
public AgencyDao(EbeanServer ebeanServer) {
super(ebeanServer);
}

Expand Down
3 changes: 1 addition & 2 deletions app/dao/BaseDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dao;

import annotations.CommuteEbeanServer;
import io.ebean.EbeanServer;
import io.ebean.config.ServerConfig;
import io.ebeaninternal.api.SpiEbeanServer;
Expand All @@ -15,7 +14,7 @@
class BaseDao {
EbeanServer mEbeanServer;

BaseDao(@CommuteEbeanServer EbeanServer ebeanServer) {
BaseDao(EbeanServer ebeanServer) {
mEbeanServer = ebeanServer;
}

Expand Down
3 changes: 1 addition & 2 deletions app/dao/DeviceDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dao;

import annotations.CommuteEbeanServer;
import io.ebean.EbeanServer;
import io.ebean.OrderBy;
import models.devices.Device;
Expand All @@ -20,7 +19,7 @@
public class DeviceDao extends BaseDao {

@Inject
public DeviceDao(@CommuteEbeanServer EbeanServer ebeanServer) {
public DeviceDao(EbeanServer ebeanServer) {
super(ebeanServer);
}

Expand Down
8 changes: 6 additions & 2 deletions app/injection/modules/DatabaseModule.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package injection.modules;

import annotations.CommuteEbeanServer;
import com.google.inject.AbstractModule;
import injection.providers.CommuteEbeanServerProvider;
import injection.providers.CommuteEbeanConfigProvider;
import io.ebean.EbeanServer;
import play.db.ebean.EbeanConfig;

/**
* GuiseModule for invoking the ebean Database provider.
Expand All @@ -12,8 +13,11 @@ public class DatabaseModule extends AbstractModule {

@Override
protected void configure() {
bind(EbeanConfig.class)
.toProvider(CommuteEbeanConfigProvider.class)
.asEagerSingleton();

bind(EbeanServer.class)
.annotatedWith(CommuteEbeanServer.class)
.toProvider(CommuteEbeanServerProvider.class)
.asEagerSingleton();
}
Expand Down
95 changes: 95 additions & 0 deletions app/injection/providers/CommuteEbeanConfigProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package injection.providers;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;
import io.ebean.config.ServerConfig;
import main.Constants;
import models.accounts.Account;
import models.accounts.PlatformAccount;
import models.alerts.Agency;
import models.alerts.Alert;
import models.alerts.Location;
import models.alerts.Route;
import models.devices.Device;
import models.devices.Subscription;
import play.Environment;
import play.db.DBApi;
import play.db.ebean.EbeanConfig;
import play.inject.ApplicationLifecycle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;

/**
* GNU General Public License v3.0.
* (This means you can use it as you wish, host and share modifications.)
* Copyright 25/03/2018 Splendid Bits.
*/
public class CommuteEbeanConfigProvider implements Provider<EbeanConfig> {
private final ServerConfig serverConfig;

@Inject
public CommuteEbeanConfigProvider(Config config, ApplicationLifecycle lifecycle, Environment environment, DBApi dbApi) {
if (config == null || config.isEmpty()) {
throw new RuntimeException("No Play Framework configuration found.");
}

Config configuration = config.getConfig("db." + Constants.DATABASE_SERVER_NAME);
if (configuration == null) {
throw new RuntimeException("No commutealerts configuration found");
}

// Build custom properties from main configuration.
Properties properties = new Properties();
for (Map.Entry<String, ConfigValue> configEntry : configuration.entrySet()) {
String value = configEntry.getValue().render();
if (configEntry.getValue().valueType().equals(ConfigValueType.STRING)) {
value = (String) configEntry.getValue().unwrapped();
}
properties.put(configEntry.getKey(), value);
}

ArrayList<Class<?>> models = new ArrayList<>();
models.add(Account.class);
models.add(PlatformAccount.class);
models.add(Agency.class);
models.add(Route.class);
models.add(Alert.class);
models.add(Location.class);
models.add(Device.class);
models.add(Subscription.class);

ServerConfig serverConfig = new ServerConfig();
serverConfig.setName(Constants.DATABASE_SERVER_NAME);
serverConfig.loadFromProperties(properties);

serverConfig.setRegister(true);
serverConfig.setDefaultServer(true);
serverConfig.setUpdatesDeleteMissingChildren(false);
serverConfig.setClasses(models);
serverConfig.setDdlGenerate(false);
serverConfig.setUpdateChangesOnly(false);

this.serverConfig = serverConfig;
}

@Override
public EbeanConfig get() {
return new EbeanConfig() {
@Override
public String defaultServer() {
return serverConfig.getName();
}

@Override
public Map<String, ServerConfig> serverConfigs() {
return Collections.singletonMap(serverConfig.getName(), serverConfig);
}
};
}
}
71 changes: 5 additions & 66 deletions app/injection/providers/CommuteEbeanServerProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,29 @@

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import io.ebean.EbeanServerFactory;
import io.ebean.config.ServerConfig;
import main.Constants;
import models.accounts.Account;
import models.accounts.PlatformAccount;
import models.alerts.Agency;
import models.alerts.Alert;
import models.alerts.Location;
import models.alerts.Route;
import models.devices.Device;
import models.devices.Subscription;
import play.Environment;
import play.db.DBApi;
import play.db.ebean.EbeanConfig;
import play.inject.ApplicationLifecycle;

import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

/**
* GNU General Public License v3.0.
* (This means you can use it as you wish, host and share modifications.)
* Copyright 4/2/16 Splendid Bits.
*/
public class CommuteEbeanServerProvider implements Provider<EbeanServer> {

private final Config config;
private final ServerConfig serverConfig;

@Inject
public CommuteEbeanServerProvider(Config config, ServerConfig serverConfig, EbeanConfig ebeanConfig,
ApplicationLifecycle lifecycle, Environment environment, DBApi dbApi) {
this.config = config;
public CommuteEbeanServerProvider(EbeanConfig ebeanConfig) {
serverConfig = ebeanConfig.serverConfigs().get(Constants.DATABASE_SERVER_NAME);
Ebean.register(EbeanServerFactory.create(serverConfig), true);
}

@Override
public EbeanServer get() {
if (config == null || config.isEmpty()) {
throw new RuntimeException("No Play Framework configuration found.");
}

Config configuration = config.getConfig("db." + Constants.DATABASE_SERVER_NAME);
if (configuration == null) {
throw new RuntimeException("No commutealerts configuration found");
}

// Build custom properties from main configuration.
Properties properties = new Properties();
for (Map.Entry<String, ConfigValue> configEntry : configuration.entrySet()) {
String value = configEntry.getValue().render();
if (configEntry.getValue().valueType().equals(ConfigValueType.STRING)) {
value = (String) configEntry.getValue().unwrapped();
}
properties.put(configEntry.getKey(), value);
}

ArrayList<Class<?>> models = new ArrayList<>();
models.add(Account.class);
models.add(PlatformAccount.class);
models.add(Agency.class);
models.add(Route.class);
models.add(Alert.class);
models.add(Location.class);
models.add(Device.class);
models.add(Subscription.class);

ServerConfig serverConfig = new ServerConfig();
serverConfig.setName(Constants.DATABASE_SERVER_NAME);
serverConfig.loadFromProperties(properties);

serverConfig.setRegister(true);
serverConfig.setDefaultServer(true);
serverConfig.setUpdatesDeleteMissingChildren(false);
serverConfig.setClasses(models);
serverConfig.setDdlGenerate(false);
serverConfig.setUpdateChangesOnly(false);

EbeanServer ebeanServer = EbeanServerFactory.create(serverConfig);
Ebean.register(ebeanServer, true);
return ebeanServer;
return EbeanServerFactory.create(serverConfig);
}
}
3 changes: 1 addition & 2 deletions app/main/LifecycleListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package main;

import annotations.CommuteEbeanServer;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.ebean.EbeanServer;
Expand All @@ -12,7 +11,7 @@
public class LifecycleListener {

@Inject
public LifecycleListener(@CommuteEbeanServer EbeanServer ebeanServer, ApplicationLifecycle applicationLifecycle) {
public LifecycleListener(EbeanServer ebeanServer, ApplicationLifecycle applicationLifecycle) {
applicationLifecycle.addStopHook(() -> CompletableFuture.runAsync(() -> {
ebeanServer.shutdown(true, false);
}));
Expand Down
2 changes: 1 addition & 1 deletion conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ play.ws.ssl {
# To fix ebean / play injection race bug.
# https://github.com/playframework/playframework/issues/7017
# https://github.com/playframework/play-ebean/issues/51
//play.modules.disabled += "play.db.ebean.EbeanModule"
play.modules.disabled += "play.db.ebean.EbeanModule"

# !!! WARNING !!
# EBeanORM Configuration
Expand Down

0 comments on commit 079e758

Please sign in to comment.