Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SystemLifecycleController.java 3.6KB

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