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.

ParserFactory.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.profiles.Profile;
  24. import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
  25. import com.dmdirc.parser.common.MyInfo;
  26. import com.dmdirc.parser.interfaces.Parser;
  27. import com.dmdirc.parser.interfaces.ProtocolDescription;
  28. import com.dmdirc.plugins.NoSuchProviderException;
  29. import com.dmdirc.plugins.Service;
  30. import com.dmdirc.plugins.ServiceManager;
  31. import com.dmdirc.plugins.ServiceProvider;
  32. import java.net.URI;
  33. import java.net.URISyntaxException;
  34. import java.util.Optional;
  35. import javax.annotation.Nullable;
  36. import javax.inject.Inject;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import static com.dmdirc.util.LogUtils.APP_ERROR;
  40. import static com.dmdirc.util.LogUtils.USER_ERROR;
  41. import static com.google.common.base.Preconditions.checkArgument;
  42. import static com.google.common.base.Preconditions.checkNotNull;
  43. /**
  44. * Provides a method to retrieve a parser.
  45. *
  46. * @since 0.6
  47. */
  48. public class ParserFactory {
  49. private static final Logger LOG = LoggerFactory.getLogger(ParserFactory.class);
  50. /** The name of the general domain. */
  51. private static final String DOMAIN_GENERAL = "general";
  52. /** The name of the server domain. */
  53. private static final String DOMAIN_SERVER = "server";
  54. /** ServiceManager used by this ParserFactory */
  55. private final ServiceManager serviceManager;
  56. /**
  57. * Creates a new instance of {@link ParserFactory}.
  58. */
  59. @Inject
  60. public ParserFactory(final ServiceManager serviceManager) {
  61. this.serviceManager = serviceManager;
  62. }
  63. /**
  64. * Creates a new parser instance.
  65. *
  66. * @param profile The profile to use
  67. * @param address The address of the server to connect to
  68. * @param configManager The config manager to read settings from
  69. *
  70. * @return An appropriately configured parser
  71. *
  72. * @since 0.6.3
  73. */
  74. public Optional<Parser> getParser(final Profile profile, final URI address,
  75. final ReadOnlyConfigProvider configManager) {
  76. final Object obj = getExportResult(address, "getParser", buildMyInfo(profile), address);
  77. if (obj instanceof Parser) {
  78. final Parser parser = (Parser) obj;
  79. parser.setPingTimerInterval(configManager.getOptionInt(DOMAIN_SERVER, "pingtimer"));
  80. parser.setPingTimerFraction((int) (configManager.getOptionInt(DOMAIN_SERVER,
  81. "pingfrequency") / parser.getPingTimerInterval()));
  82. if (configManager.hasOptionString(DOMAIN_GENERAL, "bindip")) {
  83. parser.setBindIP(configManager.getOption(DOMAIN_GENERAL, "bindip"));
  84. }
  85. parser.setProxy(buildProxyURI(configManager));
  86. return Optional.of(parser);
  87. }
  88. return Optional.empty();
  89. }
  90. /**
  91. * Retrieves the MyInfo object used for the Parser.
  92. *
  93. * @return The MyInfo object for our profile
  94. */
  95. private MyInfo buildMyInfo(final Profile profile) {
  96. checkNotNull(profile);
  97. checkArgument(!profile.getNicknames().isEmpty());
  98. final MyInfo myInfo = new MyInfo();
  99. myInfo.setNickname(profile.getNicknames().get(0));
  100. myInfo.setRealname(profile.getRealname());
  101. profile.getIdent().ifPresent(myInfo::setUsername);
  102. return myInfo;
  103. }
  104. /**
  105. * Constructs a URI for the configured proxy for this server, if any.
  106. *
  107. * @return An appropriate URI or null if no proxy is configured
  108. */
  109. @Nullable
  110. private URI buildProxyURI(final ReadOnlyConfigProvider configManager) {
  111. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.address")) {
  112. final String type;
  113. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.type")) {
  114. type = configManager.getOption(DOMAIN_SERVER, "proxy.type");
  115. } else {
  116. type = "socks";
  117. }
  118. final int port;
  119. if (configManager.hasOptionInt(DOMAIN_SERVER, "proxy.port")) {
  120. port = configManager.getOptionInt(DOMAIN_SERVER, "proxy.port");
  121. } else {
  122. port = 8080;
  123. }
  124. final String host = configManager.getOptionString(DOMAIN_SERVER, "proxy.address");
  125. final String userInfo;
  126. if (configManager.hasOptionString(DOMAIN_SERVER, "proxy.username")
  127. && configManager.hasOptionString(DOMAIN_SERVER, "proxy.password")) {
  128. userInfo = configManager.getOption(DOMAIN_SERVER, "proxy.username")
  129. + configManager.getOption(DOMAIN_SERVER, "proxy.password");
  130. } else {
  131. userInfo = "";
  132. }
  133. try {
  134. return new URI(type, userInfo, host, port, "", "", "");
  135. } catch (URISyntaxException ex) {
  136. LOG.warn(APP_ERROR, "Unable to create proxy URI", ex);
  137. }
  138. }
  139. return null;
  140. }
  141. /**
  142. * Retrieves a protocol description.
  143. *
  144. * @param address The address to retrieve a description for
  145. *
  146. * @return A corresponding protocol description
  147. *
  148. * @since 0.6.4
  149. */
  150. public ProtocolDescription getDescription(final URI address) {
  151. final Object obj = getExportResult(address, "getProtocolDescription");
  152. if (obj instanceof ProtocolDescription) {
  153. return (ProtocolDescription) obj;
  154. }
  155. return null;
  156. }
  157. /**
  158. * Retrieves and executes an exported service with the specified name from a
  159. * {@link ServiceProvider} which can provide a parser for the specified address. If no such
  160. * provider or service is found, null is returned.
  161. *
  162. * @param address The address a provider is required for
  163. * @param serviceName The name of the service to be retrieved
  164. * @param args The arguments to be passed to the exported service
  165. *
  166. * @return The result from a relevant exported service, or null
  167. *
  168. * @since 0.6.4
  169. */
  170. protected Object getExportResult(final URI address,
  171. final String serviceName, final Object... args) {
  172. // TODO: Move default scheme to a setting
  173. final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
  174. try {
  175. final Service service = serviceManager.getService("parser", scheme);
  176. if (service != null && !service.getProviders().isEmpty()) {
  177. final ServiceProvider provider = service.getProviders().get(0);
  178. if (provider != null) {
  179. provider.activateServices();
  180. return provider.getExportedService(serviceName)
  181. .execute(args);
  182. }
  183. }
  184. } catch (NoSuchProviderException nspe) {
  185. LOG.warn(USER_ERROR, "No parser found for {}", address.getScheme(), nspe);
  186. }
  187. return null;
  188. }
  189. }