Add method to wait for removal of a plugin
[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.concurrent.atomic.AtomicBoolean;
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 static final RandomIdentifierGenerator IDENTIFIER = new RandomIdentifierGenerator();
24         private final ListeningExecutorService threadPool;
25         private final ConnectionSupplier connectionSupplier;
26         private final RemovePlugin removePlugin = new RemovePlugin(IDENTIFIER.generate());
27
28         public RemovePluginCommandImpl(ExecutorService threadPool, ConnectionSupplier connectionSupplier) {
29                 this.threadPool = MoreExecutors.listeningDecorator(threadPool);
30                 this.connectionSupplier = connectionSupplier;
31         }
32
33         @Override
34         public RemovePluginCommand waitFor(int milliseconds) {
35                 removePlugin.setMaxWaitTime(milliseconds);
36                 return this;
37         }
38
39         @Override
40         public Executable<Boolean> plugin(String pluginClass) {
41                 removePlugin.setPluginName(pluginClass);
42                 return this::execute;
43         }
44
45         private ListenableFuture<Boolean> execute() {
46                 return threadPool.submit(this::executeDialog);
47         }
48
49         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
50                 try (RemovePluginDialog removePluginDialog = new RemovePluginDialog()) {
51                         return removePluginDialog.send(removePlugin).get();
52                 }
53         }
54
55         private class RemovePluginDialog extends FcpDialog<Boolean> {
56
57                 private final AtomicBoolean finished = new AtomicBoolean();
58                 private final AtomicBoolean pluginRemoved = new AtomicBoolean();
59
60                 public RemovePluginDialog() throws IOException {
61                         super(threadPool, connectionSupplier.get());
62                 }
63
64                 @Override
65                 protected boolean isFinished() {
66                         return finished.get();
67                 }
68
69                 @Override
70                 protected Boolean getResult() {
71                         return pluginRemoved.get();
72                 }
73
74                 @Override
75                 protected void consumePluginRemoved(PluginRemoved pluginRemoved) {
76                         this.pluginRemoved.set(true);
77                         finished.set(true);
78                 }
79
80                 @Override
81                 protected void consumeProtocolError(ProtocolError protocolError) {
82                         finished.set(true);
83                 }
84
85         }
86
87 }