mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-04-22 16:39:23 -04:00
Introduce #close hook
This commit is contained in:
parent
c71d9a0fb8
commit
e4a50bbcea
@ -17,14 +17,12 @@
|
||||
|
||||
package io.bitsquare;
|
||||
|
||||
import io.bitsquare.btc.WalletFacade;
|
||||
import io.bitsquare.di.BitsquareModule;
|
||||
import io.bitsquare.gui.AWTSystemTray;
|
||||
import io.bitsquare.gui.Navigation;
|
||||
import io.bitsquare.gui.components.Popups;
|
||||
import io.bitsquare.gui.util.ImageUtil;
|
||||
import io.bitsquare.gui.util.Profiler;
|
||||
import io.bitsquare.msg.MessageFacade;
|
||||
import io.bitsquare.persistence.Persistence;
|
||||
import io.bitsquare.settings.Settings;
|
||||
import io.bitsquare.user.User;
|
||||
@ -53,11 +51,13 @@ public class BitsquareUI extends Application {
|
||||
private static final Logger log = LoggerFactory.getLogger(BitsquareUI.class);
|
||||
|
||||
private static Stage primaryStage;
|
||||
private WalletFacade walletFacade;
|
||||
private MessageFacade messageFacade;
|
||||
|
||||
public void BitsquareUI() {
|
||||
Profiler.init();
|
||||
private final BitsquareModule bitsquareModule;
|
||||
private final Injector injector;
|
||||
|
||||
public BitsquareUI() {
|
||||
this.bitsquareModule = new BitsquareModule();
|
||||
this.injector = Guice.createInjector(bitsquareModule);
|
||||
}
|
||||
|
||||
public static Stage getPrimaryStage() {
|
||||
@ -78,15 +78,10 @@ public class BitsquareUI extends Application {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
|
||||
final Injector injector = Guice.createInjector(new BitsquareModule());
|
||||
|
||||
// currently there is not SystemTray support for java fx (planned for version 3) so we use the old AWT
|
||||
AWTSystemTray.createSystemTray(primaryStage, injector.getInstance(ActorSystem.class), this);
|
||||
|
||||
walletFacade = injector.getInstance(WalletFacade.class);
|
||||
messageFacade = injector.getInstance(MessageFacade.class);
|
||||
Profiler.printMsgWithTime("Bitsquare: messageFacade, walletFacade created");
|
||||
|
||||
// apply stored data
|
||||
final User user = injector.getInstance(User.class);
|
||||
final Settings settings = injector.getInstance(Settings.class);
|
||||
@ -149,11 +144,8 @@ public class BitsquareUI extends Application {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
walletFacade.shutDown();
|
||||
messageFacade.shutDown();
|
||||
|
||||
super.stop();
|
||||
public void stop() {
|
||||
bitsquareModule.close(injector);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.params.RegTestParams;
|
||||
import org.bitcoinj.params.TestNet3Params;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
@ -49,6 +49,11 @@ public class BitcoinModule extends AbstractBitsquareModule {
|
||||
bind(NetworkParameters.class).toInstance(network());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doClose(Injector injector) {
|
||||
injector.getInstance(WalletFacade.class).shutDown();
|
||||
}
|
||||
|
||||
private NetworkParameters network() {
|
||||
String networkName = properties.getProperty("networkType", defaultNetwork.name());
|
||||
|
||||
|
@ -17,15 +17,49 @@
|
||||
|
||||
package io.bitsquare.di;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Injector;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class AbstractBitsquareModule extends AbstractModule {
|
||||
|
||||
protected final Properties properties;
|
||||
|
||||
private final Set<AbstractBitsquareModule> modules = Sets.newHashSet();
|
||||
|
||||
public AbstractBitsquareModule(Properties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
protected void install(AbstractBitsquareModule module) {
|
||||
super.install(module);
|
||||
modules.add(module);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close any instances this module is responsible for and recursively close any
|
||||
* sub-modules installed via {@link #install(AbstractBitsquareModule)}. This method
|
||||
* must be called manually, e.g. at the end of a main() method or in the stop() method
|
||||
* of a JavaFX Application; alternatively it may be registered as a JVM shutdown hook.
|
||||
*
|
||||
* @param injector the Injector originally initialized with this module
|
||||
* @see #doClose(com.google.inject.Injector)
|
||||
*/
|
||||
public final void close(Injector injector) {
|
||||
modules.forEach(module -> module.close(injector));
|
||||
doClose(injector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually perform closing of any instances this module is responsible for. Called by
|
||||
* {@link #close(Injector)}.
|
||||
*
|
||||
* @param injector the Injector originally initialized with this module
|
||||
*/
|
||||
protected void doClose(Injector injector) {
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ package io.bitsquare.msg;
|
||||
|
||||
import io.bitsquare.di.AbstractBitsquareModule;
|
||||
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.name.Names;
|
||||
|
||||
import java.util.Properties;
|
||||
@ -43,4 +44,9 @@ public class DefaultMessageModule extends AbstractBitsquareModule implements Mes
|
||||
.annotatedWith(Names.named("defaultSeedNode"))
|
||||
.toInstance(SeedNodeAddress.StaticSeedNodeAddresses.DIGITAL_OCEAN1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doClose(Injector injector) {
|
||||
injector.getInstance(MessageFacade.class).shutDown();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user