From 690d7445a1a3fef8877f6ad929b01c4cc349337d Mon Sep 17 00:00:00 2001 From: Christoph Sturm Date: Tue, 6 Aug 2019 19:20:18 +0200 Subject: [PATCH] singletonify the rest of the core module --- core/src/main/java/bisq/core/CoreModule.java | 23 ++-------------- .../bisq/core/notifications/MobileModel.java | 2 ++ .../MobileNotificationService.java | 2 ++ .../MobileNotificationValidator.java | 2 ++ .../alerts/DisputeMsgEvents.java | 2 ++ .../alerts/MyOfferTakenEvents.java | 2 ++ .../notifications/alerts/TradeEvents.java | 2 ++ .../alerts/market/MarketAlerts.java | 2 ++ .../alerts/price/PriceAlert.java | 2 ++ .../network/CoreNetworkProtoResolver.java | 2 ++ .../CorePersistenceProtoResolver.java | 2 ++ .../java/bisq/desktop/GuiceSetupTest.java | 27 +++++++++++++++++++ 12 files changed, 49 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/bisq/core/CoreModule.java b/core/src/main/java/bisq/core/CoreModule.java index df5d6b856a7..adcbf3a163e 100644 --- a/core/src/main/java/bisq/core/CoreModule.java +++ b/core/src/main/java/bisq/core/CoreModule.java @@ -25,15 +25,6 @@ import bisq.core.dao.DaoModule; import bisq.core.filter.FilterModule; import bisq.core.network.p2p.seed.DefaultSeedNodeRepository; -import bisq.core.notifications.MobileMessageEncryption; -import bisq.core.notifications.MobileModel; -import bisq.core.notifications.MobileNotificationService; -import bisq.core.notifications.MobileNotificationValidator; -import bisq.core.notifications.alerts.DisputeMsgEvents; -import bisq.core.notifications.alerts.MyOfferTakenEvents; -import bisq.core.notifications.alerts.TradeEvents; -import bisq.core.notifications.alerts.market.MarketAlerts; -import bisq.core.notifications.alerts.price.PriceAlert; import bisq.core.offer.OfferModule; import bisq.core.presentation.CorePresentationModule; import bisq.core.proto.network.CoreNetworkProtoResolver; @@ -55,7 +46,6 @@ import org.springframework.core.env.Environment; -import com.google.inject.Singleton; import com.google.inject.name.Names; import java.io.File; @@ -82,8 +72,8 @@ protected void configure() { File keyStorageDir = new File(environment.getRequiredProperty(KeyStorage.KEY_STORAGE_DIR)); bind(File.class).annotatedWith(named(KeyStorage.KEY_STORAGE_DIR)).toInstance(keyStorageDir); - bind(NetworkProtoResolver.class).to(CoreNetworkProtoResolver.class).in(Singleton.class); - bind(PersistenceProtoResolver.class).to(CorePersistenceProtoResolver.class).in(Singleton.class); + bind(NetworkProtoResolver.class).to(CoreNetworkProtoResolver.class); + bind(PersistenceProtoResolver.class).to(CorePersistenceProtoResolver.class); Boolean useDevPrivilegeKeys = environment.getProperty(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS, Boolean.class, false); bind(boolean.class).annotatedWith(Names.named(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS)).toInstance(useDevPrivilegeKeys); @@ -94,15 +84,6 @@ protected void configure() { String referralId = environment.getProperty(AppOptionKeys.REFERRAL_ID, String.class, ""); bind(String.class).annotatedWith(Names.named(AppOptionKeys.REFERRAL_ID)).toInstance(referralId); - bind(MobileNotificationService.class).in(Singleton.class); - bind(MobileMessageEncryption.class).in(Singleton.class); - bind(MobileNotificationValidator.class).in(Singleton.class); - bind(MobileModel.class).in(Singleton.class); - bind(MyOfferTakenEvents.class).in(Singleton.class); - bind(TradeEvents.class).in(Singleton.class); - bind(DisputeMsgEvents.class).in(Singleton.class); - bind(PriceAlert.class).in(Singleton.class); - bind(MarketAlerts.class).in(Singleton.class); // ordering is used for shut down sequence install(tradeModule()); diff --git a/core/src/main/java/bisq/core/notifications/MobileModel.java b/core/src/main/java/bisq/core/notifications/MobileModel.java index ffd015afa0b..2c212e7e4e9 100644 --- a/core/src/main/java/bisq/core/notifications/MobileModel.java +++ b/core/src/main/java/bisq/core/notifications/MobileModel.java @@ -18,6 +18,7 @@ package bisq.core.notifications; import javax.inject.Inject; +import javax.inject.Singleton; import com.google.common.annotations.VisibleForTesting; @@ -29,6 +30,7 @@ @Data @Slf4j +@Singleton public class MobileModel { public static final String PHONE_SEPARATOR_ESCAPED = "\\|"; // see https://stackoverflow.com/questions/5675704/java-string-split-not-returning-the-right-values public static final String PHONE_SEPARATOR_WRITING = "|"; diff --git a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java index 3b80e0f8940..e93845f0a44 100644 --- a/core/src/main/java/bisq/core/notifications/MobileNotificationService.java +++ b/core/src/main/java/bisq/core/notifications/MobileNotificationService.java @@ -29,6 +29,7 @@ import com.google.gson.Gson; import com.google.inject.Inject; +import com.google.inject.Singleton; import javax.inject.Named; @@ -54,6 +55,7 @@ import static com.google.common.base.Preconditions.checkNotNull; @Slf4j +@Singleton public class MobileNotificationService { // Used in Relay app to response of a success state. We won't want a code dependency just for that string so we keep it // duplicated in relay and here. Must not be changed. diff --git a/core/src/main/java/bisq/core/notifications/MobileNotificationValidator.java b/core/src/main/java/bisq/core/notifications/MobileNotificationValidator.java index 95eab45fff7..5cf6f2aeb51 100644 --- a/core/src/main/java/bisq/core/notifications/MobileNotificationValidator.java +++ b/core/src/main/java/bisq/core/notifications/MobileNotificationValidator.java @@ -18,10 +18,12 @@ package bisq.core.notifications; import javax.inject.Inject; +import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j; @Slf4j +@Singleton public class MobileNotificationValidator { @Inject public MobileNotificationValidator() { diff --git a/core/src/main/java/bisq/core/notifications/alerts/DisputeMsgEvents.java b/core/src/main/java/bisq/core/notifications/alerts/DisputeMsgEvents.java index e989317f770..fc9f21e0aaf 100644 --- a/core/src/main/java/bisq/core/notifications/alerts/DisputeMsgEvents.java +++ b/core/src/main/java/bisq/core/notifications/alerts/DisputeMsgEvents.java @@ -28,6 +28,7 @@ import bisq.network.p2p.P2PService; import javax.inject.Inject; +import javax.inject.Singleton; import javafx.collections.ListChangeListener; @@ -36,6 +37,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j +@Singleton public class DisputeMsgEvents { private final P2PService p2PService; private final MobileNotificationService mobileNotificationService; diff --git a/core/src/main/java/bisq/core/notifications/alerts/MyOfferTakenEvents.java b/core/src/main/java/bisq/core/notifications/alerts/MyOfferTakenEvents.java index 2ed8a7ebb7f..842dbadacc4 100644 --- a/core/src/main/java/bisq/core/notifications/alerts/MyOfferTakenEvents.java +++ b/core/src/main/java/bisq/core/notifications/alerts/MyOfferTakenEvents.java @@ -25,6 +25,7 @@ import bisq.core.offer.OpenOfferManager; import javax.inject.Inject; +import javax.inject.Singleton; import javafx.collections.ListChangeListener; @@ -33,6 +34,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j +@Singleton public class MyOfferTakenEvents { private final OpenOfferManager openOfferManager; private final MobileNotificationService mobileNotificationService; diff --git a/core/src/main/java/bisq/core/notifications/alerts/TradeEvents.java b/core/src/main/java/bisq/core/notifications/alerts/TradeEvents.java index 43f51ec80e5..adef19d1b1c 100644 --- a/core/src/main/java/bisq/core/notifications/alerts/TradeEvents.java +++ b/core/src/main/java/bisq/core/notifications/alerts/TradeEvents.java @@ -28,6 +28,7 @@ import bisq.common.crypto.PubKeyRing; import javax.inject.Inject; +import javax.inject.Singleton; import javafx.collections.ListChangeListener; @@ -38,6 +39,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j +@Singleton public class TradeEvents { private final PubKeyRing pubKeyRing; private final TradeManager tradeManager; diff --git a/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java b/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java index ac2a70a4321..fad6fc2a2f1 100644 --- a/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java +++ b/core/src/main/java/bisq/core/notifications/alerts/market/MarketAlerts.java @@ -38,6 +38,7 @@ import org.bitcoinj.utils.Fiat; import javax.inject.Inject; +import javax.inject.Singleton; import java.util.List; import java.util.UUID; @@ -45,6 +46,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j +@Singleton public class MarketAlerts { private final OfferBookService offerBookService; private final MobileNotificationService mobileNotificationService; diff --git a/core/src/main/java/bisq/core/notifications/alerts/price/PriceAlert.java b/core/src/main/java/bisq/core/notifications/alerts/price/PriceAlert.java index c7b21aeabaf..f52a048f60e 100644 --- a/core/src/main/java/bisq/core/notifications/alerts/price/PriceAlert.java +++ b/core/src/main/java/bisq/core/notifications/alerts/price/PriceAlert.java @@ -33,10 +33,12 @@ import org.bitcoinj.utils.Fiat; import javax.inject.Inject; +import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j; @Slf4j +@Singleton public class PriceAlert { private final PriceFeedService priceFeedService; private final MobileNotificationService mobileNotificationService; diff --git a/core/src/main/java/bisq/core/proto/network/CoreNetworkProtoResolver.java b/core/src/main/java/bisq/core/proto/network/CoreNetworkProtoResolver.java index 99dbf83ca77..3d0c065fc42 100644 --- a/core/src/main/java/bisq/core/proto/network/CoreNetworkProtoResolver.java +++ b/core/src/main/java/bisq/core/proto/network/CoreNetworkProtoResolver.java @@ -79,11 +79,13 @@ import bisq.common.proto.network.NetworkProtoResolver; import javax.inject.Inject; +import javax.inject.Singleton; import lombok.extern.slf4j.Slf4j; // TODO Use ProtobufferException instead of ProtobufferRuntimeException @Slf4j +@Singleton public class CoreNetworkProtoResolver extends CoreProtoResolver implements NetworkProtoResolver { @Inject diff --git a/core/src/main/java/bisq/core/proto/persistable/CorePersistenceProtoResolver.java b/core/src/main/java/bisq/core/proto/persistable/CorePersistenceProtoResolver.java index 4e4e5114c90..96e7bc9775e 100644 --- a/core/src/main/java/bisq/core/proto/persistable/CorePersistenceProtoResolver.java +++ b/core/src/main/java/bisq/core/proto/persistable/CorePersistenceProtoResolver.java @@ -56,6 +56,7 @@ import javax.inject.Inject; import javax.inject.Named; +import javax.inject.Singleton; import java.io.File; @@ -63,6 +64,7 @@ // TODO Use ProtobufferException instead of ProtobufferRuntimeException @Slf4j +@Singleton public class CorePersistenceProtoResolver extends CoreProtoResolver implements PersistenceProtoResolver { private final Provider btcWalletService; private final NetworkProtoResolver networkProtoResolver; diff --git a/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java b/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java index c436271611d..daa0fc76bc2 100644 --- a/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java +++ b/desktop/src/test/java/bisq/desktop/GuiceSetupTest.java @@ -24,7 +24,16 @@ import bisq.core.locale.CurrencyUtil; import bisq.core.locale.Res; import bisq.core.network.p2p.seed.DefaultSeedNodeRepository; +import bisq.core.notifications.MobileModel; +import bisq.core.notifications.MobileNotificationService; +import bisq.core.notifications.MobileNotificationValidator; +import bisq.core.notifications.alerts.MyOfferTakenEvents; +import bisq.core.notifications.alerts.TradeEvents; +import bisq.core.notifications.alerts.market.MarketAlerts; +import bisq.core.notifications.alerts.price.PriceAlert; import bisq.core.payment.TradeLimits; +import bisq.core.proto.network.CoreNetworkProtoResolver; +import bisq.core.proto.persistable.CorePersistenceProtoResolver; import bisq.core.user.Preferences; import bisq.core.user.User; import bisq.core.util.BSFormatter; @@ -36,6 +45,8 @@ import bisq.common.ClockWatcher; import bisq.common.crypto.KeyRing; import bisq.common.crypto.KeyStorage; +import bisq.common.proto.network.NetworkProtoResolver; +import bisq.common.proto.persistable.PersistenceProtoResolver; import bisq.common.storage.CorruptedDatabaseFilesHandler; import org.springframework.mock.env.MockPropertySource; @@ -47,6 +58,7 @@ import org.junit.Test; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; public class GuiceSetupTest { @@ -84,6 +96,7 @@ public void testGuiceSetup() { // core module // assertSingleton(BisqSetup.class); // this is a can of worms +// assertSingleton(DisputeMsgEvents.class); assertSingleton(TorSetup.class); assertSingleton(P2PNetworkSetup.class); assertSingleton(WalletAppSetup.class); @@ -98,6 +111,20 @@ public void testGuiceSetup() { assertSingleton(AvoidStandbyModeService.class); assertSingleton(DefaultSeedNodeRepository.class); assertSingleton(SeedNodeRepository.class); + assertTrue(injector.getInstance(SeedNodeRepository.class) instanceof DefaultSeedNodeRepository); + assertSingleton(CoreNetworkProtoResolver.class); + assertSingleton(NetworkProtoResolver.class); + assertTrue(injector.getInstance(NetworkProtoResolver.class) instanceof CoreNetworkProtoResolver); + assertSingleton(PersistenceProtoResolver.class); + assertSingleton(CorePersistenceProtoResolver.class); + assertTrue(injector.getInstance(PersistenceProtoResolver.class) instanceof CorePersistenceProtoResolver); + assertSingleton(MobileNotificationService.class); + assertSingleton(MobileNotificationValidator.class); + assertSingleton(MobileModel.class); + assertSingleton(MyOfferTakenEvents.class); + assertSingleton(TradeEvents.class); + assertSingleton(PriceAlert.class); + assertSingleton(MarketAlerts.class); } private void assertSingleton(Class type) {