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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2013 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.logger.ErrorLevel;
  24. import com.dmdirc.logger.Logger;
  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.PluginManager;
  30. import com.dmdirc.plugins.Service;
  31. import com.dmdirc.plugins.ServiceProvider;
  32. import java.net.URI;
  33. import lombok.RequiredArgsConstructor;
  34. /**
  35. * Provides a method to retrieve a parser.
  36. *
  37. * @since 0.6
  38. */
  39. @RequiredArgsConstructor
  40. public class ParserFactory {
  41. /** PluginManager used by this ParserFactory */
  42. private final PluginManager pluginManager;
  43. /**
  44. * Retrieves a parser instance.
  45. *
  46. * @param myInfo The client information to use
  47. * @param address The address of the server to connect to
  48. * @return An appropriately configured parser
  49. * @since 0.6.3
  50. */
  51. public Parser getParser(final MyInfo myInfo, final URI address) {
  52. // TODO: Hacky Hack McHack
  53. if ("irc-test".equals(address.getScheme())) {
  54. try {
  55. return (Parser) Class.forName("com.dmdirc.harness.parser.TestParser")
  56. .getConstructor(MyInfo.class, URI.class)
  57. .newInstance(myInfo, address);
  58. } catch (Exception ex) {
  59. Logger.userError(ErrorLevel.UNKNOWN, "Unable to create parser", ex);
  60. }
  61. }
  62. final Object obj = getExportResult(address, "getParser", myInfo, address);
  63. if (obj instanceof Parser) {
  64. return (Parser) obj;
  65. }
  66. return null;
  67. }
  68. /**
  69. * Retrieves a protocol description.
  70. *
  71. * @param address The address to retrieve a description for
  72. * @return A corresponding protocol description
  73. * @since 0.6.4
  74. */
  75. public ProtocolDescription getDescription(final URI address) {
  76. final Object obj = getExportResult(address, "getProtocolDescription");
  77. if (obj instanceof ProtocolDescription) {
  78. return (ProtocolDescription) obj;
  79. }
  80. return null;
  81. }
  82. /**
  83. * Retrieves and executes an exported service with the specified name from a
  84. * {@link ServiceProvider} which can provide a parser for the specified
  85. * address. If no such provider or service is found, null is returned.
  86. *
  87. * @param address The address a provider is required for
  88. * @param serviceName The name of the service to be retrieved
  89. * @param args The arguments to be passed to the exported service
  90. * @return The result from a relevant exported service, or null
  91. * @since 0.6.4
  92. */
  93. protected Object getExportResult(final URI address,
  94. final String serviceName, final Object ... args) {
  95. // TODO: Move default scheme to a setting
  96. final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
  97. try {
  98. final Service service = pluginManager.getService("parser", scheme);
  99. if (service != null && !service.getProviders().isEmpty()) {
  100. final ServiceProvider provider = service.getProviders().get(0);
  101. if (provider != null) {
  102. provider.activateServices();
  103. return provider.getExportedService(serviceName)
  104. .execute(args);
  105. }
  106. }
  107. } catch (NoSuchProviderException nspe) {
  108. Logger.userError(ErrorLevel.MEDIUM,
  109. "No parser found for: " + address.getScheme(), nspe);
  110. }
  111. return null;
  112. }
  113. }