Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ServerFactoryImpl.java 5.9KB

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