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ů.

ServerFactoryImpl.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.commandparser.CommandType;
  19. import com.dmdirc.commandparser.parsers.ServerCommandParser;
  20. import com.dmdirc.config.UserConfig;
  21. import com.dmdirc.config.profiles.Profile;
  22. import com.dmdirc.interfaces.CommandController;
  23. import com.dmdirc.events.eventbus.EventBus;
  24. import com.dmdirc.config.provider.ConfigProvider;
  25. import com.dmdirc.config.provider.ConfigProviderMigrator;
  26. import com.dmdirc.interfaces.config.IdentityFactory;
  27. import com.dmdirc.ui.core.components.WindowComponent;
  28. import com.dmdirc.ui.input.TabCompleterFactory;
  29. import com.dmdirc.ui.messages.BackBufferFactory;
  30. import java.net.URI;
  31. import java.util.Arrays;
  32. import java.util.concurrent.ScheduledExecutorService;
  33. import javax.inject.Inject;
  34. import javax.inject.Provider;
  35. import javax.inject.Singleton;
  36. /**
  37. * Factory for {@link Server}s.
  38. */
  39. @Singleton
  40. public class ServerFactoryImpl {
  41. private final ParserFactory parserFactory;
  42. private final TabCompleterFactory tabCompleterFactory;
  43. private final IdentityFactory identityFactory;
  44. private final Provider<QueryFactory> queryFactory;
  45. private final Provider<CommandController> commandController;
  46. private final EventBus eventBus;
  47. private final MessageEncoderFactory messageEncoderFactory;
  48. private final ConfigProvider userSettings;
  49. private final BackBufferFactory backBufferFactory;
  50. private final GroupChatManagerImplFactory groupChatManagerFactory;
  51. private final UserManager userManager;
  52. @Inject
  53. public ServerFactoryImpl(
  54. final ParserFactory parserFactory,
  55. final TabCompleterFactory tabCompleterFactory,
  56. final IdentityFactory identityFactory,
  57. final Provider<QueryFactory> queryFactory,
  58. final Provider<CommandController> commandController,
  59. final EventBus eventBus,
  60. final MessageEncoderFactory messageEncoderFactory,
  61. @UserConfig final ConfigProvider userSettings,
  62. final BackBufferFactory backBufferFactory,
  63. final GroupChatManagerImplFactory groupChatManagerFactory,
  64. final UserManager userManager) {
  65. this.parserFactory = parserFactory;
  66. this.tabCompleterFactory = tabCompleterFactory;
  67. this.identityFactory = identityFactory;
  68. this.queryFactory = queryFactory;
  69. this.commandController = commandController;
  70. this.eventBus = eventBus;
  71. this.messageEncoderFactory = messageEncoderFactory;
  72. this.userSettings = userSettings;
  73. this.backBufferFactory = backBufferFactory;
  74. this.groupChatManagerFactory = groupChatManagerFactory;
  75. this.userManager = userManager;
  76. }
  77. public Server getServer(
  78. final ConfigProviderMigrator configMigrator,
  79. final ScheduledExecutorService executorService,
  80. final URI uri,
  81. final Profile profile) {
  82. final FrameContainer windowModel =
  83. new FrameContainer("server-disconnected", getHost(uri), getHost(uri),
  84. configMigrator.getConfigProvider(), backBufferFactory, eventBus,
  85. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  86. WindowComponent.INPUTFIELD.getIdentifier(),
  87. WindowComponent.CERTIFICATE_VIEWER.getIdentifier()));
  88. final Server server = new Server(windowModel, configMigrator, parserFactory,
  89. identityFactory, queryFactory.get(),
  90. messageEncoderFactory, userSettings, groupChatManagerFactory, executorService,
  91. uri, profile, userManager);
  92. windowModel.setConnection(server);
  93. windowModel.initBackBuffer();
  94. windowModel.setInputModel(new DefaultInputModel(
  95. server::sendLine,
  96. new ServerCommandParser(
  97. server.getWindowModel().getConfigManager(),
  98. commandController.get(),
  99. eventBus,
  100. server),
  101. tabCompleterFactory.getTabCompleter(
  102. configMigrator.getConfigProvider(),
  103. CommandType.TYPE_SERVER,
  104. CommandType.TYPE_GLOBAL),
  105. server::getMaxLineLength));
  106. return server;
  107. }
  108. /**
  109. * Retrieves the host component of the specified URI, or throws a relevant exception if this is
  110. * not possible.
  111. *
  112. * @param uri The URI to be processed
  113. *
  114. * @return The URI's host component, as returned by {@link URI#getHost()}.
  115. *
  116. * @throws NullPointerException If <code>uri</code> is null
  117. * @throws IllegalArgumentException If the specified URI has no host
  118. * @since 0.6.4
  119. */
  120. private static String getHost(final URI uri) {
  121. if (uri.getHost() == null) {
  122. throw new IllegalArgumentException("URIs must have hosts");
  123. }
  124. return uri.getHost();
  125. }
  126. }