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.

ServerFactoryImpl.java 5.9KB

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