From 079e758edd09a9549deebb5449ea6af1d4c0aae7 Mon Sep 17 00:00:00 2001 From: Daniel Watson Date: Sun, 25 Mar 2018 05:01:02 -0400 Subject: [PATCH] To resolve: https://github.com/playframework/playframework/issues/7017 --- app/annotations/CommuteEbeanServer.java | 12 --- app/controllers/SignupController.java | 5 +- app/dao/AccountDao.java | 3 +- app/dao/AgencyDao.java | 3 +- app/dao/BaseDao.java | 3 +- app/dao/DeviceDao.java | 3 +- app/injection/modules/DatabaseModule.java | 8 +- .../providers/CommuteEbeanConfigProvider.java | 95 +++++++++++++++++++ .../providers/CommuteEbeanServerProvider.java | 71 +------------- app/main/LifecycleListener.java | 3 +- conf/application.conf | 2 +- 11 files changed, 114 insertions(+), 94 deletions(-) delete mode 100644 app/annotations/CommuteEbeanServer.java create mode 100644 app/injection/providers/CommuteEbeanConfigProvider.java diff --git a/app/annotations/CommuteEbeanServer.java b/app/annotations/CommuteEbeanServer.java deleted file mode 100644 index 014b0ec..0000000 --- a/app/annotations/CommuteEbeanServer.java +++ /dev/null @@ -1,12 +0,0 @@ -package annotations; - -import com.google.inject.BindingAnnotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@BindingAnnotation -@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.CONSTRUCTOR }) @Retention(RetentionPolicy.RUNTIME) -public @interface CommuteEbeanServer {} \ No newline at end of file diff --git a/app/controllers/SignupController.java b/app/controllers/SignupController.java index 730baa0..f758998 100644 --- a/app/controllers/SignupController.java +++ b/app/controllers/SignupController.java @@ -1,6 +1,5 @@ package controllers; -import annotations.CommuteEbeanServer; import com.google.inject.Inject; import enums.pushservices.PlatformType; import helpers.ValidationHelper; @@ -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; @@ -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; } diff --git a/app/dao/AccountDao.java b/app/dao/AccountDao.java index 245fbe7..59f3c2f 100644 --- a/app/dao/AccountDao.java +++ b/app/dao/AccountDao.java @@ -1,6 +1,5 @@ package dao; -import annotations.CommuteEbeanServer; import enums.pushservices.PlatformType; import io.ebean.EbeanServer; import io.ebean.FetchConfig; @@ -22,7 +21,7 @@ public class AccountDao extends BaseDao { @Inject - public AccountDao(@CommuteEbeanServer EbeanServer ebeanServer) { + public AccountDao(EbeanServer ebeanServer) { super(ebeanServer); } diff --git a/app/dao/AgencyDao.java b/app/dao/AgencyDao.java index c8d6252..9fb78f3 100644 --- a/app/dao/AgencyDao.java +++ b/app/dao/AgencyDao.java @@ -1,6 +1,5 @@ package dao; -import annotations.CommuteEbeanServer; import io.ebean.EbeanServer; import io.ebean.FetchConfig; import io.ebean.OrderBy; @@ -23,7 +22,7 @@ public class AgencyDao extends BaseDao { @Inject - public AgencyDao(@CommuteEbeanServer EbeanServer ebeanServer) { + public AgencyDao(EbeanServer ebeanServer) { super(ebeanServer); } diff --git a/app/dao/BaseDao.java b/app/dao/BaseDao.java index 79ce0b6..8d2e293 100644 --- a/app/dao/BaseDao.java +++ b/app/dao/BaseDao.java @@ -1,6 +1,5 @@ package dao; -import annotations.CommuteEbeanServer; import io.ebean.EbeanServer; import io.ebean.config.ServerConfig; import io.ebeaninternal.api.SpiEbeanServer; @@ -15,7 +14,7 @@ class BaseDao { EbeanServer mEbeanServer; - BaseDao(@CommuteEbeanServer EbeanServer ebeanServer) { + BaseDao(EbeanServer ebeanServer) { mEbeanServer = ebeanServer; } diff --git a/app/dao/DeviceDao.java b/app/dao/DeviceDao.java index f06bc90..2a64d63 100644 --- a/app/dao/DeviceDao.java +++ b/app/dao/DeviceDao.java @@ -1,6 +1,5 @@ package dao; -import annotations.CommuteEbeanServer; import io.ebean.EbeanServer; import io.ebean.OrderBy; import models.devices.Device; @@ -20,7 +19,7 @@ public class DeviceDao extends BaseDao { @Inject - public DeviceDao(@CommuteEbeanServer EbeanServer ebeanServer) { + public DeviceDao(EbeanServer ebeanServer) { super(ebeanServer); } diff --git a/app/injection/modules/DatabaseModule.java b/app/injection/modules/DatabaseModule.java index 563863e..2e8c328 100644 --- a/app/injection/modules/DatabaseModule.java +++ b/app/injection/modules/DatabaseModule.java @@ -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. @@ -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(); } diff --git a/app/injection/providers/CommuteEbeanConfigProvider.java b/app/injection/providers/CommuteEbeanConfigProvider.java new file mode 100644 index 0000000..dddf422 --- /dev/null +++ b/app/injection/providers/CommuteEbeanConfigProvider.java @@ -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 { + 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 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> 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 serverConfigs() { + return Collections.singletonMap(serverConfig.getName(), serverConfig); + } + }; + } +} diff --git a/app/injection/providers/CommuteEbeanServerProvider.java b/app/injection/providers/CommuteEbeanServerProvider.java index af1f8c8..7a377d2 100644 --- a/app/injection/providers/CommuteEbeanServerProvider.java +++ b/app/injection/providers/CommuteEbeanServerProvider.java @@ -2,30 +2,12 @@ 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. @@ -33,59 +15,16 @@ * Copyright 4/2/16 Splendid Bits. */ public class CommuteEbeanServerProvider implements Provider { - - 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 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> 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); } } \ No newline at end of file diff --git a/app/main/LifecycleListener.java b/app/main/LifecycleListener.java index a7eaecd..979821a 100644 --- a/app/main/LifecycleListener.java +++ b/app/main/LifecycleListener.java @@ -1,6 +1,5 @@ package main; -import annotations.CommuteEbeanServer; import com.google.inject.Inject; import com.google.inject.Singleton; import io.ebean.EbeanServer; @@ -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); })); diff --git a/conf/application.conf b/conf/application.conf index eb56b61..7ef0c69 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -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