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.

ServerListServiceProvider.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2014 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.addons.serverlists.service;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.ParserFactory;
  25. import com.dmdirc.addons.serverlists.ServerGroup;
  26. import com.dmdirc.addons.serverlists.ServerGroupItem;
  27. import com.dmdirc.addons.serverlists.ServerList;
  28. import com.dmdirc.parser.common.MyInfo;
  29. import com.dmdirc.parser.interfaces.Parser;
  30. import com.dmdirc.plugins.ExportedService;
  31. import com.dmdirc.plugins.PluginManager;
  32. import com.dmdirc.plugins.Service;
  33. import com.dmdirc.plugins.ServiceProvider;
  34. import java.net.URI;
  35. import java.util.Arrays;
  36. import java.util.List;
  37. /**
  38. * Provides a fake parser service which handles <code>serverlist://</code> URIs, and returns a
  39. * suitable parser for the corresponding server list.
  40. *
  41. * @since 0.6.4
  42. */
  43. public class ServerListServiceProvider implements ServiceProvider {
  44. /** The server list that we're providing for. */
  45. private final ServerList serverList;
  46. /** The services that this provider providers. */
  47. private final List<Service> services;
  48. /** Plugin Manager */
  49. private final PluginManager pluginManager;
  50. /** The event bus to post errors to. */
  51. private final DMDircMBassador eventBus;
  52. /**
  53. * Creates a new server list service provider.
  54. *
  55. * @param pluginManager The PluginManager to use.
  56. * @param serverList The {@link ServerList} to retrieve items from
  57. * @param eventBus The event bus to post errors to
  58. */
  59. public ServerListServiceProvider(final PluginManager pluginManager, final ServerList serverList,
  60. final DMDircMBassador eventBus) {
  61. this.serverList = serverList;
  62. this.pluginManager = pluginManager;
  63. this.eventBus = eventBus;
  64. this.services = Arrays.asList(pluginManager.getService("parser", "serverlist", true));
  65. }
  66. /**
  67. * Registers this service provider.
  68. */
  69. public void register() {
  70. for (Service service : services) {
  71. service.addProvider(this);
  72. }
  73. }
  74. @Override
  75. public boolean isActive() {
  76. return true;
  77. }
  78. @Override
  79. public void activateServices() {
  80. // Do nothing
  81. }
  82. @Override
  83. public List<Service> getServices() {
  84. return services;
  85. }
  86. @Override
  87. public String getProviderName() {
  88. return "Serverlist service provider";
  89. }
  90. @Override
  91. public ExportedService getExportedService(final String name) {
  92. if ("getParser".equals(name)) {
  93. return new ExportedService(ServerListServiceProvider.class, "getParser", this);
  94. } else {
  95. return null;
  96. }
  97. }
  98. /**
  99. * Retrieves a parser for the specified details.
  100. *
  101. * @param myInfo The user-supplied client identification information
  102. * @param address The address to connect to (a serverlist:// URI)
  103. *
  104. * @return A corresponding Parser instance, or null if none applicable
  105. */
  106. public Parser getParser(final MyInfo myInfo, final URI address) {
  107. ServerGroup group = serverList.getGroupByName(address.getHost());
  108. if (address.getPath() != null && !address.getPath().isEmpty()) {
  109. for (String part : address.getPath().split("/")) {
  110. if (part.isEmpty()) {
  111. continue;
  112. }
  113. final ServerGroupItem item = group.getItemByName(part);
  114. if (item == null) {
  115. return null;
  116. } else if (item instanceof ServerGroup) {
  117. group = (ServerGroup) item;
  118. } else {
  119. return getParserForItem(myInfo, item);
  120. }
  121. }
  122. }
  123. return getParserForItem(myInfo, group);
  124. }
  125. /**
  126. * Retrieves a parser for the specified item.
  127. *
  128. * @param myInfo The user-supplied client identification information
  129. * @param item The item to retrieve a URI from
  130. *
  131. * @return A corresponding parser instance
  132. */
  133. protected Parser getParserForItem(final MyInfo myInfo, final ServerGroupItem item) {
  134. return new ParserFactory(pluginManager, eventBus).getParser(myInfo, item.getAddress());
  135. }
  136. }