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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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. /**
  34. * Provides a method to retrieve a parser.
  35. *
  36. * @since 0.6
  37. * @author chris
  38. */
  39. public class ParserFactory {
  40. /**
  41. * Retrieves a parser instance.
  42. *
  43. * @param myInfo The client information to use
  44. * @param address The address of the server to connect to
  45. * @return An appropriately configured parser
  46. * @since 0.6.3
  47. */
  48. public Parser getParser(final MyInfo myInfo, final URI address) {
  49. // TODO: Hacky Hack McHack
  50. if ("irc-test".equals(address.getScheme())) {
  51. try {
  52. return (Parser) Class.forName("com.dmdirc.harness.parser.TestParser")
  53. .getConstructor(MyInfo.class, URI.class)
  54. .newInstance(myInfo, address);
  55. } catch (Exception ex) {
  56. Logger.userError(ErrorLevel.UNKNOWN, "Unable to create parser", ex);
  57. }
  58. }
  59. final Object obj = getExportResult(address, "getParser", myInfo, address);
  60. if (obj instanceof Parser) {
  61. return (Parser) obj;
  62. } else {
  63. Logger.userError(ErrorLevel.MEDIUM,
  64. "Unable to create parser for: " + address.getScheme());
  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. } else {
  80. Logger.userError(ErrorLevel.MEDIUM,
  81. "Unable to create protocol description for: " + address.getScheme());
  82. }
  83. return null;
  84. }
  85. /**
  86. * Retrieves and executes an exported service with the specified name from a
  87. * {@link ServiceProvider} which can provide a parser for the specified
  88. * address. If no such provider or service is found, null is returned.
  89. *
  90. * @param address The address a provider is required for
  91. * @param serviceName The name of the service to be retrieved
  92. * @param args The arguments to be passed to the exported service
  93. * @return The result from a relevant exported service, or null
  94. * @since 0.6.4
  95. */
  96. protected Object getExportResult(final URI address,
  97. final String serviceName, final Object ... args) {
  98. // TODO: Move default scheme to a setting
  99. final String scheme = address.getScheme() == null ? "irc" : address.getScheme();
  100. try {
  101. final Service service = PluginManager.getPluginManager().getService("parser", scheme);
  102. if (service != null && !service.getProviders().isEmpty()) {
  103. final ServiceProvider provider = service.getProviders().get(0);
  104. if (provider != null) {
  105. provider.activateServices();
  106. return provider.getExportedService(serviceName)
  107. .execute(args);
  108. }
  109. }
  110. } catch (NoSuchProviderException nspe) {
  111. Logger.userError(ErrorLevel.MEDIUM,
  112. "No parser found for: " + address.getScheme(), nspe);
  113. }
  114. return null;
  115. }
  116. }