--- /dev/null
+package net.pterodactylus.fcp;
+
+/**
+ * The “ReloadPlugin” message is used to reload a plugin.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public class ReloadPlugin extends FcpMessage {
+
+ public ReloadPlugin(String identifier) {
+ super("ReloadPlugin");
+ setField("Identifier", identifier);
+ }
+
+ public void setPluginName(String pluginName) {
+ setField("PluginName", pluginName);
+ }
+
+ public void setMaxWaitTime(int maxWaitTime) {
+ setField("MaxWaitTime", String.valueOf(maxWaitTime));
+ }
+
+ public void setPurge(boolean purge) {
+ setField("Purge", String.valueOf(purge));
+ }
+
+ public void setStore(boolean store) {
+ setField("Store", String.valueOf(store));
+ }
+
+}
return new LoadPluginCommandImpl(threadPool, this::connect);
}
+ @Override
+ public ReloadPluginCommand reloadPlugin() {
+ return new ReloadPluginCommandImpl(threadPool, this::connect);
+ }
+
}
ModifyPeerNoteCommand modifyPeerNote();
LoadPluginCommand loadPlugin();
+ ReloadPluginCommand reloadPlugin();
}
--- /dev/null
+package net.pterodactylus.fcp.quelaton;
+
+import java.util.Optional;
+
+import net.pterodactylus.fcp.PluginInfo;
+
+/**
+ * Reloads a plugin.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public interface ReloadPluginCommand {
+
+ Executable<Optional<PluginInfo>> plugin(String pluginClassName);
+
+}
--- /dev/null
+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.PluginInfo;
+import net.pterodactylus.fcp.ProtocolError;
+import net.pterodactylus.fcp.ReloadPlugin;
+
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
+import com.google.common.util.concurrent.MoreExecutors;
+
+/**
+ * Default {@link ReloadPluginCommand} implementation based on {@link FcpDialog}.
+ *
+ * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
+ */
+public class ReloadPluginCommandImpl implements ReloadPluginCommand {
+
+ private static final RandomIdentifierGenerator IDENTIFIER = new RandomIdentifierGenerator();
+ private final ListeningExecutorService threadPool;
+ private final ConnectionSupplier connectionSupplier;
+ private final ReloadPlugin reloadPlugin = new ReloadPlugin(IDENTIFIER.generate());
+
+ public ReloadPluginCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
+ this.threadPool = MoreExecutors.listeningDecorator(threadPool);
+ this.connectionSupplier = connectionSupplier;
+ }
+
+ @Override
+ public Executable<Optional<PluginInfo>> plugin(String pluginClassName) {
+ reloadPlugin.setPluginName(pluginClassName);
+ return this::execute;
+ }
+
+ private ListenableFuture<Optional<PluginInfo>> execute() {
+ return threadPool.submit(this::executeDialog);
+ }
+
+ private Optional<PluginInfo> executeDialog() throws IOException, ExecutionException, InterruptedException {
+ try (ReloadPluginDialog loadPluginDialog = new ReloadPluginDialog()) {
+ return loadPluginDialog.send(reloadPlugin).get();
+ }
+ }
+
+ private class ReloadPluginDialog extends FcpDialog<Optional<PluginInfo>> {
+
+ private final AtomicBoolean finished = new AtomicBoolean();
+ private final AtomicReference<PluginInfo> pluginInfo = new AtomicReference<>();
+
+ public ReloadPluginDialog() throws IOException {
+ super(threadPool, connectionSupplier.get());
+ }
+
+ @Override
+ protected boolean isFinished() {
+ return finished.get();
+ }
+
+ @Override
+ protected Optional<PluginInfo> 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);
+ }
+
+ }
+
+}
}
+ public class ReloadPlugin {
+
+ private static final String CLASS_NAME = "foo.plugin.Plugin";
+ private List<String> lines;
+ private String identifier;
+
+ private void connectAndAssert(Supplier<Matcher<List<String>>> requestMatcher)
+ throws InterruptedException, ExecutionException, IOException {
+ connectNode();
+ lines = fcpServer.collectUntil(is("EndMessage"));
+ identifier = extractIdentifier(lines);
+ assertThat(lines, requestMatcher.get());
+ }
+
+ @Test
+ public void reloadingPluginWorks() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<PluginInfo>> pluginInfo = fcpClient.reloadPlugin().plugin(CLASS_NAME).execute();
+ connectAndAssert(() -> matchesFcpMessage(
+ "ReloadPlugin",
+ "Identifier=" + identifier,
+ "PluginName=" + CLASS_NAME,
+ "EndMessage"
+ ));
+ replyWithPluginInfo();
+ verifyPluginInfo(pluginInfo);
+ }
+
+ private void replyWithPluginInfo() throws IOException {
+ fcpServer.writeLine(
+ "PluginInfo",
+ "Identifier=" + identifier,
+ "PluginName=superPlugin",
+ "IsTalkable=true",
+ "LongVersion=1.2.3",
+ "Version=42",
+ "OriginUri=superPlugin",
+ "Started=true",
+ "EndMessage"
+ );
+ }
+
+ private void verifyPluginInfo(Future<Optional<PluginInfo>> pluginInfo)
+ throws InterruptedException, ExecutionException {
+ assertThat(pluginInfo.get().get().getPluginName(), is("superPlugin"));
+ assertThat(pluginInfo.get().get().getOriginalURI(), is("superPlugin"));
+ assertThat(pluginInfo.get().get().isTalkable(), is(true));
+ assertThat(pluginInfo.get().get().getVersion(), is("42"));
+ assertThat(pluginInfo.get().get().getLongVersion(), is("1.2.3"));
+ assertThat(pluginInfo.get().get().isStarted(), is(true));
+ }
+
+ }
+
}