Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ServerFactoryImpl.java 5.9KB

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