From d0fb310aedd61fd3f6120b3ced8886ab1eb92189 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 22 Aug 2015 00:39:42 +0200 Subject: [PATCH] Add command that returns information about a plugin --- .../fcp/quelaton/DefaultFcpClient.java | 5 ++ .../net/pterodactylus/fcp/quelaton/FcpClient.java | 1 + .../fcp/quelaton/GetPluginInfoCommand.java | 16 +++++ .../fcp/quelaton/GetPluginInfoCommandImpl.java | 83 ++++++++++++++++++++++ .../fcp/quelaton/DefaultFcpClientTest.java | 21 ++++++ 5 files changed, 126 insertions(+) create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommand.java create mode 100644 src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java index 3f485c0..fe7300d 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/DefaultFcpClient.java @@ -129,5 +129,10 @@ public class DefaultFcpClient implements FcpClient { return new RemovePluginCommandImpl(threadPool, this::connect); } + @Override + public GetPluginInfoCommand getPluginInfo() { + return new GetPluginInfoCommandImpl(threadPool, this::connect); + } + } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java index 67c11ee..b799f27 100644 --- a/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java +++ b/src/main/java/net/pterodactylus/fcp/quelaton/FcpClient.java @@ -27,5 +27,6 @@ public interface FcpClient { LoadPluginCommand loadPlugin(); ReloadPluginCommand reloadPlugin(); RemovePluginCommand removePlugin(); + GetPluginInfoCommand getPluginInfo(); } diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommand.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommand.java new file mode 100644 index 0000000..047024b --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommand.java @@ -0,0 +1,16 @@ +package net.pterodactylus.fcp.quelaton; + +import java.util.Optional; + +import net.pterodactylus.fcp.PluginInfo; + +/** + * Returns information about a plugin. + * + * @author David ‘Bombe’ Roden + */ +public interface GetPluginInfoCommand { + + Executable> plugin(String pluginName); + +} diff --git a/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java new file mode 100644 index 0000000..8063bc7 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/quelaton/GetPluginInfoCommandImpl.java @@ -0,0 +1,83 @@ +package net.pterodactylus.fcp.quelaton; + +import java.io.IOException; +import java.util.Optional; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import net.pterodactylus.fcp.GetPluginInfo; +import net.pterodactylus.fcp.PluginInfo; +import net.pterodactylus.fcp.ProtocolError; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + +/** + * Default {@link GetPluginInfoCommand} implementation based on {@link FcpDialog}. + * + * @author David ‘Bombe’ Roden + */ +public class GetPluginInfoCommandImpl implements GetPluginInfoCommand { + + private static final RandomIdentifierGenerator IDENTIFIER = new RandomIdentifierGenerator(); + private final ListeningExecutorService threadPool; + private final ConnectionSupplier connectionSupplier; + private final GetPluginInfo getPluginInfo = new GetPluginInfo(IDENTIFIER.generate()); + + public GetPluginInfoCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) { + this.threadPool = MoreExecutors.listeningDecorator(threadPool); + this.connectionSupplier = connectionSupplier; + } + + @Override + public Executable> plugin(String pluginName) { + getPluginInfo.setPluginName(pluginName); + return this::execute; + } + + private ListenableFuture> execute() { + return threadPool.submit(this::executeDialog); + } + + private Optional executeDialog() throws IOException, ExecutionException, InterruptedException { + try (GetPluginInfoDialog getPluginInfoDialog = new GetPluginInfoDialog()) { + return getPluginInfoDialog.send(getPluginInfo).get(); + } + } + + private class GetPluginInfoDialog extends FcpDialog> { + + private final AtomicBoolean finished = new AtomicBoolean(); + private final AtomicReference pluginInfo = new AtomicReference<>(); + + public GetPluginInfoDialog() throws IOException { + super(threadPool, connectionSupplier.get()); + } + + @Override + protected boolean isFinished() { + return finished.get(); + } + + @Override + protected Optional getResult() { + return Optional.ofNullable(pluginInfo.get()); + } + + @Override + protected void consumePluginInfo(PluginInfo pluginInfo) { + this.pluginInfo.set(pluginInfo); + finished.set(true); + } + + @Override + protected void consumeProtocolError(ProtocolError protocolError) { + finished.set(true); + } + + } + +} diff --git a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java index ed8a02e..002086d 100644 --- a/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/quelaton/DefaultFcpClientTest.java @@ -2240,6 +2240,27 @@ public class DefaultFcpClientTest { } + public class GetPluginInfo { + + @Test + public void gettingPluginInfoWorks() throws InterruptedException, ExecutionException, IOException { + Future> pluginInfo = fcpClient.getPluginInfo().plugin(CLASS_NAME).execute(); + connectAndAssert(() -> matchGetPluginInfoMessage()); + replyWithPluginInfo(); + verifyPluginInfo(pluginInfo); + } + + private Matcher> matchGetPluginInfoMessage() { + return matchesFcpMessage( + "GetPluginInfo", + "Identifier=" + identifier, + "PluginName=" + CLASS_NAME, + "EndMessage" + ); + } + + } + } } -- 2.7.4