Refactor FCP dialog
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / quelaton / RemovePluginCommandImpl.java
1 package net.pterodactylus.fcp.quelaton;
2
3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.ExecutorService;
6 import java.util.function.Supplier;
7
8 import net.pterodactylus.fcp.PluginRemoved;
9 import net.pterodactylus.fcp.ProtocolError;
10 import net.pterodactylus.fcp.RemovePlugin;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.ListeningExecutorService;
14 import com.google.common.util.concurrent.MoreExecutors;
15
16 /**
17  * Default {@link RemovePluginCommand} implementation based on {@link FcpDialog}.
18  *
19  * @author <a href="mailto:bombe@freenetproject.org">David ‘Bombe’ Roden</a>
20  */
21 public class RemovePluginCommandImpl implements RemovePluginCommand {
22
23         private final ListeningExecutorService threadPool;
24         private final ConnectionSupplier connectionSupplier;
25         private final RemovePlugin removePlugin;
26
27         public RemovePluginCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier, Supplier<String> identifierGenerator) {
28                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
29                 this.connectionSupplier = connectionSupplier;
30                 removePlugin = new RemovePlugin(identifierGenerator.get());
31         }
32
33         @Override
34         public RemovePluginCommand waitFor(int milliseconds) {
35                 removePlugin.setMaxWaitTime(milliseconds);
36                 return this;
37         }
38
39         @Override
40         public RemovePluginCommand purge() {
41                 removePlugin.setPurge(true);
42                 return this;
43         }
44
45         @Override
46         public Executable<Boolean> plugin(String pluginClass) {
47                 removePlugin.setPluginName(pluginClass);
48                 return this::execute;
49         }
50
51         private ListenableFuture<Boolean> execute() {
52                 return threadPool.submit(this::executeDialog);
53         }
54
55         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
56                 try (RemovePluginDialog removePluginDialog = new RemovePluginDialog()) {
57                         return removePluginDialog.send(removePlugin).get();
58                 }
59         }
60
61         private class RemovePluginDialog extends FcpDialog<Boolean> {
62
63                 public RemovePluginDialog() throws IOException {
64                         super(threadPool, connectionSupplier.get(), false);
65                 }
66
67                 @Override
68                 protected void consumePluginRemoved(PluginRemoved pluginRemoved) {
69                         setResult(true);
70                 }
71
72                 @Override
73                 protected void consumeProtocolError(ProtocolError protocolError) {
74                         finish();
75                 }
76
77         }
78
79 }