Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ServerListServiceProvider.java 5.1KB

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