123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.serverlists.service;
-
- import com.dmdirc.DMDircMBassador;
- import com.dmdirc.ParserFactory;
- import com.dmdirc.addons.serverlists.ServerGroup;
- import com.dmdirc.addons.serverlists.ServerGroupItem;
- import com.dmdirc.addons.serverlists.ServerList;
- import com.dmdirc.parser.common.MyInfo;
- import com.dmdirc.parser.interfaces.Parser;
- import com.dmdirc.plugins.ExportedService;
- import com.dmdirc.plugins.PluginManager;
- import com.dmdirc.plugins.Service;
- import com.dmdirc.plugins.ServiceProvider;
-
- import java.net.URI;
- import java.util.Arrays;
- import java.util.List;
-
- /**
- * Provides a fake parser service which handles <code>serverlist://</code> URIs, and returns a
- * suitable parser for the corresponding server list.
- *
- * @since 0.6.4
- */
- public class ServerListServiceProvider implements ServiceProvider {
-
- /** The server list that we're providing for. */
- private final ServerList serverList;
- /** The services that this provider providers. */
- private final List<Service> services;
- /** Plugin Manager */
- private final PluginManager pluginManager;
- /** The event bus to post errors to. */
- private final DMDircMBassador eventBus;
-
- /**
- * Creates a new server list service provider.
- *
- * @param pluginManager The PluginManager to use.
- * @param serverList The {@link ServerList} to retrieve items from
- * @param eventBus The event bus to post errors to
- */
- public ServerListServiceProvider(final PluginManager pluginManager, final ServerList serverList,
- final DMDircMBassador eventBus) {
- this.serverList = serverList;
- this.pluginManager = pluginManager;
- this.eventBus = eventBus;
- this.services = Arrays.asList(pluginManager.getService("parser", "serverlist", true));
- }
-
- /**
- * Registers this service provider.
- */
- public void register() {
- for (Service service : services) {
- service.addProvider(this);
- }
- }
-
- @Override
- public boolean isActive() {
- return true;
- }
-
- @Override
- public void activateServices() {
- // Do nothing
- }
-
- @Override
- public List<Service> getServices() {
- return services;
- }
-
- @Override
- public String getProviderName() {
- return "Serverlist service provider";
- }
-
- @Override
- public ExportedService getExportedService(final String name) {
- if ("getParser".equals(name)) {
- return new ExportedService(ServerListServiceProvider.class, "getParser", this);
- } else {
- return null;
- }
- }
-
- /**
- * Retrieves a parser for the specified details.
- *
- * @param myInfo The user-supplied client identification information
- * @param address The address to connect to (a serverlist:// URI)
- *
- * @return A corresponding Parser instance, or null if none applicable
- */
- public Parser getParser(final MyInfo myInfo, final URI address) {
- ServerGroup group = serverList.getGroupByName(address.getHost());
-
- if (address.getPath() != null && !address.getPath().isEmpty()) {
- for (String part : address.getPath().split("/")) {
- if (part.isEmpty()) {
- continue;
- }
-
- final ServerGroupItem item = group.getItemByName(part);
-
- if (item == null) {
- return null;
- } else if (item instanceof ServerGroup) {
- group = (ServerGroup) item;
- } else {
- return getParserForItem(myInfo, item);
- }
- }
- }
-
- return getParserForItem(myInfo, group);
- }
-
- /**
- * Retrieves a parser for the specified item.
- *
- * @param myInfo The user-supplied client identification information
- * @param item The item to retrieve a URI from
- *
- * @return A corresponding parser instance
- */
- protected Parser getParserForItem(final MyInfo myInfo, final ServerGroupItem item) {
- return new ParserFactory(pluginManager, eventBus).getParser(myInfo, item.getAddress());
- }
-
- }
|