b4a6b790618fae5233944a3597491ffa633234ad
[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 Executable<Boolean> plugin(String pluginClass) {
35                 removePlugin.setPluginName(pluginClass);
36                 return this::execute;
37         }
38
39         private ListenableFuture<Boolean> execute() {
40                 return threadPool.submit(this::executeDialog);
41         }
42
43         private boolean executeDialog() throws IOException, ExecutionException, InterruptedException {
44                 try (RemovePluginDialog removePluginDialog = new RemovePluginDialog()) {
45                         return removePluginDialog.send(removePlugin).get();
46                 }
47         }
48
49         private class RemovePluginDialog extends FcpDialog<Boolean> {
50
51                 private final AtomicBoolean finished = new AtomicBoolean();
52                 private final AtomicBoolean pluginRemoved = new AtomicBoolean();
53
54                 public RemovePluginDialog() throws IOException {
55                         super(threadPool, connectionSupplier.get());
56                 }
57
58                 @Override
59                 protected boolean isFinished() {
60                         return finished.get();
61                 }
62
63                 @Override
64                 protected Boolean getResult() {
65                         return pluginRemoved.get();
66                 }
67
68                 @Override
69                 protected void consumePluginRemoved(PluginRemoved pluginRemoved) {
70                         this.pluginRemoved.set(true);
71                         finished.set(true);
72                 }
73
74                 @Override
75                 protected void consumeProtocolError(ProtocolError protocolError) {
76                         finished.set(true);
77                 }
78
79         }
80
81 }