You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemLifecycleController.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.config.GlobalConfig;
  19. import com.dmdirc.events.ClientClosedEvent;
  20. import com.dmdirc.interfaces.ConnectionManager;
  21. import com.dmdirc.events.eventbus.EventBus;
  22. import com.dmdirc.interfaces.LifecycleController;
  23. import com.dmdirc.interfaces.SystemLifecycleComponent;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.interfaces.config.IdentityController;
  26. import java.util.HashSet;
  27. import java.util.Set;
  28. import javax.inject.Inject;
  29. import javax.inject.Singleton;
  30. /**
  31. * Simple {@link LifecycleController} implementation that calls {@link System#exit(int)}.
  32. */
  33. @Singleton
  34. public class SystemLifecycleController implements LifecycleController {
  35. /** Controller to retrieve settings from. */
  36. private final AggregateConfigProvider configProvider;
  37. /** Components to shut down when the client quits. */
  38. private final Set<SystemLifecycleComponent> lifecycleComponents;
  39. /** Manager to use to disconnect servers. */
  40. private final ConnectionManager connectionManager;
  41. /** The event bus to raise client closed events on. */
  42. private final EventBus eventBus;
  43. /** The identity controller to save when quitting. */
  44. private final IdentityController identityController;
  45. @Inject
  46. public SystemLifecycleController(
  47. @GlobalConfig final AggregateConfigProvider configProvider,
  48. final Set<SystemLifecycleComponent> lifecycleComponents,
  49. final ConnectionManager connectionManager,
  50. final EventBus eventBus,
  51. final IdentityController identityController) {
  52. this.configProvider = configProvider;
  53. this.lifecycleComponents = new HashSet<>(lifecycleComponents);
  54. this.connectionManager = connectionManager;
  55. this.eventBus = eventBus;
  56. this.identityController = identityController;
  57. }
  58. @Override
  59. public void quit() {
  60. quit(0);
  61. }
  62. @Override
  63. public void quit(final int exitCode) {
  64. quit(configProvider.getOption("general", "closemessage"), exitCode);
  65. }
  66. @Override
  67. public void quit(final String reason) {
  68. quit(reason, 0);
  69. }
  70. @Override
  71. public void quit(final String reason, final int exitCode) {
  72. lifecycleComponents.forEach(SystemLifecycleComponent::shutDown);
  73. // TODO: Make all of these into lifecycle components
  74. eventBus.publish(new ClientClosedEvent());
  75. identityController.saveAll();
  76. connectionManager.disconnectAll(reason);
  77. System.exit(exitCode);
  78. }
  79. }